# Source for file Frontbase.php

Documentation is available at Frontbase.php

1. <?php
2. /*
3. * $Id$
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
21. Doctrine::autoload('Doctrine_Export');
22. /**
23. * Doctrine_Export_Frontbase
24. *
25. * @package Doctrine
26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. * @version $Revision$
33. */
34. class Doctrine_Export_Frontbase extends Doctrine_Export
35. {
36. /**
37. * create a new database
38. *
39. * @param string $name name of the database that should be created
40. * @return string
41. */
42. public function createDatabaseSql($name)
43. {
44. $name = $this->conn->quoteIdentifier($name, true);
45. return 'CREATE DATABASE ' . $name;
46. }
47. /**
48. * drop an existing database
49. *
50. * @param string $name name of the database that should be dropped
51. * @return string
52. */
53. public function dropDatabaseSql($name)
54. {
55. $name = $this->conn->quoteIdentifier($name, true);
56. return 'DELETE DATABASE ' . $name;
57. }
58. /**
59. * drop an existing table
60. *
61. * @param object $this->conns database object that is extended by this class
62. * @param string $name name of the table that should be dropped
63. * @return string
64. */
65. public function dropTableSql($name)
66. {
67. $name = $this->conn->quoteIdentifier($name, true);
68. return 'DROP TABLE ' . $name . ' CASCADE';
69. }
70. /**
71. * alter an existing table
72. *
73. * @param string $name name of the table that is intended to be changed.
74. * @param array $changes associative array that contains the details of each type
75. * of change that is intended to be performed. The types of
76. * changes that are currently supported are defined as follows:
77. *
78. * name
79. *
80. * New name for the table.
81. *
82. * add
83. *
84. * Associative array with the names of fields to be added as
85. * indexes of the array. The value of each entry of the array
86. * should be set to another associative array with the properties
87. * of the fields to be added. The properties of the fields should
88. * be the same as defined by the MDB2 parser.
89. *
90. *
91. * remove
92. *
93. * Associative array with the names of fields to be removed as indexes
94. * of the array. Currently the values assigned to each entry are ignored.
95. * An empty array should be used for future compatibility.
96. *
97. * rename
98. *
99. * Associative array with the names of fields to be renamed as indexes
100. * of the array. The value of each entry of the array should be set to
101. * another associative array with the entry named name with the new
102. * field name and the entry named Declaration that is expected to contain
103. * the portion of the field declaration already in DBMS specific SQL code
104. * as it is used in the CREATE TABLE statement.
105. *
106. * change
107. *
108. * Associative array with the names of the fields to be changed as indexes
109. * of the array. Keep in mind that if it is intended to change either the
110. * name of a field and any other properties, the change array entries
111. * should have the new names of the fields as array indexes.
112. *
113. * The value of each entry of the array should be set to another associative
114. * array with the properties of the fields to that are meant to be changed as
115. * array entries. These entries should be assigned to the new values of the
116. * respective properties. The properties of the fields should be the same
117. * as defined by the MDB2 parser.
118. *
119. * Example
120. * array(
121. * 'name' => 'userlist',
122. * 'add' => array(
123. * 'quota' => array(
124. * 'type' => 'integer',
125. * 'unsigned' => 1
126. * )
127. * ),
128. * 'remove' => array(
129. * 'file_limit' => array(),
130. * 'time_limit' => array()
131. * ),
132. * 'change' => array(
133. * 'name' => array(
134. * 'length' => '20',
135. * 'definition' => array(
136. * 'type' => 'text',
137. * 'length' => 20,
138. * ),
139. * )
140. * ),
141. * 'rename' => array(
142. * 'sex' => array(
143. * 'name' => 'gender',
144. * 'definition' => array(
145. * 'type' => 'text',
146. * 'length' => 1,
147. * 'default' => 'M',
148. * ),
149. * )
150. * )
151. * )
152. *
153. * @param boolean $check indicates whether the function should just check if the DBMS driver
154. * can perform the requested table alterations if the value is true or
155. * actually perform them otherwise.
156. * @access public
157. *
158. * @return boolean
159. */
160. public function alterTable($name, array $changes, $check)
161. {
162. foreach ($changes as $changeName => $change){
163. switch ($changeName) {
164. case 'add':
165. case 'remove':
166. case 'change':
167. case 'rename':
168. case 'name':
169. break;
170. default:
171. throw new Doctrine_Export_Exception('change type "'.$changeName.'" not yet supported');
172. }
173. }
175. if ($check) {
176. return true;
177. }
179. $query = '';
180. if (!empty($changes['name'])) {
181. $changeName = $this->conn->quoteIdentifier($changes['name'], true);
182. $query .= 'RENAME TO ' . $changeName;
183. }
185. if (!empty($changes['add']) && is_array($changes['add'])) {
186. foreach ($changes['add'] as $fieldName => $field) {
187. if ($query) {
188. $query.= ', ';
189. }
190. $query.= 'ADD ' . $this->conn->getDeclaration($field['type'], $fieldName, $field);
191. }
192. }
194. if (!empty($changes['remove']) && is_array($changes['remove'])) {
195. foreach ($changes['remove'] as $fieldName => $field) {
196. if ($query) {
197. $query.= ', ';
198. }
199. $fieldName = $this->conn->quoteIdentifier($fieldName, true);
200. $query.= 'DROP ' . $fieldName;
201. }
202. }
204. $rename = array();
205. if (!empty($changes['rename']) && is_array($changes['rename'])) {
206. foreach ($changes['rename'] as $fieldName => $field) {
207. $rename[$field['name']] = $fieldName;
208. }
209. }
211. if (!empty($changes['change']) && is_array($changes['change'])) {
212. foreach ($changes['change'] as $fieldName => $field) {
213. if ($query) {
214. $query.= ', ';
215. }
216. if (isset($rename[$fieldName])) {
217. $oldFieldName = $rename[$fieldName];
218. unset($rename[$fieldName]);
219. } else {
220. $oldFieldName = $fieldName;
221. }
222. $oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
223. $query.= 'CHANGE ' . $oldFieldName . ' ' . $this->conn->getDeclaration($field['definition']['type'], $oldFieldName, $field['definition']);
224. }
225. }
227. if (!empty($rename) && is_array($rename)) {
228. foreach ($rename as $renamedFieldName => $renamed_field) {
229. if ($query) {
230. $query.= ', ';
231. }
232. $oldFieldName = $rename[$renamedFieldName];
233. $field = $changes['rename'][$oldFieldName];
234. $query.= 'CHANGE ' . $this->conn->getDeclaration($field['definition']['type'], $oldFieldName, $field['definition']);
235. }
236. }
238. if (!$query) {
239. return true;
240. }
242. $name = $this->conn->quoteIdentifier($name, true);
243. return $this->conn->exec('ALTER TABLE ' . $name . ' ' . $query);
244. }
245. /**
246. * create sequence
247. *
248. * @param string $seqName name of the sequence to be created
249. * @param string $start start value of the sequence; default is 1
250. * @param array $options An associative array of table options:
251. * array(
252. * 'comment' => 'Foo',
253. * 'charset' => 'utf8',
254. * 'collate' => 'utf8_unicode_ci',
255. * );
256. * @return void
257. */
258. public function createSequence($sequenceName, $start = 1, array $options = array())
259. {
260. $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true);
261. $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
263. $query = 'CREATE TABLE ' . $sequenceName . ' (' . $seqcolName . ' INTEGER DEFAULT UNIQUE, PRIMARY KEY(' . $seqcolName . '))';
264. $res = $this->conn->exec($query);
265. $res = $this->conn->exec('SET UNIQUE = 1 FOR ' . $sequenceName);
267. if ($start == 1) {
268. return true;
269. }
271. try {
272. $this->conn->exec('INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (' . ($start-1) . ')');
273. } catch(Doctrine_Connection_Exception $e) {
274. // Handle error
275. try {
276. $this->conn->exec('DROP TABLE ' . $sequenceName);
277. } catch(Doctrine_Connection_Exception $e) {
278. throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
279. }
281. throw new Doctrine_Export_Exception('could not create sequence table');
282. }
283. }
284. /**
285. * drop existing sequence
286. *
287. * @param string $seqName name of the sequence to be dropped
288. * @return string
289. */
290. public function dropSequenceSql($seqName)
291. {
292. $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
294. return 'DROP TABLE ' . $sequenceName . ' CASCADE';
295. }
296. /**
297. * drop existing index
298. *
299. * @param string $table name of table that should be used in method
300. * @param string $name name of the index to be dropped
301. * @return boolean
302. */
303. public function dropIndexSql($table, $name)
304. {
305. $table = $this->conn->quoteIdentifier($table, true);
306. $name = $this->conn->quoteIdentifier($this->conn->getIndexName($name), true);
308. return 'ALTER TABLE ' . $table . ' DROP INDEX ' . $name;
309. }
310. }