00001 <?php
00002
00016
00017
00018
00019
00020
00021 if (!defined('ADODB_DIR')) die();
00022
00023 function Lens_ParseTest()
00024 {
00025 $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";
00026 print "<p>$str</p>";
00027 $a= Lens_ParseArgs($str);
00028 print "<pre>";
00029 print_r($a);
00030 print "</pre>";
00031 }
00032
00033
00034 if (!function_exists('ctype_alnum')) {
00035 function ctype_alnum($text) {
00036 return preg_match('/^[a-z0-9]*$/i', $text);
00037 }
00038 }
00039
00040
00041
00054 function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
00055 {
00056 $pos = 0;
00057 $intoken = false;
00058 $stmtno = 0;
00059 $endquote = false;
00060 $tokens = array();
00061 $tokens[$stmtno] = array();
00062 $max = strlen($args);
00063 $quoted = false;
00064 $tokarr = array();
00065
00066 while ($pos < $max) {
00067 $ch = substr($args,$pos,1);
00068 switch($ch) {
00069 case ' ':
00070 case "\t":
00071 case "\n":
00072 case "\r":
00073 if (!$quoted) {
00074 if ($intoken) {
00075 $intoken = false;
00076 $tokens[$stmtno][] = implode('',$tokarr);
00077 }
00078 break;
00079 }
00080
00081 $tokarr[] = $ch;
00082 break;
00083
00084 case '`':
00085 if ($intoken) $tokarr[] = $ch;
00086 case '(':
00087 case ')':
00088 case '"':
00089 case "'":
00090
00091 if ($intoken) {
00092 if (empty($endquote)) {
00093 $tokens[$stmtno][] = implode('',$tokarr);
00094 if ($ch == '(') $endquote = ')';
00095 else $endquote = $ch;
00096 $quoted = true;
00097 $intoken = true;
00098 $tokarr = array();
00099 } else if ($endquote == $ch) {
00100 $ch2 = substr($args,$pos+1,1);
00101 if ($ch2 == $endquote) {
00102 $pos += 1;
00103 $tokarr[] = $ch2;
00104 } else {
00105 $quoted = false;
00106 $intoken = false;
00107 $tokens[$stmtno][] = implode('',$tokarr);
00108 $endquote = '';
00109 }
00110 } else
00111 $tokarr[] = $ch;
00112
00113 }else {
00114
00115 if ($ch == '(') $endquote = ')';
00116 else $endquote = $ch;
00117 $quoted = true;
00118 $intoken = true;
00119 $tokarr = array();
00120 if ($ch == '`') $tokarr[] = '`';
00121 }
00122 break;
00123
00124 default:
00125
00126 if (!$intoken) {
00127 if ($ch == $endstmtchar) {
00128 $stmtno += 1;
00129 $tokens[$stmtno] = array();
00130 break;
00131 }
00132
00133 $intoken = true;
00134 $quoted = false;
00135 $endquote = false;
00136 $tokarr = array();
00137
00138 }
00139
00140 if ($quoted) $tokarr[] = $ch;
00141 else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
00142 else {
00143 if ($ch == $endstmtchar) {
00144 $tokens[$stmtno][] = implode('',$tokarr);
00145 $stmtno += 1;
00146 $tokens[$stmtno] = array();
00147 $intoken = false;
00148 $tokarr = array();
00149 break;
00150 }
00151 $tokens[$stmtno][] = implode('',$tokarr);
00152 $tokens[$stmtno][] = $ch;
00153 $intoken = false;
00154 }
00155 }
00156 $pos += 1;
00157 }
00158 if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
00159
00160 return $tokens;
00161 }
00162
00163
00164 class ADODB_DataDict {
00165 var $connection;
00166 var $debug = false;
00167 var $dropTable = 'DROP TABLE %s';
00168 var $renameTable = 'RENAME TABLE %s TO %s';
00169 var $dropIndex = 'DROP INDEX %s';
00170 var $addCol = ' ADD';
00171 var $alterCol = ' ALTER COLUMN';
00172 var $dropCol = ' DROP COLUMN';
00173 var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s';
00174 var $nameRegex = '\w';
00175 var $nameRegexBrackets = 'a-zA-Z0-9_\(\)';
00176 var $schema = false;
00177 var $serverInfo = array();
00178 var $autoIncrement = false;
00179 var $dataProvider;
00180 var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME');
00181 var $blobSize = 100;
00182
00183
00184 function GetCommentSQL($table,$col)
00185 {
00186 return false;
00187 }
00188
00189 function SetCommentSQL($table,$col,$cmt)
00190 {
00191 return false;
00192 }
00193
00194 function MetaTables()
00195 {
00196 if (!$this->connection->IsConnected()) return array();
00197 return $this->connection->MetaTables();
00198 }
00199
00200 function MetaColumns($tab, $upper=true, $schema=false)
00201 {
00202 if (!$this->connection->IsConnected()) return array();
00203 return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema);
00204 }
00205
00206 function MetaPrimaryKeys($tab,$owner=false,$intkey=false)
00207 {
00208 if (!$this->connection->IsConnected()) return array();
00209 return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
00210 }
00211
00212 function MetaIndexes($table, $primary = false, $owner = false)
00213 {
00214 if (!$this->connection->IsConnected()) return array();
00215 return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner);
00216 }
00217
00218 function MetaType($t,$len=-1,$fieldobj=false)
00219 {
00220 return ADORecordSet::MetaType($t,$len,$fieldobj);
00221 }
00222
00223 function NameQuote($name = NULL,$allowBrackets=false)
00224 {
00225 if (!is_string($name)) {
00226 return FALSE;
00227 }
00228
00229 $name = trim($name);
00230
00231 if ( !is_object($this->connection) ) {
00232 return $name;
00233 }
00234
00235 $quote = $this->connection->nameQuote;
00236
00237
00238 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
00239 return $quote . $matches[1] . $quote;
00240 }
00241
00242
00243 $regex = ($allowBrackets) ? $this->nameRegexBrackets : $this->nameRegex;
00244
00245 if ( !preg_match('/^[' . $regex . ']+$/', $name) ) {
00246 return $quote . $name . $quote;
00247 }
00248
00249 return $name;
00250 }
00251
00252 function TableName($name)
00253 {
00254 if ( $this->schema ) {
00255 return $this->NameQuote($this->schema) .'.'. $this->NameQuote($name);
00256 }
00257 return $this->NameQuote($name);
00258 }
00259
00260
00261 function ExecuteSQLArray($sql, $continueOnError = true)
00262 {
00263 $rez = 2;
00264 $conn = &$this->connection;
00265 $saved = $conn->debug;
00266 foreach($sql as $line) {
00267
00268 if ($this->debug) $conn->debug = true;
00269 $ok = $conn->Execute($line);
00270 $conn->debug = $saved;
00271 if (!$ok) {
00272 if ($this->debug) ADOConnection::outp($conn->ErrorMsg());
00273 if (!$continueOnError) return 0;
00274 $rez = 1;
00275 }
00276 }
00277 return $rez;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 function ActualType($meta)
00299 {
00300 return $meta;
00301 }
00302
00303 function CreateDatabase($dbname,$options=false)
00304 {
00305 $options = $this->_Options($options);
00306 $sql = array();
00307
00308 $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
00309 if (isset($options[$this->upperName]))
00310 $s .= ' '.$options[$this->upperName];
00311
00312 $sql[] = $s;
00313 return $sql;
00314 }
00315
00316
00317
00318
00319 function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
00320 {
00321 if (!is_array($flds)) {
00322 $flds = explode(',',$flds);
00323 }
00324
00325 foreach($flds as $key => $fld) {
00326 # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
00327 $flds[$key] = $this->NameQuote($fld,$allowBrackets=true);
00328 }
00329
00330 return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
00331 }
00332
00333 function DropIndexSQL ($idxname, $tabname = NULL)
00334 {
00335 return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname)));
00336 }
00337
00338 function SetSchema($schema)
00339 {
00340 $this->schema = $schema;
00341 }
00342
00343 function AddColumnSQL($tabname, $flds)
00344 {
00345 $tabname = $this->TableName ($tabname);
00346 $sql = array();
00347 list($lines,$pkey) = $this->_GenFields($flds);
00348 $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
00349 foreach($lines as $v) {
00350 $sql[] = $alter . $v;
00351 }
00352 return $sql;
00353 }
00354
00366 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
00367 {
00368 $tabname = $this->TableName ($tabname);
00369 $sql = array();
00370 list($lines,$pkey) = $this->_GenFields($flds);
00371 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
00372 foreach($lines as $v) {
00373 $sql[] = $alter . $v;
00374 }
00375 return $sql;
00376 }
00377
00388 function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
00389 {
00390 $tabname = $this->TableName ($tabname);
00391 if ($flds) {
00392 list($lines,$pkey) = $this->_GenFields($flds);
00393 list(,$first) = each($lines);
00394 list(,$column_def) = split("[\t ]+",$first,2);
00395 }
00396 return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
00397 }
00398
00410 function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
00411 {
00412 $tabname = $this->TableName ($tabname);
00413 if (!is_array($flds)) $flds = explode(',',$flds);
00414 $sql = array();
00415 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
00416 foreach($flds as $v) {
00417 $sql[] = $alter . $this->NameQuote($v);
00418 }
00419 return $sql;
00420 }
00421
00422 function DropTableSQL($tabname)
00423 {
00424 return array (sprintf($this->dropTable, $this->TableName($tabname)));
00425 }
00426
00427 function RenameTableSQL($tabname,$newname)
00428 {
00429 return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
00430 }
00431
00432
00433
00434
00435 function CreateTableSQL($tabname, $flds, $tableoptions=false)
00436 {
00437 if (!$tableoptions) $tableoptions = array();
00438
00439 list($lines,$pkey) = $this->_GenFields($flds, true);
00440
00441 $taboptions = $this->_Options($tableoptions);
00442 $tabname = $this->TableName ($tabname);
00443 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
00444
00445 $tsql = $this->_Triggers($tabname,$taboptions);
00446 foreach($tsql as $s) $sql[] = $s;
00447
00448 return $sql;
00449 }
00450
00451 function _GenFields($flds,$widespacing=false)
00452 {
00453 if (is_string($flds)) {
00454 $padding = ' ';
00455 $txt = $flds.$padding;
00456 $flds = array();
00457 $flds0 = Lens_ParseArgs($txt,',');
00458 $hasparam = false;
00459 foreach($flds0 as $f0) {
00460 $f1 = array();
00461 foreach($f0 as $token) {
00462 switch (strtoupper($token)) {
00463 case 'CONSTRAINT':
00464 case 'DEFAULT':
00465 $hasparam = $token;
00466 break;
00467 default:
00468 if ($hasparam) $f1[$hasparam] = $token;
00469 else $f1[] = $token;
00470 $hasparam = false;
00471 break;
00472 }
00473 }
00474 $flds[] = $f1;
00475
00476 }
00477 }
00478 $this->autoIncrement = false;
00479 $lines = array();
00480 $pkey = array();
00481 foreach($flds as $fld) {
00482 $fld = _array_change_key_case($fld);
00483
00484 $fname = false;
00485 $fdefault = false;
00486 $fautoinc = false;
00487 $ftype = false;
00488 $fsize = false;
00489 $fprec = false;
00490 $fprimary = false;
00491 $fnoquote = false;
00492 $fdefts = false;
00493 $fdefdate = false;
00494 $fconstraint = false;
00495 $fnotnull = false;
00496 $funsigned = false;
00497
00498
00499
00500 foreach($fld as $attr => $v) {
00501 if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
00502 else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
00503
00504 switch($attr) {
00505 case '0':
00506 case 'NAME': $fname = $v; break;
00507 case '1':
00508 case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
00509
00510 case 'SIZE':
00511 $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
00512 if ($dotat === false) $fsize = $v;
00513 else {
00514 $fsize = substr($v,0,$dotat);
00515 $fprec = substr($v,$dotat+1);
00516 }
00517 break;
00518 case 'UNSIGNED': $funsigned = true; break;
00519 case 'AUTOINCREMENT':
00520 case 'AUTO': $fautoinc = true; $fnotnull = true; break;
00521 case 'KEY':
00522 case 'PRIMARY': $fprimary = $v; $fnotnull = true; break;
00523 case 'DEF':
00524 case 'DEFAULT': $fdefault = $v; break;
00525 case 'NOTNULL': $fnotnull = $v; break;
00526 case 'NOQUOTE': $fnoquote = $v; break;
00527 case 'DEFDATE': $fdefdate = $v; break;
00528 case 'DEFTIMESTAMP': $fdefts = $v; break;
00529 case 'CONSTRAINT': $fconstraint = $v; break;
00530 }
00531 }
00532
00533
00534
00535 if (!strlen($fname)) {
00536 if ($this->debug) ADOConnection::outp("Undefined NAME");
00537 return false;
00538 }
00539
00540 $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
00541 $fname = $this->NameQuote($fname);
00542
00543 if (!strlen($ftype)) {
00544 if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'");
00545 return false;
00546 } else {
00547 $ftype = strtoupper($ftype);
00548 }
00549
00550 $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
00551
00552 if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false;
00553
00554 if ($fprimary) $pkey[] = $fname;
00555
00556
00557 if ($ty == 'X') $fdefault = false;
00558
00559
00560
00561 if ($fdefts) {
00562 if (substr($this->connection->databaseType,0,5) == 'mysql') {
00563 $ftype = 'TIMESTAMP';
00564 } else {
00565 $fdefault = $this->connection->sysTimeStamp;
00566 }
00567 } else if ($fdefdate) {
00568 if (substr($this->connection->databaseType,0,5) == 'mysql') {
00569 $ftype = 'TIMESTAMP';
00570 } else {
00571 $fdefault = $this->connection->sysDate;
00572 }
00573 } else if ($fdefault !== false && !$fnoquote)
00574 if ($ty == 'C' or $ty == 'X' or
00575 ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault)))
00576 if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
00577 $fdefault = trim($fdefault);
00578 else if (strtolower($fdefault) != 'null')
00579 $fdefault = $this->connection->qstr($fdefault);
00580 $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
00581
00582 if ($widespacing) $fname = str_pad($fname,24);
00583 $lines[$fid] = $fname.' '.$ftype.$suffix;
00584
00585 if ($fautoinc) $this->autoIncrement = true;
00586 }
00587
00588 return array($lines,$pkey);
00589 }
00590
00591
00592
00593
00594
00595 function _GetSize($ftype, $ty, $fsize, $fprec)
00596 {
00597 if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
00598 $ftype .= "(".$fsize;
00599 if (strlen($fprec)) $ftype .= ",".$fprec;
00600 $ftype .= ')';
00601 }
00602 return $ftype;
00603 }
00604
00605
00606
00607 function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
00608 {
00609 $suffix = '';
00610 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
00611 if ($fnotnull) $suffix .= ' NOT NULL';
00612 if ($fconstraint) $suffix .= ' '.$fconstraint;
00613 return $suffix;
00614 }
00615
00616 function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
00617 {
00618 $sql = array();
00619
00620 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
00621 $sql[] = sprintf ($this->dropIndex, $idxname);
00622 if ( isset($idxoptions['DROP']) )
00623 return $sql;
00624 }
00625
00626 if ( empty ($flds) ) {
00627 return $sql;
00628 }
00629
00630 $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
00631
00632 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
00633
00634 if ( isset($idxoptions[$this->upperName]) )
00635 $s .= $idxoptions[$this->upperName];
00636
00637 if ( is_array($flds) )
00638 $flds = implode(', ',$flds);
00639 $s .= '(' . $flds . ')';
00640 $sql[] = $s;
00641
00642 return $sql;
00643 }
00644
00645 function _DropAutoIncrement($tabname)
00646 {
00647 return false;
00648 }
00649
00650 function _TableSQL($tabname,$lines,$pkey,$tableoptions)
00651 {
00652 $sql = array();
00653
00654 if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
00655 $sql[] = sprintf($this->dropTable,$tabname);
00656 if ($this->autoIncrement) {
00657 $sInc = $this->_DropAutoIncrement($tabname);
00658 if ($sInc) $sql[] = $sInc;
00659 }
00660 if ( isset ($tableoptions['DROP']) ) {
00661 return $sql;
00662 }
00663 }
00664 $s = "CREATE TABLE $tabname (\n";
00665 $s .= implode(",\n", $lines);
00666 if (sizeof($pkey)>0) {
00667 $s .= ",\n PRIMARY KEY (";
00668 $s .= implode(", ",$pkey).")";
00669 }
00670 if (isset($tableoptions['CONSTRAINTS']))
00671 $s .= "\n".$tableoptions['CONSTRAINTS'];
00672
00673 if (isset($tableoptions[$this->upperName.'_CONSTRAINTS']))
00674 $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
00675
00676 $s .= "\n)";
00677 if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
00678 $sql[] = $s;
00679
00680 return $sql;
00681 }
00682
00683
00684
00685
00686
00687 function _Triggers($tabname,$taboptions)
00688 {
00689 return array();
00690 }
00691
00692
00693
00694
00695 function _Options($opts)
00696 {
00697 if (!is_array($opts)) return array();
00698 $newopts = array();
00699 foreach($opts as $k => $v) {
00700 if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
00701 else $newopts[strtoupper($k)] = $v;
00702 }
00703 return $newopts;
00704 }
00705
00706
00707
00708
00709
00710
00711
00712 function ChangeTableSQL($tablename, $flds, $tableoptions = false)
00713 {
00714 global $ADODB_FETCH_MODE;
00715
00716 $save = $ADODB_FETCH_MODE;
00717 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00718 if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false);
00719
00720
00721 $save_handler = $this->connection->raiseErrorFn;
00722 $this->connection->raiseErrorFn = '';
00723 $cols = $this->MetaColumns($tablename);
00724 $this->connection->raiseErrorFn = $save_handler;
00725
00726 if (isset($savem)) $this->connection->SetFetchMode($savem);
00727 $ADODB_FETCH_MODE = $save;
00728
00729 if ( empty($cols)) {
00730 return $this->CreateTableSQL($tablename, $flds, $tableoptions);
00731 }
00732
00733 if (is_array($flds)) {
00734
00735
00736
00737
00738 $holdflds = array();
00739 foreach($flds as $k=>$v) {
00740 if ( isset($cols[$k]) && is_object($cols[$k]) ) {
00741
00742 $obj = $cols[$k];
00743 if (isset($obj->not_null) && $obj->not_null)
00744 $v = str_replace('NOT NULL','',$v);
00745
00746 $c = $cols[$k];
00747 $ml = $c->max_length;
00748 $mt = $this->MetaType($c->type,$ml);
00749 if ($ml == -1) $ml = '';
00750 if ($mt == 'X') $ml = $v['SIZE'];
00751 if (($mt != $v['TYPE']) || $ml != $v['SIZE']) {
00752 $holdflds[$k] = $v;
00753 }
00754 } else {
00755 $holdflds[$k] = $v;
00756 }
00757 }
00758 $flds = $holdflds;
00759 }
00760
00761
00762
00763 list($lines,$pkey) = $this->_GenFields($flds);
00764 $alter = 'ALTER TABLE ' . $this->TableName($tablename);
00765 $sql = array();
00766
00767 foreach ( $lines as $id => $v ) {
00768 if ( isset($cols[$id]) && is_object($cols[$id]) ) {
00769
00770 $flds = Lens_ParseArgs($v,',');
00771
00772
00773 if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) continue;
00774
00775 $sql[] = $alter . $this->alterCol . ' ' . $v;
00776 } else {
00777 $sql[] = $alter . $this->addCol . ' ' . $v;
00778 }
00779 }
00780
00781 return $sql;
00782 }
00783 }
00784 ?>