datadict-mysql.inc.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. @version v5.20.17 31-Mar-2020
  4. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  5. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 4 for best viewing.
  10. */
  11. // security - hide paths
  12. if (!defined('ADODB_DIR')) die();
  13. class ADODB2_mysql extends ADODB_DataDict {
  14. var $databaseType = 'mysql';
  15. var $alterCol = ' MODIFY COLUMN';
  16. var $alterTableAddIndex = true;
  17. var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later
  18. var $dropIndex = 'DROP INDEX %s ON %s';
  19. var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition!
  20. function MetaType($t,$len=-1,$fieldobj=false)
  21. {
  22. if (is_object($t)) {
  23. $fieldobj = $t;
  24. $t = $fieldobj->type;
  25. $len = $fieldobj->max_length;
  26. }
  27. $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
  28. $len = -1; // mysql max_length is not accurate
  29. switch (strtoupper($t)) {
  30. case 'STRING':
  31. case 'CHAR':
  32. case 'VARCHAR':
  33. case 'TINYBLOB':
  34. case 'TINYTEXT':
  35. case 'ENUM':
  36. case 'SET':
  37. if ($len <= $this->blobSize) return 'C';
  38. case 'TEXT':
  39. case 'LONGTEXT':
  40. case 'MEDIUMTEXT':
  41. return 'X';
  42. // php_mysql extension always returns 'blob' even if 'text'
  43. // so we have to check whether binary...
  44. case 'IMAGE':
  45. case 'LONGBLOB':
  46. case 'BLOB':
  47. case 'MEDIUMBLOB':
  48. return !empty($fieldobj->binary) ? 'B' : 'X';
  49. case 'YEAR':
  50. case 'DATE': return 'D';
  51. case 'TIME':
  52. case 'DATETIME':
  53. case 'TIMESTAMP': return 'T';
  54. case 'FLOAT':
  55. case 'DOUBLE':
  56. return 'F';
  57. case 'INT':
  58. case 'INTEGER': return $is_serial ? 'R' : 'I';
  59. case 'TINYINT': return $is_serial ? 'R' : 'I1';
  60. case 'SMALLINT': return $is_serial ? 'R' : 'I2';
  61. case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
  62. case 'BIGINT': return $is_serial ? 'R' : 'I8';
  63. default: return 'N';
  64. }
  65. }
  66. function ActualType($meta)
  67. {
  68. switch(strtoupper($meta)) {
  69. case 'C': return 'VARCHAR';
  70. case 'XL':return 'LONGTEXT';
  71. case 'X': return 'TEXT';
  72. case 'C2': return 'VARCHAR';
  73. case 'X2': return 'LONGTEXT';
  74. case 'B': return 'LONGBLOB';
  75. case 'D': return 'DATE';
  76. case 'TS':
  77. case 'T': return 'DATETIME';
  78. case 'L': return 'TINYINT';
  79. case 'R':
  80. case 'I4':
  81. case 'I': return 'INTEGER';
  82. case 'I1': return 'TINYINT';
  83. case 'I2': return 'SMALLINT';
  84. case 'I8': return 'BIGINT';
  85. case 'F': return 'DOUBLE';
  86. case 'N': return 'NUMERIC';
  87. default:
  88. return $meta;
  89. }
  90. }
  91. // return string must begin with space
  92. function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
  93. {
  94. $suffix = '';
  95. if ($funsigned) $suffix .= ' UNSIGNED';
  96. if ($fnotnull) $suffix .= ' NOT NULL';
  97. if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
  98. if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
  99. if ($fconstraint) $suffix .= ' '.$fconstraint;
  100. return $suffix;
  101. }
  102. /*
  103. CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
  104. [table_options] [select_statement]
  105. create_definition:
  106. col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
  107. [PRIMARY KEY] [reference_definition]
  108. or PRIMARY KEY (index_col_name,...)
  109. or KEY [index_name] (index_col_name,...)
  110. or INDEX [index_name] (index_col_name,...)
  111. or UNIQUE [INDEX] [index_name] (index_col_name,...)
  112. or FULLTEXT [INDEX] [index_name] (index_col_name,...)
  113. or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
  114. [reference_definition]
  115. or CHECK (expr)
  116. */
  117. /*
  118. CREATE [UNIQUE|FULLTEXT] INDEX index_name
  119. ON tbl_name (col_name[(length)],... )
  120. */
  121. function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
  122. {
  123. $sql = array();
  124. if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
  125. if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
  126. else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
  127. if ( isset($idxoptions['DROP']) )
  128. return $sql;
  129. }
  130. if ( empty ($flds) ) {
  131. return $sql;
  132. }
  133. if (isset($idxoptions['FULLTEXT'])) {
  134. $unique = ' FULLTEXT';
  135. } elseif (isset($idxoptions['UNIQUE'])) {
  136. $unique = ' UNIQUE';
  137. } else {
  138. $unique = '';
  139. }
  140. if ( is_array($flds) ) $flds = implode(', ',$flds);
  141. if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
  142. else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
  143. $s .= ' (' . $flds . ')';
  144. if ( isset($idxoptions[$this->upperName]) )
  145. $s .= $idxoptions[$this->upperName];
  146. $sql[] = $s;
  147. return $sql;
  148. }
  149. }