3 * ADOdb Data Dictionary base class.
5 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
8 * @link https://adodb.org Project's web site and documentation
9 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
11 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
12 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
13 * any later version. This means you can use it in proprietary products.
14 * See the LICENSE.md file distributed with this source code for details.
15 * @license BSD-3-Clause
16 * @license LGPL-2.1-or-later
18 * @copyright 2000-2013 John Lim
19 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
22 // security - hide paths
23 if (!defined('ADODB_DIR')) die();
26 * Test script for parser
28 function lens_ParseTest()
30 $str = "`zcol ACOL` NUMBER(32,2) DEFAULT 'The \"cow\" (and Jim''s dog) jumps over the moon' PRIMARY, INTI INT AUTO DEFAULT 0, zcol2\"afs ds";
32 $a= lens_ParseArgs($str);
39 if (!function_exists('ctype_alnum')) {
40 function ctype_alnum($text) {
41 return preg_match('/^[a-z0-9]*$/i', $text);
48 Parse arguments, treat "text" (text) and 'text' as quotation marks.
49 To escape, use "" or '' or ))
51 Will read in "abc def" sans quotes, as: abc def
53 However if `abc def`, then will read in as `abc def`
55 @param endstmtchar Character that indicates end of statement
56 @param tokenchars Include the following characters in tokens apart from A-Z and 0-9
57 @returns 2 dimensional array containing parsed tokens.
59 function lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
66 $tokens[$stmtno] = array();
72 $ch = substr($args,$pos,1);
81 $tokens[$stmtno][] = implode('',$tokarr);
90 if ($intoken) $tokarr[] = $ch;
97 if (empty($endquote)) {
98 $tokens[$stmtno][] = implode('',$tokarr);
99 if ($ch == '(') $endquote = ')';
100 else $endquote = $ch;
104 } else if ($endquote == $ch) {
105 $ch2 = substr($args,$pos+
1,1);
106 if ($ch2 == $endquote) {
112 $tokens[$stmtno][] = implode('',$tokarr);
120 if ($ch == '(') $endquote = ')';
121 else $endquote = $ch;
125 if ($ch == '`') $tokarr[] = '`';
132 if ($ch == $endstmtchar) {
134 $tokens[$stmtno] = array();
145 if ($quoted) $tokarr[] = $ch;
146 else if (ctype_alnum($ch) ||
strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
148 if ($ch == $endstmtchar) {
149 $tokens[$stmtno][] = implode('',$tokarr);
151 $tokens[$stmtno] = array();
156 $tokens[$stmtno][] = implode('',$tokarr);
157 $tokens[$stmtno][] = $ch;
163 if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
169 class ADODB_DataDict
{
170 /** @var ADOConnection */
173 var $dropTable = 'DROP TABLE %s';
174 var $renameTable = 'RENAME TABLE %s TO %s';
175 var $dropIndex = 'DROP INDEX %s';
176 var $addCol = ' ADD';
177 var $alterCol = ' ALTER COLUMN';
178 var $dropCol = ' DROP COLUMN';
179 var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s'; // table, old-column, new-column, column-definitions (not used by default)
180 var $nameRegex = '\w';
181 var $nameRegexBrackets = 'a-zA-Z0-9_\(\)';
183 var $serverInfo = array();
184 var $autoIncrement = false;
186 var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changeTableSQL
187 var $blobSize = 100; /// any varchar/char field this size or greater is treated as a blob
188 /// in other words, we use a text area for editing.
189 /** @var string Uppercase driver name */
193 * Indicates whether a BLOB/CLOB field will allow a NOT NULL setting
194 * The type is whatever is matched to an X or X2 or B type. We must
195 * explicitly set the value in the driver to switch the behaviour on
197 public $blobAllowsNotNull;
199 * Indicates whether a BLOB/CLOB field will allow a DEFAULT set
200 * The type is whatever is matched to an X or X2 or B type. We must
201 * explicitly set the value in the driver to switch the behaviour on
203 public $blobAllowsDefaultValue;
207 * @var string String to use to quote identifiers and names
211 function getCommentSQL($table,$col)
216 function setCommentSQL($table,$col,$cmt)
221 function metaTables()
223 if (!$this->connection
->isConnected()) return array();
224 return $this->connection
->metaTables();
227 function metaColumns($tab, $upper=true, $schema=false)
229 if (!$this->connection
->isConnected()) return array();
230 return $this->connection
->metaColumns($this->tableName($tab), $upper, $schema);
233 function metaPrimaryKeys($tab,$owner=false,$intkey=false)
235 if (!$this->connection
->isConnected()) return array();
236 return $this->connection
->metaPrimaryKeys($this->tableName($tab), $owner, $intkey);
239 function metaIndexes($table, $primary = false, $owner = false)
241 if (!$this->connection
->isConnected()) return array();
242 return $this->connection
->metaIndexes($this->tableName($table), $primary, $owner);
245 function metaType($t,$len=-1,$fieldobj=false)
247 static $typeMap = array(
258 'INTERVAL' => 'C', # Postgres
259 'MACADDR' => 'C', # postgres
260 'VAR_STRING' => 'C', # mysql
278 'YEAR' => 'D', // mysql
282 'UNIQUEIDENTIFIER' => 'C', # MS SQL Server
287 'TIMESTAMPTZ' => 'T',
288 'SMALLDATETIME' => 'T',
290 'TIMESTAMP WITHOUT TIME ZONE' => 'T', // postgresql
299 'SERIAL' => 'R', // ifx
300 'INT IDENTITY' => 'R',
307 'INTEGER UNSIGNED' => 'I',
313 'LONG' => 'N', // interbase is numeric, oci8 is blob
314 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers
319 'DOUBLE PRECISION' => 'N',
339 'SQLINTERVAL' => 'N',
344 "SQLSERIAL8" => 'I8',
347 "SQLLVARCHAR" => 'X',
351 if (!$this->connection
->isConnected()) {
353 if (isset($typeMap[$t])) return $typeMap[$t];
354 return ADODB_DEFAULT_METATYPE
;
356 return $this->connection
->metaType($t,$len,$fieldobj);
359 function nameQuote($name = NULL,$allowBrackets=false)
361 if (!is_string($name)) {
367 if ( !is_object($this->connection
) ) {
371 $quote = $this->connection
->nameQuote
;
373 // if name is of the form `name`, quote it
374 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
375 return $quote . $matches[1] . $quote;
378 // if name contains special characters, quote it
379 $regex = ($allowBrackets) ?
$this->nameRegexBrackets
: $this->nameRegex
;
381 if ( !preg_match('/^[' . $regex . ']+$/', $name) ) {
382 return $quote . $name . $quote;
388 function tableName($name)
390 if ( $this->schema
) {
391 return $this->nameQuote($this->schema
) .'.'. $this->nameQuote($name);
393 return $this->nameQuote($name);
396 // Executes the sql array returned by getTableSQL and getIndexSQL
397 function executeSQLArray($sql, $continueOnError = true)
400 $conn = $this->connection
;
401 $saved = $conn->debug
;
402 foreach($sql as $line) {
404 if ($this->debug
) $conn->debug
= true;
405 $ok = $conn->execute($line);
406 $conn->debug
= $saved;
408 if ($this->debug
) ADOConnection
::outp($conn->errorMsg());
409 if (!$continueOnError) return 0;
417 Returns the actual type given a character code.
420 X: CLOB (character large object) or largest varchar size if CLOB is not supported
421 C2: Multibyte varchar
424 B: BLOB (binary large object)
428 L: Integer field suitable for storing booleans (0 or 1)
430 F: Floating point number
431 N: Numeric or decimal number
434 function actualType($meta)
436 $meta = strtoupper($meta);
439 * Add support for custom meta types. We do this
440 * first, that allows us to override existing types
442 if (isset($this->connection
->customMetaTypes
[$meta]))
443 return $this->connection
->customMetaTypes
[$meta]['actual'];
448 function createDatabase($dbname,$options=false)
450 $options = $this->_options($options);
453 $s = 'CREATE DATABASE ' . $this->nameQuote($dbname);
454 if (isset($options[$this->upperName
]))
455 $s .= ' '.$options[$this->upperName
];
462 Generates the SQL to create index. Returns an array of sql strings.
464 function createIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
466 if (!is_array($flds)) {
467 $flds = explode(',',$flds);
470 foreach($flds as $key => $fld) {
471 # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
472 $flds[$key] = $this->nameQuote($fld,$allowBrackets=true);
475 return $this->_indexSQL($this->nameQuote($idxname), $this->tableName($tabname), $flds, $this->_options($idxoptions));
478 function dropIndexSQL ($idxname, $tabname = NULL)
480 return array(sprintf($this->dropIndex
, $this->nameQuote($idxname), $this->tableName($tabname)));
483 function setSchema($schema)
485 $this->schema
= $schema;
488 function addColumnSQL($tabname, $flds)
490 $tabname = $this->tableName($tabname);
492 list($lines,$pkey,$idxs) = $this->_genFields($flds);
493 // genfields can return FALSE at times
494 if ($lines == null) $lines = array();
495 $alter = 'ALTER TABLE ' . $tabname . $this->addCol
. ' ';
496 foreach($lines as $v) {
497 $sql[] = $alter . $v;
499 if (is_array($idxs)) {
500 foreach($idxs as $idx => $idxdef) {
501 $sql_idxs = $this->createIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
502 $sql = array_merge($sql, $sql_idxs);
509 * Change the definition of one column
511 * As some DBMs can't do that on their own, you need to supply the complete definition of the new table,
512 * to allow recreating the table and copying the content over to the new table
513 * @param string $tabname table-name
514 * @param string $flds column-name and type for the changed column
515 * @param string $tableflds='' complete definition of the new table, eg. for postgres, default ''
516 * @param array|string $tableoptions='' options for the new table see createTableSQL, default ''
517 * @return array with SQL strings
519 function alterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
521 $tabname = $this->tableName($tabname);
523 list($lines,$pkey,$idxs) = $this->_genFields($flds);
524 // genfields can return FALSE at times
525 if ($lines == null) $lines = array();
526 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol
. ' ';
527 foreach($lines as $v) {
528 $sql[] = $alter . $v;
530 if (is_array($idxs)) {
531 foreach($idxs as $idx => $idxdef) {
532 $sql_idxs = $this->createIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
533 $sql = array_merge($sql, $sql_idxs);
543 * Some DBMs can only do this together with changeing the type of the column (even if that stays the same, eg. mysql)
544 * @param string $tabname table-name
545 * @param string $oldcolumn column-name to be renamed
546 * @param string $newcolumn new column-name
547 * @param string $flds='' complete column-definition-string like for addColumnSQL, only used by mysql atm., default=''
548 * @return array with SQL strings
550 function renameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
552 $tabname = $this->tableName($tabname);
554 list($lines,$pkey,$idxs) = $this->_genFields($flds);
555 // genfields can return FALSE at times
556 if ($lines == null) $lines = array();
557 $first = current($lines);
558 list(,$column_def) = preg_split("/[\t ]+/",$first,2);
560 return array(sprintf($this->renameColumn
,$tabname,$this->nameQuote($oldcolumn),$this->nameQuote($newcolumn),$column_def));
566 * Some DBM's can't do that on their own, you need to supply the complete definition of the new table,
567 * to allow, recreating the table and copying the content over to the new table
568 * @param string $tabname table-name
569 * @param string $flds column-name and type for the changed column
570 * @param string $tableflds='' complete definition of the new table, eg. for postgres, default ''
571 * @param array|string $tableoptions='' options for the new table see createTableSQL, default ''
572 * @return array with SQL strings
574 function dropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
576 $tabname = $this->tableName($tabname);
577 if (!is_array($flds)) $flds = explode(',',$flds);
579 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol
. ' ';
580 foreach($flds as $v) {
581 $sql[] = $alter . $this->nameQuote($v);
586 function dropTableSQL($tabname)
588 return array (sprintf($this->dropTable
, $this->tableName($tabname)));
591 function renameTableSQL($tabname,$newname)
593 return array (sprintf($this->renameTable
, $this->tableName($tabname),$this->tableName($newname)));
597 Generate the SQL to create table. Returns an array of sql strings.
599 function createTableSQL($tabname, $flds, $tableoptions=array())
601 list($lines,$pkey,$idxs) = $this->_genFields($flds, true);
602 // genfields can return FALSE at times
603 if ($lines == null) $lines = array();
605 $taboptions = $this->_options($tableoptions);
606 $tabname = $this->tableName($tabname);
607 $sql = $this->_tableSQL($tabname,$lines,$pkey,$taboptions);
609 // ggiunta - 2006/10/12 - KLUDGE:
610 // if we are on autoincrement, and table options includes REPLACE, the
611 // autoincrement sequence has already been dropped on table creation sql, so
612 // we avoid passing REPLACE to trigger creation code. This prevents
613 // creating sql that double-drops the sequence
614 if ($this->autoIncrement
&& isset($taboptions['REPLACE']))
615 unset($taboptions['REPLACE']);
616 $tsql = $this->_triggers($tabname,$taboptions);
617 foreach($tsql as $s) $sql[] = $s;
619 if (is_array($idxs)) {
620 foreach($idxs as $idx => $idxdef) {
621 $sql_idxs = $this->createIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
622 $sql = array_merge($sql, $sql_idxs);
631 function _genFields($flds,$widespacing=false)
633 if (is_string($flds)) {
635 $txt = $flds.$padding;
637 $flds0 = lens_ParseArgs($txt,',');
639 foreach($flds0 as $f0) {
641 foreach($f0 as $token) {
642 switch (strtoupper($token)) {
645 // fall through intentionally
651 if ($hasparam) $f1[$hasparam] = $token;
657 // 'index' token without a name means single column index: name it after column
658 if (array_key_exists('INDEX', $f1) && $f1['INDEX'] == '') {
659 $f1['INDEX'] = isset($f0['NAME']) ?
$f0['NAME'] : $f0[0];
660 // check if column name used to create an index name was quoted
661 if (($f1['INDEX'][0] == '"' ||
$f1['INDEX'][0] == "'" ||
$f1['INDEX'][0] == "`") &&
662 ($f1['INDEX'][0] == substr($f1['INDEX'], -1))) {
663 $f1['INDEX'] = $f1['INDEX'][0].'idx_'.substr($f1['INDEX'], 1, -1).$f1['INDEX'][0];
666 $f1['INDEX'] = 'idx_'.$f1['INDEX'];
668 // reset it, so we don't get next field 1st token as INDEX...
675 $this->autoIncrement
= false;
679 foreach($flds as $fld) {
681 $fld = array_change_key_case($fld,CASE_UPPER
);
692 $fconstraint = false;
696 $funiqueindex = false;
701 foreach($fld as $attr => $v) {
702 if ($attr == 2 && is_numeric($v))
704 elseif ($attr == 2 && strtoupper($ftype) == 'ENUM')
706 else if (is_numeric($attr) && $attr > 1 && !is_numeric($v))
707 $attr = strtoupper($v);
711 case 'NAME': $fname = $v; break;
716 $ftype = $this->actualType(strtoupper($v));
720 $dotat = strpos($v,'.');
721 if ($dotat === false)
722 $dotat = strpos($v,',');
723 if ($dotat === false)
727 $fsize = substr($v,0,$dotat);
728 $fprec = substr($v,$dotat+
1);
732 case 'UNSIGNED': $funsigned = true; break;
733 case 'AUTOINCREMENT':
734 case 'AUTO': $fautoinc = true; $fnotnull = true; break;
736 // a primary key col can be non unique in itself (if key spans many cols...)
737 case 'PRIMARY': $fprimary = $v; $fnotnull = true; /*$funiqueindex = true;*/ break;
739 case 'DEFAULT': $fdefault = $v; break;
740 case 'NOTNULL': $fnotnull = $v; break;
741 case 'NOQUOTE': $fnoquote = $v; break;
742 case 'DEFDATE': $fdefdate = $v; break;
743 case 'DEFTIMESTAMP': $fdefts = $v; break;
744 case 'CONSTRAINT': $fconstraint = $v; break;
745 // let INDEX keyword create a 'very standard' index on column
746 case 'INDEX': $findex = $v; break;
747 case 'UNIQUE': $funiqueindex = true; break;
749 $fOptions['ENUM'] = $v; break;
753 //--------------------
754 // VALIDATE FIELD INFO
755 if (!strlen($fname)) {
756 if ($this->debug
) ADOConnection
::outp("Undefined NAME");
760 $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
761 $fname = $this->nameQuote($fname);
763 if (!strlen($ftype)) {
764 if ($this->debug
) ADOConnection
::outp("Undefined TYPE for field '$fname'");
767 $ftype = strtoupper($ftype);
770 $ftype = $this->_getSize($ftype, $ty, $fsize, $fprec, $fOptions);
772 if (($ty == 'X' ||
$ty == 'X2' ||
$ty == 'XL' ||
$ty == 'B') && !$this->blobAllowsNotNull
)
774 * some blob types do not accept nulls, so we override the
775 * previously defined value
782 if (($ty == 'X' ||
$ty == 'X2' ||
$ty == 'XL' ||
$ty == 'B') && !$this->blobAllowsDefaultValue
)
784 * some databases do not allow blobs to have defaults, so we
785 * override the previously defined value
789 // build list of indexes
791 if (array_key_exists($findex, $idxs)) {
792 $idxs[$findex]['cols'][] = ($fname);
793 if (in_array('UNIQUE', $idxs[$findex]['opts']) != $funiqueindex) {
794 if ($this->debug
) ADOConnection
::outp("Index $findex defined once UNIQUE and once not");
796 if ($funiqueindex && !in_array('UNIQUE', $idxs[$findex]['opts']))
797 $idxs[$findex]['opts'][] = 'UNIQUE';
801 $idxs[$findex] = array();
802 $idxs[$findex]['cols'] = array($fname);
804 $idxs[$findex]['opts'] = array('UNIQUE');
806 $idxs[$findex]['opts'] = array();
810 //--------------------
811 // CONSTRUCT FIELD SQL
813 if (substr($this->connection
->databaseType
,0,5) == 'mysql') {
814 $ftype = 'TIMESTAMP';
816 $fdefault = $this->connection
->sysTimeStamp
;
818 } else if ($fdefdate) {
819 if (substr($this->connection
->databaseType
,0,5) == 'mysql') {
820 $ftype = 'TIMESTAMP';
822 $fdefault = $this->connection
->sysDate
;
824 } else if ($fdefault !== false && !$fnoquote) {
825 if ($ty == 'C' or $ty == 'X' or
826 ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) {
828 if (($ty == 'D' ||
$ty == 'T') && strtolower($fdefault) != 'null') {
829 // convert default date into database-aware code
832 $fdefault = $this->connection
->dbTimeStamp($fdefault);
836 $fdefault = $this->connection
->dbDate($fdefault);
840 if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
841 $fdefault = trim($fdefault);
842 else if (strtolower($fdefault) != 'null')
843 $fdefault = $this->connection
->qstr($fdefault);
846 $suffix = $this->_createSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
848 // add index creation
849 if ($widespacing) $fname = str_pad($fname,24);
851 // check for field names appearing twice
852 if (array_key_exists($fid, $lines)) {
853 ADOConnection
::outp("Field '$fname' defined twice");
856 $lines[$fid] = $fname.' '.$ftype.$suffix;
858 if ($fautoinc) $this->autoIncrement
= true;
861 return array($lines,$pkey,$idxs);
865 GENERATE THE SIZE PART OF THE DATATYPE
866 $ftype is the actual type
867 $ty is the type defined originally in the DDL
869 function _getSize($ftype, $ty, $fsize, $fprec, $options=false)
871 if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
872 $ftype .= "(".$fsize;
873 if (strlen($fprec)) $ftype .= ",".$fprec;
878 * Handle additional options
880 if (is_array($options))
882 foreach($options as $type=>$value)
887 $ftype .= '(' . $value . ')';
899 // return string must begin with space
900 function _createSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
903 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
904 if ($fnotnull) $suffix .= ' NOT NULL';
905 if ($fconstraint) $suffix .= ' '.$fconstraint;
909 function _indexSQL($idxname, $tabname, $flds, $idxoptions)
913 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
914 $sql[] = sprintf ($this->dropIndex
, $idxname);
915 if ( isset($idxoptions['DROP']) )
919 if ( empty ($flds) ) {
923 $unique = isset($idxoptions['UNIQUE']) ?
' UNIQUE' : '';
925 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
927 if ( isset($idxoptions[$this->upperName
]) )
928 $s .= $idxoptions[$this->upperName
];
930 if ( is_array($flds) )
931 $flds = implode(', ',$flds);
932 $s .= '(' . $flds . ')';
938 function _dropAutoIncrement($tabname)
943 function _tableSQL($tabname,$lines,$pkey,$tableoptions)
947 if (isset($tableoptions['REPLACE']) ||
isset ($tableoptions['DROP'])) {
948 $sql[] = sprintf($this->dropTable
,$tabname);
949 if ($this->autoIncrement
) {
950 $sInc = $this->_dropAutoIncrement($tabname);
951 if ($sInc) $sql[] = $sInc;
953 if ( isset ($tableoptions['DROP']) ) {
958 $s = "CREATE TABLE $tabname (\n";
959 $s .= implode(",\n", $lines);
960 if (sizeof($pkey)>0) {
961 $s .= ",\n PRIMARY KEY (";
962 $s .= implode(", ",$pkey).")";
964 if (isset($tableoptions['CONSTRAINTS']))
965 $s .= "\n".$tableoptions['CONSTRAINTS'];
967 if (isset($tableoptions[$this->upperName
.'_CONSTRAINTS']))
968 $s .= "\n".$tableoptions[$this->upperName
.'_CONSTRAINTS'];
971 if (isset($tableoptions[$this->upperName
])) $s .= $tableoptions[$this->upperName
];
978 GENERATE TRIGGERS IF NEEDED
979 used when table has auto-incrementing field that is emulated using triggers
981 function _triggers($tabname,$taboptions)
987 Sanitize options, so that array elements with no keys are promoted to keys
989 function _options($opts)
991 if (!is_array($opts)) return array();
993 foreach($opts as $k => $v) {
994 if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
995 else $newopts[strtoupper($k)] = $v;
1001 function _getSizePrec($size)
1005 $dotat = strpos($size,'.');
1006 if ($dotat === false) $dotat = strpos($size,',');
1007 if ($dotat === false) $fsize = $size;
1009 $fsize = substr($size,0,$dotat);
1010 $fprec = substr($size,$dotat+
1);
1012 return array($fsize, $fprec);
1016 * This function changes/adds new fields to your table.
1018 * You don't have to know if the col is new or not. It will check on its own.
1020 * @param string $tablename
1021 * @param string $flds
1022 * @param string[] $tableoptions
1023 * @param bool $dropOldFlds
1025 * @return string[] Array of SQL Commands
1027 function changeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFlds=false)
1029 global $ADODB_FETCH_MODE;
1031 $save = $ADODB_FETCH_MODE;
1032 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
;
1033 if ($this->connection
->fetchMode
!== false) $savem = $this->connection
->setFetchMode(false);
1035 // check table exists
1036 $save_handler = $this->connection
->raiseErrorFn
;
1037 $this->connection
->raiseErrorFn
= '';
1038 $cols = $this->metaColumns($tablename);
1039 $this->connection
->raiseErrorFn
= $save_handler;
1041 if (isset($savem)) $this->connection
->setFetchMode($savem);
1042 $ADODB_FETCH_MODE = $save;
1044 if ( empty($cols)) {
1045 return $this->createTableSQL($tablename, $flds, $tableoptions);
1048 if (is_array($flds)) {
1049 // Cycle through the update fields, comparing
1050 // existing fields to fields to update.
1051 // if the Metatype and size is exactly the
1052 // same, ignore - by Mark Newham
1053 $holdflds = array();
1054 foreach($flds as $k=>$v) {
1055 if ( isset($cols[$k]) && is_object($cols[$k]) ) {
1056 // If already not allowing nulls, then don't change
1058 if (isset($obj->not_null
) && $obj->not_null
)
1059 $v = str_replace('NOT NULL','',$v);
1060 if (isset($obj->auto_increment
) && $obj->auto_increment
&& empty($v['AUTOINCREMENT']))
1061 $v = str_replace('AUTOINCREMENT','',$v);
1064 $ml = $c->max_length
;
1065 $mt = $this->metaType($c->type
,$ml);
1067 if (isset($c->scale
)) $sc = $c->scale
;
1068 else $sc = 99; // always force change if scale not known.
1070 if ($sc == -1) $sc = false;
1071 list($fsize, $fprec) = $this->_getSizePrec($v['SIZE']);
1073 if ($ml == -1) $ml = '';
1074 if ($mt == 'X') $ml = $v['SIZE'];
1075 if (($mt != $v['TYPE']) ||
($ml != $fsize ||
$sc != $fprec) ||
(isset($v['AUTOINCREMENT']) && $v['AUTOINCREMENT'] != $obj->auto_increment
)) {
1085 $sql = $this->alterColumnSql($tablename, $flds);
1088 foreach ($cols as $id => $v) {
1089 if (!isset($lines[$id])) {
1090 $sql[] = $this->dropColumnSQL($tablename, $flds);