Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Pixie/QueryBuilder/Adapters/BaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function replace($statements, array $data)
{
return $this->doInsert($statements, $data, 'REPLACE');
}

/**
* Build fields assignment part of SET ... or ON DUBLICATE KEY UPDATE ... statements
*
Expand All @@ -224,7 +224,6 @@ private function getUpdateStatement($data)
{
$bindings = array();
$statement = '';

foreach ($data as $key => $value) {
if ($value instanceof Raw) {
$statement .= $this->wrapSanitizer($key) . '=' . $value . ',';
Expand All @@ -233,7 +232,6 @@ private function getUpdateStatement($data)
$bindings[] = $value;
}
}

$statement = trim($statement, ',');
return array($statement, $bindings);
}
Expand Down Expand Up @@ -406,7 +404,7 @@ protected function buildCriteria($statements, $bindValues = true)
break;
}
} elseif ($value instanceof Raw) {
$criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value";
$criteria .= "{$statement['joiner']} {$key} {$statement['operator']} $value ";
} else {
// Usual where like criteria

Expand Down
67 changes: 59 additions & 8 deletions src/Pixie/QueryBuilder/QueryBuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class QueryBuilderHandler
* @var array
*/
protected $fetchParameters = array(\PDO::FETCH_OBJ);

/**
* @var integer
*/
protected $transactions = 0;

/**
* @param null|\Pixie\Connection $connection
Expand Down Expand Up @@ -788,20 +793,66 @@ public function join($table, $key, $operator = null, $value = null, $type = 'inn

return $this;
}

public function transaction(\Closure $callback)

/**
* @return void
*/
public function beginTransaction()
{
try {
++$this->transactions;

if ($this->transactions === 1) {
$this->pdo->beginTransaction();

$callback($this);
}
}

/**
* @return void
*/
public function commit()
{
if ($this->transactions === 1) {
$this->pdo->commit();
}

--$this->transactions;
}

/**
* @return void
*/
public function rollback()
{
if ($this->transactions == 1) {
$this->transactions = 0;
$this->pdo->rollBack();
}
else {
--$this->transactions;
}
}

/**
* @param \Closure $callback
*
* @return mixed
*
* @throws \Exception
*/
public function transaction(\Closure $callback)
{
try {
$this->beginTransaction();

return true;
$result = $callback($this);
$this->commit();
} catch (\Exception $e) {
$this->pdo->rollBack();
return false;
$this->rollback();

throw $e;
}

return $result;
}

/**
Expand Down