PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/gulliver/thirdparty/creole/drivers/mysql/metadata/MySQLTableInfo.php

https://bitbucket.org/ferOnti/processmaker
PHP | 252 lines | 155 code | 40 blank | 57 comment | 30 complexity | e6877502c2b2dc568b4fb4a43127aab3 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: MySQLTableInfo.php,v 1.20 2006/01/17 19:44:39 hlellelid Exp $
  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 please see
  19. * <http://creole.phpdb.org>.
  20. */
  21. require_once 'creole/metadata/TableInfo.php';
  22. /**
  23. * MySQL implementation of TableInfo.
  24. *
  25. * @author Hans Lellelid <hans@xmpl.org>
  26. * @version $Revision: 1.20 $
  27. * @package creole.drivers.mysql.metadata
  28. */
  29. class MySQLTableInfo extends TableInfo {
  30. /** Loads the columns for this table. */
  31. protected function initColumns()
  32. {
  33. include_once 'creole/metadata/ColumnInfo.php';
  34. include_once 'creole/drivers/mysql/MySQLTypes.php';
  35. if (!@mysql_select_db($this->dbname, $this->conn->getResource())) {
  36. throw new SQLException('No database selected');
  37. }
  38. // To get all of the attributes we need, we use
  39. // the MySQL "SHOW COLUMNS FROM $tablename" SQL. We cannot
  40. // use the API functions (e.g. mysql_list_fields() because they
  41. // do not return complete information -- e.g. precision / scale, default
  42. // values).
  43. $res = mysql_query("SHOW COLUMNS FROM `" . $this->name . "`", $this->conn->getResource());
  44. $defaults = array();
  45. $nativeTypes = array();
  46. $precisions = array();
  47. while($row = mysql_fetch_assoc($res)) {
  48. $name = $row['Field'];
  49. $is_nullable = ($row['Null'] == 'YES');
  50. $is_auto_increment = (strpos($row['Extra'], 'auto_increment') !== false);
  51. $size = null;
  52. $precision = null;
  53. $scale = null;
  54. if (preg_match('/^(\w+)[\(]?([\d,]*)[\)]?( |$)/', $row['Type'], $matches)) {
  55. // colname[1] size/precision[2]
  56. $nativeType = $matches[1];
  57. if ($matches[2]) {
  58. if ( ($cpos = strpos($matches[2], ',')) !== false) {
  59. $size = (int) substr($matches[2], 0, $cpos);
  60. $precision = $size;
  61. $scale = (int) substr($matches[2], $cpos + 1);
  62. } else {
  63. $size = (int) $matches[2];
  64. }
  65. }
  66. } elseif (preg_match('/^(\w+)\(/', $row['Type'], $matches)) {
  67. $nativeType = $matches[1];
  68. } else {
  69. $nativeType = $row['Type'];
  70. }
  71. //BLOBs can't have any default values in MySQL
  72. $default = preg_match('~blob|text~', $nativeType) ? null : $row['Default'];
  73. $this->columns[$name] = new ColumnInfo($this,
  74. $name,
  75. MySQLTypes::getType($nativeType),
  76. $nativeType,
  77. $size,
  78. $precision,
  79. $scale,
  80. $is_nullable,
  81. $default,
  82. $is_auto_increment,
  83. $row);
  84. }
  85. $this->colsLoaded = true;
  86. }
  87. /** Loads the primary key information for this table. */
  88. protected function initPrimaryKey()
  89. {
  90. include_once 'creole/metadata/PrimaryKeyInfo.php';
  91. // columns have to be loaded first
  92. if (!$this->colsLoaded) $this->initColumns();
  93. if (!@mysql_select_db($this->dbname, $this->conn->getResource())) {
  94. throw new SQLException('No database selected');
  95. }
  96. // Primary Keys
  97. $res = mysql_query("SHOW KEYS FROM `" . $this->name . "`", $this->conn->getResource());
  98. // Loop through the returned results, grouping the same key_name together
  99. // adding each column for that key.
  100. while($row = mysql_fetch_assoc($res)) {
  101. // Skip any non-primary keys.
  102. if ($row['Key_name'] !== 'PRIMARY') {
  103. continue;
  104. }
  105. $name = $row["Column_name"];
  106. if (!isset($this->primaryKey)) {
  107. $this->primaryKey = new PrimaryKeyInfo($name, $row);
  108. }
  109. $this->primaryKey->addColumn($this->columns[$name]);
  110. }
  111. $this->pkLoaded = true;
  112. }
  113. /** Loads the indexes for this table. */
  114. protected function initIndexes() {
  115. include_once 'creole/metadata/IndexInfo.php';
  116. // columns have to be loaded first
  117. if (!$this->colsLoaded) $this->initColumns();
  118. if (!@mysql_select_db($this->dbname, $this->conn->getResource())) {
  119. throw new SQLException('No database selected');
  120. }
  121. // Indexes
  122. $res = mysql_query("SHOW INDEX FROM `" . $this->name . "`", $this->conn->getResource());
  123. // Loop through the returned results, grouping the same key_name together
  124. // adding each column for that key.
  125. while($row = mysql_fetch_assoc($res)) {
  126. $colName = $row["Column_name"];
  127. $name = $row["Key_name"];
  128. if($name == "PRIMARY") {
  129. continue;
  130. }
  131. if (!isset($this->indexes[$name])) {
  132. $isUnique = ($row["Non_unique"] == 0);
  133. $this->indexes[$name] = new IndexInfo($name, $isUnique, $row);
  134. }
  135. $this->indexes[$name]->addColumn($this->columns[$colName]);
  136. }
  137. $this->indexesLoaded = true;
  138. }
  139. /**
  140. * Load foreign keys for supporting versions of MySQL.
  141. * @author Tony Bibbs
  142. */
  143. protected function initForeignKeys() {
  144. // First make sure we have supported version of MySQL:
  145. $res = mysql_query("SELECT VERSION()");
  146. $row = mysql_fetch_row($res);
  147. // Yes, it is OK to hardcode this...this was the first version of MySQL
  148. // that supported foreign keys
  149. if ($row[0] < '3.23.44') {
  150. $this->fksLoaded = true;
  151. return;
  152. }
  153. include_once 'creole/metadata/ForeignKeyInfo.php';
  154. // columns have to be loaded first
  155. if (!$this->colsLoaded) $this->initColumns();
  156. if (!@mysql_select_db($this->dbname, $this->conn->getResource())) {
  157. throw new SQLException('No database selected');
  158. }
  159. // Get the CREATE TABLE syntax
  160. $res = mysql_query("SHOW CREATE TABLE `" . $this->name . "`", $this->conn->getResource());
  161. $row = mysql_fetch_row($res);
  162. // Get the information on all the foreign keys
  163. $regEx = '/FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)(.*)/';
  164. if (preg_match_all($regEx,$row[1],$matches)) {
  165. $tmpArray = array_keys($matches[0]);
  166. foreach ($tmpArray as $curKey) {
  167. $name = $matches[1][$curKey];
  168. $ftbl = $matches[2][$curKey];
  169. $fcol = $matches[3][$curKey];
  170. $fkey = $matches[4][$curKey];
  171. if (!isset($this->foreignKeys[$name])) {
  172. $this->foreignKeys[$name] = new ForeignKeyInfo($name);
  173. if ($this->database->hasTable($ftbl)) {
  174. $foreignTable = $this->database->getTable($ftbl);
  175. } else {
  176. $foreignTable = new MySQLTableInfo($this->database, $ftbl);
  177. $this->database->addTable($foreignTable);
  178. }
  179. if ($foreignTable->hasColumn($fcol)) {
  180. $foreignCol = $foreignTable->getColumn($fcol);
  181. } else {
  182. $foreignCol = new ColumnInfo($foreignTable, $fcol);
  183. $foreignTable->addColumn($foreignCol);
  184. }
  185. //typical for mysql is RESTRICT
  186. $fkactions = array(
  187. 'ON DELETE' => ForeignKeyInfo::RESTRICT,
  188. 'ON UPDATE' => ForeignKeyInfo::RESTRICT,
  189. );
  190. if ($fkey) {
  191. //split foreign key information -> search for ON DELETE and afterwords for ON UPDATE action
  192. foreach (array_keys($fkactions) as $fkaction) {
  193. $result = NULL;
  194. preg_match('/' . $fkaction . ' (' . ForeignKeyInfo::CASCADE . '|' . ForeignKeyInfo::SETNULL . ')/', $fkey, $result);
  195. if ($result && is_array($result) && isset($result[1])) {
  196. $fkactions[$fkaction] = $result[1];
  197. }
  198. }
  199. }
  200. $this->foreignKeys[$name]->addReference($this->columns[$name], $foreignCol, $fkactions['ON DELETE'], $fkactions['ON UPDATE']);
  201. }
  202. }
  203. }
  204. $this->fksLoaded = true;
  205. }
  206. protected function initVendorSpecificInfo()
  207. {
  208. $res = mysql_query("SHOW TABLE STATUS LIKE '" . $this->name . "'", $this->conn->getResource());
  209. $this->vendorSpecificInfo = mysql_fetch_assoc($res);
  210. $this->vendorLoaded = true;
  211. }
  212. }