Fixed binding too many values to PDO statements
This commit is contained in:
parent
f59b92e06c
commit
a4fadb218b
2 changed files with 21 additions and 22 deletions
|
@ -24,19 +24,21 @@ class Database
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function makeStatement(SqlStatement $stmt)
|
protected static function convertStatement(SqlStatement $stmt)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$pdoStatement = self::$pdo->prepare($stmt->getAsString());
|
$stmtText = $stmt->getAsString();
|
||||||
|
$stmtPdo = self::$pdo->prepare($stmtText);
|
||||||
foreach ($stmt->getBindings() as $key => $value)
|
foreach ($stmt->getBindings() as $key => $value)
|
||||||
$pdoStatement->bindValue(is_numeric($key) ? $key + 1 : ltrim($key, ':'), $value);
|
if (strpos($stmtText, $key) !== false)
|
||||||
|
$stmtPdo->bindValue($key, $value);
|
||||||
}
|
}
|
||||||
catch (Exception $e)
|
catch (Exception $e)
|
||||||
{
|
{
|
||||||
throw new Exception('Problem with ' . $stmt->getAsString() . ' (' . $e->getMessage() . ')');
|
throw new Exception('Problem with ' . $stmt->getAsString() . ' creation (' . $e->getMessage() . ')');
|
||||||
}
|
}
|
||||||
return $pdoStatement;
|
return $stmtPdo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function disconnect()
|
public static function disconnect()
|
||||||
|
@ -53,29 +55,29 @@ class Database
|
||||||
{
|
{
|
||||||
if (!self::connected())
|
if (!self::connected())
|
||||||
throw new Exception('Database is not connected');
|
throw new Exception('Database is not connected');
|
||||||
$statement = self::makeStatement($stmt);
|
$stmtPdo = self::convertStatement($stmt);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement->execute();
|
$stmtPdo->execute();
|
||||||
}
|
}
|
||||||
catch (Exception $e)
|
catch (Exception $e)
|
||||||
{
|
{
|
||||||
throw new Exception('Problem with ' . $stmt->getAsString() . ' (' . $e->getMessage() . ')');
|
throw new Exception('Problem with ' . $stmt->getAsString() . ' execution (' . $e->getMessage() . ')');
|
||||||
}
|
}
|
||||||
self::$queries []= $stmt;
|
self::$queries []= $stmt;
|
||||||
return $statement;
|
return $stmtPdo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fetchOne(SqlStatement $stmt)
|
public static function fetchOne(SqlStatement $stmt)
|
||||||
{
|
{
|
||||||
$statement = self::exec($stmt);
|
$stmtPdo = self::exec($stmt);
|
||||||
return $statement->fetch();
|
return $stmtPdo->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fetchAll(SqlStatement $stmt)
|
public static function fetchAll(SqlStatement $stmt)
|
||||||
{
|
{
|
||||||
$statement = self::exec($stmt);
|
$stmtPdo = self::exec($stmt);
|
||||||
return $statement->fetchAll();
|
return $stmtPdo->fetchAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getLogs()
|
public static function getLogs()
|
||||||
|
|
|
@ -14,14 +14,9 @@ abstract class SqlExpression
|
||||||
|
|
||||||
public function getBindings()
|
public function getBindings()
|
||||||
{
|
{
|
||||||
$stack = array_merge([], $this->subExpressions);
|
|
||||||
$bindings = $this->bindings;
|
$bindings = $this->bindings;
|
||||||
while (!empty($stack))
|
foreach ($this->subExpressions as $subExpression)
|
||||||
{
|
$bindings = array_merge($bindings, $subExpression->getBindings());
|
||||||
$item = array_pop($stack);
|
|
||||||
$stack = array_merge($stack, $item->subExpressions);
|
|
||||||
$bindings = array_merge($bindings, $item->bindings);
|
|
||||||
}
|
|
||||||
return $bindings;
|
return $bindings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,8 +24,10 @@ abstract class SqlExpression
|
||||||
{
|
{
|
||||||
if ($object instanceof SqlBinding)
|
if ($object instanceof SqlBinding)
|
||||||
{
|
{
|
||||||
$this->bind($object->getName(), $object->getValue());
|
$expr = new SqlStringExpression($object->getName());
|
||||||
return new SqlStringExpression($object->getName());
|
$expr->bind($object->getName(), $object->getValue());
|
||||||
|
$this->subExpressions []= $expr;
|
||||||
|
return $expr;
|
||||||
}
|
}
|
||||||
else if ($object instanceof SqlExpression)
|
else if ($object instanceof SqlExpression)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue