PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/php/MDB/Manager.php

https://bitbucket.org/adarshj/convenient_website
PHP | 2148 lines | 1657 code | 85 blank | 406 comment | 439 complexity | bd1ebbc794ec7f9e153393dfd80db32c MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@backendmedia.com> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: Manager.php,v 1.75.4.4 2004/03/10 14:42:59 lsmith Exp $
  46. //
  47. require_once('MDB/Parser.php');
  48. define('MDB_MANAGER_DUMP_ALL', 0);
  49. define('MDB_MANAGER_DUMP_STRUCTURE', 1);
  50. define('MDB_MANAGER_DUMP_CONTENT', 2);
  51. /**
  52. * The database manager is a class that provides a set of database
  53. * management services like installing, altering and dumping the data
  54. * structures of databases.
  55. *
  56. * @package MDB
  57. * @category Database
  58. * @author Lukas Smith <smith@backendmedia.com>
  59. */
  60. class MDB_Manager extends PEAR
  61. {
  62. // {{{ properties
  63. var $database;
  64. var $options = array(
  65. 'fail_on_invalid_names' => 1,
  66. 'debug' => 0
  67. );
  68. var $invalid_names = array(
  69. 'user' => array(),
  70. 'is' => array(),
  71. 'file' => array(
  72. 'oci' => array(),
  73. 'oracle' => array()
  74. ),
  75. 'notify' => array(
  76. 'pgsql' => array()
  77. ),
  78. 'restrict' => array(
  79. 'mysql' => array()
  80. ),
  81. 'password' => array(
  82. 'ibase' => array()
  83. )
  84. );
  85. var $default_values = array(
  86. 'integer' => 0,
  87. 'float' => 0,
  88. 'decimal' => 0,
  89. 'text' => '',
  90. 'timestamp' => '0001-01-01 00:00:00',
  91. 'date' => '0001-01-01',
  92. 'time' => '00:00:00'
  93. );
  94. var $warnings = array();
  95. var $database_definition = array(
  96. 'name' => '',
  97. 'create' => 0,
  98. 'TABLES' => array()
  99. );
  100. // }}}
  101. // {{{ raiseError()
  102. /**
  103. * This method is used to communicate an error and invoke error
  104. * callbacks etc. Basically a wrapper for PEAR::raiseError
  105. * without the message string.
  106. *
  107. * @param mixed $code integer error code, or a PEAR error object (all
  108. * other parameters are ignored if this parameter is an object
  109. * @param int $mode error mode, see PEAR_Error docs
  110. * @param mixed $options If error mode is PEAR_ERROR_TRIGGER, this is the
  111. * error level (E_USER_NOTICE etc). If error mode is
  112. * PEAR_ERROR_CALLBACK, this is the callback function, either as a
  113. * function name, or as an array of an object and method name. For
  114. * other error modes this parameter is ignored.
  115. * @param string $userinfo Extra debug information. Defaults to the last
  116. * query and native error code.
  117. * @param mixed $nativecode Native error code, integer or string depending
  118. * the backend.
  119. * @return object a PEAR error object
  120. * @access public
  121. * @see PEAR_Error
  122. */
  123. function &raiseError($code = MDB_MANAGER_ERROR, $mode = NULL, $options = NULL,
  124. $userinfo = NULL, $nativecode = NULL)
  125. {
  126. // The error is yet a MDB error object
  127. if(is_object($code)) {
  128. $err = PEAR::raiseError($code, NULL, NULL, NULL, NULL, NULL, TRUE);
  129. return($err);
  130. }
  131. $err = PEAR::raiseError(NULL, $code, $mode, $options, $userinfo,
  132. 'MDB_Error', TRUE);
  133. return($err);
  134. }
  135. // }}}
  136. // {{{ captureDebugOutput()
  137. /**
  138. * set a debug handler
  139. *
  140. * @param string $capture name of the function that should be used in
  141. * debug()
  142. * @access public
  143. * @see debug()
  144. */
  145. function captureDebugOutput($capture)
  146. {
  147. $this->options['debug'] = $capture;
  148. $this->database->captureDebugOutput(1);
  149. }
  150. // }}}
  151. // {{{ debugOutput()
  152. /**
  153. * output debug info
  154. *
  155. * @return string content of the debug_output class variable
  156. * @access public
  157. */
  158. function debugOutput()
  159. {
  160. return($this->database->debugOutput());
  161. }
  162. // }}}
  163. // {{{ resetWarnings()
  164. /**
  165. * reset the warning array
  166. *
  167. * @access public
  168. */
  169. function resetWarnings()
  170. {
  171. $this->warnings = array();
  172. }
  173. // }}}
  174. // {{{ getWarnings()
  175. /**
  176. * get all warnings in reverse order.
  177. * This means that the last warning is the first element in the array
  178. *
  179. * @return array with warnings
  180. * @access public
  181. * @see resetWarnings()
  182. */
  183. function getWarnings()
  184. {
  185. return array_reverse($this->warnings);
  186. }
  187. // }}}
  188. // {{{ setOption()
  189. /**
  190. * set the option for the db class
  191. *
  192. * @param string $option option name
  193. * @param mixed $value value for the option
  194. * @return mixed MDB_OK or MDB_Error
  195. * @access public
  196. */
  197. function setOption($option, $value)
  198. {
  199. if(isset($this->options[$option])) {
  200. $this->options[$option] = $value;
  201. return(MDB_OK);
  202. }
  203. return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, "unknown option $option"));
  204. }
  205. // }}}
  206. // {{{ getOption()
  207. /**
  208. * returns the value of an option
  209. *
  210. * @param string $option option name
  211. * @return mixed the option value or error object
  212. * @access public
  213. */
  214. function getOption($option)
  215. {
  216. if(isset($this->options[$option])) {
  217. return($this->options[$option]);
  218. }
  219. return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, "unknown option $option"));
  220. }
  221. // }}}
  222. // {{{ connect()
  223. /**
  224. * Create a new MDB connection object and connect to the specified
  225. * database
  226. *
  227. * @param mixed $dbinfo 'data source name', see the MDB::parseDSN
  228. * method for a description of the dsn format.
  229. * Can also be specified as an array of the
  230. * format returned by MDB::parseDSN.
  231. * Finally you can also pass an existing db
  232. * object to be used.
  233. * @param mixed $options An associative array of option names and
  234. * their values.
  235. * @return mixed MDB_OK on success, or a MDB error object
  236. * @access public
  237. * @see MDB::parseDSN
  238. */
  239. function &connect(&$dbinfo, $options = FALSE)
  240. {
  241. if(is_object($this->database) && !MDB::isError($this->database)) {
  242. $this->disconnect();
  243. }
  244. if(is_object($dbinfo)) {
  245. $this->database =& $dbinfo;
  246. } else {
  247. $this->database =& MDB::connect($dbinfo, $options);
  248. if(MDB::isError($this->database)) {
  249. return($this->database);
  250. }
  251. }
  252. if(is_array($options)) {
  253. $this->options = array_merge($options, $this->options);
  254. }
  255. return(MDB_OK);
  256. }
  257. // }}}
  258. // {{{ disconnect()
  259. /**
  260. * Log out and disconnect from the database.
  261. *
  262. * @access public
  263. */
  264. function disconnect()
  265. {
  266. if(is_object($this->database) && !MDB::isError($this->database)) {
  267. $this->database->disconnect();
  268. unset($this->database);
  269. }
  270. }
  271. // }}}
  272. // {{{ setDatabase()
  273. /**
  274. * Select a different database
  275. *
  276. * @param string $name name of the database that should be selected
  277. * @return string name of the database previously connected to
  278. * @access public
  279. */
  280. function setDatabase($name)
  281. {
  282. return($this->database->setDatabase($name));
  283. }
  284. // }}}
  285. // {{{ _createTable()
  286. /**
  287. * create a table and inititialize the table if data is available
  288. *
  289. * @param string $table_name name of the table to be created
  290. * @param array $table multi dimensional array that containts the
  291. * structure and optional data of the table
  292. * @param boolean $overwrite determine if the table/index should be
  293. overwritten if it already exists
  294. * @return mixed MDB_OK on success, or a MDB error object
  295. * @access private
  296. */
  297. function _createTable($table_name, $table, $overwrite = FALSE)
  298. {
  299. $this->expectError(MDB_ERROR_ALREADY_EXISTS);
  300. $result = $this->database->createTable($table_name, $table['FIELDS']);
  301. $this->popExpect();
  302. if(MDB::isError($result)) {
  303. if($result->getCode() === MDB_ERROR_ALREADY_EXISTS) {
  304. $this->warnings[] = 'Table already exists: '.$table_name;
  305. if($overwrite) {
  306. $this->database->debug('Overwritting Table');
  307. $result = $this->database->dropTable($table_name);
  308. if(MDB::isError($result)) {
  309. return($result);
  310. }
  311. $result = $this->database->createTable($table_name, $table['FIELDS']);
  312. if(MDB::isError($result)) {
  313. return($result);
  314. }
  315. } else {
  316. $result = MDB_OK;
  317. }
  318. } else {
  319. $this->database->debug('Create table error: '.$table_name);
  320. return($result);
  321. }
  322. }
  323. if(isset($table['initialization']) && is_array($table['initialization'])) {
  324. foreach($table['initialization'] as $instruction) {
  325. switch($instruction['type']) {
  326. case 'insert':
  327. $query_fields = $query_values = array();
  328. if(isset($instruction['FIELDS']) && is_array($instruction['FIELDS'])) {
  329. foreach($instruction['FIELDS'] as $field_name => $field) {
  330. $query_fields[] = $field_name;
  331. $query_values[] = '?';
  332. }
  333. $query_fields = implode(',',$query_fields);
  334. $query_values = implode(',',$query_values);
  335. $result = $prepared_query = $this->database->prepareQuery(
  336. "INSERT INTO $table_name ($query_fields) VALUES ($query_values)");
  337. }
  338. if(!MDB::isError($prepared_query)) {
  339. if(isset($instruction['FIELDS']) && is_array($instruction['FIELDS'])) {
  340. $lobs = array();
  341. $field_number = 0;
  342. foreach($instruction['FIELDS'] as $field_name => $field) {
  343. $field_number++;
  344. $query = $field_name;
  345. switch($table['FIELDS'][$field_name]['type']) {
  346. case 'integer':
  347. $result = $this->database->setParamInteger($prepared_query,
  348. $field_number, intval($field));
  349. break;
  350. case 'text':
  351. $result = $this->database->setParamText($prepared_query,
  352. $field_number, $field);
  353. break;
  354. case 'clob':
  355. $lob_definition = array(
  356. 'Database' => $this->database,
  357. 'Error' => '',
  358. 'Data' => $field
  359. );
  360. if(MDB::isError($result = $this->database->createLob($lob_definition)))
  361. {
  362. break;
  363. }
  364. $lob = count($lobs);
  365. $lobs[$lob] = $result;
  366. $result = $this->database->setParamClob($prepared_query,
  367. $field_number, $lobs[$lob], $field_name);
  368. break;
  369. case 'blob':
  370. $lob_definition = array(
  371. 'Database' => $this->database,
  372. 'Error' => '',
  373. 'Data' => $field
  374. );
  375. if(MDB::isError($result = $this->database->createLob($lob_definition))) {
  376. break;
  377. }
  378. $lob = count($lobs);
  379. $lobs[$lob] = $result;
  380. $result = $this->database->setParamBlob($prepared_query,
  381. $field_number, $lobs[$lob], $field_name);
  382. break;
  383. case 'boolean':
  384. $result = $this->database->setParamBoolean($prepared_query,
  385. $field_number, intval($field));
  386. break;
  387. case 'date':
  388. $result = $this->database->setParamDate($prepared_query,
  389. $field_number, $field);
  390. break;
  391. case 'timestamp':
  392. $result = $this->database->setParamTimestamp($prepared_query,
  393. $field_number, $field);
  394. break;
  395. case 'time':
  396. $result = $this->database->setParamTime($prepared_query,
  397. $field_number, $field);
  398. break;
  399. case 'float':
  400. $result = $this->database->setParamFloat($prepared_query,
  401. $field_number, doubleval($field));
  402. break;
  403. case 'decimal':
  404. $result = $this->database->setParamDecimal($prepared_query,
  405. $field_number, $field);
  406. break;
  407. default:
  408. $result = $this->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
  409. 'type "'.$field['type'].'" is not yet supported');
  410. break;
  411. }
  412. if(MDB::isError($result)) {
  413. break;
  414. }
  415. }
  416. }
  417. if(!MDB::isError($result)) {
  418. $result = $this->database->executeQuery($prepared_query);
  419. }
  420. for($lob = 0; $lob < count($lobs); $lob++) {
  421. $this->database->destroyLOB($lobs[$lob]);
  422. }
  423. $this->database->freePreparedQuery($prepared_query);
  424. }
  425. break;
  426. }
  427. }
  428. };
  429. if(!MDB::isError($result) && isset($table['INDEXES']) && is_array($table['INDEXES'])) {
  430. if(!$this->database->support('Indexes')) {
  431. return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL,
  432. 'indexes are not supported'));
  433. }
  434. foreach($table['INDEXES'] as $index_name => $index) {
  435. $this->expectError(MDB_ERROR_ALREADY_EXISTS);
  436. $result = $this->database->createIndex($table_name, $index_name, $index);
  437. $this->popExpect();
  438. if(MDB::isError($result)) {
  439. if($result->getCode() === MDB_ERROR_ALREADY_EXISTS) {
  440. $this->warnings[] = 'Index already exists: '.$index_name;
  441. if($overwrite) {
  442. $this->database->debug('Overwritting Index');
  443. $result = $this->database->dropIndex($table_name, $index_name);
  444. if(MDB::isError($result)) {
  445. break;
  446. }
  447. $result = $this->database->createIndex($table_name, $index_name, $index);
  448. if(MDB::isError($result)) {
  449. break;
  450. }
  451. } else {
  452. $result = MDB_OK;
  453. }
  454. } else {
  455. $this->database->debug('Create index error: '.$table_name);
  456. break;
  457. }
  458. }
  459. }
  460. }
  461. if(MDB::isError($result)) {
  462. $result = $this->database->dropTable($table_name);
  463. if(MDB::isError($result)) {
  464. $result = $this->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
  465. 'could not drop the table ('
  466. .$result->getMessage().' ('.$result->getUserinfo(),'))',
  467. 'MDB_Error', TRUE);
  468. }
  469. return($result);
  470. }
  471. return(MDB_OK);
  472. }
  473. // }}}
  474. // {{{ _dropTable()
  475. /**
  476. * drop a table
  477. *
  478. * @param string $table_name name of the table to be dropped
  479. * @return mixed MDB_OK on success, or a MDB error object
  480. * @access private
  481. */
  482. function _dropTable($table_name)
  483. {
  484. return($this->database->dropTable($table_name));
  485. }
  486. // }}}
  487. // {{{ _createSequence()
  488. /**
  489. * create a sequence
  490. *
  491. * @param string $sequence_name name of the sequence to be created
  492. * @param array $sequence multi dimensional array that containts the
  493. * structure and optional data of the table
  494. * @param string $created_on_table
  495. * @param boolean $overwrite determine if the sequence should be overwritten
  496. if it already exists
  497. * @return mixed MDB_OK on success, or a MDB error object
  498. * @access private
  499. */
  500. function _createSequence($sequence_name, $sequence, $created_on_table, $overwrite = FALSE)
  501. {
  502. if(!$this->database->support('Sequences')) {
  503. return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL,
  504. 'sequences are not supported'));
  505. }
  506. if(!isset($sequence_name) || !strcmp($sequence_name, '')) {
  507. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  508. 'no valid sequence name specified'));
  509. }
  510. $this->database->debug('Create sequence: '.$sequence_name);
  511. if(isset($sequence['start']) && $sequence['start'] != '') {
  512. $start = $sequence['start'];
  513. } else if(isset($sequence['on']) && !$created_on_table) {
  514. $table = $sequence['on']['table'];
  515. $field = $sequence['on']['field'];
  516. if($this->database->support('Summaryfunctions')) {
  517. $field = "MAX($field)";
  518. }
  519. $start = $this->database->queryOne("SELECT $field FROM $table");
  520. if(MDB::isError($start)) {
  521. return($start);
  522. }
  523. } else {
  524. $start = 1;
  525. }
  526. $this->expectError(MDB_ERROR_ALREADY_EXISTS);
  527. $result = $this->database->createSequence($sequence_name, $start);
  528. $this->popExpect();
  529. if(MDB::isError($result)) {
  530. if($result->getCode() === MDB_ERROR_ALREADY_EXISTS) {
  531. $this->warnings[] = 'Sequence already exists: '.$sequence_name;
  532. if($overwrite) {
  533. $this->database->debug('Overwritting Sequence');
  534. $result = $this->database->dropSequence($sequence_name);
  535. if(MDB::isError($result)) {
  536. return($result);
  537. }
  538. $result = $this->database->createSequence($sequence_name, $start);
  539. if(MDB::isError($result)) {
  540. return($result);
  541. }
  542. } else {
  543. return(MDB_OK);
  544. }
  545. } else {
  546. $this->database->debug('Create sequence error: '.$sequence_name);
  547. return($result);
  548. }
  549. }
  550. }
  551. // }}}
  552. // {{{ _dropSequence()
  553. /**
  554. * drop a table
  555. *
  556. * @param string $sequence_name name of the sequence to be dropped
  557. * @return mixed MDB_OK on success, or a MDB error object
  558. * @access private
  559. */
  560. function _dropSequence($sequence_name)
  561. {
  562. if(!$this->database->support('Sequences')) {
  563. return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL,
  564. 'sequences are not supported'));
  565. }
  566. $this->database->debug('Dropping sequence: '.$sequence_name);
  567. if(!isset($sequence_name) || !strcmp($sequence_name, '')) {
  568. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  569. 'no valid sequence name specified'));
  570. }
  571. return($this->database->dropSequence($sequence_name));
  572. }
  573. // }}}
  574. // {{{ _createDatabase()
  575. /**
  576. * Create a database space within which may be created database objects
  577. * like tables, indexes and sequences. The implementation of this function
  578. * is highly DBMS specific and may require special permissions to run
  579. * successfully. Consult the documentation or the DBMS drivers that you
  580. * use to be aware of eventual configuration requirements.
  581. *
  582. * @return mixed MDB_OK on success, or a MDB error object
  583. * @access private
  584. */
  585. function _createDatabase()
  586. {
  587. if(!isset($this->database_definition['name'])
  588. || !strcmp($this->database_definition['name'], '')
  589. ) {
  590. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  591. 'no valid database name specified'));
  592. }
  593. $create = (isset($this->database_definition['create']) && $this->database_definition['create']);
  594. $overwrite = (isset($this->database_definition['overwrite']) && $this->database_definition['overwrite']);
  595. if($create) {
  596. $this->database->debug('Create database: '.$this->database_definition['name']);
  597. $this->expectError(MDB_ERROR_ALREADY_EXISTS);
  598. $result = $this->database->createDatabase($this->database_definition['name']);
  599. $this->popExpect();
  600. if(MDB::isError($result)) {
  601. if($result->getCode() === MDB_ERROR_ALREADY_EXISTS) {
  602. $this->warnings[] = 'Database already exists: '.$this->database_definition['name'];
  603. if($overwrite) {
  604. $this->database->debug('Overwritting Database');
  605. $result = $this->database->dropDatabase($this->database_definition['name']);
  606. if(MDB::isError($result)) {
  607. return($result);
  608. }
  609. $result = $this->database->createDatabase($this->database_definition['name']);
  610. if(MDB::isError($result)) {
  611. return($result);
  612. }
  613. } else {
  614. $result = MDB_OK;
  615. }
  616. } else {
  617. $this->database->debug('Create database error.');
  618. return($result);
  619. }
  620. }
  621. }
  622. $previous_database_name = $this->database->setDatabase($this->database_definition['name']);
  623. if(($support_transactions = $this->database->support('Transactions'))
  624. && MDB::isError($result = $this->database->autoCommit(FALSE))
  625. ) {
  626. return($result);
  627. }
  628. $created_objects = 0;
  629. if(isset($this->database_definition['TABLES'])
  630. && is_array($this->database_definition['TABLES'])
  631. ) {
  632. foreach($this->database_definition['TABLES'] as $table_name => $table) {
  633. $result = $this->_createTable($table_name, $table, $overwrite);
  634. if(MDB::isError($result)) {
  635. break;
  636. }
  637. $created_objects++;
  638. }
  639. }
  640. if(!MDB::isError($result)
  641. && isset($this->database_definition['SEQUENCES'])
  642. && is_array($this->database_definition['SEQUENCES'])
  643. ) {
  644. foreach($this->database_definition['SEQUENCES'] as $sequence_name => $sequence) {
  645. $result = $this->_createSequence($sequence_name, $sequence, 0, $overwrite);
  646. if(MDB::isError($result)) {
  647. break;
  648. }
  649. $created_objects++;
  650. }
  651. }
  652. if(MDB::isError($result)) {
  653. if($created_objects) {
  654. if($support_transactions) {
  655. $res = $this->database->rollback();
  656. if(MDB::isError($res))
  657. $result = $this->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
  658. 'Could not rollback the partially created database alterations ('
  659. .$result->getMessage().' ('.$result->getUserinfo(),'))',
  660. 'MDB_Error', TRUE);
  661. } else {
  662. $result = $this->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
  663. 'the database was only partially created ('
  664. .$result->getMessage().' ('.$result->getUserinfo(),'))',
  665. 'MDB_Error', TRUE);
  666. }
  667. }
  668. } else {
  669. if($support_transactions) {
  670. $res = $this->database->autoCommit(TRUE);
  671. if(MDB::isError($res))
  672. $result = $this->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
  673. 'Could not end transaction after successfully created the database ('
  674. .$res->getMessage().' ('.$res->getUserinfo(),'))',
  675. 'MDB_Error', TRUE);
  676. }
  677. }
  678. $this->database->setDatabase($previous_database_name);
  679. if(MDB::isError($result)
  680. && $create
  681. && MDB::isError($res = $this->database->dropDatabase($this->database_definition['name']))
  682. ) {
  683. return($this->raiseError(MDB_ERROR_MANAGER, NULL, NULL,
  684. 'Could not drop the created database after unsuccessful creation attempt ('
  685. .$res->getMessage().' ('.$res->getUserinfo(),'))',
  686. 'MDB_Error', TRUE));
  687. }
  688. if(MDB::isError($result)) {
  689. return($result);
  690. }
  691. return(MDB_OK);
  692. }
  693. // }}}
  694. // {{{ _addDefinitionChange()
  695. /**
  696. * add change to an array of multiple changes
  697. *
  698. * @param array &$changes
  699. * @param string $definition
  700. * @param string $item
  701. * @param array $change
  702. * @return mixed MDB_OK on success, or a MDB error object
  703. * @access private
  704. */
  705. function _addDefinitionChange(&$changes, $definition, $item, $change)
  706. {
  707. if(!isset($changes[$definition][$item])) {
  708. $changes[$definition][$item] = array();
  709. }
  710. foreach($change as $change_data_name => $change_data) {
  711. if(isset($change_data) && is_array($change_data)) {
  712. if(!isset($changes[$definition][$item][$change_data_name])) {
  713. $changes[$definition][$item][$change_data_name] = array();
  714. }
  715. foreach($change_data as $change_part_name => $change_part) {
  716. $changes[$definition][$item][$change_data_name][$change_part_name] = $change_part;
  717. }
  718. } else {
  719. $changes[$definition][$item][$change_data_name] = $change_data;
  720. }
  721. }
  722. return(MDB_OK);
  723. }
  724. // }}}
  725. // {{{ _compareDefinitions()
  726. /**
  727. * compare a previous definition with the currenlty parsed definition
  728. *
  729. * @param array multi dimensional array that contains the previous definition
  730. * @return mixed array of changes on success, or a MDB error object
  731. * @access private
  732. */
  733. function _compareDefinitions($previous_definition)
  734. {
  735. $defined_tables = $changes = array();
  736. if(isset($this->database_definition['TABLES']) && is_array($this->database_definition['TABLES'])) {
  737. foreach($this->database_definition['TABLES'] as $table_name => $table) {
  738. $was_table_name = $table['was'];
  739. if(isset($previous_definition['TABLES'][$table_name])
  740. && isset($previous_definition['TABLES'][$table_name]['was'])
  741. && !strcmp($previous_definition['TABLES'][$table_name]['was'], $was_table_name)
  742. ) {
  743. $was_table_name = $table_name;
  744. }
  745. if(isset($previous_definition['TABLES'][$was_table_name])) {
  746. if(strcmp($was_table_name, $table_name)) {
  747. $this->_addDefinitionChange($changes, 'TABLES', $was_table_name, array('name' => $table_name));
  748. $this->database->debug("Renamed table '$was_table_name' to '$table_name'");
  749. }
  750. if(isset($defined_tables[$was_table_name])) {
  751. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  752. 'the table "'.$was_table_name.'" was specified as base of more than of table of the database',
  753. 'MDB_Error', TRUE));
  754. }
  755. $defined_tables[$was_table_name] = 1;
  756. $previous_fields = $previous_definition['TABLES'][$was_table_name]['FIELDS'];
  757. $defined_fields = array();
  758. if(isset($table['FIELDS']) && is_array($table['FIELDS'])) {
  759. foreach($table['FIELDS'] as $field_name => $field) {
  760. $was_field_name = $field['was'];
  761. if(isset($previous_fields[$field_name])
  762. && isset($previous_fields[$field_name]['was'])
  763. && !strcmp($previous_fields[$field_name]['was'], $was_field_name)
  764. ) {
  765. $was_field_name = $field_name;
  766. }
  767. if(isset($previous_fields[$was_field_name])) {
  768. if(strcmp($was_field_name, $field_name)) {
  769. $query = $this->database->getFieldDeclaration($field_name, $field);
  770. if(MDB::isError($query)) {
  771. return($query);
  772. }
  773. $this->_addDefinitionChange($changes, 'TABLES', $was_table_name,
  774. array(
  775. 'RenamedFields' => array(
  776. $was_field_name => array(
  777. 'name' => $field_name,
  778. 'Declaration' => $query
  779. )
  780. )
  781. )
  782. );
  783. $this->database->debug("Renamed field '$was_field_name' to '$field_name' in table '$table_name'");
  784. }
  785. if(isset($defined_fields[$was_field_name])) {
  786. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  787. 'the field "'.$was_table_name.'" was specified as base of more than one field of table',
  788. 'MDB_Error', TRUE));
  789. }
  790. $defined_fields[$was_field_name] = 1;
  791. $change = array();
  792. if($field['type'] == $previous_fields[$was_field_name]['type']) {
  793. switch($field['type']) {
  794. case 'integer':
  795. $previous_unsigned = isset($previous_fields[$was_field_name]['unsigned']);
  796. $unsigned = isset($fields[$field_name]['unsigned']);
  797. if(strcmp($previous_unsigned, $unsigned)) {
  798. $change['unsigned'] = $unsigned;
  799. $this->database->debug("Changed field '$field_name' type from '".($previous_unsigned ? 'unsigned ' : '').$previous_fields[$was_field_name]['type']."' to '".($unsigned ? 'unsigned ' : '').$field['type']."' in table '$table_name'");
  800. }
  801. break;
  802. case 'text':
  803. case 'clob':
  804. case 'blob':
  805. $previous_length = (isset($previous_fields[$was_field_name]['length']) ? $previous_fields[$was_field_name]['length'] : 0);
  806. $length = (isset($field['length']) ? $field['length'] : 0);
  807. if(strcmp($previous_length, $length)) {
  808. $change['length'] = $length;
  809. $this->database->debug("Changed field '$field_name' length from '".$previous_fields[$was_field_name]['type'].($previous_length == 0 ? ' no length' : "($previous_length)")."' to '".$field['type'].($length == 0 ? ' no length' : "($length)")."' in table '$table_name'");
  810. }
  811. break;
  812. case 'date':
  813. case 'timestamp':
  814. case 'time':
  815. case 'boolean':
  816. case 'float':
  817. case 'decimal':
  818. break;
  819. default:
  820. return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL,
  821. 'type "'.$field['type'].'" is not yet supported',
  822. 'MDB_Error', TRUE));
  823. }
  824. $previous_notnull = isset($previous_fields[$was_field_name]['notnull']);
  825. $notnull = isset($field['notnull']);
  826. if($previous_notnull != $notnull) {
  827. $change['ChangedNotNull'] = 1;
  828. if($notnull) {
  829. $change['notnull'] = isset($field['notnull']);
  830. }
  831. $this->database->debug("Changed field '$field_name' notnull from $previous_notnull to $notnull in table '$table_name'");
  832. }
  833. $previous_default = isset($previous_fields[$was_field_name]['default']);
  834. $default = isset($field['default']);
  835. if(strcmp($previous_default, $default)) {
  836. $change['ChangedDefault'] = 1;
  837. if($default) {
  838. $change['default'] = $field['default'];
  839. }
  840. $this->database->debug("Changed field '$field_name' default from ".($previous_default ? "'".$previous_fields[$was_field_name]['default']."'" : 'NULL').' TO '.($default ? "'".$fields[$field_name]['default']."'" : 'NULL')." IN TABLE '$table_name'");
  841. } else {
  842. if($default
  843. && strcmp($previous_fields[$was_field_name]['default'], $field['default'])
  844. ) {
  845. $change['ChangedDefault'] = 1;
  846. $change['default'] = $field['default'];
  847. $this->database->debug("Changed field '$field_name' default from '".$previous_fields[$was_field_name]['default']."' to '".$fields[$field_name]['default']."' in table '$table_name'");
  848. }
  849. }
  850. } else {
  851. $change['type'] = $field['type'];
  852. $this->database->debug("Changed field '$field_name' type from '".$previous_fields[$was_field_name]['type']."' to '".$fields[$field_name]['type']."' in table '$table_name'");
  853. }
  854. if(count($change)) {
  855. $query = $this->database->getFieldDeclaration($field_name, $field);
  856. if(MDB::isError($query)) {
  857. return($query);
  858. }
  859. $change['Declaration'] = $query;
  860. $change['Definition'] = $field;
  861. $this->_addDefinitionChange($changes, 'TABLES', $was_table_name, array('ChangedFields' => array($field_name => $change)));
  862. }
  863. } else {
  864. if(strcmp($field_name, $was_field_name)) {
  865. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  866. 'it was specified a previous field name ("'
  867. .$was_field_name.'") for field "'.$field_name.'" of table "'
  868. .$table_name.'" that does not exist',
  869. 'MDB_Error', TRUE));
  870. }
  871. $query = $this->database->getFieldDeclaration($field_name, $field);
  872. if(MDB::isError($query)) {
  873. return($query);
  874. }
  875. $change['Declaration'] = $query;
  876. $this->_addDefinitionChange($changes, 'TABLES', $table_name, array('AddedFields' => array($field_name => $change)));
  877. $this->database->debug("Added field '$field_name' to table '$table_name'");
  878. }
  879. }
  880. }
  881. if(isset($previous_fields) && is_array($previous_fields)) {
  882. foreach ($previous_fields as $field_previous_name => $field_previous) {
  883. if(!isset($defined_fields[$field_previous_name])) {
  884. $this->_addDefinitionChange($changes, 'TABLES', $table_name, array('RemovedFields' => array($field_previous_name => array())));
  885. $this->database->debug("Removed field '$field_name' from table '$table_name'");
  886. }
  887. }
  888. }
  889. $indexes = array();
  890. if(isset($this->database_definition['TABLES'][$table_name]['INDEXES'])
  891. && is_array($this->database_definition['TABLES'][$table_name]['INDEXES'])
  892. ) {
  893. $indexes = $this->database_definition['TABLES'][$table_name]['INDEXES'];
  894. }
  895. $previous_indexes = array();
  896. if(isset($previous_definition['TABLES'][$was_table_name]['INDEXES'])
  897. && is_array($previous_definition['TABLES'][$was_table_name]['INDEXES'])
  898. ) {
  899. $previous_indexes = $previous_definition['TABLES'][$was_table_name]['INDEXES'];
  900. }
  901. $defined_indexes = array();
  902. foreach($indexes as $index_name => $index) {
  903. $was_index_name = $index['was'];
  904. if(isset($previous_indexes[$index_name])
  905. && isset($previous_indexes[$index_name]['was'])
  906. && !strcmp($previous_indexes[$index_name]['was'], $was_index_name)
  907. ) {
  908. $was_index_name = $index_name;
  909. }
  910. if(isset($previous_indexes[$was_index_name])) {
  911. $change = array();
  912. if(strcmp($was_index_name, $index_name)) {
  913. $change['name'] = $was_index_name;
  914. $this->database->debug("Changed index '$was_index_name' name to '$index_name' in table '$table_name'");
  915. }
  916. if(isset($defined_indexes[$was_index_name])) {
  917. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  918. 'the index "'.$was_index_name.'" was specified as base of'
  919. .' more than one index of table "'.$table_name.'"',
  920. 'MDB_Error', TRUE));
  921. }
  922. $defined_indexes[$was_index_name] = 1;
  923. $previous_unique = isset($previous_indexes[$was_index_name]['unique']);
  924. $unique = isset($index['unique']);
  925. if($previous_unique != $unique) {
  926. $change['ChangedUnique'] = 1;
  927. if($unique) {
  928. $change['unique'] = $unique;
  929. }
  930. $this->database->debug("Changed index '$index_name' unique from $previous_unique to $unique in table '$table_name'");
  931. }
  932. $defined_fields = array();
  933. $previous_fields = $previous_indexes[$was_index_name]['FIELDS'];
  934. if(isset($index['FIELDS']) && is_array($index['FIELDS'])) {
  935. foreach($index['FIELDS'] as $field_name => $field) {
  936. if(isset($previous_fields[$field_name])) {
  937. $defined_fields[$field_name] = 1;
  938. $sorting = (isset($field['sorting']) ? $field['sorting'] : '');
  939. $previous_sorting = (isset($previous_fields[$field_name]['sorting']) ? $previous_fields[$field_name]['sorting'] : '');
  940. if(strcmp($sorting, $previous_sorting)) {
  941. $this->database->debug("Changed index field '$field_name' sorting default from '$previous_sorting' to '$sorting' in table '$table_name'");
  942. $change['ChangedFields'] = 1;
  943. }
  944. } else {
  945. $change['ChangedFields'] = 1;
  946. $this->database->debug("Added field '$field_name' to index '$index_name' of table '$table_name'");
  947. }
  948. }
  949. }
  950. if(isset($previous_fields) && is_array($previous_fields)) {
  951. foreach($previous_fields as $field_name => $field) {
  952. if(!isset($defined_fields[$field_name])) {
  953. $change['ChangedFields'] = 1;
  954. $this->database->debug("Removed field '$field_name' from index '$index_name' of table '$table_name'");
  955. }
  956. }
  957. }
  958. if(count($change)) {
  959. $this->_addDefinitionChange($changes, 'INDEXES', $table_name,array('ChangedIndexes' => array($index_name => $change)));
  960. }
  961. } else {
  962. if(strcmp($index_name, $was_index_name)) {
  963. return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,
  964. 'it was specified a previous index name ("'.$was_index_name
  965. .') for index "'.$index_name.'" of table "'.$table_name.'" that does not exist',
  966. 'MDB_Error', TRUE));
  967. }
  968. $this->_addDefinitionChange($changes, 'INDEXES', $table_name,array('AddedIndexes' => array($index_name => $indexes[$index_name])));
  969. $this->database->debug("Added index '$index_name' to table '$table_name'");
  970. }
  971. }

Large files files are truncated, but you can click here to view the full file