Skip to content
Browse files

Literal with parameters

  • Loading branch information...
1 parent 9abe893 commit 0304c6ed095c858364616cbcec885300cf716d9f @vrana committed Jul 27, 2011
Showing with 50 additions and 12 deletions.
  1. +7 −1 NotORM/Literal.php
  2. +31 −11 NotORM/Result.php
  3. +12 −0 tests/28-literal.phpt
View
8 NotORM/Literal.php
@@ -3,14 +3,20 @@
/** SQL literal value
*/
class NotORM_Literal {
- /** @var string */
protected $value = '';
+ /** @var array */
+ public $parameters = array();
+
/** Create literal value
* @param string
+ * @param mixed parameter
+ * @param mixed ...
*/
function __construct($value) {
$this->value = $value;
+ $this->parameters = func_get_args();
+ array_shift($this->parameters);
}
/** Get literal value
View
42 NotORM/Result.php
@@ -135,12 +135,12 @@ function __toString() {
return $return;
}
- protected function query($query) {
+ protected function query($query, $parameters) {
if ($this->notORM->debug) {
if (!is_callable($this->notORM->debug)) {
$debug = "$query;";
- if ($this->parameters) {
- $debug .= " -- " . implode(", ", array_map(array($this->notORM->connection, 'quote'), $this->parameters));
+ if ($parameters) {
+ $debug .= " -- " . implode(", ", array_map(array($this->notORM->connection, 'quote'), $parameters));
}
$pattern = '(^' . preg_quote(dirname(__FILE__)) . '(\\.php$|[/\\\\]))'; // can be static
foreach (debug_backtrace() as $backtrace) {
@@ -149,12 +149,12 @@ protected function query($query) {
}
}
fwrite(STDERR, "$backtrace[file]:$backtrace[line]:$debug\n");
- } elseif (call_user_func($this->notORM->debug, $query, $this->parameters) === false) {
+ } elseif (call_user_func($this->notORM->debug, $query, $parameters) === false) {
return false;
}
}
$return = $this->notORM->connection->prepare($query);
- if (!$return || !$return->execute($this->parameters)) {
+ if (!$return || !$return->execute($parameters)) {
return false;
}
return $return;
@@ -188,7 +188,9 @@ function insert($data) {
if ($this->notORM->freeze) {
return false;
}
+ $parameters = array();
if ($data instanceof NotORM_Result) {
+ $parameters = $data->parameters; //! other parameters
$data = (string) $data;
} elseif ($data instanceof Traversable) {
$data = iterator_to_array($data);
@@ -201,12 +203,15 @@ function insert($data) {
$val = iterator_to_array($val);
}
$values[] = $this->quote($val);
+ if ($val instanceof NotORM_Literal && $val->parameters) {
+ $parameters = array_merge($parameters, $val->parameters);
+ }
}
//! driver specific empty $data and extended insert
$insert = "(" . implode(", ", array_keys($data)) . ") VALUES " . implode(", ", $values);
}
// requires empty $this->parameters
- $return = $this->query("INSERT INTO $this->table $insert");
+ $return = $this->query("INSERT INTO $this->table $insert", $parameters);
if (!$return) {
return false;
}
@@ -232,12 +237,19 @@ function update(array $data) {
return 0;
}
$values = array();
+ $parameters = array();
foreach ($data as $key => $val) {
// doesn't use binding because $this->parameters can be filled by ? or :name
$values[] = "$key = " . $this->quote($val);
+ if ($val instanceof NotORM_Literal && $val->parameters) {
+ $parameters = array_merge($parameters, $val->parameters);
+ }
+ }
+ if ($this->parameters) {
+ $parameters = array_merge($parameters, $this->parameters);
}
// joins in UPDATE are supported only in MySQL
- $return = $this->query("UPDATE" . $this->topString() . " $this->table SET " . implode(", ", $values) . $this->whereString());
+ $return = $this->query("UPDATE" . $this->topString() . " $this->table SET " . implode(", ", $values) . $this->whereString(), $parameters);
if (!$return) {
return false;
}
@@ -256,10 +268,12 @@ function insert_update(array $unique, array $insert, array $update = array()) {
}
$insert = $unique + $insert;
$values = "(" . implode(", ", array_keys($insert)) . ") VALUES " . $this->quote($insert);
+ //! parameters
if ($this->notORM->driver == "mysql") {
$set = array();
foreach ($update as $key => $val) {
$set[] = "$key = " . $this->quote($val);
+ //! parameters
}
return $this->insert("$values ON DUPLICATE KEY UPDATE " . implode(", ", $set));
} else {
@@ -293,7 +307,7 @@ function delete() {
if ($this->notORM->freeze) {
return false;
}
- $return = $this->query("DELETE" . $this->topString() . " FROM $this->table" . $this->whereString());
+ $return = $this->query("DELETE" . $this->topString() . " FROM $this->table" . $this->whereString(), $this->parameters);
if (!$return) {
return false;
}
@@ -475,7 +489,7 @@ function aggregation($function) {
if ($this->where) {
$query .= " WHERE (" . implode(") AND (", $this->where) . ")";
}
- foreach ($this->query($query)->fetch() as $return) {
+ foreach ($this->query($query, $this->parameters)->fetch() as $return) {
return $return;
}
}
@@ -523,16 +537,22 @@ protected function execute() {
if (!isset($this->rows)) {
$result = false;
$exception = null;
+ $parameters = array();
+ foreach (array_merge($this->select, array($this), $this->order, $this->unionOrder) as $val) {
+ if (($val instanceof NotORM_Literal || $val instanceof self) && $val->parameters) {
+ $parameters = array_merge($parameters, $val->parameters);
+ }
+ }
try {
- $result = $this->query($this->__toString());
+ $result = $this->query($this->__toString(), $parameters);
} catch (PDOException $exception) {
// handled later
}
if (!$result) {
if (!$this->select && $this->accessed) {
$this->accessed = '';
$this->access = array();
- $result = $this->query($this->__toString());
+ $result = $this->query($this->__toString(), $parameters);
} elseif ($exception) {
throw $exception;
}
View
12 tests/28-literal.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Literal value with parameters
+--FILE--
+<?php
+include_once dirname(__FILE__) . "/connect.inc.php";
+
+foreach ($software->author()->select(new NotORM_Literal("? + ?", 1, 2))->fetch() as $val) {
+ echo "$val\n";
+}
+?>
+--EXPECTF--
+3

0 comments on commit 0304c6e

Please sign in to comment.
Something went wrong with that request. Please try again.