xpdomanager.class.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * Copyright 2010-2015 by MODX, LLC.
  4. *
  5. * This file is part of xPDO.
  6. *
  7. * xPDO is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * xPDO is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * xPDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * The xPDOManager class provides data source management.
  22. *
  23. * @package xpdo
  24. * @subpackage om
  25. */
  26. /**
  27. * Provides data source management for an xPDO instance.
  28. *
  29. * These are utility functions that only need to be loaded under special
  30. * circumstances, such as creating tables, adding indexes, altering table
  31. * structures, etc. xPDOManager class implementations are specific to a
  32. * database driver and should include this base class in order to extend it.
  33. *
  34. * @abstract
  35. * @package xpdo
  36. * @subpackage om
  37. */
  38. abstract class xPDOManager {
  39. /**
  40. * @var xPDO A reference to the XPDO instance using this manager.
  41. * @access public
  42. */
  43. public $xpdo= null;
  44. /**
  45. * @var xPDOGenerator The generator class for forward and reverse
  46. * engineering tasks (loaded only on demand).
  47. */
  48. public $generator= null;
  49. /**
  50. * @var xPDOTransport The data transport class for migrating data.
  51. */
  52. public $transport= null;
  53. /**
  54. * Get a xPDOManager instance.
  55. *
  56. * @param object $xpdo A reference to a specific modDataSource instance.
  57. */
  58. public function __construct(& $xpdo) {
  59. if ($xpdo !== null && $xpdo instanceof xPDO) {
  60. $this->xpdo= & $xpdo;
  61. }
  62. }
  63. /**
  64. * Creates the physical container representing a data source.
  65. *
  66. * @param array $dsnArray An array of xPDO configuration properties.
  67. * @param string $username Database username with privileges to create tables.
  68. * @param string $password Database user password.
  69. * @param array $containerOptions An array of options for controlling the creation of the container.
  70. * @return boolean True if the database is created successfully or already exists.
  71. */
  72. abstract public function createSourceContainer($dsnArray = null, $username= null, $password= null, $containerOptions= array ());
  73. /**
  74. * Drops a physical data source container, if it exists.
  75. *
  76. * @param string $dsn Represents the database connection string.
  77. * @param string $username Database username with privileges to drop tables.
  78. * @param string $password Database user password.
  79. * @return boolean Returns true on successful drop, false on failure.
  80. */
  81. abstract public function removeSourceContainer($dsnArray = null, $username= null, $password= null);
  82. /**
  83. * Creates the container for a persistent data object.
  84. *
  85. * An object container is a synonym for a database table.
  86. *
  87. * @param string $className The class of object to create a source container for.
  88. * @return boolean Returns true on successful creation, false on failure.
  89. */
  90. abstract public function createObjectContainer($className);
  91. /**
  92. * Alter the structure of an existing persistent data object container.
  93. *
  94. * @param string $className The class of object to alter the container of.
  95. * @param array $options An array of options describing the alterations to be made.
  96. * @return boolean Returns true on successful alteration, false on failure.
  97. */
  98. abstract public function alterObjectContainer($className, array $options = array());
  99. /**
  100. * Drop an object container (i.e. database table), if it exists.
  101. *
  102. * @param string $className The object container to drop.
  103. * @return boolean Returns true on successful drop, false on failure.
  104. */
  105. abstract public function removeObjectContainer($className);
  106. /**
  107. * Add a field to an object container, e.g. ADD COLUMN.
  108. *
  109. * @param string $class The object class to add the field to.
  110. * @param string $name The name of the field to add.
  111. * @param array $options An array of options for the process.
  112. * @return boolean True if the column is added successfully, otherwise false.
  113. */
  114. abstract public function addField($class, $name, array $options = array());
  115. /**
  116. * Alter an existing field of an object container, e.g. ALTER COLUMN.
  117. *
  118. * @param string $class The object class to alter the field of.
  119. * @param string $name The name of the field to alter.
  120. * @param array $options An array of options for the process.
  121. * @return boolean True if the column is altered successfully, otherwise false.
  122. */
  123. abstract public function alterField($class, $name, array $options = array());
  124. /**
  125. * Remove a field from an object container, e.g. DROP COLUMN.
  126. *
  127. * @param string $class The object class to drop the field from.
  128. * @param string $name The name of the field to drop.
  129. * @param array $options An array of options for the process.
  130. * @return boolean True if the column is dropped successfully, otherwise false.
  131. */
  132. abstract public function removeField($class, $name, array $options = array());
  133. /**
  134. * Add an index to an object container, e.g. ADD INDEX.
  135. *
  136. * @param string $class The object class to add the index to.
  137. * @param string $name The name of the index to add.
  138. * @param array $options An array of options for the process.
  139. * @return boolean True if the index is added successfully, otherwise false.
  140. */
  141. abstract public function addIndex($class, $name, array $options = array());
  142. /**
  143. * Remove an index from an object container, e.g. DROP INDEX.
  144. *
  145. * @param string $class The object class to drop the index from.
  146. * @param string $name The name of the index to drop.
  147. * @param array $options An array of options for the process.
  148. * @return boolean True if the index is dropped successfully, otherwise false.
  149. */
  150. abstract public function removeIndex($class, $name, array $options = array());
  151. /**
  152. * Add a constraint to an object container, e.g. ADD CONSTRAINT.
  153. *
  154. * @param string $class The object class to add the constraint to.
  155. * @param string $name The name of the constraint to add.
  156. * @param array $options An array of options for the process.
  157. * @return boolean True if the constraint is added successfully, otherwise false.
  158. */
  159. abstract public function addConstraint($class, $name, array $options = array());
  160. /**
  161. * Remove a constraint from an object container, e.g. DROP CONSTRAINT.
  162. *
  163. * @param string $class The object class to drop the constraint from.
  164. * @param string $name The name of the constraint to drop.
  165. * @param array $options An array of options for the process.
  166. * @return boolean True if the constraint is dropped successfully, otherwise false.
  167. */
  168. abstract public function removeConstraint($class, $name, array $options = array());
  169. /**
  170. * Gets an XML schema parser / generator for this manager instance.
  171. *
  172. * @return xPDOGenerator A generator class for this manager.
  173. */
  174. public function getGenerator() {
  175. if ($this->generator === null || !$this->generator instanceof xPDOGenerator) {
  176. $loaded= include_once(XPDO_CORE_PATH . 'om/' . $this->xpdo->config['dbtype'] . '/xpdogenerator.class.php');
  177. if ($loaded) {
  178. $generatorClass = 'xPDOGenerator_' . $this->xpdo->config['dbtype'];
  179. $this->generator= new $generatorClass ($this);
  180. }
  181. if ($this->generator === null || !$this->generator instanceof xPDOGenerator) {
  182. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not load xPDOGenerator [{$generatorClass}] class.");
  183. }
  184. }
  185. return $this->generator;
  186. }
  187. /**
  188. * Gets a data transport mechanism for this xPDOManager instance.
  189. */
  190. public function getTransport() {
  191. if ($this->transport === null || !$this->transport instanceof xPDOTransport) {
  192. if (!isset($this->xpdo->config['xPDOTransport.class']) || !$transportClass= $this->xpdo->loadClass($this->xpdo->config['xPDOTransport.class'], '', false, true)) {
  193. $transportClass= $this->xpdo->loadClass('transport.xPDOTransport', XPDO_CORE_PATH, true, true);
  194. }
  195. if ($transportClass) {
  196. $this->transport= new $transportClass ($this);
  197. }
  198. if ($this->transport === null || !$this->transport instanceof xPDOTransport) {
  199. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "Could not load xPDOTransport [{$transportClass}] class.");
  200. }
  201. }
  202. return $this->transport;
  203. }
  204. /**
  205. * Get the SQL necessary to define a column for a specific database engine.
  206. *
  207. * @param string $class The name of the class the column represents a field of.
  208. * @param string $name The name of the field and physical database column.
  209. * @param string $meta The metadata defining the field.
  210. * @param array $options An array of options for the process.
  211. * @return string A string of SQL representing the column definition.
  212. */
  213. abstract protected function getColumnDef($class, $name, $meta, array $options = array());
  214. /**
  215. * Get the SQL necessary to define an index for a specific database engine.
  216. *
  217. * @param string $class The name of the class the index is defined for.
  218. * @param string $name The name of the index.
  219. * @param string $meta The metadata defining the index.
  220. * @param array $options An array of options for the process.
  221. * @return string A string of SQL representing the index definition.
  222. */
  223. abstract protected function getIndexDef($class, $name, $meta, array $options = array());
  224. }