/src/typo3_src-4.2.0alpha1/typo3/sysext/adodb/adodb/adodb-datadict.inc.php

00001 <?php
00002 
00016 /*
00017         Test script for parser
00018 */
00019 
00020 // security - hide paths
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 //Lens_ParseTest();
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';    // table, old-column, new-column, column-definitions (not used by default)
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'); // for changetablesql
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                 // if name is of the form `name`, quote it
00238                 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
00239                         return $quote . $matches[1] . $quote;
00240                 }
00241                 
00242                 // if name contains special characters, quote it
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         // Executes the sql array returned by GetTableSQL and GetIndexSQL
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         
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          Generates the SQL to create index. Returns an array of sql strings.
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,$idxs) = $this->_GenFields($flds);
00348                 // genfields can return FALSE at times
00349                 if ($lines  == null) $lines = array();
00350                 $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
00351                 foreach($lines as $v) {
00352                         $sql[] = $alter . $v;
00353                 }
00354                 if (is_array($idxs)) {
00355                         foreach($idxs as $idx => $idxdef) {
00356                                 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
00357                                 $sql = array_merge($sql, $sql_idxs);
00358                         }
00359                 }
00360                 return $sql;
00361         }
00362         
00374         function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
00375         {
00376                 $tabname = $this->TableName ($tabname);
00377                 $sql = array();
00378                 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
00379                 // genfields can return FALSE at times
00380                 if ($lines == null) $lines = array();
00381                 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
00382                 foreach($lines as $v) {
00383                         $sql[] = $alter . $v;
00384                 }
00385                 if (is_array($idxs)) {
00386                         foreach($idxs as $idx => $idxdef) {
00387                                 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
00388                                 $sql = array_merge($sql, $sql_idxs);
00389                         }
00390 
00391                 }
00392                 return $sql;
00393         }
00394         
00405         function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
00406         {
00407                 $tabname = $this->TableName ($tabname);
00408                 if ($flds) {
00409                         list($lines,$pkey,$idxs) = $this->_GenFields($flds);
00410                         // genfields can return FALSE at times
00411                         if ($lines == null) $lines = array();
00412                         list(,$first) = each($lines);
00413                         list(,$column_def) = split("[\t ]+",$first,2);
00414                 }
00415                 return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
00416         }
00417                 
00429         function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
00430         {
00431                 $tabname = $this->TableName ($tabname);
00432                 if (!is_array($flds)) $flds = explode(',',$flds);
00433                 $sql = array();
00434                 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
00435                 foreach($flds as $v) {
00436                         $sql[] = $alter . $this->NameQuote($v);
00437                 }
00438                 return $sql;
00439         }
00440         
00441         function DropTableSQL($tabname)
00442         {
00443                 return array (sprintf($this->dropTable, $this->TableName($tabname)));
00444         }
00445         
00446         function RenameTableSQL($tabname,$newname)
00447         {
00448                 return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
00449         }       
00450         
00454         function CreateTableSQL($tabname, $flds, $tableoptions=array())
00455         {
00456                 list($lines,$pkey,$idxs) = $this->_GenFields($flds, true);
00457                 // genfields can return FALSE at times
00458                 if ($lines == null) $lines = array();
00459                 
00460                 $taboptions = $this->_Options($tableoptions);
00461                 $tabname = $this->TableName ($tabname);
00462                 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
00463                 
00464                 // ggiunta - 2006/10/12 - KLUDGE:
00465         // if we are on autoincrement, and table options includes REPLACE, the
00466         // autoincrement sequence has already been dropped on table creation sql, so
00467         // we avoid passing REPLACE to trigger creation code. This prevents
00468         // creating sql that double-drops the sequence
00469         if ($this->autoIncrement && isset($taboptions['REPLACE']))
00470                 unset($taboptions['REPLACE']);
00471                 $tsql = $this->_Triggers($tabname,$taboptions);
00472                 foreach($tsql as $s) $sql[] = $s;
00473                 
00474                 if (is_array($idxs)) {
00475                         foreach($idxs as $idx => $idxdef) {
00476                                 $sql_idxs = $this->CreateIndexSql($idx, $tabname,  $idxdef['cols'], $idxdef['opts']);
00477                                 $sql = array_merge($sql, $sql_idxs);
00478                         }
00479                 }
00480 
00481                 return $sql;
00482         }
00483         
00484         function _GenFields($flds,$widespacing=false)
00485         {
00486                 if (is_string($flds)) {
00487                         $padding = '     ';
00488                         $txt = $flds.$padding;
00489                         $flds = array();
00490                         $flds0 = Lens_ParseArgs($txt,',');
00491                         $hasparam = false;
00492                         foreach($flds0 as $f0) {
00493                                 $f1 = array();
00494                                 foreach($f0 as $token) {
00495                                         switch (strtoupper($token)) {
00496                                         case 'INDEX':
00497                                                 $f1['INDEX'] = '';
00498                                                 // fall through intentionally
00499                                         case 'CONSTRAINT':
00500                                         case 'DEFAULT': 
00501                                                 $hasparam = $token;
00502                                                 break;
00503                                         default:
00504                                                 if ($hasparam) $f1[$hasparam] = $token;
00505                                                 else $f1[] = $token;
00506                                                 $hasparam = false;
00507                                                 break;
00508                                         }
00509                                 }
00510                                 // 'index' token without a name means single column index: name it after column
00511                                 if (array_key_exists('INDEX', $f1) && $f1['INDEX'] == '') {
00512                                         $f1['INDEX'] = isset($f0['NAME']) ? $f0['NAME'] : $f0[0];
00513                                         // check if column name used to create an index name was quoted
00514                                         if (($f1['INDEX'][0] == '"' || $f1['INDEX'][0] == "'" || $f1['INDEX'][0] == "`") &&
00515                                                 ($f1['INDEX'][0] == substr($f1['INDEX'], -1))) {
00516                                                 $f1['INDEX'] = $f1['INDEX'][0].'idx_'.substr($f1['INDEX'], 1, -1).$f1['INDEX'][0];
00517                                         }
00518                                         else
00519                                                 $f1['INDEX'] = 'idx_'.$f1['INDEX'];
00520                                 }
00521                                 // reset it, so we don't get next field 1st token as INDEX...
00522                                 $hasparam = false;
00523 
00524                                 $flds[] = $f1;
00525                                 
00526                         }
00527                 }
00528                 $this->autoIncrement = false;
00529                 $lines = array();
00530                 $pkey = array();
00531                 $idxs = array();
00532                 foreach($flds as $fld) {
00533                         $fld = _array_change_key_case($fld);
00534                         
00535                         $fname = false;
00536                         $fdefault = false;
00537                         $fautoinc = false;
00538                         $ftype = false;
00539                         $fsize = false;
00540                         $fprec = false;
00541                         $fprimary = false;
00542                         $fnoquote = false;
00543                         $fdefts = false;
00544                         $fdefdate = false;
00545                         $fconstraint = false;
00546                         $fnotnull = false;
00547                         $funsigned = false;
00548                         $findex = '';
00549                         $funiqueindex = false;
00550                         
00551                         //-----------------
00552                         // Parse attributes
00553                         foreach($fld as $attr => $v) {
00554                                 if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
00555                                 else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
00556                                 
00557                                 switch($attr) {
00558                                 case '0':
00559                                 case 'NAME':    $fname = $v; break;
00560                                 case '1':
00561                                 case 'TYPE':    $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
00562                                 
00563                                 case 'SIZE':    
00564                                                                 $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
00565                                                                 if ($dotat === false) $fsize = $v;
00566                                                                 else {
00567                                                                         $fsize = substr($v,0,$dotat);
00568                                                                         $fprec = substr($v,$dotat+1);
00569                                                                 }
00570                                                                 break;
00571                                 case 'UNSIGNED': $funsigned = true; break;
00572                                 case 'AUTOINCREMENT':
00573                                 case 'AUTO':    $fautoinc = true; $fnotnull = true; break;
00574                                 case 'KEY':
00575                 // a primary key col can be non unique in itself (if key spans many cols...)
00576                                 case 'PRIMARY': $fprimary = $v; $fnotnull = true; /*$funiqueindex = true;*/ break;
00577                                 case 'DEF':
00578                                 case 'DEFAULT': $fdefault = $v; break;
00579                                 case 'NOTNULL': $fnotnull = $v; break;
00580                                 case 'NOQUOTE': $fnoquote = $v; break;
00581                                 case 'DEFDATE': $fdefdate = $v; break;
00582                                 case 'DEFTIMESTAMP': $fdefts = $v; break;
00583                                 case 'CONSTRAINT': $fconstraint = $v; break;
00584                                 // let INDEX keyword create a 'very standard' index on column
00585                                 case 'INDEX': $findex = $v; break;
00586                                 case 'UNIQUE': $funiqueindex = true; break;
00587                                 } //switch
00588                         } // foreach $fld
00589                         
00590                         //--------------------
00591                         // VALIDATE FIELD INFO
00592                         if (!strlen($fname)) {
00593                                 if ($this->debug) ADOConnection::outp("Undefined NAME");
00594                                 return false;
00595                         }
00596                         
00597                         $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
00598                         $fname = $this->NameQuote($fname);
00599                         
00600                         if (!strlen($ftype)) {
00601                                 if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'");
00602                                 return false;
00603                         } else {
00604                                 $ftype = strtoupper($ftype);
00605                         }
00606                         
00607                         $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
00608                         
00609                         if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
00610                         
00611                         if ($fprimary) $pkey[] = $fname;
00612                         
00613                         // some databases do not allow blobs to have defaults
00614                         if ($ty == 'X') $fdefault = false;
00615                         
00616                         // build list of indexes
00617                         if ($findex != '') {
00618                                 if (array_key_exists($findex, $idxs)) {
00619                                         $idxs[$findex]['cols'][] = ($fname);
00620                                         if (in_array('UNIQUE', $idxs[$findex]['opts']) != $funiqueindex) {
00621                                                 if ($this->debug) ADOConnection::outp("Index $findex defined once UNIQUE and once not");
00622                                         }
00623                                         if ($funiqueindex && !in_array('UNIQUE', $idxs[$findex]['opts']))
00624                                                 $idxs[$findex]['opts'][] = 'UNIQUE';
00625                                 }
00626                                 else
00627                                 {
00628                                         $idxs[$findex] = array();
00629                                         $idxs[$findex]['cols'] = array($fname);
00630                                         if ($funiqueindex)
00631                                                 $idxs[$findex]['opts'] = array('UNIQUE');
00632                                         else
00633                                                 $idxs[$findex]['opts'] = array();
00634                                 }
00635                         }
00636 
00637                         //--------------------
00638                         // CONSTRUCT FIELD SQL
00639                         if ($fdefts) {
00640                                 if (substr($this->connection->databaseType,0,5) == 'mysql') {
00641                                         $ftype = 'TIMESTAMP';
00642                                 } else {
00643                                         $fdefault = $this->connection->sysTimeStamp;
00644                                 }
00645                         } else if ($fdefdate) {
00646                                 if (substr($this->connection->databaseType,0,5) == 'mysql') {
00647                                         $ftype = 'TIMESTAMP';
00648                                 } else {
00649                                         $fdefault = $this->connection->sysDate;
00650                                 }
00651                         } else if ($fdefault !== false && !$fnoquote) {
00652                                 if ($ty == 'C' or $ty == 'X' or 
00653                                         ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) {
00654 
00655                                         if (($ty == 'D' || $ty == 'T') && strtolower($fdefault) != 'null') {
00656                                                 // convert default date into database-aware code
00657                                                 if ($ty == 'T')
00658                                                 {
00659                                                         $fdefault = $this->connection->DBTimeStamp($fdefault);
00660                                                 }
00661                                                 else
00662                                                 {
00663                                                         $fdefault = $this->connection->DBDate($fdefault);
00664                                                 }
00665                                         }
00666                                         else
00667                                         if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ') 
00668                                                 $fdefault = trim($fdefault);
00669                                         else if (strtolower($fdefault) != 'null')
00670                                                 $fdefault = $this->connection->qstr($fdefault);
00671                                 }
00672                         }
00673                         $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
00674                         
00675                         // add index creation
00676                         if ($widespacing) $fname = str_pad($fname,24);
00677                         
00678                          // check for field names appearing twice
00679             if (array_key_exists($fid, $lines)) {
00680                  ADOConnection::outp("Field '$fname' defined twice");
00681             }
00682                         
00683                         $lines[$fid] = $fname.' '.$ftype.$suffix;
00684                         
00685                         if ($fautoinc) $this->autoIncrement = true;
00686                 } // foreach $flds
00687                 
00688                 return array($lines,$pkey,$idxs);
00689         }
00690 
00696         function _GetSize($ftype, $ty, $fsize, $fprec)
00697         {
00698                 if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
00699                         $ftype .= "(".$fsize;
00700                         if (strlen($fprec)) $ftype .= ",".$fprec;
00701                         $ftype .= ')';
00702                 }
00703                 return $ftype;
00704         }
00705         
00706         
00707         // return string must begin with space
00708         function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
00709         {       
00710                 $suffix = '';
00711                 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
00712                 if ($fnotnull) $suffix .= ' NOT NULL';
00713                 if ($fconstraint) $suffix .= ' '.$fconstraint;
00714                 return $suffix;
00715         }
00716         
00717         function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
00718         {
00719                 $sql = array();
00720                 
00721                 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
00722                         $sql[] = sprintf ($this->dropIndex, $idxname);
00723                         if ( isset($idxoptions['DROP']) )
00724                                 return $sql;
00725                 }
00726                 
00727                 if ( empty ($flds) ) {
00728                         return $sql;
00729                 }
00730                 
00731                 $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
00732         
00733                 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
00734                 
00735                 if ( isset($idxoptions[$this->upperName]) )
00736                         $s .= $idxoptions[$this->upperName];
00737                 
00738                 if ( is_array($flds) )
00739                         $flds = implode(', ',$flds);
00740                 $s .= '(' . $flds . ')';
00741                 $sql[] = $s;
00742                 
00743                 return $sql;
00744         }
00745         
00746         function _DropAutoIncrement($tabname)
00747         {
00748                 return false;
00749         }
00750         
00751         function _TableSQL($tabname,$lines,$pkey,$tableoptions)
00752         {
00753                 $sql = array();
00754                 
00755                 if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
00756                         $sql[] = sprintf($this->dropTable,$tabname);
00757                         if ($this->autoIncrement) {
00758                                 $sInc = $this->_DropAutoIncrement($tabname);
00759                                 if ($sInc) $sql[] = $sInc;
00760                         }
00761                         if ( isset ($tableoptions['DROP']) ) {
00762                                 return $sql;
00763                         }
00764                 }
00765                 $s = "CREATE TABLE $tabname (\n";
00766                 $s .= implode(",\n", $lines);
00767                 if (sizeof($pkey)>0) {
00768                         $s .= ",\n                 PRIMARY KEY (";
00769                         $s .= implode(", ",$pkey).")";
00770                 }
00771                 if (isset($tableoptions['CONSTRAINTS'])) 
00772                         $s .= "\n".$tableoptions['CONSTRAINTS'];
00773                 
00774                 if (isset($tableoptions[$this->upperName.'_CONSTRAINTS'])) 
00775                         $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
00776                 
00777                 $s .= "\n)";
00778                 if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
00779                 $sql[] = $s;
00780                 
00781                 return $sql;
00782         }
00783         
00788         function _Triggers($tabname,$taboptions)
00789         {
00790                 return array();
00791         }
00792         
00796         function _Options($opts)
00797         {
00798                 if (!is_array($opts)) return array();
00799                 $newopts = array();
00800                 foreach($opts as $k => $v) {
00801                         if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
00802                         else $newopts[strtoupper($k)] = $v;
00803                 }
00804                 return $newopts;
00805         }
00806         
00813         function ChangeTableSQL($tablename, $flds, $tableoptions = false)
00814         {
00815         global $ADODB_FETCH_MODE;
00816         
00817                 $save = $ADODB_FETCH_MODE;
00818                 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
00819                 if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false);
00820                 
00821                 // check table exists
00822                 $save_handler = $this->connection->raiseErrorFn;
00823                 $this->connection->raiseErrorFn = '';
00824                 $cols = $this->MetaColumns($tablename);
00825                 $this->connection->raiseErrorFn = $save_handler;
00826                 
00827                 if (isset($savem)) $this->connection->SetFetchMode($savem);
00828                 $ADODB_FETCH_MODE = $save;
00829                 
00830                 if ( empty($cols)) { 
00831                         return $this->CreateTableSQL($tablename, $flds, $tableoptions);
00832                 }
00833                 
00834                 if (is_array($flds)) {
00835                 // Cycle through the update fields, comparing
00836                 // existing fields to fields to update.
00837                 // if the Metatype and size is exactly the
00838                 // same, ignore - by Mark Newham
00839                         $holdflds = array();
00840                         foreach($flds as $k=>$v) {
00841                                 if ( isset($cols[$k]) && is_object($cols[$k]) ) {
00842                                         // If already not allowing nulls, then don't change
00843                                         $obj = $cols[$k];
00844                                         if (isset($obj->not_null) && $obj->not_null)
00845                                                 $v = str_replace('NOT NULL','',$v);
00846 
00847                                         $c = $cols[$k];
00848                                         $ml = $c->max_length;
00849                                         $mt = $this->MetaType($c->type,$ml);
00850                                         if ($ml == -1) $ml = '';
00851                                         if ($mt == 'X') $ml = $v['SIZE'];
00852                                         if (($mt != $v['TYPE']) ||  $ml != $v['SIZE']) {
00853                                                 $holdflds[$k] = $v;
00854                                         }
00855                                 } else {
00856                                         $holdflds[$k] = $v;
00857                                 }               
00858                         }
00859                         $flds = $holdflds;
00860                 }
00861         
00862 
00863                 // already exists, alter table instead
00864                 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
00865                 // genfields can return FALSE at times
00866                 if ($lines == null) $lines = array();
00867                 $alter = 'ALTER TABLE ' . $this->TableName($tablename);
00868                 $sql = array();
00869 
00870                 foreach ( $lines as $id => $v ) {
00871                         if ( isset($cols[$id]) && is_object($cols[$id]) ) {
00872                         
00873                                 $flds = Lens_ParseArgs($v,',');
00874                                 
00875                                 //  We are trying to change the size of the field, if not allowed, simply ignore the request.
00876                                 if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) {
00877                                         echo "<h3>$this->alterCol cannot be changed to $flds currently</h3>";
00878                                         continue;        
00879                                 }
00880                                 $sql[] = $alter . $this->alterCol . ' ' . $v;
00881                         } else {
00882                                 $sql[] = $alter . $this->addCol . ' ' . $v;
00883                         }
00884                 }
00885                 
00886                 return $sql;
00887         }
00888 } // class
00889 ?>

This documentation has been generated automatically from TYPO3 source code using Doxygen and is provided as is by Cast Iron Coding as a courtesy to other TYPO3 developers and users. Please consider Cast Iron Coding — a full-service web development agency in Portland, Oregon specializing in TYPO3 extension development — for all of your TYPO3 development and consulting needs!