Skip to content

Commit

Permalink
Merge pull request #6 from skipperbent/v3-development
Browse files Browse the repository at this point in the history
Changed event behavior so it always fires with $queryObject available.
  • Loading branch information
skipperbent authored Aug 18, 2017
2 parents a5602f1 + d35e1e2 commit 0047813
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,7 @@ public function statement($sql, $bindings = [])
*/
public function get()
{
$eventResult = $this->fireEvents('before-select');
if ($eventResult !== null) {
return $eventResult;
}

$queryObject = null;
$executionTime = 0;
if ($this->pdoStatement === null) {
$queryObject = $this->getQuery('select');
Expand All @@ -205,10 +201,11 @@ public function get()
}

$start = microtime(true);
$this->fireEvents('before-select', $queryObject);
$result = call_user_func_array([$this->pdoStatement, 'fetchAll'], $this->fetchParameters);
$executionTime += microtime(true) - $start;
$this->pdoStatement = null;
$this->fireEvents('after-select', $result, $executionTime);
$this->fireEvents('after-select', $queryObject, $result, $executionTime);

return $result;
}
Expand Down Expand Up @@ -347,36 +344,37 @@ public function subQuery(QueryBuilderHandler $queryBuilder, $alias = null)
*/
private function doInsert($data, $type)
{
$eventResult = $this->fireEvents('before-insert');
if ($eventResult !== null) {
return $eventResult;
}
$queryObject = null;

// If first value is not an array
// Its not a batch insert
if (!is_array(current($data))) {
$queryObject = $this->getQuery($type, $data);

$this->fireEvents('before-insert', $queryObject);
list($result, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings());

$return = $result->rowCount() === 1 ? $this->pdo->lastInsertId() : null;
$this->fireEvents('after-insert', $queryObject, $return, $executionTime);

} else {
// Its a batch insert
$return = [];
$executionTime = 0;
foreach ($data as $subData) {
$queryObject = $this->getQuery($type, $subData);

list($result, $time) = $this->statement($queryObject->getSql(), $queryObject->getBindings());
$executionTime += $time;
$this->fireEvents('before-insert', $queryObject);

list($result, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings());

$result = $result->rowCount() === 1 ? $this->pdo->lastInsertId() : null;

$this->fireEvents('after-insert', $queryObject, $result, $executionTime);

if ($result->rowCount() === 1) {
$return[] = $this->pdo->lastInsertId();
}
$return[] = $result;
}
}

$this->fireEvents('after-insert', $return, $executionTime);


return $return;
}
Expand Down Expand Up @@ -418,13 +416,10 @@ public function replace($data)
*/
public function update($data)
{
$eventResult = $this->fireEvents('before-update');
if ($eventResult !== null) {
return $eventResult;
}

$queryObject = $this->getQuery('update', $data);

$this->fireEvents('before-update', $queryObject);

list($response, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings());
$this->fireEvents('after-update', $queryObject, $executionTime);

Expand Down Expand Up @@ -461,13 +456,10 @@ public function onDuplicateKeyUpdate($data)
*/
public function delete()
{
$eventResult = $this->fireEvents('before-delete');
if ($eventResult !== null) {
return $eventResult;
}

$queryObject = $this->getQuery('delete');

$this->fireEvents('before-delete', $queryObject);

list($response, $executionTime) = $this->statement($queryObject->getSql(), $queryObject->getBindings());
$this->fireEvents('after-delete', $queryObject, $executionTime);

Expand Down

0 comments on commit 0047813

Please sign in to comment.