/lib/xmldb/classes/XMLDBTable.class.php

https://github.com/jarednipper/HSU-common-code · PHP · 1080 lines · 692 code · 109 blank · 279 comment · 124 complexity · 5bc477c6e530d7083feb7560939af414 MD5 · raw file

  1. <?php // $Id: XMLDBTable.class.php,v 1.22 2007/10/10 05:25:14 nicolasconnault Exp $
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
  11. // //
  12. // This program is free software; you can redistribute it and/or modify //
  13. // it under the terms of the GNU General Public License as published by //
  14. // the Free Software Foundation; either version 2 of the License, or //
  15. // (at your option) any later version. //
  16. // //
  17. // This program is distributed in the hope that it will be useful, //
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  20. // GNU General Public License for more details: //
  21. // //
  22. // http://www.gnu.org/copyleft/gpl.html //
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////
  25. /// This class represent one XMLDB table
  26. class XMLDBTable extends XMLDBObject {
  27. var $fields;
  28. var $keys;
  29. var $indexes;
  30. /**
  31. * Creates one new XMLDBTable
  32. */
  33. function XMLDBTable($name) {
  34. parent::XMLDBObject($name);
  35. $this->fields = array();
  36. $this->keys = array();
  37. $this->indexes = array();
  38. }
  39. /**
  40. * Add one field to the table, allowing to specify the desired order
  41. * If it's not specified, then the field is added at the end
  42. */
  43. function addField(&$field, $after=NULL) {
  44. /// Calculate the previous and next fields
  45. $prevfield = NULL;
  46. $nextfield = NULL;
  47. if (!$after) {
  48. $allfields =& $this->getFields();
  49. if (!empty($allfields)) {
  50. end($allfields);
  51. $prevfield =& $allfields[key($allfields)];
  52. }
  53. } else {
  54. $prevfield =& $this->getField($after);
  55. }
  56. if ($prevfield && $prevfield->getNext()) {
  57. $nextfield =& $this->getField($prevfield->getNext());
  58. }
  59. /// Set current field previous and next attributes
  60. if ($prevfield) {
  61. $field->setPrevious($prevfield->getName());
  62. $prevfield->setNext($field->getName());
  63. }
  64. if ($nextfield) {
  65. $field->setNext($nextfield->getName());
  66. $nextfield->setPrevious($field->getName());
  67. }
  68. /// Some more attributes
  69. $field->setLoaded(true);
  70. $field->setChanged(true);
  71. /// Add the new field
  72. $this->fields[] = $field;
  73. /// Reorder the field
  74. $this->orderFields($this->fields);
  75. /// Recalculate the hash
  76. $this->calculateHash(true);
  77. /// We have one new field, so the table has changed
  78. $this->setChanged(true);
  79. return $field;
  80. }
  81. /**
  82. * Add one key to the table, allowing to specify the desired order
  83. * If it's not specified, then the key is added at the end
  84. */
  85. function addKey(&$key, $after=NULL) {
  86. /// Calculate the previous and next keys
  87. $prevkey = NULL;
  88. $nextkey = NULL;
  89. if (!$after) {
  90. $allkeys =& $this->getKeys();
  91. if (!empty($allkeys)) {
  92. end($allkeys);
  93. $prevkey =& $allkeys[key($allkeys)];
  94. }
  95. } else {
  96. $prevkey =& $this->getKey($after);
  97. }
  98. if ($prevkey && $prevkey->getNext()) {
  99. $nextkey =& $this->getKey($prevkey->getNext());
  100. }
  101. /// Set current key previous and next attributes
  102. if ($prevkey) {
  103. $key->setPrevious($prevkey->getName());
  104. $prevkey->setNext($key->getName());
  105. }
  106. if ($nextkey) {
  107. $key->setNext($nextkey->getName());
  108. $nextkey->setPrevious($key->getName());
  109. }
  110. /// Some more attributes
  111. $key->setLoaded(true);
  112. $key->setChanged(true);
  113. /// Add the new key
  114. $this->keys[] = $key;
  115. /// Reorder the keys
  116. $this->orderKeys($this->keys);
  117. /// Recalculate the hash
  118. $this->calculateHash(true);
  119. /// We have one new field, so the table has changed
  120. $this->setChanged(true);
  121. }
  122. /**
  123. * Add one index to the table, allowing to specify the desired order
  124. * If it's not specified, then the index is added at the end
  125. */
  126. function addIndex(&$index, $after=NULL) {
  127. /// Calculate the previous and next indexes
  128. $previndex = NULL;
  129. $nextindex = NULL;
  130. if (!$after) {
  131. $allindexes =& $this->getIndexes();
  132. if (!empty($allindexes)) {
  133. end($allindexes);
  134. $previndex =& $allindexes[key($allindexes)];
  135. }
  136. } else {
  137. $previndex =& $this->getIndex($after);
  138. }
  139. if ($previndex && $previndex->getNext()) {
  140. $nextindex =& $this->getIndex($previndex->getNext());
  141. }
  142. /// Set current index previous and next attributes
  143. if ($previndex) {
  144. $index->setPrevious($previndex->getName());
  145. $previndex->setNext($index->getName());
  146. }
  147. if ($nextindex) {
  148. $index->setNext($nextindex->getName());
  149. $nextindex->setPrevious($index->getName());
  150. }
  151. /// Some more attributes
  152. $index->setLoaded(true);
  153. $index->setChanged(true);
  154. /// Add the new index
  155. $this->indexes[] = $index;
  156. /// Reorder the indexes
  157. $this->orderIndexes($this->indexes);
  158. /// Recalculate the hash
  159. $this->calculateHash(true);
  160. /// We have one new index, so the table has changed
  161. $this->setChanged(true);
  162. }
  163. /**
  164. * This function will return the array of fields in the table
  165. */
  166. function &getFields() {
  167. return $this->fields;
  168. }
  169. /**
  170. * This function will return the array of keys in the table
  171. */
  172. function &getKeys() {
  173. return $this->keys;
  174. }
  175. /**
  176. * This function will return the array of indexes in the table
  177. */
  178. function &getIndexes() {
  179. return $this->indexes;
  180. }
  181. /**
  182. * Returns one XMLDBField
  183. */
  184. function &getField($fieldname) {
  185. $i = $this->findFieldInArray($fieldname);
  186. if ($i !== NULL) {
  187. return $this->fields[$i];
  188. }
  189. $null = NULL;
  190. return $null;
  191. }
  192. /**
  193. * Returns the position of one field in the array.
  194. */
  195. function &findFieldInArray($fieldname) {
  196. foreach ($this->fields as $i => $field) {
  197. if ($fieldname == $field->getName()) {
  198. return $i;
  199. }
  200. }
  201. $null = NULL;
  202. return $null;
  203. }
  204. /**
  205. * This function will reorder the array of fields
  206. */
  207. function orderFields() {
  208. $result = $this->orderElements($this->fields);
  209. if ($result) {
  210. $this->setFields($result);
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. /**
  217. * Returns one XMLDBKey
  218. */
  219. function &getKey($keyname) {
  220. $i = $this->findKeyInArray($keyname);
  221. if ($i !== NULL) {
  222. return $this->keys[$i];
  223. }
  224. $null = NULL;
  225. return $null;
  226. }
  227. /**
  228. * Returns the position of one key in the array.
  229. */
  230. function &findKeyInArray($keyname) {
  231. foreach ($this->keys as $i => $key) {
  232. if ($keyname == $key->getName()) {
  233. return $i;
  234. }
  235. }
  236. $null = NULL;
  237. return $null;
  238. }
  239. /**
  240. * This function will reorder the array of keys
  241. */
  242. function orderKeys() {
  243. $result = $this->orderElements($this->keys);
  244. if ($result) {
  245. $this->setKeys($result);
  246. return true;
  247. } else {
  248. return false;
  249. }
  250. }
  251. /**
  252. * Returns one XMLDBIndex
  253. */
  254. function &getIndex($indexname) {
  255. $i = $this->findIndexInArray($indexname);
  256. if ($i !== NULL) {
  257. return $this->indexes[$i];
  258. }
  259. $null = NULL;
  260. return $null;
  261. }
  262. /**
  263. * Returns the position of one index in the array.
  264. */
  265. function &findIndexInArray($indexname) {
  266. foreach ($this->indexes as $i => $index) {
  267. if ($indexname == $index->getName()) {
  268. return $i;
  269. }
  270. }
  271. $null = NULL;
  272. return $null;
  273. }
  274. /**
  275. * This function will reorder the array of indexes
  276. */
  277. function orderIndexes() {
  278. $result = $this->orderElements($this->indexes);
  279. if ($result) {
  280. $this->setIndexes($result);
  281. return true;
  282. } else {
  283. return false;
  284. }
  285. }
  286. /**
  287. * This function will set the array of fields in the table
  288. */
  289. function setFields($fields) {
  290. $this->fields = $fields;
  291. }
  292. /**
  293. * This function will set the array of keys in the table
  294. */
  295. function setKeys($keys) {
  296. $this->keys = $keys;
  297. }
  298. /**
  299. * This function will set the array of indexes in the table
  300. */
  301. function setIndexes($indexes) {
  302. $this->indexes = $indexes;
  303. }
  304. /**
  305. * Delete one field from the table
  306. */
  307. function deleteField($fieldname) {
  308. $field =& $this->getField($fieldname);
  309. if ($field) {
  310. $i = $this->findFieldInArray($fieldname);
  311. $prevfield = NULL;
  312. $nextfield = NULL;
  313. /// Look for prev and next field
  314. $prevfield =& $this->getField($field->getPrevious());
  315. $nextfield =& $this->getField($field->getNext());
  316. /// Change their previous and next attributes
  317. if ($prevfield) {
  318. $prevfield->setNext($field->getNext());
  319. }
  320. if ($nextfield) {
  321. $nextfield->setPrevious($field->getPrevious());
  322. }
  323. /// Delete the field
  324. unset($this->fields[$i]);
  325. /// Reorder the whole structure
  326. $this->orderFields($this->fields);
  327. /// Recalculate the hash
  328. $this->calculateHash(true);
  329. /// We have one deleted field, so the table has changed
  330. $this->setChanged(true);
  331. }
  332. }
  333. /**
  334. * Delete one key from the table
  335. */
  336. function deleteKey($keyname) {
  337. $key =& $this->getKey($keyname);
  338. if ($key) {
  339. $i = $this->findKeyInArray($keyname);
  340. $prevkey = NULL;
  341. $nextkey = NULL;
  342. /// Look for prev and next key
  343. $prevkey =& $this->getKey($key->getPrevious());
  344. $nextkey =& $this->getKey($key->getNext());
  345. /// Change their previous and next attributes
  346. if ($prevkey) {
  347. $prevkey->setNext($key->getNext());
  348. }
  349. if ($nextkey) {
  350. $nextkey->setPrevious($key->getPrevious());
  351. }
  352. /// Delete the key
  353. unset($this->keys[$i]);
  354. /// Reorder the Keys
  355. $this->orderKeys($this->keys);
  356. /// Recalculate the hash
  357. $this->calculateHash(true);
  358. /// We have one deleted key, so the table has changed
  359. $this->setChanged(true);
  360. }
  361. }
  362. /**
  363. * Delete one index from the table
  364. */
  365. function deleteIndex($indexname) {
  366. $index =& $this->getIndex($indexname);
  367. if ($index) {
  368. $i = $this->findIndexInArray($indexname);
  369. $previndex = NULL;
  370. $nextindex = NULL;
  371. /// Look for prev and next index
  372. $previndex =& $this->getIndex($index->getPrevious());
  373. $nextindex =& $this->getIndex($index->getNext());
  374. /// Change their previous and next attributes
  375. if ($previndex) {
  376. $previndex->setNext($index->getNext());
  377. }
  378. if ($nextindex) {
  379. $nextindex->setPrevious($index->getPrevious());
  380. }
  381. /// Delete the index
  382. unset($this->indexes[$i]);
  383. /// Reorder the indexes
  384. $this->orderIndexes($this->indexes);
  385. /// Recalculate the hash
  386. $this->calculateHash(true);
  387. /// We have one deleted index, so the table has changed
  388. $this->setChanged(true);
  389. }
  390. }
  391. /**
  392. * Load data from XML to the table
  393. */
  394. function arr2XMLDBTable($xmlarr) {
  395. global $CFG;
  396. $result = true;
  397. /// Debug the table
  398. /// traverse_xmlize($xmlarr); //Debug
  399. /// print_object ($GLOBALS['traverse_array']); //Debug
  400. /// $GLOBALS['traverse_array']=""; //Debug
  401. /// Process table attributes (name, comment, previoustable and nexttable)
  402. if (isset($xmlarr['@']['NAME'])) {
  403. $this->name = trim($xmlarr['@']['NAME']);
  404. } else {
  405. $this->errormsg = 'Missing NAME attribute';
  406. $this->debug($this->errormsg);
  407. $result = false;
  408. }
  409. if (isset($xmlarr['@']['COMMENT'])) {
  410. $this->comment = trim($xmlarr['@']['COMMENT']);
  411. } else if (!empty($CFG->xmldbdisablecommentchecking)) {
  412. $this->comment = '';
  413. } else {
  414. $this->errormsg = 'Missing COMMENT attribute';
  415. $this->debug($this->errormsg);
  416. $result = false;
  417. }
  418. if (isset($xmlarr['@']['PREVIOUS'])) {
  419. $this->previous = trim($xmlarr['@']['PREVIOUS']);
  420. }
  421. if (isset($xmlarr['@']['NEXT'])) {
  422. $this->next = trim($xmlarr['@']['NEXT']);
  423. }
  424. /// Iterate over fields
  425. if (isset($xmlarr['#']['FIELDS']['0']['#']['FIELD'])) {
  426. foreach ($xmlarr['#']['FIELDS']['0']['#']['FIELD'] as $xmlfield) {
  427. if (!$result) { //Skip on error
  428. continue;
  429. }
  430. $name = trim($xmlfield['@']['NAME']);
  431. $field = new XMLDBField($name);
  432. $field->arr2XMLDBField($xmlfield);
  433. $this->fields[] = $field;
  434. if (!$field->isLoaded()) {
  435. $this->errormsg = 'Problem loading field ' . $name;
  436. $this->debug($this->errormsg);
  437. $result = false;
  438. }
  439. }
  440. } else {
  441. $this->errormsg = 'Missing FIELDS section';
  442. $this->debug($this->errormsg);
  443. $result = false;
  444. }
  445. /// Perform some general checks over fields
  446. if ($result && $this->fields) {
  447. /// Check field names are ok (lowercase, a-z _-)
  448. if (!$this->checkNameValues($this->fields)) {
  449. $this->errormsg = 'Some FIELDS name values are incorrect';
  450. $this->debug($this->errormsg);
  451. $result = false;
  452. }
  453. /// Check previous & next are ok (duplicates and existing fields)
  454. $this->fixPrevNext($this->fields);
  455. if ($result && !$this->checkPreviousNextValues($this->fields)) {
  456. $this->errormsg = 'Some FIELDS previous/next values are incorrect';
  457. $this->debug($this->errormsg);
  458. $result = false;
  459. }
  460. /// Order fields
  461. if ($result && !$this->orderFields($this->fields)) {
  462. $this->errormsg = 'Error ordering the fields';
  463. $this->debug($this->errormsg);
  464. $result = false;
  465. }
  466. }
  467. /// Iterate over keys
  468. if (isset($xmlarr['#']['KEYS']['0']['#']['KEY'])) {
  469. foreach ($xmlarr['#']['KEYS']['0']['#']['KEY'] as $xmlkey) {
  470. if (!$result) { //Skip on error
  471. continue;
  472. }
  473. $name = trim($xmlkey['@']['NAME']);
  474. $key = new XMLDBKey($name);
  475. $key->arr2XMLDBKey($xmlkey);
  476. $this->keys[] = $key;
  477. if (!$key->isLoaded()) {
  478. $this->errormsg = 'Problem loading key ' . $name;
  479. $this->debug($this->errormsg);
  480. $result = false;
  481. }
  482. }
  483. } else {
  484. $this->errormsg = 'Missing KEYS section (at least one PK must exist)';
  485. $this->debug($this->errormsg);
  486. $result = false;
  487. }
  488. /// Perform some general checks over keys
  489. if ($result && $this->keys) {
  490. /// Check keys names are ok (lowercase, a-z _-)
  491. if (!$this->checkNameValues($this->keys)) {
  492. $this->errormsg = 'Some KEYS name values are incorrect';
  493. $this->debug($this->errormsg);
  494. $result = false;
  495. }
  496. /// Check previous & next are ok (duplicates and existing keys)
  497. $this->fixPrevNext($this->keys);
  498. if ($result && !$this->checkPreviousNextValues($this->keys)) {
  499. $this->errormsg = 'Some KEYS previous/next values are incorrect';
  500. $this->debug($this->errormsg);
  501. $result = false;
  502. }
  503. /// Order keys
  504. if ($result && !$this->orderKeys($this->keys)) {
  505. $this->errormsg = 'Error ordering the keys';
  506. $this->debug($this->errormsg);
  507. $result = false;
  508. }
  509. /// TODO: Only one PK
  510. /// TODO: Not keys with repeated fields
  511. /// TODO: Check fields and reffieds exist in table
  512. }
  513. /// Iterate over indexes
  514. if (isset($xmlarr['#']['INDEXES']['0']['#']['INDEX'])) {
  515. foreach ($xmlarr['#']['INDEXES']['0']['#']['INDEX'] as $xmlindex) {
  516. if (!$result) { //Skip on error
  517. continue;
  518. }
  519. $name = trim($xmlindex['@']['NAME']);
  520. $index = new XMLDBIndex($name);
  521. $index->arr2XMLDBIndex($xmlindex);
  522. $this->indexes[] = $index;
  523. if (!$index->isLoaded()) {
  524. $this->errormsg = 'Problem loading index ' . $name;
  525. $this->debug($this->errormsg);
  526. $result = false;
  527. }
  528. }
  529. }
  530. /// Perform some general checks over indexes
  531. if ($result && $this->indexes) {
  532. /// Check field names are ok (lowercase, a-z _-)
  533. if (!$this->checkNameValues($this->indexes)) {
  534. $this->errormsg = 'Some INDEXES name values are incorrect';
  535. $this->debug($this->errormsg);
  536. $result = false;
  537. }
  538. /// Check previous & next are ok (duplicates and existing INDEXES)
  539. $this->fixPrevNext($this->indexes);
  540. if ($result && !$this->checkPreviousNextValues($this->indexes)) {
  541. $this->errormsg = 'Some INDEXES previous/next values are incorrect';
  542. $this->debug($this->errormsg);
  543. $result = false;
  544. }
  545. /// Order indexes
  546. if ($result && !$this->orderIndexes($this->indexes)) {
  547. $this->errormsg = 'Error ordering the indexes';
  548. $this->debug($this->errormsg);
  549. $result = false;
  550. }
  551. /// TODO: Not indexes with repeated fields
  552. /// TODO: Check fields exist in table
  553. }
  554. /// Set some attributes
  555. if ($result) {
  556. $this->loaded = true;
  557. }
  558. $this->calculateHash();
  559. return $result;
  560. }
  561. /**
  562. * This function calculate and set the hash of one XMLDBTable
  563. */
  564. function calculateHash($recursive = false) {
  565. if (!$this->loaded) {
  566. $this->hash = NULL;
  567. } else {
  568. $key = $this->name . $this->comment;
  569. if ($this->fields) {
  570. foreach ($this->fields as $fie) {
  571. $field =& $this->getField($fie->getName());
  572. if ($recursive) {
  573. $field->calculateHash($recursive);
  574. }
  575. $key .= $field->getHash();
  576. }
  577. }
  578. if ($this->keys) {
  579. foreach ($this->keys as $ke) {
  580. $k =& $this->getKey($ke->getName());
  581. if ($recursive) {
  582. $k->calculateHash($recursive);
  583. }
  584. $key .= $k->getHash();
  585. }
  586. }
  587. if ($this->indexes) {
  588. foreach ($this->indexes as $in) {
  589. $index =& $this->getIndex($in->getName());
  590. if ($recursive) {
  591. $index->calculateHash($recursive);
  592. }
  593. $key .= $index->getHash();
  594. }
  595. }
  596. $this->hash = md5($key);
  597. }
  598. }
  599. /**
  600. * This function will output the XML text for one table
  601. */
  602. function xmlOutput() {
  603. $o = '';
  604. $o.= ' <TABLE NAME="' . $this->name . '"';
  605. if ($this->comment) {
  606. $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
  607. }
  608. if ($this->previous) {
  609. $o.= ' PREVIOUS="' . $this->previous . '"';
  610. }
  611. if ($this->next) {
  612. $o.= ' NEXT="' . $this->next . '"';
  613. }
  614. $o.= '>' . "\n";
  615. /// Now the fields
  616. if ($this->fields) {
  617. $o.= ' <FIELDS>' . "\n";
  618. foreach ($this->fields as $field) {
  619. $o.= $field->xmlOutput();
  620. }
  621. $o.= ' </FIELDS>' . "\n";
  622. }
  623. /// Now the keys
  624. if ($this->keys) {
  625. $o.= ' <KEYS>' . "\n";
  626. foreach ($this->keys as $key) {
  627. $o.= $key->xmlOutput();
  628. }
  629. $o.= ' </KEYS>' . "\n";
  630. }
  631. /// Now the indexes
  632. if ($this->indexes) {
  633. $o.= ' <INDEXES>' . "\n";
  634. foreach ($this->indexes as $index) {
  635. $o.= $index->xmlOutput();
  636. }
  637. $o.= ' </INDEXES>' . "\n";
  638. }
  639. $o.= ' </TABLE>' . "\n";
  640. return $o;
  641. }
  642. /**
  643. * This function will add one new field to the table with all
  644. * its attributes defined
  645. *
  646. * @param string name name of the field
  647. * @param string type XMLDB_TYPE_INTEGER, XMLDB_TYPE_NUMBER, XMLDB_TYPE_CHAR, XMLDB_TYPE_TEXT, XMLDB_TYPE_BINARY
  648. * @param string precision length for integers and chars, two-comma separated numbers for numbers and 'small', 'medium', 'big' for texts and binaries
  649. * @param string unsigned XMLDB_UNSIGNED or null (or false)
  650. * @param string notnull XMLDB_NOTNULL or null (or false)
  651. * @param string sequence XMLDB_SEQUENCE or null (or false)
  652. * @param string enum XMLDB_ENUM or null (or false)
  653. * @param array enumvalues an array of possible values if XMLDB_ENUM is set
  654. * @param string default meaningful default o null (or false)
  655. * @param string previous name of the previous field in the table or null (or false)
  656. */
  657. function addFieldInfo($name, $type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $enum=null, $enumvalues=null, $default=null, $previous=null) {
  658. $field = new XMLDBField($name);
  659. $field->setAttributes($type, $precision, $unsigned, $notnull, $sequence, $enum, $enumvalues, $default);
  660. $this->addField($field, $previous);
  661. return $field;
  662. }
  663. /**
  664. * This function will add one new key to the table with all
  665. * its attributes defined
  666. *
  667. * @param string name name of the key
  668. * @param string type XMLDB_KEY_PRIMARY, XMLDB_KEY_UNIQUE, XMLDB_KEY_FOREIGN
  669. * @param array fields an array of fieldnames to build the key over
  670. * @param string reftable name of the table the FK points to or null
  671. * @param array reffields an array of fieldnames in the FK table or null
  672. */
  673. function addKeyInfo($name, $type, $fields, $reftable=null, $reffields=null) {
  674. $key = new XMLDBKey($name);
  675. $key->setAttributes($type, $fields, $reftable, $reffields);
  676. $this->addKey($key);
  677. }
  678. /**
  679. * This function will add one new index to the table with all
  680. * its attributes defined
  681. *
  682. * @param string name name of the index
  683. * @param string type XMLDB_INDEX_UNIQUE, XMLDB_INDEX_NOTUNIQUE
  684. * @param array fields an array of fieldnames to build the index over
  685. */
  686. function addIndexInfo($name, $type, $fields) {
  687. $index = new XMLDBIndex($name);
  688. $index->setAttributes($type, $fields);
  689. $this->addIndex($index);
  690. }
  691. /**
  692. * This function will return all the errors found in one table
  693. * looking recursively inside each field/key/index. Returns
  694. * an array of errors or false
  695. */
  696. function getAllErrors() {
  697. $errors = array();
  698. /// First the table itself
  699. if ($this->getError()) {
  700. $errors[] = $this->getError();
  701. }
  702. /// Delegate to fields
  703. if ($fields = $this->getFields()) {
  704. foreach ($fields as $field) {
  705. if ($field->getError()) {
  706. $errors[] = $field->getError();
  707. }
  708. }
  709. }
  710. /// Delegate to keys
  711. if ($keys = $this->getKeys()) {
  712. foreach ($keys as $key) {
  713. if ($key->getError()) {
  714. $errors[] = $key->getError();
  715. }
  716. }
  717. }
  718. /// Delegate to indexes
  719. if ($indexes = $this->getIndexes()) {
  720. foreach ($indexes as $index) {
  721. if ($index->getError()) {
  722. $errors[] = $index->getError();
  723. }
  724. }
  725. }
  726. /// Return decision
  727. if (count($errors)) {
  728. return $errors;
  729. } else {
  730. return false;
  731. }
  732. }
  733. /**
  734. * This function will return the SQL code needed to create the table for the specified DB and
  735. * prefix. Just one simple wrapper over generators.
  736. */
  737. function getCreateTableSQL ($dbtype, $prefix, $statement_end=true) {
  738. $results = array();
  739. $classname = 'XMLDB' . $dbtype;
  740. $generator = new $classname();
  741. $generator->setPrefix($prefix);
  742. $results = $generator->getCreateTableSQL($this);
  743. if ($statement_end) {
  744. $results = $generator->getEndedStatements($results);
  745. }
  746. return $results;
  747. }
  748. /**
  749. * This function will return the SQL code needed to create the table for the specified DB and
  750. * prefix. Just one simple wrapper over generators.
  751. */
  752. function getRenameTableSQL ($dbtype, $prefix, $newname, $statement_end=true) {
  753. $results = array();
  754. $classname = 'XMLDB' . $dbtype;
  755. $generator = new $classname();
  756. $generator->setPrefix($prefix);
  757. $results = $generator->getRenameTableSQL($this, $newname);
  758. if ($statement_end) {
  759. $results = $generator->getEndedStatements($results);
  760. }
  761. return $results;
  762. }
  763. /**
  764. * This function will return the SQL code needed to drop the table for the specified DB and
  765. * prefix. Just one simple wrapper over generators.
  766. */
  767. function getDropTableSQL ($dbtype, $prefix, $statement_end=true) {
  768. $results = array();
  769. $classname = 'XMLDB' . $dbtype;
  770. $generator = new $classname();
  771. $generator->setPrefix($prefix);
  772. $results = $generator->getDropTableSQL($this);
  773. if ($statement_end) {
  774. $results = $generator->getEndedStatements($results);
  775. }
  776. return $results;
  777. }
  778. /**
  779. * This function will return the SQL code needed to add one field to the table for the specified DB and
  780. * prefix. Just one simple wrapper over generators.
  781. */
  782. function getAddFieldSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) {
  783. $results = array();
  784. $classname = 'XMLDB' . $dbtype;
  785. $generator = new $classname();
  786. $generator->setPrefix($prefix);
  787. $results = $generator->getAddFieldSQL($this, $xmldb_field);
  788. if ($statement_end) {
  789. $results = $generator->getEndedStatements($results);
  790. }
  791. return $results;
  792. }
  793. /**
  794. * This function will return the SQL code needed to drop one field from the table for the specified DB and
  795. * prefix. Just one simple wrapper over generators.
  796. */
  797. function getDropFieldSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) {
  798. $results = array();
  799. $classname = 'XMLDB' . $dbtype;
  800. $generator = new $classname();
  801. $generator->setPrefix($prefix);
  802. $results = $generator->getDropFieldSQL($this, $xmldb_field);
  803. if ($statement_end) {
  804. $results = $generator->getEndedStatements($results);
  805. }
  806. return $results;
  807. }
  808. /**
  809. * This function will return the SQL code needed to rename one field from the table for the specified DB and
  810. * prefix. Just one simple wrapper over generators.
  811. */
  812. function getRenameFieldSQL ($dbtype, $prefix, $xmldb_field, $newname, $statement_end=true) {
  813. $results = array();
  814. $classname = 'XMLDB' . $dbtype;
  815. $generator = new $classname();
  816. $generator->setPrefix($prefix);
  817. $results = $generator->getRenameFieldSQL($this, $xmldb_field, $newname);
  818. if ($statement_end) {
  819. $results = $generator->getEndedStatements($results);
  820. }
  821. return $results;
  822. }
  823. /**
  824. * This function will return the SQL code needed to alter one field in the table for the specified DB and
  825. * prefix. Just one simple wrapper over generators.
  826. */
  827. function getAlterFieldSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) {
  828. $results = array();
  829. $classname = 'XMLDB' . $dbtype;
  830. $generator = new $classname();
  831. $generator->setPrefix($prefix);
  832. $results = $generator->getAlterFieldSQL($this, $xmldb_field);
  833. if ($statement_end) {
  834. $results = $generator->getEndedStatements($results);
  835. }
  836. return $results;
  837. }
  838. /**
  839. * This function will return the SQL code needed to modify the enum of one field in the table for the specified DB and
  840. * prefix. Just one simple wrapper over generators.
  841. */
  842. function getModifyEnumSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) {
  843. $results = array();
  844. $classname = 'XMLDB' . $dbtype;
  845. $generator = new $classname();
  846. $generator->setPrefix($prefix);
  847. $results = $generator->getModifyEnumSQL($this, $xmldb_field);
  848. if ($statement_end) {
  849. $results = $generator->getEndedStatements($results);
  850. }
  851. return $results;
  852. }
  853. /**
  854. * This function will return the SQL code needed to modify the default of one field in the table for the specified DB and
  855. * prefix. Just one simple wrapper over generators.
  856. */
  857. function getModifyDefaultSQL ($dbtype, $prefix, $xmldb_field, $statement_end=true) {
  858. $results = array();
  859. $classname = 'XMLDB' . $dbtype;
  860. $generator = new $classname();
  861. $generator->setPrefix($prefix);
  862. $results = $generator->getModifyDefaultSQL($this, $xmldb_field);
  863. if ($statement_end) {
  864. $results = $generator->getEndedStatements($results);
  865. }
  866. return $results;
  867. }
  868. /**
  869. * This function will return the SQL code needed to add one key to the table for the specified DB and
  870. * prefix. Just one simple wrapper over generators.
  871. */
  872. function getAddKeySQL ($dbtype, $prefix, $xmldb_key, $statement_end=true) {
  873. $results = array();
  874. $classname = 'XMLDB' . $dbtype;
  875. $generator = new $classname();
  876. $generator->setPrefix($prefix);
  877. $results = $generator->getAddKeySQL($this, $xmldb_key);
  878. if ($statement_end) {
  879. $results = $generator->getEndedStatements($results);
  880. }
  881. return $results;
  882. }
  883. /**
  884. * This function will return the SQL code needed to drop one key from the table for the specified DB and
  885. * prefix. Just one simple wrapper over generators.
  886. */
  887. function getDropKeySQL ($dbtype, $prefix, $xmldb_key, $statement_end=true) {
  888. $results = array();
  889. $classname = 'XMLDB' . $dbtype;
  890. $generator = new $classname();
  891. $generator->setPrefix($prefix);
  892. $results = $generator->getDropKeySQL($this, $xmldb_key);
  893. if ($statement_end) {
  894. $results = $generator->getEndedStatements($results);
  895. }
  896. return $results;
  897. }
  898. /**
  899. * This function will return the SQL code needed to rename one key from the table for the specified DB and
  900. * prefix. Just one simple wrapper over generators.
  901. */
  902. function getRenameKeySQL ($dbtype, $prefix, $xmldb_key, $newname, $statement_end=true) {
  903. $results = array();
  904. $classname = 'XMLDB' . $dbtype;
  905. $generator = new $classname();
  906. $generator->setPrefix($prefix);
  907. $results = $generator->getRenameKeySQL($this, $xmldb_key, $newname);
  908. if ($statement_end) {
  909. $results = $generator->getEndedStatements($results);
  910. }
  911. return $results;
  912. }
  913. /**
  914. * This function will return the SQL code needed to add one index to the table for the specified DB and
  915. * prefix. Just one simple wrapper over generators.
  916. */
  917. function getAddIndexSQL ($dbtype, $prefix, $xmldb_index, $statement_end=true) {
  918. $results = array();
  919. $classname = 'XMLDB' . $dbtype;
  920. $generator = new $classname();
  921. $generator->setPrefix($prefix);
  922. $results = $generator->getAddIndexSQL($this, $xmldb_index);
  923. if ($statement_end) {
  924. $results = $generator->getEndedStatements($results);
  925. }
  926. return $results;
  927. }
  928. /**
  929. * This function will return the SQL code needed to drop one index from the table for the specified DB and
  930. * prefix. Just one simple wrapper over generators.
  931. */
  932. function getDropIndexSQL ($dbtype, $prefix, $xmldb_index, $statement_end=true) {
  933. $results = array();
  934. $classname = 'XMLDB' . $dbtype;
  935. $generator = new $classname();
  936. $generator->setPrefix($prefix);
  937. $results = $generator->getDropIndexSQL($this, $xmldb_index);
  938. if ($statement_end) {
  939. $results = $generator->getEndedStatements($results);
  940. }
  941. return $results;
  942. }
  943. /**
  944. * This function will return the SQL code needed to rename one index from the table for the specified DB and
  945. * prefix. Just one simple wrapper over generators.
  946. * Experimental. Shouldn't be used at all!
  947. */
  948. function getRenameIndexSQL ($dbtype, $prefix, $xmldb_index, $newname, $statement_end=true) {
  949. $results = array();
  950. $classname = 'XMLDB' . $dbtype;
  951. $generator = new $classname();
  952. $generator->setPrefix($prefix);
  953. $results = $generator->getRenameIndexSQL($this, $xmldb_index, $newname);
  954. if ($statement_end) {
  955. $results = $generator->getEndedStatements($results);
  956. }
  957. return $results;
  958. }
  959. /**
  960. * This function will return the name of the sequence created for the pk of the table specified
  961. * Just one simple wrapper over generators. Returns false if not found
  962. * Note that not all DB use sequences (only Oracle and PostgreSQL)
  963. */
  964. function getSequenceFromDB($dbtype, $prefix) {
  965. $classname = 'XMLDB' . $dbtype;
  966. $generator = new $classname();
  967. $generator->setPrefix($prefix);
  968. return $generator->getSequenceFromDB($this);
  969. }
  970. }
  971. ?>