new approach to logging database access and upgraded adodb
[openemr.git] / library / adodb / adodb-datadict.inc.php
blob31acb2ddafd1be3d72fe8de273e559987c71c85d
1 <?php
3 /**
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.
11 DOCUMENTATION:
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";
26 print "<p>$str</p>";
27 $a= Lens_ParseArgs($str);
28 print "<pre>";
29 print_r($a);
30 print "</pre>";
34 if (!function_exists('ctype_alnum')) {
35 function ctype_alnum($text) {
36 return preg_match('/^[a-z0-9]*$/i', $text);
40 //Lens_ParseTest();
42 /**
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
47 Same with '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='_.-')
56 $pos = 0;
57 $intoken = false;
58 $stmtno = 0;
59 $endquote = false;
60 $tokens = array();
61 $tokens[$stmtno] = array();
62 $max = strlen($args);
63 $quoted = false;
64 $tokarr = array();
66 while ($pos < $max) {
67 $ch = substr($args,$pos,1);
68 switch($ch) {
69 case ' ':
70 case "\t":
71 case "\n":
72 case "\r":
73 if (!$quoted) {
74 if ($intoken) {
75 $intoken = false;
76 $tokens[$stmtno][] = implode('',$tokarr);
78 break;
81 $tokarr[] = $ch;
82 break;
84 case '`':
85 if ($intoken) $tokarr[] = $ch;
86 case '(':
87 case ')':
88 case '"':
89 case "'":
91 if ($intoken) {
92 if (empty($endquote)) {
93 $tokens[$stmtno][] = implode('',$tokarr);
94 if ($ch == '(') $endquote = ')';
95 else $endquote = $ch;
96 $quoted = true;
97 $intoken = true;
98 $tokarr = array();
99 } else if ($endquote == $ch) {
100 $ch2 = substr($args,$pos+1,1);
101 if ($ch2 == $endquote) {
102 $pos += 1;
103 $tokarr[] = $ch2;
104 } else {
105 $quoted = false;
106 $intoken = false;
107 $tokens[$stmtno][] = implode('',$tokarr);
108 $endquote = '';
110 } else
111 $tokarr[] = $ch;
113 }else {
115 if ($ch == '(') $endquote = ')';
116 else $endquote = $ch;
117 $quoted = true;
118 $intoken = true;
119 $tokarr = array();
120 if ($ch == '`') $tokarr[] = '`';
122 break;
124 default:
126 if (!$intoken) {
127 if ($ch == $endstmtchar) {
128 $stmtno += 1;
129 $tokens[$stmtno] = array();
130 break;
133 $intoken = true;
134 $quoted = false;
135 $endquote = false;
136 $tokarr = array();
140 if ($quoted) $tokarr[] = $ch;
141 else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
142 else {
143 if ($ch == $endstmtchar) {
144 $tokens[$stmtno][] = implode('',$tokarr);
145 $stmtno += 1;
146 $tokens[$stmtno] = array();
147 $intoken = false;
148 $tokarr = array();
149 break;
151 $tokens[$stmtno][] = implode('',$tokarr);
152 $tokens[$stmtno][] = $ch;
153 $intoken = false;
156 $pos += 1;
158 if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
160 return $tokens;
164 class ADODB_DataDict {
165 var $connection;
166 var $debug = false;
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_\(\)';
176 var $schema = false;
177 var $serverInfo = array();
178 var $autoIncrement = false;
179 var $dataProvider;
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)
186 return false;
189 function SetCommentSQL($table,$col,$cmt)
191 return false;
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(
221 'VARCHAR' => 'C',
222 'VARCHAR2' => 'C',
223 'CHAR' => 'C',
224 'C' => 'C',
225 'STRING' => 'C',
226 'NCHAR' => 'C',
227 'NVARCHAR' => 'C',
228 'VARYING' => 'C',
229 'BPCHAR' => 'C',
230 'CHARACTER' => 'C',
231 'INTERVAL' => 'C', # Postgres
232 'MACADDR' => 'C', # postgres
233 'VAR_STRING' => 'C', # mysql
235 'LONGCHAR' => 'X',
236 'TEXT' => 'X',
237 'NTEXT' => 'X',
238 'M' => 'X',
239 'X' => 'X',
240 'CLOB' => 'X',
241 'NCLOB' => 'X',
242 'LVARCHAR' => 'X',
244 'BLOB' => 'B',
245 'IMAGE' => 'B',
246 'BINARY' => 'B',
247 'VARBINARY' => 'B',
248 'LONGBINARY' => 'B',
249 'B' => 'B',
251 'YEAR' => 'D', // mysql
252 'DATE' => 'D',
253 'D' => 'D',
255 'UNIQUEIDENTIFIER' => 'C', # MS SQL Server
257 'TIME' => 'T',
258 'TIMESTAMP' => 'T',
259 'DATETIME' => 'T',
260 'TIMESTAMPTZ' => 'T',
261 'SMALLDATETIME' => 'T',
262 'T' => 'T',
263 'TIMESTAMP WITHOUT TIME ZONE' => 'T', // postgresql
265 'BOOL' => 'L',
266 'BOOLEAN' => 'L',
267 'BIT' => 'L',
268 'L' => 'L',
270 'COUNTER' => 'R',
271 'R' => 'R',
272 'SERIAL' => 'R', // ifx
273 'INT IDENTITY' => 'R',
275 'INT' => 'I',
276 'INT2' => 'I',
277 'INT4' => 'I',
278 'INT8' => 'I',
279 'INTEGER' => 'I',
280 'INTEGER UNSIGNED' => 'I',
281 'SHORT' => 'I',
282 'TINYINT' => 'I',
283 'SMALLINT' => 'I',
284 'I' => 'I',
286 'LONG' => 'N', // interbase is numeric, oci8 is blob
287 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers
288 'DECIMAL' => 'N',
289 'DEC' => 'N',
290 'REAL' => 'N',
291 'DOUBLE' => 'N',
292 'DOUBLE PRECISION' => 'N',
293 'SMALLFLOAT' => 'N',
294 'FLOAT' => 'N',
295 'NUMBER' => 'N',
296 'NUM' => 'N',
297 'NUMERIC' => 'N',
298 'MONEY' => 'N',
300 ## informix 9.2
301 'SQLINT' => 'I',
302 'SQLSERIAL' => 'I',
303 'SQLSMINT' => 'I',
304 'SQLSMFLOAT' => 'N',
305 'SQLFLOAT' => 'N',
306 'SQLMONEY' => 'N',
307 'SQLDECIMAL' => 'N',
308 'SQLDATE' => 'D',
309 'SQLVCHAR' => 'C',
310 'SQLCHAR' => 'C',
311 'SQLDTIME' => 'T',
312 'SQLINTERVAL' => 'N',
313 'SQLBYTES' => 'B',
314 'SQLTEXT' => 'X',
315 ## informix 10
316 "SQLINT8" => 'I8',
317 "SQLSERIAL8" => 'I8',
318 "SQLNCHAR" => 'C',
319 "SQLNVCHAR" => 'C',
320 "SQLLVARCHAR" => 'X',
321 "SQLBOOL" => 'L'
324 if (!$this->connection->IsConnected()) {
325 $t = strtoupper($t);
326 if (isset($typeMap[$t])) return $typeMap[$t];
327 return 'N';
329 return $this->connection->MetaType($t,$len,$fieldobj);
332 function NameQuote($name = NULL,$allowBrackets=false)
334 if (!is_string($name)) {
335 return FALSE;
338 $name = trim($name);
340 if ( !is_object($this->connection) ) {
341 return $name;
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;
358 return $name;
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)
372 $rez = 2;
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;
380 if (!$ok) {
381 if ($this->debug) ADOConnection::outp($conn->ErrorMsg());
382 if (!$continueOnError) return 0;
383 $rez = 1;
386 return $rez;
390 Returns the actual type given a character code.
392 C: varchar
393 X: CLOB (character large object) or largest varchar size if CLOB is not supported
394 C2: Multibyte varchar
395 X2: Multibyte CLOB
397 B: BLOB (binary large object)
399 D: Date
400 T: Date-time
401 L: Integer field suitable for storing booleans (0 or 1)
402 I: Integer
403 F: Floating point number
404 N: Numeric or decimal number
407 function ActualType($meta)
409 return $meta;
412 function CreateDatabase($dbname,$options=false)
414 $options = $this->_Options($options);
415 $sql = array();
417 $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
418 if (isset($options[$this->upperName]))
419 $s .= ' '.$options[$this->upperName];
421 $sql[] = $s;
422 return $sql;
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);
455 $sql = array();
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);
469 return $sql;
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);
486 $sql = array();
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);
501 return $sql;
505 * Rename one column
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);
517 if ($flds) {
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));
528 * Drop one column
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);
542 $sql = array();
543 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
544 foreach($flds as $v) {
545 $sql[] = $alter . $this->NameQuote($v);
547 return $sql;
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);
590 return $sql;
595 function _GenFields($flds,$widespacing=false)
597 if (is_string($flds)) {
598 $padding = ' ';
599 $txt = $flds.$padding;
600 $flds = array();
601 $flds0 = Lens_ParseArgs($txt,',');
602 $hasparam = false;
603 foreach($flds0 as $f0) {
604 $f1 = array();
605 foreach($f0 as $token) {
606 switch (strtoupper($token)) {
607 case 'INDEX':
608 $f1['INDEX'] = '';
609 // fall through intentionally
610 case 'CONSTRAINT':
611 case 'DEFAULT':
612 $hasparam = $token;
613 break;
614 default:
615 if ($hasparam) $f1[$hasparam] = $token;
616 else $f1[] = $token;
617 $hasparam = false;
618 break;
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];
629 else
630 $f1['INDEX'] = 'idx_'.$f1['INDEX'];
632 // reset it, so we don't get next field 1st token as INDEX...
633 $hasparam = false;
635 $flds[] = $f1;
639 $this->autoIncrement = false;
640 $lines = array();
641 $pkey = array();
642 $idxs = array();
643 foreach($flds as $fld) {
644 $fld = _array_change_key_case($fld);
646 $fname = false;
647 $fdefault = false;
648 $fautoinc = false;
649 $ftype = false;
650 $fsize = false;
651 $fprec = false;
652 $fprimary = false;
653 $fnoquote = false;
654 $fdefts = false;
655 $fdefdate = false;
656 $fconstraint = false;
657 $fnotnull = false;
658 $funsigned = false;
659 $findex = '';
660 $funiqueindex = false;
662 //-----------------
663 // Parse attributes
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);
668 switch($attr) {
669 case '0':
670 case 'NAME': $fname = $v; break;
671 case '1':
672 case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
674 case 'SIZE':
675 $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
676 if ($dotat === false) $fsize = $v;
677 else {
678 $fsize = substr($v,0,$dotat);
679 $fprec = substr($v,$dotat+1);
681 break;
682 case 'UNSIGNED': $funsigned = true; break;
683 case 'AUTOINCREMENT':
684 case 'AUTO': $fautoinc = true; $fnotnull = true; break;
685 case 'KEY':
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;
688 case 'DEF':
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;
698 } //switch
699 } // foreach $fld
701 //--------------------
702 // VALIDATE FIELD INFO
703 if (!strlen($fname)) {
704 if ($this->debug) ADOConnection::outp("Undefined NAME");
705 return false;
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'");
713 return false;
714 } else {
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
728 if ($findex != '') {
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';
737 else
739 $idxs[$findex] = array();
740 $idxs[$findex]['cols'] = array($fname);
741 if ($funiqueindex)
742 $idxs[$findex]['opts'] = array('UNIQUE');
743 else
744 $idxs[$findex]['opts'] = array();
748 //--------------------
749 // CONSTRUCT FIELD SQL
750 if ($fdefts) {
751 if (substr($this->connection->databaseType,0,5) == 'mysql') {
752 $ftype = 'TIMESTAMP';
753 } else {
754 $fdefault = $this->connection->sysTimeStamp;
756 } else if ($fdefdate) {
757 if (substr($this->connection->databaseType,0,5) == 'mysql') {
758 $ftype = 'TIMESTAMP';
759 } else {
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
768 if ($ty == 'T')
770 $fdefault = $this->connection->DBTimeStamp($fdefault);
772 else
774 $fdefault = $this->connection->DBDate($fdefault);
777 else
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;
797 } // foreach $flds
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;
812 $ftype .= ')';
814 return $ftype;
818 // return string must begin with space
819 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
821 $suffix = '';
822 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
823 if ($fnotnull) $suffix .= ' NOT NULL';
824 if ($fconstraint) $suffix .= ' '.$fconstraint;
825 return $suffix;
828 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
830 $sql = array();
832 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
833 $sql[] = sprintf ($this->dropIndex, $idxname);
834 if ( isset($idxoptions['DROP']) )
835 return $sql;
838 if ( empty ($flds) ) {
839 return $sql;
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 . ')';
852 $sql[] = $s;
854 return $sql;
857 function _DropAutoIncrement($tabname)
859 return false;
862 function _TableSQL($tabname,$lines,$pkey,$tableoptions)
864 $sql = array();
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']) ) {
873 return $sql;
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'];
888 $s .= "\n)";
889 if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
890 $sql[] = $s;
892 return $sql;
896 GENERATE TRIGGERS IF NEEDED
897 used when table has auto-incrementing field that is emulated using triggers
899 function _Triggers($tabname,$taboptions)
901 return array();
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();
910 $newopts = array();
911 foreach($opts as $k => $v) {
912 if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
913 else $newopts[strtoupper($k)] = $v;
915 return $newopts;
919 function _getSizePrec($size)
921 $fsize = false;
922 $fprec = false;
923 $dotat = strpos($size,'.');
924 if ($dotat === false) $dotat = strpos($size,',');
925 if ($dotat === false) $fsize = $size;
926 else {
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;
956 if ( empty($cols)) {
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
965 $holdflds = array();
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
969 $obj = $cols[$k];
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);
975 $c = $cols[$k];
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)) {
988 $holdflds[$k] = $v;
990 } else {
991 $holdflds[$k] = $v;
994 $flds = $holdflds;
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);
1003 $sql = array();
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>";
1016 continue;
1018 $sql[] = $alter . $this->alterCol . ' ' . $v;
1019 } else {
1020 $sql[] = $alter . $this->addCol . ' ' . $v;
1024 if ($dropOldFlds) {
1025 foreach ( $cols as $id => $v )
1026 if ( !isset($lines[$id]) )
1027 $sql[] = $alter . $this->dropCol . ' ' . $v->name;
1029 return $sql;
1031 } // class