4 @version v5.20.16 12-Jan-2020
5 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
6 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
11 Set tabs to 4 for best viewing.
15 See adodb/tests/test-datadict.php for docs and examples.
19 Test script for parser
22 // security - hide paths
23 if (!defined('ADODB_DIR')) die();
25 function Lens_ParseTest()
27 $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";
29 $a= Lens_ParseArgs($str);
36 if (!function_exists('ctype_alnum')) {
37 function ctype_alnum($text) {
38 return preg_match('/^[a-z0-9]*$/i', $text);
45 Parse arguments, treat "text" (text) and 'text' as quotation marks.
46 To escape, use "" or '' or ))
48 Will read in "abc def" sans quotes, as: abc def
50 However if `abc def`, then will read in as `abc def`
52 @param endstmtchar Character that indicates end of statement
53 @param tokenchars Include the following characters in tokens apart from A-Z and 0-9
54 @returns 2 dimensional array containing parsed tokens.
56 function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
63 $tokens[$stmtno] = array();
69 $ch = substr($args,$pos,1);
78 $tokens[$stmtno][] = implode('',$tokarr);
87 if ($intoken) $tokarr[] = $ch;
94 if (empty($endquote)) {
95 $tokens[$stmtno][] = implode('',$tokarr);
96 if ($ch == '(') $endquote = ')';
101 } else if ($endquote == $ch) {
102 $ch2 = substr($args,$pos+
1,1);
103 if ($ch2 == $endquote) {
109 $tokens[$stmtno][] = implode('',$tokarr);
117 if ($ch == '(') $endquote = ')';
118 else $endquote = $ch;
122 if ($ch == '`') $tokarr[] = '`';
129 if ($ch == $endstmtchar) {
131 $tokens[$stmtno] = array();
142 if ($quoted) $tokarr[] = $ch;
143 else if (ctype_alnum($ch) ||
strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
145 if ($ch == $endstmtchar) {
146 $tokens[$stmtno][] = implode('',$tokarr);
148 $tokens[$stmtno] = array();
153 $tokens[$stmtno][] = implode('',$tokarr);
154 $tokens[$stmtno][] = $ch;
160 if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
166 class ADODB_DataDict
{
169 var $dropTable = 'DROP TABLE %s';
170 var $renameTable = 'RENAME TABLE %s TO %s';
171 var $dropIndex = 'DROP INDEX %s';
172 var $addCol = ' ADD';
173 var $alterCol = ' ALTER COLUMN';
174 var $dropCol = ' DROP COLUMN';
175 var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s'; // table, old-column, new-column, column-definitions (not used by default)
176 var $nameRegex = '\w';
177 var $nameRegexBrackets = 'a-zA-Z0-9_\(\)';
179 var $serverInfo = array();
180 var $autoIncrement = false;
182 var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changetablesql
183 var $blobSize = 100; /// any varchar/char field this size or greater is treated as a blob
184 /// in other words, we use a text area for editting.
186 function GetCommentSQL($table,$col)
191 function SetCommentSQL($table,$col,$cmt)
196 function MetaTables()
198 if (!$this->connection
->IsConnected()) return array();
199 return $this->connection
->MetaTables();
202 function MetaColumns($tab, $upper=true, $schema=false)
204 if (!$this->connection
->IsConnected()) return array();
205 return $this->connection
->MetaColumns($this->TableName($tab), $upper, $schema);
208 function MetaPrimaryKeys($tab,$owner=false,$intkey=false)
210 if (!$this->connection
->IsConnected()) return array();
211 return $this->connection
->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
214 function MetaIndexes($table, $primary = false, $owner = false)
216 if (!$this->connection
->IsConnected()) return array();
217 return $this->connection
->MetaIndexes($this->TableName($table), $primary, $owner);
220 function MetaType($t,$len=-1,$fieldobj=false)
222 static $typeMap = array(
233 'INTERVAL' => 'C', # Postgres
234 'MACADDR' => 'C', # postgres
235 'VAR_STRING' => 'C', # mysql
253 'YEAR' => 'D', // mysql
257 'UNIQUEIDENTIFIER' => 'C', # MS SQL Server
262 'TIMESTAMPTZ' => 'T',
263 'SMALLDATETIME' => 'T',
265 'TIMESTAMP WITHOUT TIME ZONE' => 'T', // postgresql
274 'SERIAL' => 'R', // ifx
275 'INT IDENTITY' => 'R',
282 'INTEGER UNSIGNED' => 'I',
288 'LONG' => 'N', // interbase is numeric, oci8 is blob
289 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers
294 'DOUBLE PRECISION' => 'N',
314 'SQLINTERVAL' => 'N',
319 "SQLSERIAL8" => 'I8',
322 "SQLLVARCHAR" => 'X',
326 if (!$this->connection
->IsConnected()) {
328 if (isset($typeMap[$t])) return $typeMap[$t];
331 return $this->connection
->MetaType($t,$len,$fieldobj);
334 function NameQuote($name = NULL,$allowBrackets=false)
336 if (!is_string($name)) {
342 if ( !is_object($this->connection
) ) {
346 $quote = $this->connection
->nameQuote
;
348 // if name is of the form `name`, quote it
349 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
350 return $quote . $matches[1] . $quote;
353 // if name contains special characters, quote it
354 $regex = ($allowBrackets) ?
$this->nameRegexBrackets
: $this->nameRegex
;
356 if ( !preg_match('/^[' . $regex . ']+$/', $name) ) {
357 return $quote . $name . $quote;
363 function TableName($name)
365 if ( $this->schema
) {
366 return $this->NameQuote($this->schema
) .'.'. $this->NameQuote($name);
368 return $this->NameQuote($name);
371 // Executes the sql array returned by GetTableSQL and GetIndexSQL
372 function ExecuteSQLArray($sql, $continueOnError = true)
375 $conn = $this->connection
;
376 $saved = $conn->debug
;
377 foreach($sql as $line) {
379 if ($this->debug
) $conn->debug
= true;
380 $ok = $conn->Execute($line);
381 $conn->debug
= $saved;
383 if ($this->debug
) ADOConnection
::outp($conn->ErrorMsg());
384 if (!$continueOnError) return 0;
392 Returns the actual type given a character code.
395 X: CLOB (character large object) or largest varchar size if CLOB is not supported
396 C2: Multibyte varchar
399 B: BLOB (binary large object)
403 L: Integer field suitable for storing booleans (0 or 1)
405 F: Floating point number
406 N: Numeric or decimal number
409 function ActualType($meta)
414 function CreateDatabase($dbname,$options=false)
416 $options = $this->_Options($options);
419 $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
420 if (isset($options[$this->upperName
]))
421 $s .= ' '.$options[$this->upperName
];
428 Generates the SQL to create index. Returns an array of sql strings.
430 function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
432 if (!is_array($flds)) {
433 $flds = explode(',',$flds);
436 foreach($flds as $key => $fld) {
437 # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
438 $flds[$key] = $this->NameQuote($fld,$allowBrackets=true);
441 return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
444 function DropIndexSQL ($idxname, $tabname = NULL)
446 return array(sprintf($this->dropIndex
, $this->NameQuote($idxname), $this->TableName($tabname)));
449 function SetSchema($schema)
451 $this->schema
= $schema;
454 function AddColumnSQL($tabname, $flds)
456 $tabname = $this->TableName ($tabname);
458 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
459 // genfields can return FALSE at times
460 if ($lines == null) $lines = array();
461 $alter = 'ALTER TABLE ' . $tabname . $this->addCol
. ' ';
462 foreach($lines as $v) {
463 $sql[] = $alter . $v;
465 if (is_array($idxs)) {
466 foreach($idxs as $idx => $idxdef) {
467 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
468 $sql = array_merge($sql, $sql_idxs);
475 * Change the definition of one column
477 * As some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
478 * to allow, recreating the table and copying the content over to the new table
479 * @param string $tabname table-name
480 * @param string $flds column-name and type for the changed column
481 * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
482 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
483 * @return array with SQL strings
485 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
487 $tabname = $this->TableName ($tabname);
489 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
490 // genfields can return FALSE at times
491 if ($lines == null) $lines = array();
492 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol
. ' ';
493 foreach($lines as $v) {
494 $sql[] = $alter . $v;
496 if (is_array($idxs)) {
497 foreach($idxs as $idx => $idxdef) {
498 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
499 $sql = array_merge($sql, $sql_idxs);
509 * Some DBM's can only do this together with changeing the type of the column (even if that stays the same, eg. mysql)
510 * @param string $tabname table-name
511 * @param string $oldcolumn column-name to be renamed
512 * @param string $newcolumn new column-name
513 * @param string $flds='' complete column-defintion-string like for AddColumnSQL, only used by mysql atm., default=''
514 * @return array with SQL strings
516 function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
518 $tabname = $this->TableName ($tabname);
520 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
521 // genfields can return FALSE at times
522 if ($lines == null) $lines = array();
523 $first = current($lines);
524 list(,$column_def) = preg_split("/[\t ]+/",$first,2);
526 return array(sprintf($this->renameColumn
,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
532 * Some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
533 * to allow, recreating the table and copying the content over to the new table
534 * @param string $tabname table-name
535 * @param string $flds column-name and type for the changed column
536 * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
537 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
538 * @return array with SQL strings
540 function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
542 $tabname = $this->TableName ($tabname);
543 if (!is_array($flds)) $flds = explode(',',$flds);
545 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol
. ' ';
546 foreach($flds as $v) {
547 $sql[] = $alter . $this->NameQuote($v);
552 function DropTableSQL($tabname)
554 return array (sprintf($this->dropTable
, $this->TableName($tabname)));
557 function RenameTableSQL($tabname,$newname)
559 return array (sprintf($this->renameTable
, $this->TableName($tabname),$this->TableName($newname)));
563 Generate the SQL to create table. Returns an array of sql strings.
565 function CreateTableSQL($tabname, $flds, $tableoptions=array())
567 list($lines,$pkey,$idxs) = $this->_GenFields($flds, true);
568 // genfields can return FALSE at times
569 if ($lines == null) $lines = array();
571 $taboptions = $this->_Options($tableoptions);
572 $tabname = $this->TableName ($tabname);
573 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
575 // ggiunta - 2006/10/12 - KLUDGE:
576 // if we are on autoincrement, and table options includes REPLACE, the
577 // autoincrement sequence has already been dropped on table creation sql, so
578 // we avoid passing REPLACE to trigger creation code. This prevents
579 // creating sql that double-drops the sequence
580 if ($this->autoIncrement
&& isset($taboptions['REPLACE']))
581 unset($taboptions['REPLACE']);
582 $tsql = $this->_Triggers($tabname,$taboptions);
583 foreach($tsql as $s) $sql[] = $s;
585 if (is_array($idxs)) {
586 foreach($idxs as $idx => $idxdef) {
587 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
588 $sql = array_merge($sql, $sql_idxs);
597 function _GenFields($flds,$widespacing=false)
599 if (is_string($flds)) {
601 $txt = $flds.$padding;
603 $flds0 = Lens_ParseArgs($txt,',');
605 foreach($flds0 as $f0) {
607 foreach($f0 as $token) {
608 switch (strtoupper($token)) {
611 // fall through intentionally
617 if ($hasparam) $f1[$hasparam] = $token;
623 // 'index' token without a name means single column index: name it after column
624 if (array_key_exists('INDEX', $f1) && $f1['INDEX'] == '') {
625 $f1['INDEX'] = isset($f0['NAME']) ?
$f0['NAME'] : $f0[0];
626 // check if column name used to create an index name was quoted
627 if (($f1['INDEX'][0] == '"' ||
$f1['INDEX'][0] == "'" ||
$f1['INDEX'][0] == "`") &&
628 ($f1['INDEX'][0] == substr($f1['INDEX'], -1))) {
629 $f1['INDEX'] = $f1['INDEX'][0].'idx_'.substr($f1['INDEX'], 1, -1).$f1['INDEX'][0];
632 $f1['INDEX'] = 'idx_'.$f1['INDEX'];
634 // reset it, so we don't get next field 1st token as INDEX...
641 $this->autoIncrement
= false;
645 foreach($flds as $fld) {
646 $fld = _array_change_key_case($fld);
658 $fconstraint = false;
662 $funiqueindex = false;
666 foreach($fld as $attr => $v) {
667 if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
668 else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
672 case 'NAME': $fname = $v; break;
674 case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
677 $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
678 if ($dotat === false) $fsize = $v;
680 $fsize = substr($v,0,$dotat);
681 $fprec = substr($v,$dotat+
1);
684 case 'UNSIGNED': $funsigned = true; break;
685 case 'AUTOINCREMENT':
686 case 'AUTO': $fautoinc = true; $fnotnull = true; break;
688 // a primary key col can be non unique in itself (if key spans many cols...)
689 case 'PRIMARY': $fprimary = $v; $fnotnull = true; /*$funiqueindex = true;*/ break;
691 case 'DEFAULT': $fdefault = $v; break;
692 case 'NOTNULL': $fnotnull = $v; break;
693 case 'NOQUOTE': $fnoquote = $v; break;
694 case 'DEFDATE': $fdefdate = $v; break;
695 case 'DEFTIMESTAMP': $fdefts = $v; break;
696 case 'CONSTRAINT': $fconstraint = $v; break;
697 // let INDEX keyword create a 'very standard' index on column
698 case 'INDEX': $findex = $v; break;
699 case 'UNIQUE': $funiqueindex = true; break;
703 //--------------------
704 // VALIDATE FIELD INFO
705 if (!strlen($fname)) {
706 if ($this->debug
) ADOConnection
::outp("Undefined NAME");
710 $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
711 $fname = $this->NameQuote($fname);
713 if (!strlen($ftype)) {
714 if ($this->debug
) ADOConnection
::outp("Undefined TYPE for field '$fname'");
717 $ftype = strtoupper($ftype);
720 $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
722 if ($ty == 'X' ||
$ty == 'X2' ||
$ty == 'B') $fnotnull = false; // some blob types do not accept nulls
724 if ($fprimary) $pkey[] = $fname;
726 // some databases do not allow blobs to have defaults
727 if ($ty == 'X') $fdefault = false;
729 // build list of indexes
731 if (array_key_exists($findex, $idxs)) {
732 $idxs[$findex]['cols'][] = ($fname);
733 if (in_array('UNIQUE', $idxs[$findex]['opts']) != $funiqueindex) {
734 if ($this->debug
) ADOConnection
::outp("Index $findex defined once UNIQUE and once not");
736 if ($funiqueindex && !in_array('UNIQUE', $idxs[$findex]['opts']))
737 $idxs[$findex]['opts'][] = 'UNIQUE';
741 $idxs[$findex] = array();
742 $idxs[$findex]['cols'] = array($fname);
744 $idxs[$findex]['opts'] = array('UNIQUE');
746 $idxs[$findex]['opts'] = array();
750 //--------------------
751 // CONSTRUCT FIELD SQL
753 if (substr($this->connection
->databaseType
,0,5) == 'mysql') {
754 $ftype = 'TIMESTAMP';
756 $fdefault = $this->connection
->sysTimeStamp
;
758 } else if ($fdefdate) {
759 if (substr($this->connection
->databaseType
,0,5) == 'mysql') {
760 $ftype = 'TIMESTAMP';
762 $fdefault = $this->connection
->sysDate
;
764 } else if ($fdefault !== false && !$fnoquote) {
765 if ($ty == 'C' or $ty == 'X' or
766 ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) {
768 if (($ty == 'D' ||
$ty == 'T') && strtolower($fdefault) != 'null') {
769 // convert default date into database-aware code
772 $fdefault = $this->connection
->DBTimeStamp($fdefault);
776 $fdefault = $this->connection
->DBDate($fdefault);
780 if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
781 $fdefault = trim($fdefault);
782 else if (strtolower($fdefault) != 'null')
783 $fdefault = $this->connection
->qstr($fdefault);
786 $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
788 // add index creation
789 if ($widespacing) $fname = str_pad($fname,24);
791 // check for field names appearing twice
792 if (array_key_exists($fid, $lines)) {
793 ADOConnection
::outp("Field '$fname' defined twice");
796 $lines[$fid] = $fname.' '.$ftype.$suffix;
798 if ($fautoinc) $this->autoIncrement
= true;
801 return array($lines,$pkey,$idxs);
805 GENERATE THE SIZE PART OF THE DATATYPE
806 $ftype is the actual type
807 $ty is the type defined originally in the DDL
809 function _GetSize($ftype, $ty, $fsize, $fprec)
811 if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
812 $ftype .= "(".$fsize;
813 if (strlen($fprec)) $ftype .= ",".$fprec;
820 // return string must begin with space
821 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
824 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
825 if ($fnotnull) $suffix .= ' NOT NULL';
826 if ($fconstraint) $suffix .= ' '.$fconstraint;
830 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
834 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
835 $sql[] = sprintf ($this->dropIndex
, $idxname);
836 if ( isset($idxoptions['DROP']) )
840 if ( empty ($flds) ) {
844 $unique = isset($idxoptions['UNIQUE']) ?
' UNIQUE' : '';
846 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
848 if ( isset($idxoptions[$this->upperName
]) )
849 $s .= $idxoptions[$this->upperName
];
851 if ( is_array($flds) )
852 $flds = implode(', ',$flds);
853 $s .= '(' . $flds . ')';
859 function _DropAutoIncrement($tabname)
864 function _TableSQL($tabname,$lines,$pkey,$tableoptions)
868 if (isset($tableoptions['REPLACE']) ||
isset ($tableoptions['DROP'])) {
869 $sql[] = sprintf($this->dropTable
,$tabname);
870 if ($this->autoIncrement
) {
871 $sInc = $this->_DropAutoIncrement($tabname);
872 if ($sInc) $sql[] = $sInc;
874 if ( isset ($tableoptions['DROP']) ) {
878 $s = "CREATE TABLE $tabname (\n";
879 $s .= implode(",\n", $lines);
880 if (sizeof($pkey)>0) {
881 $s .= ",\n PRIMARY KEY (";
882 $s .= implode(", ",$pkey).")";
884 if (isset($tableoptions['CONSTRAINTS']))
885 $s .= "\n".$tableoptions['CONSTRAINTS'];
887 if (isset($tableoptions[$this->upperName
.'_CONSTRAINTS']))
888 $s .= "\n".$tableoptions[$this->upperName
.'_CONSTRAINTS'];
891 if (isset($tableoptions[$this->upperName
])) $s .= $tableoptions[$this->upperName
];
898 GENERATE TRIGGERS IF NEEDED
899 used when table has auto-incrementing field that is emulated using triggers
901 function _Triggers($tabname,$taboptions)
907 Sanitize options, so that array elements with no keys are promoted to keys
909 function _Options($opts)
911 if (!is_array($opts)) return array();
913 foreach($opts as $k => $v) {
914 if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
915 else $newopts[strtoupper($k)] = $v;
921 function _getSizePrec($size)
925 $dotat = strpos($size,'.');
926 if ($dotat === false) $dotat = strpos($size,',');
927 if ($dotat === false) $fsize = $size;
929 $fsize = substr($size,0,$dotat);
930 $fprec = substr($size,$dotat+
1);
932 return array($fsize, $fprec);
936 "Florian Buzin [ easywe ]" <florian.buzin#easywe.de>
938 This function changes/adds new fields to your table. You don't
939 have to know if the col is new or not. It will check on its own.
941 function ChangeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFlds=false)
943 global $ADODB_FETCH_MODE;
945 $save = $ADODB_FETCH_MODE;
946 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
;
947 if ($this->connection
->fetchMode
!== false) $savem = $this->connection
->SetFetchMode(false);
949 // check table exists
950 $save_handler = $this->connection
->raiseErrorFn
;
951 $this->connection
->raiseErrorFn
= '';
952 $cols = $this->MetaColumns($tablename);
953 $this->connection
->raiseErrorFn
= $save_handler;
955 if (isset($savem)) $this->connection
->SetFetchMode($savem);
956 $ADODB_FETCH_MODE = $save;
959 return $this->CreateTableSQL($tablename, $flds, $tableoptions);
962 if (is_array($flds)) {
963 // Cycle through the update fields, comparing
964 // existing fields to fields to update.
965 // if the Metatype and size is exactly the
966 // same, ignore - by Mark Newham
968 foreach($flds as $k=>$v) {
969 if ( isset($cols[$k]) && is_object($cols[$k]) ) {
970 // If already not allowing nulls, then don't change
972 if (isset($obj->not_null
) && $obj->not_null
)
973 $v = str_replace('NOT NULL','',$v);
974 if (isset($obj->auto_increment
) && $obj->auto_increment
&& empty($v['AUTOINCREMENT']))
975 $v = str_replace('AUTOINCREMENT','',$v);
978 $ml = $c->max_length
;
979 $mt = $this->MetaType($c->type
,$ml);
981 if (isset($c->scale
)) $sc = $c->scale
;
982 else $sc = 99; // always force change if scale not known.
984 if ($sc == -1) $sc = false;
985 list($fsize, $fprec) = $this->_getSizePrec($v['SIZE']);
987 if ($ml == -1) $ml = '';
988 if ($mt == 'X') $ml = $v['SIZE'];
989 if (($mt != $v['TYPE']) ||
($ml != $fsize ||
$sc != $fprec) ||
(isset($v['AUTOINCREMENT']) && $v['AUTOINCREMENT'] != $obj->auto_increment
)) {
1000 // already exists, alter table instead
1001 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
1002 // genfields can return FALSE at times
1003 if ($lines == null) $lines = array();
1004 $alter = 'ALTER TABLE ' . $this->TableName($tablename);
1007 foreach ( $lines as $id => $v ) {
1008 if ( isset($cols[$id]) && is_object($cols[$id]) ) {
1010 $flds = Lens_ParseArgs($v,',');
1012 // We are trying to change the size of the field, if not allowed, simply ignore the request.
1013 // $flds[1] holds the type, $flds[2] holds the size -postnuke addition
1014 if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4
)
1015 && (isset($flds[0][2]) && is_numeric($flds[0][2]))) {
1016 if ($this->debug
) ADOConnection
::outp(sprintf("<h3>%s cannot be changed to %s currently</h3>", $flds[0][0], $flds[0][1]));
1017 #echo "<h3>$this->alterCol cannot be changed to $flds currently</h3>";
1020 $sql[] = $alter . $this->alterCol
. ' ' . $v;
1022 $sql[] = $alter . $this->addCol
. ' ' . $v;
1027 foreach ( $cols as $id => $v )
1028 if ( !isset($lines[$id]) )
1029 $sql[] = $alter . $this->dropCol
. ' ' . $v->name
;