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

## Source for file db.php

Documentation is available at db.php

1. <?php
2. /**
3. * File containing the ezcTreeDb 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. * ezcTreeDb contains common methods for the different database tree backends.
30. *
31. * @property-read ezcTreeDbDataStore $store
32. * The data store that is used for retrieving/storing data.
33. * @property string $nodeClassName
34. * Which class is used as tree node - this class *must* inherit
35. * the ezcTreeNode class.
36. *
37. * @package TreeDatabaseTiein
38. * @version //autogentag//
39. */
40. abstract class ezcTreeDb extends ezcTree
41. {
42. /**
43. * Contains the database connection handler.
44. *
45. * @var ezcDbHandler
46. */
47. protected $dbh ;
49. /**
50. * Contains the name of the table to retrieve the relational data from.
51. *
52. * @var string
53. */
54. protected $indexTableName ;
56. /**
57. * Constructs a new ezcTreeDb object.
58. *
59. * The different arguments to the constructor configure which database
60. * connection ($dbh) is used to access the database and the $indexTableName
61. * argument which table is used to retrieve the relation data from. The
62. * $store argument configure which data store is used with this tree.
63. *
64. * All database backends require the index table to at least define the
65. * field 'id', which can either be a string or an integer field.
66. *
67. * @param ezcDbHandler $dbh
68. * @param string $indexTableName
69. * @param ezcTreeDbDataStore $store
70. */
71. public function __construct ( ezcDbHandler $dbh, $indexTableName, ezcTreeDbDataStore $store )
72. {
73. $this-> dbh = $dbh;
74. $this-> indexTableName = $indexTableName;
75. $this-> properties ['store'] = $store;
76. $this-> properties ['autoId'] = false;
77. }
79. /**
80. * Creates the query to insert an empty node into the database, so that the last-inserted ID can be obtained.
81. *
82. * @return ezcQueryInsert
83. */
84. abstract protected function createAddEmptyNodeQuery ();
86. /**
87. * Creates the query to insert/update an empty node in the database.
88. *
89. * The query is constructed for the child with ID $id
90. *
91. * @param mixed $id
92. * @return ezcQuery
93. */
94. protected function createAddNodeQuery ( $id )
95. {
96. $db = $this-> dbh ;
98. if ( $this-> properties ['autoId'] )
99. {
100. $q = $db->createUpdateQuery();
101. $q->update( $db->quoteIdentifier( $this-> indexTableName ) )
102. ->where( $q->expr->eq( 'id', $q->bindValue( $id ) ) );
103. }
104. else
105. {
106. $q = $db->createInsertQuery();
107. $q->insertInto( $db->quoteIdentifier( $this-> indexTableName ) );
108. }
109. return $q;
110. }
112. /**
113. * This method generates the next node ID.
114. *
115. * It does so by inserting a new empty node into the database, and uses
116. * lastInsertId() to obtain the ID for the newly inserted node.
117. *
118. * @return integer
119. */
120. protected function generateNodeID ()
121. {
122. $db = $this-> dbh ;
123. $q = $this-> createAddEmptyNodeQuery ();
125. $s = $q->prepare();
127. try
128. {
129. $s->execute();
130. }
131. catch ( PDOException $e )
132. {
133. throw new ezcTreeDbInvalidSchemaException ( "generating a new node ID", $e->getMessage() );
134. }
136. $r = $db->lastInsertId( $this-> indexTableName . '_id_seq' );
137. return $r;
138. }
140. /**
141. * Returns whether the node with ID $id exists as tree node.
142. *
143. * @param string $id
144. * @return bool
145. */
146. public function nodeExists ( $id )
147. {
148. $db = $this-> dbh ;
149. $q = $db->createSelectQuery();
151. $q->select( 'id' )
152. ->from( $db->quoteIdentifier( $this-> indexTableName ) )
153. ->where( $q->expr->eq( 'id', $q->bindValue( $id ) ) );
155. $s = $q->prepare();
156. $s->execute();
158. return count ( $s->fetchAll() ) ? true : false;
159. }
161. /**
162. * Returns the ID of parent of the node with ID $childId.
163. *
164. * @param string $childId
165. * @return string
166. */
167. protected function getParentId ( $childId )
168. {
169. $db = $this-> dbh ;
170. $q = $db->createSelectQuery();
172. $q->select( 'id, parent_id' )
173. ->from( $db->quoteIdentifier( $this-> indexTableName ) )
174. ->where( $q->expr->eq( 'id', $q->bindValue( $childId ) ) );
176. $s = $q->prepare();
177. $s->execute();
178. $row = $s->fetch();
179. return $row['parent_id'];
180. }
182. /**
183. * Returns the parent node of the node with ID $id.
184. *
185. * This method returns null if there is no parent node.
186. *
187. * @param string $id
188. * @return ezcTreeNode
189. */
190. public function fetchParent ( $id )
191. {
192. $className = $this-> properties ['nodeClassName'];
193. $parentId = $this-> getParentId ( $id );
194. return $parentId !== null ? new $className( $this, $parentId ) : null;
195. }
197. /**
198. * Returns the root node.
199. *
200. * This methods returns null if there is no root node.
201. *
202. * @return ezcTreeNode
203. */
204. public function getRootNode ()
205. {
206. $className = $this-> properties ['nodeClassName'];
207. $db = $this-> dbh ;
209. // SELECT id
210. // FROM indexTable
211. // WHERE id IS null
212. $q = $db->createSelectQuery();
213. $q->select( 'id' )
214. ->from( $db->quoteIdentifier( $this-> indexTableName ) )
215. ->where( $q->expr->isNull( 'parent_id' ) );
216. $s = $q->prepare();
217. $s->execute();
218. $r = $s->fetchAll( PDO::FETCH_ASSOC );
219. if ( count ( $r ) )
220. {
221. return new $className( $this, $r[0]['id'] );
222. }
223. return null;
224. }
225. }
226. ?>

Documentation generated by phpDocumentor 1.4.3