summaryrefslogtreecommitdiff
path: root/framework/Data/Common/TDbCommandBuilder.php
blob: 5238a04519e4ed65951fd1704ad621f6ea8e885a (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
 * TDbCommandBuilder class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2007 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Data.Common
 */

/**
 * TDbCommandBuilder provides basic methods to create query commands for tables
 * giving by {@link setTableInfo TableInfo} the property.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.Common
 * @since 3.1
 */
class TDbCommandBuilder extends TComponent
{
	private $_connection;
	private $_tableInfo;

	/**
	 * @param TDbConnection database connection.
	 * @param TDbTableInfo table information.
	 */
	public function __construct($connection=null, $tableInfo=null)
	{
		$this->setDbConnection($connection);
		$this->setTableInfo($tableInfo);
	}

	/**
	 * @return TDbConnection database connection.
	 */
	public function getDbConnection()
	{
		return $this->_connection;
	}

	/**
	 * @param TDbConnection database connection.
	 */
	public function setDbConnection($value)
	{
		$this->_connection=$value;
	}

	/**
	 * @param TDbTableInfo table information.
	 */
	public function setTableInfo($value)
	{
		$this->_tableInfo=$value;
	}

	/**
	 * @param TDbTableInfo table information.
	 */
	public function getTableInfo()
	{
		return $this->_tableInfo;
	}

	/**
	 * Alters the sql to apply $limit and $offset. Default implementation is applicable
	 * for PostgreSQL, MySQL and SQLite.
	 * @param string SQL query string.
	 * @param integer maximum number of rows, -1 to ignore limit.
	 * @param integer row offset, -1 to ignore offset.
	 * @return string SQL with limit and offset.
	 */
	public function applyLimitOffset($sql, $limit=-1, $offset=-1)
	{
		$limit = $limit!==null ? intval($limit) : -1;
		$offset = $offset!==null ? intval($offset) : -1;
		$limitStr = $limit >= 0 ? ' LIMIT '.$limit : '';
		$offsetStr = $offset >= 0 ? ' OFFSET '.$offset : '';
		return $sql.$limitStr.$offsetStr;
	}

	/**
	 * @param string SQL string without existing ordering.
	 * @param array pairs of column names as key and direction as value.
	 * @return string modified SQL applied with ORDER BY.
	 */
	public function applyOrdering($sql, $ordering)
	{
		$orders=array();
		foreach($ordering as $name=>$direction)
		{
			$direction = strtolower($direction) == 'desc' ? 'DESC' : 'ASC';
			$column = $this->getTableInfo()->getColumn($name)->getColumnName();
			$orders[] = $column.' '.$direction;
		}
		if(count($orders) > 0)
			$sql .= ' ORDER BY '.implode(', ', $orders);
		return $sql;
	}

	/**
	 * Computes the SQL condition for search a set of column using regular expression
	 * to match a string of keywords. The implementation should only uses columns
	 * that permit regular expression matching. This method should be implemented in
	 * database specific command builder classes.
	 * @param array list of column id for potential search condition.
	 * @param string string of keywords
	 * @return string SQL condition for regular expression matching on a set of columns.
	 */
	//abstract public function createRegExpSearch($columnIds, $keywords);

	/**
	 * Appends the $where condition to the string "SELECT * FROM tableName WHERE ".
	 * The tableName is obtained from the {@link setTableInfo TableInfo} property.
	 * @param string query condition
	 * @param array condition parameters.
	 * @return TDbCommand query command.
	 */
	public function createFindCommand($where, $parameters=array(), $ordering=array(), $limit=-1, $offset=-1)
	{
		$table = $this->getTableInfo()->getTableFullName();
		$sql = "SELECT * FROM {$table} WHERE {$where}";
		if(count($ordering) > 0)
			$sql = $this->applyOrdering($sql, $ordering);
		if($limit>=0 || $offset>=0)
			$sql = $this->applyLimitOffset($sql, $limit, $offset);
		$command = $this->createCommand($sql);
		$this->bindArrayValues($command, $parameters);
		return $command;
	}

	/**
	 * Creates a count(*) command for the table described in {@link setTableInfo TableInfo}.
	 * @param string count condition.
	 * @param array binding parameters.
	 * @return TDbCommand count command.
	 */
	public function createCountCommand($where='true', $parameters=array(),$ordering=array(), $limit=-1, $offset=-1)
	{
		$table = $this->getTableInfo()->getTableFullName();
		$sql = "SELECT COUNT(*) FROM {$table} WHERE {$where}";
		if(count($ordering) > 0)
			$sql = $this->applyOrdering($sql, $ordering);
		if($limit>=0 || $offset>=0)
			$sql = $this->applyLimitOffset($sql, $limit, $offset);
		$command = $this->createCommand($sql);
		$this->bindArrayValues($command, $parameters);
		return $command;
	}

	/**
	 * Creates a delete command for the table described in {@link setTableInfo TableInfo}.
	 * The conditions for delete is given by the $where argument and the parameters
	 * for the condition is given by $parameters.
	 * @param string delete condition.
	 * @param array delete parameters.
	 * @return TDbCommand delete command.
	 */
	public function createDeleteCommand($where,$parameters=array())
	{
		$table = $this->getTableInfo()->getTableFullName();
		$command = $this->createCommand("DELETE FROM {$table} WHERE {$where}");
		$this->bindArrayValues($command, $parameters);
		return $command;
	}

	/**
	 * Creates an insert command for the table described in {@link setTableInfo TableInfo} for the given data.
	 * Each array key in the $data array must correspond to the column name of the table
	 * (if a column allows to be null, it may be omitted) to be inserted with
	 * the corresponding array value.
	 * @param array name-value pairs of new data to be inserted.
	 * @return TDbCommand insert command
	 */
	public function createInsertCommand($data)
	{
		$table = $this->getTableInfo()->getTableFullName();
		list($fields, $bindings) = $this->getInsertFieldBindings($data);
		$command = $this->createCommand("INSERT INTO {$table}({$fields}) VALUES ($bindings)");
		$this->bindColumnValues($command, $data);
		return $command;
	}

	/**
	 * Creates an update command for the table described in {@link setTableInfo TableInfo} for the given data.
	 * Each array key in the $data array must correspond to the column name to be updated with the corresponding array value.
	 * @param array name-value pairs of data to be updated.
	 * @param string update condition.
	 * @param array update parameters.
	 * @return TDbCommand update command.
	 */
	public function createUpdateCommand($data, $where, $parameters=array())
	{
		$table = $this->getTableInfo()->getTableFullName();
		if($this->hasIntegerKey($parameters))
			$fields = implode(', ', $this->getColumnBindings($data, true));
		else
			$fields = implode(', ', $this->getColumnBindings($data));
		$command = $this->createCommand("UPDATE {$table} SET {$fields} WHERE {$where}");
		$this->bindArrayValues($command, array_merge($data, $parameters));
		return $command;
	}

	/**
	 * Returns a list of insert field name and a list of binding names.
	 * @param object array or object to be inserted.
	 * @return array tuple ($fields, $bindings)
	 */
	protected function getInsertFieldBindings($values)
	{
		$fields = array(); $bindings=array();
		foreach(array_keys($values) as $name)
		{
			$fields[] = $this->getTableInfo()->getColumn($name)->getColumnName();
			$bindings[] = ':'.$name;
		}
		return array(implode(', ',$fields), implode(', ', $bindings));
	}

	/**
	 * Create a name-value or position-value if $position=true binding strings.
	 * @param array data for binding.
	 * @param boolean true to bind as position values.
	 * @return string update column names with corresponding binding substrings.
	 */
	protected function getColumnBindings($values, $position=false)
	{
		$bindings=array();
		foreach(array_keys($values) as $name)
		{
			$column = $this->getTableInfo()->getColumn($name)->getColumnName();
			$bindings[] = $position ? $column.' = ?' : $column.' = :'.$name;
		}
		return $bindings;
	}

	/**
	 * @param string SQL query string.
	 * @return TDbCommand corresponding database command.
	 */
	public function createCommand($sql)
	{
		$this->getDbConnection()->setActive(true);
		return $this->getDbConnection()->createCommand($sql);
	}

	/**
	 * Bind the name-value pairs of $values where the array keys correspond to column names.
	 * @param TDbCommand database command.
	 * @param array name-value pairs.
	 */
	public function bindColumnValues($command, $values)
	{
		foreach($values as $name=>$value)
		{
			$column = $this->getTableInfo()->getColumn($name);
			if($value === null && $column->getAllowNull())
				$command->bindValue(':'.$name, null, PDO::PARAM_NULL);
			else
				$command->bindValue(':'.$name, $value, $column->getPdoType());
		}
	}

	/**
	 * @param TDbCommand database command
	 * @param array values for binding.
	 */
	public function bindArrayValues($command, $values)
	{
		if($this->hasIntegerKey($values))
		{
			$values = array_values($values);
			for($i = 0, $max=count($values); $i<$max; $i++)
				$command->bindValue($i+1, $values[$i], $this->getPdoType($value));
		}
		else
		{
			foreach($values as $name=>$value)
			{
				$prop = $name[0]===':' ? $name : ':'.$name;
				$command->bindValue($prop, $value, $this->getPdoType($value));
			}
		}
	}

	/**
	 * @param mixed PHP value
	 * @return integer PDO parameter types.
	 */
	protected function getPdoType($value)
	{
		switch(gettype($value))
		{
			case 'boolean': return PDO::PARAM_BOOL;
			case 'integer': return PDO::PARAM_INT;
			case 'string' : return PDO::PARAM_STR;
			case 'NULL'   : return PDO::PARAM_NULL;
		}
	}

	/**
	 * @param array
	 * @return boolean true if any array key is an integer.
	 */
	protected function hasIntegerKey($array)
	{
		foreach($array as $k=>$v)
		{
			if(gettype($k)==='integer')
				return true;
		}
		return false;
	}
}

?>