bfd7094715d76a175a99b8fc7a32783a287ebd8d
6 * A very simple query writer used by \pq\Gateway
26 * @param string $query initial query string
27 * @param array $params intial set of params
28 * @param array $types the types of the params
30 function __construct($query = "", array $params = array(), array $types = array()) {
31 $this->query
= $query;
32 $this->params
= $params;
33 $this->types
= $types;
37 * Get the query string
40 function __toString() {
45 * Reduce arguments to write()
50 protected function reduce($q, $v) {
51 return $q . " " . (is_array($v) ?
implode(", ", $v) : $v);
55 * Get the query params
58 function getParams() {
72 * @return \pq\Query\Writer
76 $this->params
= array();
77 $this->types
= array();
82 * Append to the query string
83 * @return \pq\Query\Writer
86 $this->query
.= array_reduce(func_get_args(), array($this, "reduce"));
91 * Write a param placeholder and push the param onto the param list
96 function param($param, $type = null) {
97 if ($param instanceof Expr
) {
98 return (string) $param;
103 $this->params
[] = $param;
104 $this->types
[] = $type;
106 return "\$".count($this->params
);
110 * Write nested AND/OR criteria
111 * @param array $criteria
112 * @return \pq\Query\Writer
114 function criteria(array $criteria) {
115 if ((list($left, $right) = each($criteria))) {
116 array_shift($criteria);
118 if (is_array($right)) {
119 $this->criteria($right);
121 $this->write("(", $left, $this->param($right), ")");
123 foreach ($criteria as $left => $right) {
124 $this->write(is_int($left) && is_array($right) ?
"OR" : "AND");
125 if (is_array($right)) {
126 $this->criteria($right);
128 $this->write("(", $left, $this->param($right), ")");
137 * Execute the query through \pq\Connection::execParams($this, $this->params, $this->types)
138 * @param \pq\Connection $c
141 function exec(\pq\Connection
$c) {
142 fprintf(STDERR
, "Q: %s\n", $this);
143 fprintf(STDERR
, "P: %s\n", implode(", ", $this->params
));
144 return $c->execParams($this, $this->params
, $this->types
);