xpdogenerator.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * SQLite classes for generating xPDOObject classes and maps from an xPDO schema.
  22. *
  23. * @package xpdo
  24. * @subpackage om.sqlite
  25. */
  26. /**
  27. * Include the parent {@link xPDOGenerator} class.
  28. */
  29. include_once (dirname(__DIR__) . '/xpdogenerator.class.php');
  30. /**
  31. * An extension for generating {@link xPDOObject} class and map files for SQLite.
  32. *
  33. * A SQLite-specific extension to an {@link xPDOManager} instance that can
  34. * generate class stub and meta-data map files from a provided XML schema of a
  35. * database structure.
  36. *
  37. * @package xpdo
  38. * @subpackage om.sqlite
  39. */
  40. class xPDOGenerator_sqlite extends xPDOGenerator {
  41. public function compile($path = '') {
  42. return false;
  43. }
  44. public function getIndex($index) {
  45. return '';
  46. }
  47. /**
  48. * Write an xPDO XML Schema from your database.
  49. *
  50. * @param string $schemaFile The name (including path) of the schemaFile you
  51. * want to write.
  52. * @param string $package Name of the package to generate the classes in.
  53. * @param string $baseClass The class which all classes in the package will
  54. * extend; by default this is set to {@link xPDOObject} and any
  55. * auto_increment fields with the column name 'id' will extend {@link
  56. * xPDOSimpleObject} automatically.
  57. * @param string $tablePrefix The table prefix for the current connection,
  58. * which will be removed from all of the generated class and table names.
  59. * Specify a prefix when creating a new {@link xPDO} instance to recreate
  60. * the tables with the same prefix, but still use the generic class names.
  61. * @param boolean $restrictPrefix Only reverse-engineer tables that have the
  62. * specified tablePrefix; if tablePrefix is empty, this is ignored.
  63. * @return boolean True on success, false on failure.
  64. */
  65. public function writeSchema($schemaFile, $package= '', $baseClass= '', $tablePrefix= '', $restrictPrefix= false) {
  66. if (empty ($package))
  67. $package= $this->manager->xpdo->package;
  68. if (empty ($baseClass))
  69. $baseClass= 'xPDOObject';
  70. if (empty ($tablePrefix))
  71. $tablePrefix= $this->manager->xpdo->config[xPDO::OPT_TABLE_PREFIX];
  72. $schemaVersion = xPDO::SCHEMA_VERSION;
  73. $xmlContent = array();
  74. $xmlContent[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  75. $xmlContent[] = "<model package=\"{$package}\" baseClass=\"{$baseClass}\" platform=\"sqlite\" version=\"{$schemaVersion}\">";
  76. //read list of tables
  77. $tableLike= ($tablePrefix && $restrictPrefix) ? " LIKE '{$tablePrefix}%'" : '';
  78. $tablesStmt= $this->manager->xpdo->query("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
  79. $tables= $tablesStmt->fetchAll(PDO::FETCH_NUM);
  80. if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, print_r($tables, true));
  81. foreach ($tables as $table) {
  82. $xmlObject= array();
  83. $xmlFields= array();
  84. $xmlIndices= array();
  85. if (!$tableName= $this->getTableName($table[0], $tablePrefix, $restrictPrefix)) {
  86. continue;
  87. }
  88. $class= $this->getClassName($tableName);
  89. $extends= $baseClass;
  90. $fieldsStmt= $this->manager->xpdo->query("PRAGMA table_info({$table[0]})");
  91. $fields= $fieldsStmt->fetchAll(PDO::FETCH_ASSOC);
  92. if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Fields for table {$table[0]}: " . print_r($fields, true));
  93. $cid = 0;
  94. foreach ($fields as $field) {
  95. $name = '';
  96. $type = '';
  97. $notnull = 0;
  98. $dflt_value = null;
  99. $pk = 0;
  100. extract($field, EXTR_OVERWRITE);
  101. $Field= $name;
  102. $PhpType= $this->manager->xpdo->driver->getPhpType($type);
  103. $Null= ' null="' . ($notnull ? 'false' : 'true') . '"';
  104. $Default= $this->getDefault($dflt_value);
  105. $Extra= '';
  106. if (!empty($pk)) {
  107. if (preg_match('/INT/i', $type)) {
  108. if ($baseClass === 'xPDOObject' && $Field === 'id') {
  109. $extends= 'xPDOSimpleObject';
  110. continue;
  111. } elseif ($cid == 0) {
  112. $Extra= ' generated="native"';
  113. }
  114. }
  115. $Key = ' index="pk"';
  116. } else {
  117. $Key = $this->getIndex($field);
  118. }
  119. $xmlFields[]= "\t\t<field key=\"{$Field}\" dbtype=\"{$type}\" phptype=\"{$PhpType}\"{$Null}{$Default}{$Key}{$Extra} />";
  120. $cid++;
  121. }
  122. $indicesStmt= $this->manager->xpdo->query("PRAGMA index_list({$table[0]})");
  123. $indices= $indicesStmt->fetchAll(PDO::FETCH_ASSOC);
  124. if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Indices for table {$table[0]}: " . print_r($indices, true));
  125. foreach ($indices as $index) {
  126. $primary = preg_match('/^sqlite_autoindex/', $index['name']) ? 'true' : 'false';
  127. $unique = !empty($index['unique']) ? 'true' : 'false';
  128. $xmlIndices[]= "\t\t<index alias=\"{$index['name']}\" name=\"{$index['name']}\" primary=\"{$primary}\" unique=\"{$unique}\">";
  129. $columnsStmt = $this->manager->xpdo->query("PRAGMA index_info({$index['name']})");
  130. $columns = $columnsStmt->fetchAll(PDO::FETCH_ASSOC);
  131. if ($this->manager->xpdo->getDebug() === true) $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Columns of index {$index['name']}: " . print_r($columns, true));
  132. foreach ($columns as $column) {
  133. $xmlIndices[]= "\t\t\t<column key=\"{$column['name']}\" />";
  134. }
  135. $xmlIndices[]= "\t\t</index>";
  136. }
  137. $xmlObject[] = "\t<object class=\"{$class}\" table=\"{$tableName}\" extends=\"{$extends}\">";
  138. $xmlObject[] = implode("\n", $xmlFields);
  139. if (!empty($xmlIndices)) {
  140. $xmlObject[] = '';
  141. $xmlObject[] = implode("\n", $xmlIndices);
  142. }
  143. $xmlObject[] = "\t</object>";
  144. $xmlContent[] = implode("\n", $xmlObject);
  145. }
  146. $xmlContent[] = "</model>";
  147. if ($this->manager->xpdo->getDebug() === true) {
  148. $this->manager->xpdo->log(xPDO::LOG_LEVEL_DEBUG, implode("\n", $xmlContent));
  149. }
  150. $file= fopen($schemaFile, 'wb');
  151. $written= fwrite($file, implode("\n", $xmlContent));
  152. fclose($file);
  153. return true;
  154. }
  155. }