Skip to content
Open
2 changes: 1 addition & 1 deletion Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class Exception extends CoreException
{
const QUERY_ERROR = '%s Query: %s';
const QUERY_ERROR = '(%s | %s) - %s Query: %s';
const TABLE_NOT_SET = 'No default table set or was passed.';
const DATABASE_NOT_SET = 'No default database set or was passed.';

Expand Down
58 changes: 48 additions & 10 deletions Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
abstract class Factory extends CoreEvent
{
const INSTANCE = 0;

const FIRST = 'first';
const LAST = 'last';
const QUERY = 'Eden\\Sql\\Query';
Expand Down Expand Up @@ -62,7 +64,7 @@ public function bind($value)
}

return '('.implode(",",$value).')';
} else if(is_numeric($value)) {
} else if(is_int($value) || ctype_digit($value)) {
return $value;
}

Expand Down Expand Up @@ -116,6 +118,10 @@ public function deleteRows($table, $filters = null)
//Argument 2 must be a string or array
->test(2, 'array', 'string');

//clear binds
$this->clearBinds();

// build query
$query = $this->delete($table);

//array('post_id=%s AND post_title IN %s', 123, array('asd'));
Expand Down Expand Up @@ -174,6 +180,16 @@ public function getBinds()
{
return $this->binds;
}

/**
* Clears all the binded values
*
* @return array
*/
public function clearBinds()
{
$this->binds = array();
}

/**
* Returns the connection object
Expand Down Expand Up @@ -289,6 +305,9 @@ public function getRow($table, $name, $value)
//Argument 3 must be scalar or null
->test(3, 'scalar', 'null');

//clear binds
$this->clearBinds();

//make the query
$query = $this->select()
->from($table)
Expand Down Expand Up @@ -341,6 +360,9 @@ public function insertRow($table, array $settings, $bind = true)
//Argument 3 must be an array or bool
->test(3, 'array', 'bool');

//clear binds
$this->clearBinds();

//build insert query
$query = $this->insert($table);

Expand Down Expand Up @@ -446,12 +468,20 @@ public function query($query, array $binds = array())
//Argument 1 must be a string or null
Argument::i()->test(1, 'string', self::QUERY);

$request = new \stdClass();

$request->query = $query;
$request->binds = $binds;

//event trigger
$this->trigger('sql-query-before', $request);

$connection = $this->getConnection();
$query = (string) $query;
$query = (string) $request->query;
$stmt = $connection->prepare($query);

//bind some more values
foreach($binds as $key => $value) {
foreach($request->binds as $key => $value) {
$stmt->bindValue($key, $value);
}

Expand All @@ -463,28 +493,33 @@ public function query($query, array $binds = array())
foreach($binds as $key => $value) {
$query = str_replace($key, "'$value'", $query);
}

//clear binds
$this->clearBinds();

//throw Exception
Exception::i()
->setMessage(Exception::QUERY_ERROR)
->addVariable($query)
->addVariable($error[0])
->addVariable($error[1])
->addVariable($error[2])
->addVariable($query)
->trigger();
}

$results = $stmt->fetchAll( \PDO::FETCH_ASSOC );

//clear binds
$this->clearBinds();

//log query
$this->queries[] = array(
'query' => $query,
'binds' => $binds,
'results' => $results);

//clear binds
$this->binds = array();


//event trigger
$this->trigger('sql-query', $query, $binds, $results);
$this->trigger('sql-query-after', $query, $binds, $results);

return $results;
}
Expand Down Expand Up @@ -648,6 +683,9 @@ public function updateRows($table, array $settings, $filters = null, $bind = tru
//Argument 4 must be a string or bool
->test(4, 'array', 'bool');

//clear binds
$this->clearBinds();

//build the query
$query = $this->update($table);

Expand Down Expand Up @@ -709,7 +747,7 @@ public function updateRows($table, array $settings, $filters = null, $bind = tru
}

//run the query
$this->query($query, $this->getBinds());
$this->query($query, $this->getBinds());

//event trigger
$this->trigger('sql-update', $table, $settings, $filters);
Expand Down
9 changes: 8 additions & 1 deletion Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public function __call($name, $args)
}

//generate key
$key = $key.'=%s';
if (is_array($args[0])) {
$key = $key.' IN %s';
} else {
$key = $key.'=%s';
}

//add it to the search filter
$this->addFilter($key, $args[0]);
Expand Down Expand Up @@ -817,6 +821,9 @@ public function setTable($table)
*/
protected function getQuery()
{
//clear binds
$this->database->clearBinds();

$query = $this->database->select()->from($this->table);

foreach($this->join as $join) {
Expand Down
2 changes: 1 addition & 1 deletion Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ public function set($key, $value)

return $this;
}
}
}