4 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
5 Released under both BSD license and Lesser GPL library license.
6 Whenever there is any discrepancy between the two licenses,
7 the BSD license will take precedence.
9 Set tabs to 4 for best viewing.
13 See adodb/tests/test-datadict.php for docs and examples.
17 Test script for parser
20 // security - hide paths
21 if (!defined('ADODB_DIR')) die();
23 function Lens_ParseTest()
25 $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";
27 $a= Lens_ParseArgs($str);
34 if (!function_exists('ctype_alnum')) {
35 function ctype_alnum($text) {
36 return preg_match('/^[a-z0-9]*$/i', $text);
43 Parse arguments, treat "text" (text) and 'text' as quotation marks.
44 To escape, use "" or '' or ))
46 Will read in "abc def" sans quotes, as: abc def
48 However if `abc def`, then will read in as `abc def`
50 @param endstmtchar Character that indicates end of statement
51 @param tokenchars Include the following characters in tokens apart from A-Z and 0-9
52 @returns 2 dimensional array containing parsed tokens.
54 function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
61 $tokens[$stmtno] = array();
67 $ch = substr($args,$pos,1);
76 $tokens[$stmtno][] = implode('',$tokarr);
85 if ($intoken) $tokarr[] = $ch;
92 if (empty($endquote)) {
93 $tokens[$stmtno][] = implode('',$tokarr);
94 if ($ch == '(') $endquote = ')';
99 } else if ($endquote == $ch) {
100 $ch2 = substr($args,$pos+
1,1);
101 if ($ch2 == $endquote) {
107 $tokens[$stmtno][] = implode('',$tokarr);
115 if ($ch == '(') $endquote = ')';
116 else $endquote = $ch;
120 if ($ch == '`') $tokarr[] = '`';
127 if ($ch == $endstmtchar) {
129 $tokens[$stmtno] = array();
140 if ($quoted) $tokarr[] = $ch;
141 else if (ctype_alnum($ch) ||
strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
143 if ($ch == $endstmtchar) {
144 $tokens[$stmtno][] = implode('',$tokarr);
146 $tokens[$stmtno] = array();
151 $tokens[$stmtno][] = implode('',$tokarr);
152 $tokens[$stmtno][] = $ch;
158 if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
164 class ADODB_DataDict
{
167 var $dropTable = 'DROP TABLE %s';
168 var $renameTable = 'RENAME TABLE %s TO %s';
169 var $dropIndex = 'DROP INDEX %s';
170 var $addCol = ' ADD';
171 var $alterCol = ' ALTER COLUMN';
172 var $dropCol = ' DROP COLUMN';
173 var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s'; // table, old-column, new-column, column-definitions (not used by default)
174 var $nameRegex = '\w';
175 var $nameRegexBrackets = 'a-zA-Z0-9_\(\)';
177 var $serverInfo = array();
178 var $autoIncrement = false;
180 var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changetablesql
181 var $blobSize = 100; /// any varchar/char field this size or greater is treated as a blob
182 /// in other words, we use a text area for editting.
184 function GetCommentSQL($table,$col)
189 function SetCommentSQL($table,$col,$cmt)
194 function MetaTables()
196 if (!$this->connection
->IsConnected()) return array();
197 return $this->connection
->MetaTables();
200 function MetaColumns($tab, $upper=true, $schema=false)
202 if (!$this->connection
->IsConnected()) return array();
203 return $this->connection
->MetaColumns($this->TableName($tab), $upper, $schema);
206 function MetaPrimaryKeys($tab,$owner=false,$intkey=false)
208 if (!$this->connection
->IsConnected()) return array();
209 return $this->connection
->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
212 function MetaIndexes($table, $primary = false, $owner = false)
214 if (!$this->connection
->IsConnected()) return array();
215 return $this->connection
->MetaIndexes($this->TableName($table), $primary, $owner);
218 function MetaType($t,$len=-1,$fieldobj=false)
220 static $typeMap = array(
231 'INTERVAL' => 'C', # Postgres
232 'MACADDR' => 'C', # postgres
233 'VAR_STRING' => 'C', # mysql
251 'YEAR' => 'D', // mysql
255 'UNIQUEIDENTIFIER' => 'C', # MS SQL Server
260 'TIMESTAMPTZ' => 'T',
261 'SMALLDATETIME' => 'T',
263 'TIMESTAMP WITHOUT TIME ZONE' => 'T', // postgresql
272 'SERIAL' => 'R', // ifx
273 'INT IDENTITY' => 'R',
280 'INTEGER UNSIGNED' => 'I',
286 'LONG' => 'N', // interbase is numeric, oci8 is blob
287 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers
292 'DOUBLE PRECISION' => 'N',
312 'SQLINTERVAL' => 'N',
317 "SQLSERIAL8" => 'I8',
320 "SQLLVARCHAR" => 'X',
324 if (!$this->connection
->IsConnected()) {
326 if (isset($typeMap[$t])) return $typeMap[$t];
329 return $this->connection
->MetaType($t,$len,$fieldobj);
332 function NameQuote($name = NULL,$allowBrackets=false)
334 if (!is_string($name)) {
340 if ( !is_object($this->connection
) ) {
344 $quote = $this->connection
->nameQuote
;
346 // if name is of the form `name`, quote it
347 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
348 return $quote . $matches[1] . $quote;
351 // if name contains special characters, quote it
352 $regex = ($allowBrackets) ?
$this->nameRegexBrackets
: $this->nameRegex
;
354 if ( !preg_match('/^[' . $regex . ']+$/', $name) ) {
355 return $quote . $name . $quote;
361 function TableName($name)
363 if ( $this->schema
) {
364 return $this->NameQuote($this->schema
) .'.'. $this->NameQuote($name);
366 return $this->NameQuote($name);
369 // Executes the sql array returned by GetTableSQL and GetIndexSQL
370 function ExecuteSQLArray($sql, $continueOnError = true)
373 $conn = $this->connection
;
374 $saved = $conn->debug
;
375 foreach($sql as $line) {
377 if ($this->debug
) $conn->debug
= true;
378 $ok = $conn->Execute($line);
379 $conn->debug
= $saved;
381 if ($this->debug
) ADOConnection
::outp($conn->ErrorMsg());
382 if (!$continueOnError) return 0;
390 Returns the actual type given a character code.
393 X: CLOB (character large object) or largest varchar size if CLOB is not supported
394 C2: Multibyte varchar
397 B: BLOB (binary large object)
401 L: Integer field suitable for storing booleans (0 or 1)
403 F: Floating point number
404 N: Numeric or decimal number
407 function ActualType($meta)
412 function CreateDatabase($dbname,$options=false)
414 $options = $this->_Options($options);
417 $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
418 if (isset($options[$this->upperName
]))
419 $s .= ' '.$options[$this->upperName
];
426 Generates the SQL to create index. Returns an array of sql strings.
428 function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
430 if (!is_array($flds)) {
431 $flds = explode(',',$flds);
434 foreach($flds as $key => $fld) {
435 # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
436 $flds[$key] = $this->NameQuote($fld,$allowBrackets=true);
439 return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
442 function DropIndexSQL ($idxname, $tabname = NULL)
444 return array(sprintf($this->dropIndex
, $this->NameQuote($idxname), $this->TableName($tabname)));
447 function SetSchema($schema)
449 $this->schema
= $schema;
452 function AddColumnSQL($tabname, $flds)
454 $tabname = $this->TableName ($tabname);
456 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
457 // genfields can return FALSE at times
458 if ($lines == null) $lines = array();
459 $alter = 'ALTER TABLE ' . $tabname . $this->addCol
. ' ';
460 foreach($lines as $v) {
461 $sql[] = $alter . $v;
463 if (is_array($idxs)) {
464 foreach($idxs as $idx => $idxdef) {
465 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
466 $sql = array_merge($sql, $sql_idxs);
473 * Change the definition of one column
475 * As some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
476 * to allow, recreating the table and copying the content over to the new table
477 * @param string $tabname table-name
478 * @param string $flds column-name and type for the changed column
479 * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
480 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
481 * @return array with SQL strings
483 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
485 $tabname = $this->TableName ($tabname);
487 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
488 // genfields can return FALSE at times
489 if ($lines == null) $lines = array();
490 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol
. ' ';
491 foreach($lines as $v) {
492 $sql[] = $alter . $v;
494 if (is_array($idxs)) {
495 foreach($idxs as $idx => $idxdef) {
496 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
497 $sql = array_merge($sql, $sql_idxs);
507 * Some DBM's can only do this together with changeing the type of the column (even if that stays the same, eg. mysql)
508 * @param string $tabname table-name
509 * @param string $oldcolumn column-name to be renamed
510 * @param string $newcolumn new column-name
511 * @param string $flds='' complete column-defintion-string like for AddColumnSQL, only used by mysql atm., default=''
512 * @return array with SQL strings
514 function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
516 $tabname = $this->TableName ($tabname);
518 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
519 // genfields can return FALSE at times
520 if ($lines == null) $lines = array();
521 list(,$first) = each($lines);
522 list(,$column_def) = preg_split("/[\t ]+/",$first,2);
524 return array(sprintf($this->renameColumn
,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
530 * Some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
531 * to allow, recreating the table and copying the content over to the new table
532 * @param string $tabname table-name
533 * @param string $flds column-name and type for the changed column
534 * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
535 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
536 * @return array with SQL strings
538 function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
540 $tabname = $this->TableName ($tabname);
541 if (!is_array($flds)) $flds = explode(',',$flds);
543 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol
. ' ';
544 foreach($flds as $v) {
545 $sql[] = $alter . $this->NameQuote($v);
550 function DropTableSQL($tabname)
552 return array (sprintf($this->dropTable
, $this->TableName($tabname)));
555 function RenameTableSQL($tabname,$newname)
557 return array (sprintf($this->renameTable
, $this->TableName($tabname),$this->TableName($newname)));
561 Generate the SQL to create table. Returns an array of sql strings.
563 function CreateTableSQL($tabname, $flds, $tableoptions=array())
565 list($lines,$pkey,$idxs) = $this->_GenFields($flds, true);
566 // genfields can return FALSE at times
567 if ($lines == null) $lines = array();
569 $taboptions = $this->_Options($tableoptions);
570 $tabname = $this->TableName ($tabname);
571 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
573 // ggiunta - 2006/10/12 - KLUDGE:
574 // if we are on autoincrement, and table options includes REPLACE, the
575 // autoincrement sequence has already been dropped on table creation sql, so
576 // we avoid passing REPLACE to trigger creation code. This prevents
577 // creating sql that double-drops the sequence
578 if ($this->autoIncrement
&& isset($taboptions['REPLACE']))
579 unset($taboptions['REPLACE']);
580 $tsql = $this->_Triggers($tabname,$taboptions);
581 foreach($tsql as $s) $sql[] = $s;
583 if (is_array($idxs)) {
584 foreach($idxs as $idx => $idxdef) {
585 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
586 $sql = array_merge($sql, $sql_idxs);
595 function _GenFields($flds,$widespacing=false)
597 if (is_string($flds)) {
599 $txt = $flds.$padding;
601 $flds0 = Lens_ParseArgs($txt,',');
603 foreach($flds0 as $f0) {
605 foreach($f0 as $token) {
606 switch (strtoupper($token)) {
609 // fall through intentionally
615 if ($hasparam) $f1[$hasparam] = $token;
621 // 'index' token without a name means single column index: name it after column
622 if (array_key_exists('INDEX', $f1) && $f1['INDEX'] == '') {
623 $f1['INDEX'] = isset($f0['NAME']) ?
$f0['NAME'] : $f0[0];
624 // check if column name used to create an index name was quoted
625 if (($f1['INDEX'][0] == '"' ||
$f1['INDEX'][0] == "'" ||
$f1['INDEX'][0] == "`") &&
626 ($f1['INDEX'][0] == substr($f1['INDEX'], -1))) {
627 $f1['INDEX'] = $f1['INDEX'][0].'idx_'.substr($f1['INDEX'], 1, -1).$f1['INDEX'][0];
630 $f1['INDEX'] = 'idx_'.$f1['INDEX'];
632 // reset it, so we don't get next field 1st token as INDEX...
639 $this->autoIncrement
= false;
643 foreach($flds as $fld) {
644 $fld = _array_change_key_case($fld);
656 $fconstraint = false;
660 $funiqueindex = false;
664 foreach($fld as $attr => $v) {
665 if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
666 else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
670 case 'NAME': $fname = $v; break;
672 case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
675 $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
676 if ($dotat === false) $fsize = $v;
678 $fsize = substr($v,0,$dotat);
679 $fprec = substr($v,$dotat+
1);
682 case 'UNSIGNED': $funsigned = true; break;
683 case 'AUTOINCREMENT':
684 case 'AUTO': $fautoinc = true; $fnotnull = true; break;
686 // a primary key col can be non unique in itself (if key spans many cols...)
687 case 'PRIMARY': $fprimary = $v; $fnotnull = true; /*$funiqueindex = true;*/ break;
689 case 'DEFAULT': $fdefault = $v; break;
690 case 'NOTNULL': $fnotnull = $v; break;
691 case 'NOQUOTE': $fnoquote = $v; break;
692 case 'DEFDATE': $fdefdate = $v; break;
693 case 'DEFTIMESTAMP': $fdefts = $v; break;
694 case 'CONSTRAINT': $fconstraint = $v; break;
695 // let INDEX keyword create a 'very standard' index on column
696 case 'INDEX': $findex = $v; break;
697 case 'UNIQUE': $funiqueindex = true; break;
701 //--------------------
702 // VALIDATE FIELD INFO
703 if (!strlen($fname)) {
704 if ($this->debug
) ADOConnection
::outp("Undefined NAME");
708 $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
709 $fname = $this->NameQuote($fname);
711 if (!strlen($ftype)) {
712 if ($this->debug
) ADOConnection
::outp("Undefined TYPE for field '$fname'");
715 $ftype = strtoupper($ftype);
718 $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
720 if ($ty == 'X' ||
$ty == 'X2' ||
$ty == 'B') $fnotnull = false; // some blob types do not accept nulls
722 if ($fprimary) $pkey[] = $fname;
724 // some databases do not allow blobs to have defaults
725 if ($ty == 'X') $fdefault = false;
727 // build list of indexes
729 if (array_key_exists($findex, $idxs)) {
730 $idxs[$findex]['cols'][] = ($fname);
731 if (in_array('UNIQUE', $idxs[$findex]['opts']) != $funiqueindex) {
732 if ($this->debug
) ADOConnection
::outp("Index $findex defined once UNIQUE and once not");
734 if ($funiqueindex && !in_array('UNIQUE', $idxs[$findex]['opts']))
735 $idxs[$findex]['opts'][] = 'UNIQUE';
739 $idxs[$findex] = array();
740 $idxs[$findex]['cols'] = array($fname);
742 $idxs[$findex]['opts'] = array('UNIQUE');
744 $idxs[$findex]['opts'] = array();
748 //--------------------
749 // CONSTRUCT FIELD SQL
751 if (substr($this->connection
->databaseType
,0,5) == 'mysql') {
752 $ftype = 'TIMESTAMP';
754 $fdefault = $this->connection
->sysTimeStamp
;
756 } else if ($fdefdate) {
757 if (substr($this->connection
->databaseType
,0,5) == 'mysql') {
758 $ftype = 'TIMESTAMP';
760 $fdefault = $this->connection
->sysDate
;
762 } else if ($fdefault !== false && !$fnoquote) {
763 if ($ty == 'C' or $ty == 'X' or
764 ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) {
766 if (($ty == 'D' ||
$ty == 'T') && strtolower($fdefault) != 'null') {
767 // convert default date into database-aware code
770 $fdefault = $this->connection
->DBTimeStamp($fdefault);
774 $fdefault = $this->connection
->DBDate($fdefault);
778 if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
779 $fdefault = trim($fdefault);
780 else if (strtolower($fdefault) != 'null')
781 $fdefault = $this->connection
->qstr($fdefault);
784 $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
786 // add index creation
787 if ($widespacing) $fname = str_pad($fname,24);
789 // check for field names appearing twice
790 if (array_key_exists($fid, $lines)) {
791 ADOConnection
::outp("Field '$fname' defined twice");
794 $lines[$fid] = $fname.' '.$ftype.$suffix;
796 if ($fautoinc) $this->autoIncrement
= true;
799 return array($lines,$pkey,$idxs);
803 GENERATE THE SIZE PART OF THE DATATYPE
804 $ftype is the actual type
805 $ty is the type defined originally in the DDL
807 function _GetSize($ftype, $ty, $fsize, $fprec)
809 if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
810 $ftype .= "(".$fsize;
811 if (strlen($fprec)) $ftype .= ",".$fprec;
818 // return string must begin with space
819 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
822 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
823 if ($fnotnull) $suffix .= ' NOT NULL';
824 if ($fconstraint) $suffix .= ' '.$fconstraint;
828 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
832 if ( isset($idxoptions['REPLACE']) ||
isset($idxoptions['DROP']) ) {
833 $sql[] = sprintf ($this->dropIndex
, $idxname);
834 if ( isset($idxoptions['DROP']) )
838 if ( empty ($flds) ) {
842 $unique = isset($idxoptions['UNIQUE']) ?
' UNIQUE' : '';
844 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
846 if ( isset($idxoptions[$this->upperName
]) )
847 $s .= $idxoptions[$this->upperName
];
849 if ( is_array($flds) )
850 $flds = implode(', ',$flds);
851 $s .= '(' . $flds . ')';
857 function _DropAutoIncrement($tabname)
862 function _TableSQL($tabname,$lines,$pkey,$tableoptions)
866 if (isset($tableoptions['REPLACE']) ||
isset ($tableoptions['DROP'])) {
867 $sql[] = sprintf($this->dropTable
,$tabname);
868 if ($this->autoIncrement
) {
869 $sInc = $this->_DropAutoIncrement($tabname);
870 if ($sInc) $sql[] = $sInc;
872 if ( isset ($tableoptions['DROP']) ) {
876 $s = "CREATE TABLE $tabname (\n";
877 $s .= implode(",\n", $lines);
878 if (sizeof($pkey)>0) {
879 $s .= ",\n PRIMARY KEY (";
880 $s .= implode(", ",$pkey).")";
882 if (isset($tableoptions['CONSTRAINTS']))
883 $s .= "\n".$tableoptions['CONSTRAINTS'];
885 if (isset($tableoptions[$this->upperName
.'_CONSTRAINTS']))
886 $s .= "\n".$tableoptions[$this->upperName
.'_CONSTRAINTS'];
889 if (isset($tableoptions[$this->upperName
])) $s .= $tableoptions[$this->upperName
];
896 GENERATE TRIGGERS IF NEEDED
897 used when table has auto-incrementing field that is emulated using triggers
899 function _Triggers($tabname,$taboptions)
905 Sanitize options, so that array elements with no keys are promoted to keys
907 function _Options($opts)
909 if (!is_array($opts)) return array();
911 foreach($opts as $k => $v) {
912 if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
913 else $newopts[strtoupper($k)] = $v;
919 function _getSizePrec($size)
923 $dotat = strpos($size,'.');
924 if ($dotat === false) $dotat = strpos($size,',');
925 if ($dotat === false) $fsize = $size;
927 $fsize = substr($size,0,$dotat);
928 $fprec = substr($size,$dotat+
1);
930 return array($fsize, $fprec);
934 "Florian Buzin [ easywe ]" <florian.buzin#easywe.de>
936 This function changes/adds new fields to your table. You don't
937 have to know if the col is new or not. It will check on its own.
939 function ChangeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFlds=false)
941 global $ADODB_FETCH_MODE;
943 $save = $ADODB_FETCH_MODE;
944 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
;
945 if ($this->connection
->fetchMode
!== false) $savem = $this->connection
->SetFetchMode(false);
947 // check table exists
948 $save_handler = $this->connection
->raiseErrorFn
;
949 $this->connection
->raiseErrorFn
= '';
950 $cols = $this->MetaColumns($tablename);
951 $this->connection
->raiseErrorFn
= $save_handler;
953 if (isset($savem)) $this->connection
->SetFetchMode($savem);
954 $ADODB_FETCH_MODE = $save;
957 return $this->CreateTableSQL($tablename, $flds, $tableoptions);
960 if (is_array($flds)) {
961 // Cycle through the update fields, comparing
962 // existing fields to fields to update.
963 // if the Metatype and size is exactly the
964 // same, ignore - by Mark Newham
966 foreach($flds as $k=>$v) {
967 if ( isset($cols[$k]) && is_object($cols[$k]) ) {
968 // If already not allowing nulls, then don't change
970 if (isset($obj->not_null
) && $obj->not_null
)
971 $v = str_replace('NOT NULL','',$v);
972 if (isset($obj->auto_increment
) && $obj->auto_increment
&& empty($v['AUTOINCREMENT']))
973 $v = str_replace('AUTOINCREMENT','',$v);
976 $ml = $c->max_length
;
977 $mt = $this->MetaType($c->type
,$ml);
979 if (isset($c->scale
)) $sc = $c->scale
;
980 else $sc = 99; // always force change if scale not known.
982 if ($sc == -1) $sc = false;
983 list($fsize, $fprec) = $this->_getSizePrec($v['SIZE']);
985 if ($ml == -1) $ml = '';
986 if ($mt == 'X') $ml = $v['SIZE'];
987 if (($mt != $v['TYPE']) ||
($ml != $fsize ||
$sc != $fprec) ||
(isset($v['AUTOINCREMENT']) && $v['AUTOINCREMENT'] != $obj->auto_increment
)) {
998 // already exists, alter table instead
999 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
1000 // genfields can return FALSE at times
1001 if ($lines == null) $lines = array();
1002 $alter = 'ALTER TABLE ' . $this->TableName($tablename);
1005 foreach ( $lines as $id => $v ) {
1006 if ( isset($cols[$id]) && is_object($cols[$id]) ) {
1008 $flds = Lens_ParseArgs($v,',');
1010 // We are trying to change the size of the field, if not allowed, simply ignore the request.
1011 // $flds[1] holds the type, $flds[2] holds the size -postnuke addition
1012 if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4
)
1013 && (isset($flds[0][2]) && is_numeric($flds[0][2]))) {
1014 if ($this->debug
) ADOConnection
::outp(sprintf("<h3>%s cannot be changed to %s currently</h3>", $flds[0][0], $flds[0][1]));
1015 #echo "<h3>$this->alterCol cannot be changed to $flds currently</h3>";
1018 $sql[] = $alter . $this->alterCol
. ' ' . $v;
1020 $sql[] = $alter . $this->addCol
. ' ' . $v;
1025 foreach ( $cols as $id => $v )
1026 if ( !isset($lines[$id]) )
1027 $sql[] = $alter . $this->dropCol
. ' ' . $v->name
;