# Source for file Doctrine.php

Documentation is available at Doctrine.php

1. <?php
2. /*
3. * $Id: Doctrine.php 2255 2007-08-16 22:42:35Z Jonathan.Wage $
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. */
22. /**
23. * Doctrine
24. * the base class of Doctrine framework
25. *
26. * @package Doctrine
27. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
28. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30. * @category Object Relational Mapping
31. * @link www.phpdoctrine.com
32. * @since 1.0
33. * @version $Revision: 2255 $
34. */
35. final class Doctrine
36. {
37. /**
38. * ERROR CONSTANTS
39. */
40. const ERR = -1;
41. const ERR_SYNTAX = -2;
42. const ERR_CONSTRAINT = -3;
43. const ERR_NOT_FOUND = -4;
44. const ERR_ALREADY_EXISTS = -5;
45. const ERR_UNSUPPORTED = -6;
46. const ERR_MISMATCH = -7;
47. const ERR_INVALID = -8;
48. const ERR_NOT_CAPABLE = -9;
49. const ERR_TRUNCATED = -10;
50. const ERR_INVALID_NUMBER = -11;
51. const ERR_INVALID_DATE = -12;
52. const ERR_DIVZERO = -13;
53. const ERR_NODBSELECTED = -14;
54. const ERR_CANNOT_CREATE = -15;
55. const ERR_CANNOT_DELETE = -16;
56. const ERR_CANNOT_DROP = -17;
57. const ERR_NOSUCHTABLE = -18;
58. const ERR_NOSUCHFIELD = -19;
59. const ERR_NEED_MORE_DATA = -20;
60. const ERR_NOT_LOCKED = -21;
61. const ERR_VALUE_COUNT_ON_ROW = -22;
62. const ERR_INVALID_DSN = -23;
63. const ERR_CONNECT_FAILED = -24;
64. const ERR_EXTENSION_NOT_FOUND = -25;
65. const ERR_NOSUCHDB = -26;
66. const ERR_ACCESS_VIOLATION = -27;
67. const ERR_CANNOT_REPLACE = -28;
68. const ERR_CONSTRAINT_NOT_NULL = -29;
69. const ERR_DEADLOCK = -30;
70. const ERR_CANNOT_ALTER = -31;
71. const ERR_MANAGER = -32;
72. const ERR_MANAGER_PARSE = -33;
73. const ERR_LOADMODULE = -34;
74. const ERR_INSUFFICIENT_DATA = -35;
75. const ERR_CLASS_NAME = -36;
77. /**
78. * PDO derived constants
79. */
80. const CASE_LOWER = 2;
81. const CASE_NATURAL = 0;
82. const CASE_UPPER = 1;
83. const CURSOR_FWDONLY = 0;
84. const CURSOR_SCROLL = 1;
85. const ERRMODE_EXCEPTION = 2;
86. const ERRMODE_SILENT = 0;
87. const ERRMODE_WARNING = 1;
88. const FETCH_ASSOC = 2;
89. const FETCH_BOTH = 4;
90. const FETCH_BOUND = 6;
91. const FETCH_CLASS = 8;
92. const FETCH_CLASSTYPE = 262144;
93. const FETCH_COLUMN = 7;
94. const FETCH_FUNC = 10;
95. const FETCH_GROUP = 65536;
96. const FETCH_INTO = 9;
97. const FETCH_LAZY = 1;
98. const FETCH_NAMED = 11;
99. const FETCH_NUM = 3;
100. const FETCH_OBJ = 5;
101. const FETCH_ORI_ABS = 4;
102. const FETCH_ORI_FIRST = 2;
103. const FETCH_ORI_LAST = 3;
104. const FETCH_ORI_NEXT = 0;
105. const FETCH_ORI_PRIOR = 1;
106. const FETCH_ORI_REL = 5;
107. const FETCH_SERIALIZE = 524288;
108. const FETCH_UNIQUE = 196608;
109. const NULL_EMPTY_STRING = 1;
110. const NULL_NATURAL = 0;
111. const NULL_TO_STRING = NULL;
112. const PARAM_BOOL = 5;
113. const PARAM_INPUT_OUTPUT = -2147483648;
114. const PARAM_INT = 1;
115. const PARAM_LOB = 3;
116. const PARAM_NULL = 0;
117. const PARAM_STMT = 4;
118. const PARAM_STR = 2;
119. /**
120. * ATTRIBUTE CONSTANTS
121. */
123. /**
124. * PDO derived attributes
125. */
126. const ATTR_AUTOCOMMIT = 0;
127. const ATTR_PREFETCH = 1;
128. const ATTR_TIMEOUT = 2;
129. const ATTR_ERRMODE = 3;
130. const ATTR_SERVER_VERSION = 4;
131. const ATTR_CLIENT_VERSION = 5;
132. const ATTR_SERVER_INFO = 6;
133. const ATTR_CONNECTION_STATUS = 7;
134. const ATTR_CASE = 8;
135. const ATTR_CURSOR_NAME = 9;
136. const ATTR_CURSOR = 10;
137. const ATTR_ORACLE_NULLS = 11;
138. const ATTR_PERSISTENT = 12;
139. const ATTR_STATEMENT_CLASS = 13;
140. const ATTR_FETCH_TABLE_NAMES = 14;
141. const ATTR_FETCH_CATALOG_NAMES = 15;
142. const ATTR_DRIVER_NAME = 16;
143. const ATTR_STRINGIFY_FETCHES = 17;
144. const ATTR_MAX_COLUMN_LEN = 18;
146. /**
147. * Doctrine constants
148. */
149. const ATTR_LISTENER = 100;
150. const ATTR_QUOTE_IDENTIFIER = 101;
151. const ATTR_FIELD_CASE = 102;
152. const ATTR_IDXNAME_FORMAT = 103;
153. const ATTR_SEQNAME_FORMAT = 104;
154. const ATTR_SEQCOL_NAME = 105;
155. const ATTR_CMPNAME_FORMAT = 118;
156. const ATTR_DBNAME_FORMAT = 117;
157. const ATTR_TBLCLASS_FORMAT = 119;
158. const ATTR_EXPORT = 140;
159. const ATTR_DECIMAL_PLACES = 141;
161. const ATTR_PORTABILITY = 106;
162. const ATTR_VLD = 107;
163. const ATTR_COLL_KEY = 108;
164. const ATTR_QUERY_LIMIT = 109;
165. const ATTR_AUTO_LENGTH_VLD = 110;
166. const ATTR_AUTO_TYPE_VLD = 111;
167. const ATTR_DEFAULT_TABLE_TYPE = 112;
168. const ATTR_DEF_TEXT_LENGTH = 113;
169. const ATTR_DEF_VARCHAR_LENGTH = 114;
170. const ATTR_DEF_TABLESPACE = 115;
171. const ATTR_EMULATE_DATABASE = 116;
172. const ATTR_DEFAULT_SEQUENCE = 133;
174. /** TODO: REMOVE THE FOLLOWING CONSTANTS AND UPDATE THE DOCS ! */
177. const ATTR_FETCHMODE = 118;
178. const ATTR_BATCH_SIZE = 119;
179. const ATTR_LOCKMODE = 120;
180. const ATTR_NAME_PREFIX = 121;
181. const ATTR_CREATE_TABLES = 122;
182. const ATTR_COLL_LIMIT = 123;
183. const ATTR_ACCESSORS = 124;
184. const ATTR_ACCESSOR_PREFIX_GET = 125;
185. const ATTR_ACCESSOR_PREFIX_SET = 126;
187. /**
188. * NESTED SET CONSTANTS
189. */
190. const ATTR_NS_ROOT_COLUMN_NAME = 130;
191. const ATTR_NS_GAP_SIZE = 131;
192. const ATTR_NS_GAP_DECREASE_EXP = 132;
194. const ATTR_CACHE = 150;
195. const ATTR_CACHE_LIFESPAN = 151;
196. const ATTR_LOAD_REFERENCES = 153;
197. const ATTR_RECORD_LISTENER = 154;
198. const ATTR_THROW_EXCEPTIONS = 155;
201. /**
202. * LIMIT CONSTANTS
203. */
205. /**
206. * constant for row limiting
207. */
208. const LIMIT_ROWS = 1;
209. /**
210. * constant for record limiting
211. */
212. const LIMIT_RECORDS = 2;
214. /**
215. * FETCHMODE CONSTANTS
216. */
218. /**
219. * IMMEDIATE FETCHING
220. * mode for immediate fetching
221. */
222. const FETCH_IMMEDIATE = 0;
223. /**
224. * BATCH FETCHING
225. * mode for batch fetching
226. */
227. const FETCH_BATCH = 1;
228. /**
229. * LAZY FETCHING
230. * mode for offset fetching
231. */
232. const FETCH_OFFSET = 3;
233. /**
234. * LAZY OFFSET FETCHING
235. * mode for lazy offset fetching
236. */
237. const FETCH_LAZY_OFFSET = 4;
239. /**
240. * FETCH CONSTANTS
241. */
244. /**
245. * FETCH VALUEHOLDER
246. */
247. const FETCH_VHOLDER = 1;
248. /**
249. * FETCH RECORD
250. *
251. * Specifies that the fetch method shall return Doctrine_Record
252. * objects as the elements of the result set.
253. *
254. * This is the default fetchmode.
255. */
256. const FETCH_RECORD = 2;
257. /**
258. * FETCH ARRAY
259. */
261. const FETCH_ARRAY = 3;
262. /**
263. * PORTABILITY CONSTANTS
264. */
266. /**
267. * Portability: turn off all portability features.
268. * @see Doctrine::ATTR_PORTABILITY
269. */
270. const PORTABILITY_NONE = 0;
271. /**
272. * Portability: convert names of tables and fields to case defined in the
273. * "field_case" option when using the query*(), fetch*() methods.
274. * @see Doctrine::ATTR_PORTABILITY
275. */
276. const PORTABILITY_FIX_CASE = 1;
278. /**
279. * Portability: right trim the data output by query*() and fetch*().
280. * @see Doctrine::ATTR_PORTABILITY
281. */
282. const PORTABILITY_RTRIM = 2;
283. /**
284. * Portability: force reporting the number of rows deleted.
285. * @see Doctrine::ATTR_PORTABILITY
286. */
287. const PORTABILITY_DELETE_COUNT = 4;
288. /**
289. * Portability: convert empty values to null strings in data output by
290. * query*() and fetch*().
291. * @see Doctrine::ATTR_PORTABILITY
292. */
293. const PORTABILITY_EMPTY_TO_NULL = 8;
294. /**
295. * Portability: removes database/table qualifiers from associative indexes
296. * @see Doctrine::ATTR_PORTABILITY
297. */
298. const PORTABILITY_FIX_ASSOC_FIELD_NAMES = 16;
299. /**
300. * Portability: makes Doctrine_Expression throw exception for unportable RDBMS expressions
301. * @see Doctrine::ATTR_PORTABILITY
302. */
303. const PORTABILITY_EXPR = 32;
304. /**
305. * Portability: turn on all portability features.
306. * @see Doctrine::ATTR_PORTABILITY
307. */
308. const PORTABILITY_ALL = 33;
310. /**
311. * LOCKMODE CONSTANTS
312. */
314. /**
315. * mode for optimistic locking
316. */
317. const LOCK_OPTIMISTIC = 0;
318. /**
319. * mode for pessimistic locking
320. */
321. const LOCK_PESSIMISTIC = 1;
322. /**
323. * EXPORT CONSTANTS
324. */
326. /**
327. * turns of exporting
328. */
329. const EXPORT_NONE = 0;
330. /**
331. * export tables
332. */
333. const EXPORT_TABLES = 1;
334. /**
335. * export constraints
336. */
337. const EXPORT_CONSTRAINTS = 2;
338. /**
339. * export all
340. */
341. const EXPORT_ALL = 3;
344. /**
345. * constant for auto_increment identifier
346. */
347. const IDENTIFIER_AUTOINC = 1;
348. /**
349. * constant for sequence identifier
350. */
351. const IDENTIFIER_SEQUENCE = 2;
352. /**
353. * constant for normal identifier
354. */
355. const IDENTIFIER_NATURAL = 3;
356. /**
357. * constant for composite identifier
358. */
359. const IDENTIFIER_COMPOSITE = 4;
360. /**
361. * constructor
362. */
363. public function __construct()
364. {
365. throw new Doctrine_Exception('Doctrine is static class. No instances can be created.');
366. }
367. /**
368. * @var string $path doctrine root directory
369. */
370. private static $path;
371. /**
372. * @var boolean $_debug
373. */
374. private static $_debug = false;
376. public static function debug($bool = null)
377. {
378. if ($bool !== null) {
379. self::$_debug = (bool) $bool;
380. }
381. return self::$_debug;
382. }
383. /**
384. * getPath
385. * returns the doctrine root
386. *
387. * @return string
388. */
389. public static function getPath()
390. {
391. if ( ! self::$path) {
392. self::$path = dirname(__FILE__);
393. }
394. return self::$path;
395. }
396. /**
397. * loadAll
398. * loads all runtime classes
399. *
400. * @return void
401. */
402. public static function loadAll()
403. {
404. $classes = Doctrine_Compiler::getRuntimeClasses();
406. foreach ($classes as $class) {
407. Doctrine::autoload($class);
408. }
409. }
410. /**
411. * importSchema
412. * method for importing existing schema to Doctrine_Record classes
413. *
414. * @param string $directory
415. * @param array $info
416. * @return boolean
417. */
418. public static function importSchema($directory, array $databases = array())
419. {
420. return Doctrine_Manager::connection()->import->importSchema($directory, $databases);
421. }
422. /**
423. * exportSchema
424. * method for exporting Doctrine_Record classes to a schema
425. *
426. * @param string $directory
427. */
428. public static function exportSchema($directory = null)
429. {
430. return Doctrine_Manager::connection()->export->exportSchema($directory);
431. }
432. /**
433. * exportSql
434. * method for exporting Doctrine_Record classes to a schema
435. *
436. * @param string $directory
437. */
438. public static function exportSql($directory = null)
439. {
440. return Doctrine_Manager::connection()->export->exportSql($directory);
441. }
442. /**
443. * compile
444. * method for making a single file of most used doctrine runtime components
445. * including the compiled file instead of multiple files (in worst
446. * cases dozens of files) can improve performance by an order of magnitude
447. *
448. * @param string $target
449. *
450. * @throws Doctrine_Exception
451. * @return void
452. */
453. public static function compile($target = null)
454. {
455. Doctrine_Compiler::compile($target);
456. }
457. /**
458. * simple autoload function
459. * returns true if the class was loaded, otherwise false
460. *
461. * @param string $classname
462. * @return boolean
463. */
464. public static function autoload($classname)
465. {
466. if (class_exists($classname, false)) {
467. return false;
468. }
469. if (! self::$path) {
470. self::$path = dirname(__FILE__);
471. }
472. $class = self::$path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR,$classname) . '.php';
474. if ( ! file_exists($class)) {
475. return false;
476. }
478. require_once($class);
480. return true;
481. }
482. /**
483. * dump
484. *
485. * dumps a given variable
486. *
487. * @param mixed $var a variable of any type
488. * @param boolean $output whether to output the content
489. * @return void|string
490. */
491. public static function dump($var, $output = true)
492. {
493. $ret = array();
494. switch (gettype($var)) {
495. case 'array':
496. $ret[] = 'Array(';
497. foreach ($var as $k => $v) {
498. $ret[] = $k . ' : ' . Doctrine::dump($v, false);
499. }
500. $ret[] = ")";
501. break;
502. case 'object':
503. $ret[] = 'Object(' . get_class($var) . ')';
504. break;
505. default:
506. $ret[] = var_export($var, true);
507. }
508. if ($output) {
509. print implode("\n", $ret);
510. }
511. return implode("\n", $ret);
512. }
513. /**
514. * returns table name from class name
515. *
516. * @param string $classname
517. * @return string
518. */
519. public static function tableize($classname)
520. {
521. return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $classname));
522. }
523. /**
524. * returns class name from table name
525. *
526. * @param string $tablename
527. * @return string
528. */
529. public static function classify($tablename)
530. {
531. return preg_replace('~(_?)(_)([\w])~e', '"$1".strtoupper("$3")', ucfirst($tablename));
532. }
533. /**
534. * checks for valid class name (uses camel case and underscores)
535. *
536. * @param string $classname
537. * @return boolean
538. */
539. public static function isValidClassname($classname)
540. {
541. if (preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $classname)) {
542. return false;
543. }
545. return true;
546. }
547. }
548. ?>