Database
[ ]
[ ]
[ ]
[ ]
[ ]
Source for file query_update.php
Documentation is available at query_update.php
1. <?php
2. /**
3. * File containing the ezcQueryUpdate class.
4. *
5. * @package Database
6. * @version 1.4
7. * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
8. * @license http://ez.no/licenses/new_bsd New BSD License
9. */
10.
11. /**
12. * Class to create select database independent UPDATE queries.
13. *
14. * ezcQueryUpdate does not support updating from via a select call.
15. *
16. * Note that this class creates queries that are syntactically independant
17. * of database. Semantically the queries still differ and so the same
18. * query may produce different results on different databases. Such
19. * differences are noted throughout the documentation of this class.
20. *
21. * This class implements SQL92. If your database differs from the SQL92
22. * implementation extend this class and reimplement the methods that produce
23. * different results. Some methods implemented in ezcQuery are not defined by SQL92.
24. * These methods are marked and ezcQuery will return MySQL syntax for these cases.
25. *
26. * The examples show the SQL generated by this class.
27. * Database specific implementations may produce different results.
28. *
29. * Example:
30. * <code>
31. * $q = ezcDbInstance::get()->createUpdateQuery();
32. * $q->update( 'legends' )
33. * ->set( 'Gretzky', 99 )
34. * ->set( 'Lindros', 88 );
35. * $stmt = $q->prepare();
36. * $stmt->execute();
37. * </code>
38. *
39. * @package Database
40. * @mainclass
41. * @version 1.4
42. */
43. class ezcQueryUpdate extends ezcQuery
44. {
45. /**
46. * Holds the columns and the values that should inserted into the the table.
47. *
48. * Format array('column'=>value)
49. * @var array(string=>mixed)
50. */
51. private $values = array();
52.
53. /**
54. * The target table for the update query.
55. *
56. * @var string
57. */
58. private $table = null;
59.
60. /**
61. * Stores the WHERE part of the SQL.
62. *
63. * @var string
64. */
65. protected $whereString = null;
66.
67.
68. /**
69. * Constructs a new ezcQueryUpdate that works on the database $db and with the aliases $aliases.
70. *
71. * The parameters are passed directly to ezcQuery.
72. * @param PDO $db
73. * @param array(string=>string) $aliases
74. */
75. public function __construct( PDO $db, array $aliases = array() )
76. {
77. parent::__construct( $db, $aliases );
78. }
79.
80. /**
81. * Opens the query and sets the target table to $table.
82. *
83. * update() returns a pointer to $this.
84. *
85. * @param string $table
86. * @return ezcQueryUpdate
87. */
88. public function update( $table )
89. {
90. $table = $this->getIdentifier( $table );
91. $this->table = $table;
92. return $this;
93. }
94.
95. /**
96. * The update query will set the column $column to the value $expression.
97. *
98. * @param string $column
99. * @param string $expression
100. * @return ezcQueryUpdate
101. */
102. public function set( $column, $expression )
103. {
104. $column = $this->getIdentifier( $column );
105. $expression = $this->getIdentifier( $expression );
106. $this->values[$column] = $expression;
107. return $this;
108. }
109.
110. /**
111. * Adds a where clause with logical expressions to the query.
112. *
113. * where() accepts an arbitrary number of parameters. Each parameter
114. * must contain a logical expression or an array with logical expressions.
115. * If you specify multiple logical expression they are connected using
116. * a logical and.
117. * where() could be invoked several times. All provided arguments
118. * added to the end of $whereString and form final WHERE clause of the query.
119. *
120. *
121. * Example:
122. * <code>
123. * $q->update( 'MyTable' )->where( $q->eq( 'id', 1 ) );
124. * </code>
125. *
126. * @throws ezcQueryVariableParameterException if called with no parameters.
127. * @param string|array(string)$... Either a string with a logical expression name
128. * or an array with logical expressions.
129. * @return ezcQueryUpdate
130. */
131. public function where()
132. {
133. if ( $this->whereString == null )
134. {
135. $this->whereString = 'WHERE ';
136. }
137.
138. $args = func_get_args();
139. $expressions = self::arrayFlatten( $args );
140. if ( count( $expressions ) < 1 )
141. {
142. throw new ezcQueryVariableParameterException( 'where', count( $args ), 1 );
143. }
144.
145. // glue string should be inserted each time but not before first entry
146. if ( $this->whereString != 'WHERE ' )
147. {
148. $this->whereString .= ' AND ';
149. }
150.
151. $this->whereString .= join( ' AND ', $expressions );
152. return $this;
153. }
154.
155.
156. /**
157. * Returns the query string for this query object.
158. *
159. * @todo wrong exception
160. * @throws ezcQueryInvalidException if no table or no values have been set.
161. * @return string
162. */
163. public function getQuery()
164. {
165. if ( $this->table == null || empty( $this->values ) )
166. {
167. $problem = $this->table == null ? 'table' : 'values';
168. throw new ezcQueryInvalidException( "UPDATE", "No " . $problem . " set." );
169. }
170. $query = "UPDATE {$this->table} SET ";
171.
172. // build an append set part
173. $setString = null;
174. foreach ( $this->values as $key => $value )
175. {
176. if ( $setString === null )
177. {
178. $setString = "{$key} = {$value}";
179. }
180. else
181. {
182. $setString .= ", {$key} = {$value}";
183. }
184. }
185. $query .= $setString;
186.
187. // append where part.
188. if ( $this->whereString !== null )
189. {
190. $query .= " {$this->whereString}";
191. }
192.
193. return $query;
194. }
195. }
Last updated: Wed, 18 Jun 2008