## Apache Zeta Components Manual :: File Source for db_external.php

## Source for file db_external.php

Documentation is available at db_external.php

1. <?php
2. /**
3. * File containing the ezcTreeDbExternalTableDataStore class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @version //autogentag//
24. * @filesource
25. * @package TreeDatabaseTiein
26. */
28. /**
29. * ezcTreeDbExternalTableDataStore is an implementation of a tree node
30. * data store that uses an external table to store data in.
31. *
32. * @package TreeDatabaseTiein
33. * @version //autogentag//
34. * @mainclass
35. */
36. class ezcTreeDbExternalTableDataStore implements ezcTreeDbDataStore, ezcTreeXmlDataStore
37. {
38. /**
39. * Contains the database connection handler.
40. *
41. * @var ezcDbHandler
42. */
43. private $dbHandler;
45. /**
46. * Contains the name of the table to fetch data from.
47. *
48. * @var string
49. */
50. private $table = null;
52. /**
53. * Contains the name of the field that contains the node ID.
54. *
55. * @var string
56. */
57. private $idField = null;
59. /**
60. * Contains the name of the field to fetch data from.
61. *
62. * If this field is null, then the whole row is returned.
63. *
64. * @var string
65. */
66. private $dataField = null;
68. /**
69. * Contains the DOM representing this tree this data store stores data for.
70. *
71. * @var DOMDocument
72. */
73. protected $dom ;
75. /**
76. * Constructs a new storage backend that stores data in a table external
77. * from the node tree.
78. *
79. * The store will use the database connection specified by $dbHandler, and
80. * the table $dataTable to store the data in. The lookup field that is matched
81. * against the node ID is specified with $idField. By default the store will
82. * return the whole row unless a specific field has been configured through
83. * the $dataField argument in this constructor.
84. *
85. * @param ezcDbHandler $dbHandler
86. * @param string $dataTable
87. * @param string $idField
88. * @param string $dataField
89. */
90. public function __construct ( ezcDbHandler $dbHandler, $dataTable, $idField, $dataField = null )
91. {
92. $this->dbHandler = $dbHandler;
93. $this->table = $dataTable;
94. $this->idField = $idField;
95. $this->dataField = $dataField;
96. }
98. /**
99. * Deletes the data for the node $node from the data store.
100. *
101. * @param ezcTreeNode $node
102. public function deleteDataForNode( ezcTreeNode $node )
103. {
104. }
105. */
107. /**
108. * Deletes the data for all the nodes in the node list $nodeList.
109. *
110. * @param ezcTreeNodeList $nodeList
111. */
112. public function deleteDataForNodes ( ezcTreeNodeList $nodeList )
113. {
114. $nodeIdsToDelete = array();
115. foreach ( array_keys ( $nodeList->nodes ) as $id )
116. {
117. $nodeIdsToDelete[] = (string) $id;
118. }
120. $db = $this->dbHandler;
121. $q = $db->createDeleteQuery();
122. $q->deleteFrom( $db->quoteIdentifier( $this->table ) )
123. ->where( $q->expr->in( $db->quoteIdentifier( $this->idField ), $nodeIdsToDelete ) );
124. $s = $q->prepare();
125. $s->execute();
126. }
128. /**
129. * Deletes the data for all the nodes in the store.
130. */
131. public function deleteDataForAllNodes ()
132. {
133. $db = $this->dbHandler;
134. $q = $db->createDeleteQuery();
136. $q->deleteFrom( $db->quoteIdentifier( $this->table ) );
137. $s = $q->prepare();
138. $s->execute();
139. }
141. /**
142. * Takes the data from the executed query and uses the $dataField
143. * property to filter out the wanted data for this node.
144. *
145. * @param array $data
146. * @return mixed
147. */
148. private function filterDataFromResult( array $data )
149. {
150. if ( $this->dataField === null )
151. {
152. unset( $data[$this->idField] );
153. return $data;
154. }
155. return $data[$this->dataField];
156. }
158. /**
159. * Retrieves the data for the node $node from the data store and assigns it
160. * to the node's 'data' property.
161. *
162. * @param ezcTreeNode $node
163. */
164. public function fetchDataForNode ( ezcTreeNode $node )
165. {
166. $db = $this->dbHandler;
167. $q = $db->createSelectQuery();
169. $id = $node->id;
170. $q->select( '*' )
171. ->from( $db->quoteIdentifier( $this->table ) )
172. ->where( $q->expr->eq( $db->quoteIdentifier( $this->idField ), $q->bindValue( $id ) ) );
173. $s = $q->prepare();
174. $s->execute();
176. $result = $s->fetch( PDO::FETCH_ASSOC );
177. if ( !$result )
178. {
179. throw new ezcTreeDataStoreMissingDataException ( $node->id );
180. }
181. $node->injectData( $this->filterDataFromResult( $result ) );
182. $node->dataFetched = true;
183. }
185. /**
186. * This method *tries* to fetch the data for all the nodes in the node list
187. * $nodeList and assigns this data to the nodes' 'data' properties.
188. *
189. * @param ezcTreeNodeList $nodeList
190. */
191. public function fetchDataForNodes ( ezcTreeNodeList $nodeList )
192. {
193. $nodeIdsToFetch = array();
194. foreach ( $nodeList->nodes as $node )
195. {
196. if ( $node->dataFetched === false )
197. {
198. $nodeIdsToFetch[] = $node->id;
199. }
200. }
201. if ( count ( $nodeIdsToFetch ) === 0 )
202. {
203. return;
204. }
206. $db = $this->dbHandler;
207. $q = $db->createSelectQuery();
209. $q->select( '*' )
210. ->from( $db->quoteIdentifier( $this->table ) )
211. ->where( $q->expr->in( $db->quoteIdentifier( $this->idField ), $nodeIdsToFetch ) );
212. $s = $q->prepare();
213. $s->execute();
215. foreach ( $s as $result )
216. {
217. $nodeList[$result[$this->idField]]->injectData( $this->filterDataFromResult( $result ) );
218. $nodeList[$result[$this->idField]]->dataFetched = true;
219. }
220. }
222. /**
223. * Stores the data in the node to the data store.
224. *
225. * @param ezcTreeNode $node
226. */
227. public function storeDataForNode ( ezcTreeNode $node )
228. {
229. $db = $this->dbHandler;
231. // first we check if there is data for this node
232. $id = $node->id;
233. $q = $db->createSelectQuery();
234. $q->select( $db->quoteIdentifier( $this->idField ) )
235. ->from( $db->quoteIdentifier( $this->table ) )
236. ->where( $q->expr->eq( $db->quoteIdentifier( $this->idField ), $q->bindValue( $id ) ) );
237. $s = $q->prepare();
238. $s->execute();
240. $update = $s->fetch();
241. if ( !$update ) // we don't have data yet, create an insert query
242. {
243. $q = $db->createInsertQuery();
244. $q->insertInto( $db->quoteIdentifier( $this->table ) )
245. ->set( $db->quoteIdentifier( $this->idField ), $q->bindValue( $node->id ) );
246. }
247. else // we have data, so use update
248. {
249. $q = $db->createUpdateQuery();
250. $q->update( $db->quoteIdentifier( $this->table ) );
251. }
253. // Add set statements
254. if ( $this->dataField === null )
255. {
256. foreach ( $node->data as $field => $value )
257. {
258. $q->set( $db->quoteIdentifier( $field ), $q->bindValue( $value ) );
259. }
260. }
261. else
262. {
263. $q->set( $db->quoteIdentifier( $this->dataField ), $q->bindValue( $node->data ) );
264. }
266. if ( $update ) // add where clause if we're updating
267. {
268. $q->where( $q->expr->eq( $db->quoteIdentifier( $this->idField ), $q->bindValue( $id ) ) );
269. }
270. $s = $q->prepare();
271. $s->execute();
272. $node->dataStored = true;
273. }
275. /**
276. * Associates the DOM tree for which this data store stores data for with
277. * this store.
278. *
279. * This method is only needed for when a data store is used
280. * with an XML based tree backend. XML based tree backends call this method
281. * to associate the DOM tree with the store. This is not needed for this
282. * data store so the method is a no-op.
283. *
284. * @param DOMDocument $dom
285. */
286. public function setDomTree ( DOMDocument $dom )
287. {
288. }
289. }
290. ?>

Documentation generated by phpDocumentor 1.4.3