Merge branch 'MDL-58454-master' of git://github.com/junpataleta/moodle
[moodle.git] / lib / adodb / adodb-datadict.inc.php
blobb2a0837d3759fb29546ac6edf9b1d2138ed8cd68
1 <?php
3 /**
4 @version v5.20.9 21-Dec-2016
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.
13 DOCUMENTATION:
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";
28 print "<p>$str</p>";
29 $a= Lens_ParseArgs($str);
30 print "<pre>";
31 print_r($a);
32 print "</pre>";
36 if (!function_exists('ctype_alnum')) {
37 function ctype_alnum($text) {
38 return preg_match('/^[a-z0-9]*$/i', $text);
42 //Lens_ParseTest();
44 /**
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
49 Same with '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='_.-')
58 $pos = 0;
59 $intoken = false;
60 $stmtno = 0;
61 $endquote = false;
62 $tokens = array();
63 $tokens[$stmtno] = array();
64 $max = strlen($args);
65 $quoted = false;
66 $tokarr = array();
68 while ($pos < $max) {
69 $ch = substr($args,$pos,1);
70 switch($ch) {
71 case ' ':
72 case "\t":
73 case "\n":
74 case "\r":
75 if (!$quoted) {
76 if ($intoken) {
77 $intoken = false;
78 $tokens[$stmtno][] = implode('',$tokarr);
80 break;
83 $tokarr[] = $ch;
84 break;
86 case '`':
87 if ($intoken) $tokarr[] = $ch;
88 case '(':
89 case ')':
90 case '"':
91 case "'":
93 if ($intoken) {
94 if (empty($endquote)) {
95 $tokens[$stmtno][] = implode('',$tokarr);
96 if ($ch == '(') $endquote = ')';
97 else $endquote = $ch;
98 $quoted = true;
99 $intoken = true;
100 $tokarr = array();
101 } else if ($endquote == $ch) {
102 $ch2 = substr($args,$pos+1,1);
103 if ($ch2 == $endquote) {
104 $pos += 1;
105 $tokarr[] = $ch2;
106 } else {
107 $quoted = false;
108 $intoken = false;
109 $tokens[$stmtno][] = implode('',$tokarr);
110 $endquote = '';
112 } else
113 $tokarr[] = $ch;
115 }else {
117 if ($ch == '(') $endquote = ')';
118 else $endquote = $ch;
119 $quoted = true;
120 $intoken = true;
121 $tokarr = array();
122 if ($ch == '`') $tokarr[] = '`';
124 break;
126 default:
128 if (!$intoken) {
129 if ($ch == $endstmtchar) {
130 $stmtno += 1;
131 $tokens[$stmtno] = array();
132 break;
135 $intoken = true;
136 $quoted = false;
137 $endquote = false;
138 $tokarr = array();
142 if ($quoted) $tokarr[] = $ch;
143 else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
144 else {
145 if ($ch == $endstmtchar) {
146 $tokens[$stmtno][] = implode('',$tokarr);
147 $stmtno += 1;
148 $tokens[$stmtno] = array();
149 $intoken = false;
150 $tokarr = array();
151 break;
153 $tokens[$stmtno][] = implode('',$tokarr);
154 $tokens[$stmtno][] = $ch;
155 $intoken = false;
158 $pos += 1;
160 if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
162 return $tokens;
166 class ADODB_DataDict {
167 var $connection;
168 var $debug = false;
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_\(\)';
178 var $schema = false;
179 var $serverInfo = array();
180 var $autoIncrement = false;
181 var $dataProvider;
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)
188 return false;
191 function SetCommentSQL($table,$col,$cmt)
193 return false;
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(
223 'VARCHAR' => 'C',
224 'VARCHAR2' => 'C',
225 'CHAR' => 'C',
226 'C' => 'C',
227 'STRING' => 'C',
228 'NCHAR' => 'C',
229 'NVARCHAR' => 'C',
230 'VARYING' => 'C',
231 'BPCHAR' => 'C',
232 'CHARACTER' => 'C',
233 'INTERVAL' => 'C', # Postgres
234 'MACADDR' => 'C', # postgres
235 'VAR_STRING' => 'C', # mysql
237 'LONGCHAR' => 'X',
238 'TEXT' => 'X',
239 'NTEXT' => 'X',
240 'M' => 'X',
241 'X' => 'X',
242 'CLOB' => 'X',
243 'NCLOB' => 'X',
244 'LVARCHAR' => 'X',
246 'BLOB' => 'B',
247 'IMAGE' => 'B',
248 'BINARY' => 'B',
249 'VARBINARY' => 'B',
250 'LONGBINARY' => 'B',
251 'B' => 'B',
253 'YEAR' => 'D', // mysql
254 'DATE' => 'D',
255 'D' => 'D',
257 'UNIQUEIDENTIFIER' => 'C', # MS SQL Server
259 'TIME' => 'T',
260 'TIMESTAMP' => 'T',
261 'DATETIME' => 'T',
262 'TIMESTAMPTZ' => 'T',
263 'SMALLDATETIME' => 'T',
264 'T' => 'T',
265 'TIMESTAMP WITHOUT TIME ZONE' => 'T', // postgresql
267 'BOOL' => 'L',
268 'BOOLEAN' => 'L',
269 'BIT' => 'L',
270 'L' => 'L',
272 'COUNTER' => 'R',
273 'R' => 'R',
274 'SERIAL' => 'R', // ifx
275 'INT IDENTITY' => 'R',
277 'INT' => 'I',
278 'INT2' => 'I',
279 'INT4' => 'I',
280 'INT8' => 'I',
281 'INTEGER' => 'I',
282 'INTEGER UNSIGNED' => 'I',
283 'SHORT' => 'I',
284 'TINYINT' => 'I',
285 'SMALLINT' => 'I',
286 'I' => 'I',
288 'LONG' => 'N', // interbase is numeric, oci8 is blob
289 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers
290 'DECIMAL' => 'N',
291 'DEC' => 'N',
292 'REAL' => 'N',
293 'DOUBLE' => 'N',
294 'DOUBLE PRECISION' => 'N',
295 'SMALLFLOAT' => 'N',
296 'FLOAT' => 'N',
297 'NUMBER' => 'N',
298 'NUM' => 'N',
299 'NUMERIC' => 'N',
300 'MONEY' => 'N',
302 ## informix 9.2
303 'SQLINT' => 'I',
304 'SQLSERIAL' => 'I',
305 'SQLSMINT' => 'I',
306 'SQLSMFLOAT' => 'N',
307 'SQLFLOAT' => 'N',
308 'SQLMONEY' => 'N',
309 'SQLDECIMAL' => 'N',
310 'SQLDATE' => 'D',
311 'SQLVCHAR' => 'C',
312 'SQLCHAR' => 'C',
313 'SQLDTIME' => 'T',
314 'SQLINTERVAL' => 'N',
315 'SQLBYTES' => 'B',
316 'SQLTEXT' => 'X',
317 ## informix 10
318 "SQLINT8" => 'I8',
319 "SQLSERIAL8" => 'I8',
320 "SQLNCHAR" => 'C',
321 "SQLNVCHAR" => 'C',
322 "SQLLVARCHAR" => 'X',
323 "SQLBOOL" => 'L'
326 if (!$this->connection->IsConnected()) {
327 $t = strtoupper($t);
328 if (isset($typeMap[$t])) return $typeMap[$t];
329 return 'N';
331 return $this->connection->MetaType($t,$len,$fieldobj);
334 function NameQuote($name = NULL,$allowBrackets=false)
336 if (!is_string($name)) {
337 return FALSE;
340 $name = trim($name);
342 if ( !is_object($this->connection) ) {
343 return $name;
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;
360 return $name;
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)
374 $rez = 2;
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;
382 if (!$ok) {
383 if ($this->debug) ADOConnection::outp($conn->ErrorMsg());
384 if (!$continueOnError) return 0;
385 $rez = 1;
388 return $rez;
392 Returns the actual type given a character code.
394 C: varchar
395 X: CLOB (character large object) or largest varchar size if CLOB is not supported
396 C2: Multibyte varchar
397 X2: Multibyte CLOB
399 B: BLOB (binary large object)
401 D: Date
402 T: Date-time
403 L: Integer field suitable for storing booleans (0 or 1)
404 I: Integer
405 F: Floating point number
406 N: Numeric or decimal number
409 function ActualType($meta)
411 return $meta;
414 function CreateDatabase($dbname,$options=false)
416 $options = $this->_Options($options);
417 $sql = array();
419 $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
420 if (isset($options[$this->upperName]))
421 $s .= ' '.$options[$this->upperName];
423 $sql[] = $s;
424 return $sql;
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);
457 $sql = array();
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);
471 return $sql;
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);
488 $sql = array();
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);
503 return $sql;
507 * Rename one column
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);
519 if ($flds) {
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));
530 * Drop one column
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);
544 $sql = array();
545 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
546 foreach($flds as $v) {
547 $sql[] = $alter . $this->NameQuote($v);
549 return $sql;
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);
592 return $sql;
597 function _GenFields($flds,$widespacing=false)
599 if (is_string($flds)) {
600 $padding = ' ';
601 $txt = $flds.$padding;
602 $flds = array();
603 $flds0 = Lens_ParseArgs($txt,',');
604 $hasparam = false;
605 foreach($flds0 as $f0) {
606 $f1 = array();
607 foreach($f0 as $token) {
608 switch (strtoupper($token)) {
609 case 'INDEX':
610 $f1['INDEX'] = '';
611 // fall through intentionally
612 case 'CONSTRAINT':
613 case 'DEFAULT':
614 $hasparam = $token;
615 break;
616 default:
617 if ($hasparam) $f1[$hasparam] = $token;
618 else $f1[] = $token;
619 $hasparam = false;
620 break;
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];
631 else
632 $f1['INDEX'] = 'idx_'.$f1['INDEX'];
634 // reset it, so we don't get next field 1st token as INDEX...
635 $hasparam = false;
637 $flds[] = $f1;
641 $this->autoIncrement = false;
642 $lines = array();
643 $pkey = array();
644 $idxs = array();
645 foreach($flds as $fld) {
646 $fld = _array_change_key_case($fld);
648 $fname = false;
649 $fdefault = false;
650 $fautoinc = false;
651 $ftype = false;
652 $fsize = false;
653 $fprec = false;
654 $fprimary = false;
655 $fnoquote = false;
656 $fdefts = false;
657 $fdefdate = false;
658 $fconstraint = false;
659 $fnotnull = false;
660 $funsigned = false;
661 $findex = '';
662 $funiqueindex = false;
664 //-----------------
665 // Parse attributes
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);
670 switch($attr) {
671 case '0':
672 case 'NAME': $fname = $v; break;
673 case '1':
674 case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
676 case 'SIZE':
677 $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
678 if ($dotat === false) $fsize = $v;
679 else {
680 $fsize = substr($v,0,$dotat);
681 $fprec = substr($v,$dotat+1);
683 break;
684 case 'UNSIGNED': $funsigned = true; break;
685 case 'AUTOINCREMENT':
686 case 'AUTO': $fautoinc = true; $fnotnull = true; break;
687 case 'KEY':
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;
690 case 'DEF':
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;
700 } //switch
701 } // foreach $fld
703 //--------------------
704 // VALIDATE FIELD INFO
705 if (!strlen($fname)) {
706 if ($this->debug) ADOConnection::outp("Undefined NAME");
707 return false;
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'");
715 return false;
716 } else {
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
730 if ($findex != '') {
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';
739 else
741 $idxs[$findex] = array();
742 $idxs[$findex]['cols'] = array($fname);
743 if ($funiqueindex)
744 $idxs[$findex]['opts'] = array('UNIQUE');
745 else
746 $idxs[$findex]['opts'] = array();
750 //--------------------
751 // CONSTRUCT FIELD SQL
752 if ($fdefts) {
753 if (substr($this->connection->databaseType,0,5) == 'mysql') {
754 $ftype = 'TIMESTAMP';
755 } else {
756 $fdefault = $this->connection->sysTimeStamp;
758 } else if ($fdefdate) {
759 if (substr($this->connection->databaseType,0,5) == 'mysql') {
760 $ftype = 'TIMESTAMP';
761 } else {
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
770 if ($ty == 'T')
772 $fdefault = $this->connection->DBTimeStamp($fdefault);
774 else
776 $fdefault = $this->connection->DBDate($fdefault);
779 else
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;
799 } // foreach $flds
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;
814 $ftype .= ')';
816 return $ftype;
820 // return string must begin with space
821 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
823 $suffix = '';
824 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
825 if ($fnotnull) $suffix .= ' NOT NULL';
826 if ($fconstraint) $suffix .= ' '.$fconstraint;
827 return $suffix;
830 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
832 $sql = array();
834 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
835 $sql[] = sprintf ($this->dropIndex, $idxname);
836 if ( isset($idxoptions['DROP']) )
837 return $sql;
840 if ( empty ($flds) ) {
841 return $sql;
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 . ')';
854 $sql[] = $s;
856 return $sql;
859 function _DropAutoIncrement($tabname)
861 return false;
864 function _TableSQL($tabname,$lines,$pkey,$tableoptions)
866 $sql = array();
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']) ) {
875 return $sql;
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'];
890 $s .= "\n)";
891 if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
892 $sql[] = $s;
894 return $sql;
898 GENERATE TRIGGERS IF NEEDED
899 used when table has auto-incrementing field that is emulated using triggers
901 function _Triggers($tabname,$taboptions)
903 return array();
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();
912 $newopts = array();
913 foreach($opts as $k => $v) {
914 if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
915 else $newopts[strtoupper($k)] = $v;
917 return $newopts;
921 function _getSizePrec($size)
923 $fsize = false;
924 $fprec = false;
925 $dotat = strpos($size,'.');
926 if ($dotat === false) $dotat = strpos($size,',');
927 if ($dotat === false) $fsize = $size;
928 else {
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;
958 if ( empty($cols)) {
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
967 $holdflds = array();
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
971 $obj = $cols[$k];
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);
977 $c = $cols[$k];
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)) {
990 $holdflds[$k] = $v;
992 } else {
993 $holdflds[$k] = $v;
996 $flds = $holdflds;
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);
1005 $sql = array();
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>";
1018 continue;
1020 $sql[] = $alter . $this->alterCol . ' ' . $v;
1021 } else {
1022 $sql[] = $alter . $this->addCol . ' ' . $v;
1026 if ($dropOldFlds) {
1027 foreach ( $cols as $id => $v )
1028 if ( !isset($lines[$id]) )
1029 $sql[] = $alter . $this->dropCol . ' ' . $v->name;
1031 return $sql;
1033 } // class