Adodb Dokumentation  V5.14 8 Sept 2011
datadict/datadict-mysql.inc.php
00001 <?php
00002 
00013 // security - hide paths
00014 if (!defined('ADODB_DIR')) die();
00015 
00016 class ADODB2_mysql extends ADODB_DataDict {
00017         var $databaseType = 'mysql';
00018         var $alterCol = ' MODIFY COLUMN';
00019         var $alterTableAddIndex = true;
00020         var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later
00021         
00022         var $dropIndex = 'DROP INDEX %s ON %s';
00023         var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s';    // needs column-definition!
00024         
00025         function MetaType($t,$len=-1,$fieldobj=false)
00026         {
00027                 if (is_object($t)) {
00028                         $fieldobj = $t;
00029                         $t = $fieldobj->type;
00030                         $len = $fieldobj->max_length;
00031                 }
00032                 $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
00033                 
00034                 $len = -1; // mysql max_length is not accurate
00035                 switch (strtoupper($t)) {
00036                 case 'STRING': 
00037                 case 'CHAR':
00038                 case 'VARCHAR': 
00039                 case 'TINYBLOB': 
00040                 case 'TINYTEXT': 
00041                 case 'ENUM': 
00042                 case 'SET':
00043                         if ($len <= $this->blobSize) return 'C';
00044                         
00045                 case 'TEXT':
00046                 case 'LONGTEXT': 
00047                 case 'MEDIUMTEXT':
00048                         return 'X';
00049                         
00050                 // php_mysql extension always returns 'blob' even if 'text'
00051                 // so we have to check whether binary...
00052                 case 'IMAGE':
00053                 case 'LONGBLOB': 
00054                 case 'BLOB':
00055                 case 'MEDIUMBLOB':
00056                         return !empty($fieldobj->binary) ? 'B' : 'X';
00057                         
00058                 case 'YEAR':
00059                 case 'DATE': return 'D';
00060                 
00061                 case 'TIME':
00062                 case 'DATETIME':
00063                 case 'TIMESTAMP': return 'T';
00064                 
00065                 case 'FLOAT':
00066                 case 'DOUBLE':
00067                         return 'F';
00068                         
00069                 case 'INT': 
00070                 case 'INTEGER': return $is_serial ? 'R' : 'I';
00071                 case 'TINYINT': return $is_serial ? 'R' : 'I1';
00072                 case 'SMALLINT': return $is_serial ? 'R' : 'I2';
00073                 case 'MEDIUMINT': return $is_serial ? 'R' : 'I4';
00074                 case 'BIGINT':  return $is_serial ? 'R' : 'I8';
00075                 default: return 'N';
00076                 }
00077         }
00078 
00079         function ActualType($meta)
00080         {
00081                 switch(strtoupper($meta)) {
00082                 case 'C': return 'VARCHAR';
00083                 case 'XL':return 'LONGTEXT';
00084                 case 'X': return 'TEXT';
00085                 
00086                 case 'C2': return 'VARCHAR';
00087                 case 'X2': return 'LONGTEXT';
00088                 
00089                 case 'B': return 'LONGBLOB';
00090                         
00091                 case 'D': return 'DATE';
00092                 case 'TS':
00093                 case 'T': return 'DATETIME';
00094                 case 'L': return 'TINYINT';
00095                 
00096                 case 'R':
00097                 case 'I4':
00098                 case 'I': return 'INTEGER';
00099                 case 'I1': return 'TINYINT';
00100                 case 'I2': return 'SMALLINT';
00101                 case 'I8': return 'BIGINT';
00102                 
00103                 case 'F': return 'DOUBLE';
00104                 case 'N': return 'NUMERIC';
00105                 default:
00106                         return $meta;
00107                 }
00108         }
00109         
00110         // return string must begin with space
00111         function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
00112         {       
00113                 $suffix = '';
00114                 if ($funsigned) $suffix .= ' UNSIGNED';
00115                 if ($fnotnull) $suffix .= ' NOT NULL';
00116                 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
00117                 if ($fautoinc) $suffix .= ' AUTO_INCREMENT';
00118                 if ($fconstraint) $suffix .= ' '.$fconstraint;
00119                 return $suffix;
00120         }
00121         
00122         /*
00123         CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
00124                 [table_options] [select_statement]
00125                 create_definition:
00126                 col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
00127                 [PRIMARY KEY] [reference_definition]
00128                 or PRIMARY KEY (index_col_name,...)
00129                 or KEY [index_name] (index_col_name,...)
00130                 or INDEX [index_name] (index_col_name,...)
00131                 or UNIQUE [INDEX] [index_name] (index_col_name,...)
00132                 or FULLTEXT [INDEX] [index_name] (index_col_name,...)
00133                 or [CONSTRAINT symbol] FOREIGN KEY [index_name] (index_col_name,...)
00134                 [reference_definition]
00135                 or CHECK (expr)
00136         */
00137         
00138         /*
00139         CREATE [UNIQUE|FULLTEXT] INDEX index_name
00140                 ON tbl_name (col_name[(length)],... )
00141         */
00142         
00143         function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
00144         {
00145                 $sql = array();
00146                 
00147                 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
00148                         if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
00149                         else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
00150 
00151                         if ( isset($idxoptions['DROP']) )
00152                                 return $sql;
00153                 }
00154                 
00155                 if ( empty ($flds) ) {
00156                         return $sql;
00157                 }
00158                 
00159                 if (isset($idxoptions['FULLTEXT'])) {
00160                         $unique = ' FULLTEXT';
00161                 } elseif (isset($idxoptions['UNIQUE'])) {
00162                         $unique = ' UNIQUE';
00163                 } else {
00164                         $unique = '';
00165                 }
00166                 
00167                 if ( is_array($flds) ) $flds = implode(', ',$flds);
00168                 
00169                 if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
00170                 else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
00171                 
00172                 $s .= ' (' . $flds . ')';
00173                 
00174                 if ( isset($idxoptions[$this->upperName]) )
00175                         $s .= $idxoptions[$this->upperName];
00176                 
00177                 $sql[] = $s;
00178                 
00179                 return $sql;
00180         }
00181 }
00182 ?>