1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
<?php
/**
* PDO implementation of the database Query
* @package Database
*/
class Query_PDO_Driver extends Query_Database {
/**
* Type of the database, e.g. mysql, pgsql etc.
* @var string
* @access public
*/
protected $_db_type;
/**
* Character to use for quoting fields
* @var string
* @access public
*/
protected $_quote;
/**
* Creates a new query object, checks which driver we are using and set the character used for quoting
*
* @param DB $db Database connection
* @param string $type Query type. Available types: select, update, insert, delete, count
* @return void
* @access public
* @see Query_Database::__construct()
*/
public function __construct($db, $type) {
parent::__construct($db, $type);
$this->_db_type = $this->_db->db_type;
$this->_quote=$this->_db_type=='mysql'?'`':'"';
}
/**
* Puts quotes around a string
*
* @param string $str String to be enclosed in quotes
* @return string String surrounded with quotes
* @access protected
*/
protected function quote($str) {
return $this->_quote.$str.$this->_quote;
}
/**
* If a string is passed escapes a field by enclosing it in specified quotes.
* If you pass an Expression_Database object the value will be inserted into the query unescaped
*
* @param mixed $field Field to be escaped or an Expression_Database object
* if the field must not be escaped
* @return string Escaped field representation
* @access public
* @see Expression_Database
*/
public function escape_field($field) {
if (is_object($field) && get_class($field) == 'Expression_Database')
return $field->value.' ';
$field = explode('.', $field);
if (count($field) == 1)
array_unshift($field,$this->last_alias());
$str = $this->quote($field[0]).'.';
if (trim($field[1]) == '*')
return $str.'* ';
return $str.$this->quote($field[1]);
}
/**
* Replaces the value with ? and appends it to the parameters array
* If you pass an Expression_Database object the value will be inserted into the query unescaped
* @param mixed $val Value to be escaped or an Expression_Database object
* if the value must not be escaped
* @param array &$params Reference to parameters array
* @return string Escaped value representation
* @access public
*/
public function escape_value($val,&$params) {
if (is_object($val) && get_class($val) == 'Expression_Database')
return $val->value.' ';
$params[] = $val;
return '? ';
}
/**
* Builds a query and fills the $params array with parameter values
*
* @return array An array with a prepared query string and an array of parameters
* @access public
*/
public function query() {
$query = '';
$params = array();
if ($this->_type == 'insert') {
$query.= "INSERT INTO {$this->quote($this->_table)} ";
if (empty($this->_data) && $this->_db_type == 'pgsql'){
$query.= "DEFAULT VALUES ";
}else{
$columns = '';
$values = '';
$first = true;
foreach($this->_data as $key => $val) {
if (!$first) {
$values.= ',';
$columns.= ',';
}else {
$first=false;
}
$columns.= $this->quote($key)." ";
$values.=$this->escape_value($val,$params);
}
$query.= "({$columns}) VALUES({$values})";
}
}else{
if ($this->_type == 'select'){
$query.= "SELECT ";
if($this->_fields==null){
$query.= "* ";
}else{
$first = true;
foreach($this->_fields as $f) {
if (!$first) {
$query.=", ";
}else {
$first = false;
}
if(is_array($f)){
$query.= "{$this->escape_field($f[0])} AS {$f[1]} ";
}else {
$query.= "{$this->escape_field($f)} ";
}
}
}
$query.= "FROM {$this->quote($this->_table)} ";
}
if ($this->_type == 'count') {
$query.= "SELECT COUNT(*) as {$this->quote('count')} FROM {$this->quote($this->_table)} ";
}
if($this->_type=='delete')
$query.= "DELETE {$this->last_alias()}.* FROM {$this->quote($this->_table)} ";
if($this->_type=='update'){
$query.= "UPDATE {$this->quote($this->_table)} SET ";
$first = true;
foreach($this->_data as $key=>$val){
if (!$first) {
$query.=',';
}else {
$first=false;
}
$query.= "{$this->quote($key)}=".$this->escape_value($val,$params);
}
}
foreach($this->_joins as $join) {
$table = $join[0];
if (is_array($table)){
$table = "{$this->quote($table[0])} as {$this->quote($table[1])}";
}else {
$table="{$this->quote($table)}";
}
$query.= strtoupper($join[1])." JOIN {$table} ON ".$this->get_condition_query($join[2],$params,true,true);
}
if (!empty($this->_conditions)) {
$query.="WHERE ".$this->get_condition_query($this->_conditions,$params,true);
}
if (($this->_type == 'select' || $this->_type == 'count') && $this->_group_by!=null) {
$query.="GROUP BY ".$this->escape_field($this->_group_by);
}
if (($this->_type == 'select' || $this->_type == 'count') && !empty($this->_having)) {
$query.="HAVING ".$this->get_condition_query($this->_having,$params,true);
}
if ($this->_type == 'select' && !empty($this->_orderby)) {
$query.="ORDER BY ";
$first = true;
foreach($this->_orderby as $order) {
if (!$first) {
$query.=',';
}else {
$first=false;
}
$query.= $this->escape_field($order[0])." ";
if (isset($order[1])) {
$dir = strtoupper($order[1]);
$query.=$dir." ";
}
}
}
if($this->_type != 'count'){
if ($this->_limit != null)
$query.= "LIMIT {$this->_limit} ";
if ($this->_offset != null)
$query.= "OFFSET {$this->_offset} ";
}
}
return array($query,$params);
}
/**
* Recursively parses conditions array into a query string
*
* @param array $p Element of the cobditions array
* @param array &$params Reference to parameters array
* @param boolean $skip_first_operator Flag to skip the first logical operator in a query
* to prevent AND or OR to be at the beginning of the query
* @param boolean $value_is_field Flag if the the value in the logical operations should
* be treated as a field. E.g. for joins where the fields are
* compared between themselves and not with actual values
* @return string String representation of the conditions
* @access public
* @throws Exception If condition cannot be parsed
*/
public function get_condition_query($p,&$params,$skip_first_operator,$value_is_field=false) {
if (isset($p['field'])) {
if ($value_is_field){
$param = $this->escape_field($p['value']);
}else {
$param = $this->escape_value($p['value'],$params);
}
return $this->escape_field($p['field']).' '.$p['operator'].' '.$param.' ';
}
if (isset($p['logic'])) {
return ($skip_first_operator?'':strtoupper($p['logic'])).' '
.$this->get_condition_query($p['conditions'],$params,false,$value_is_field).' ';
}
$conds = '';
$skip=$skip_first_operator||(count($p) > 1);
foreach($p as $q) {
$conds.=$this->get_condition_query($q,$params,$skip,$value_is_field);
$skip=false;
}
if (count($p) > 1 && !$skip_first_operator)
return "( ".$conds." ) ";
return $conds;
throw new Exception("Cannot parse condition:\n".var_export($p,true));
}
}
|