[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/adodb/ -> adodb-datadict.inc.php (source)

   1  <?php
   2  
   3  /**
   4    V4.65 22 July 2005  (c) 2000-2005 John Lim (jlim@natsoft.com.my). 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.
   8      
   9    Set tabs to 4 for best viewing.
  10   
  11       DOCUMENTATION:
  12      
  13          See adodb/tests/test-datadict.php for docs and examples.
  14  */
  15  
  16  /*
  17      Test script for parser
  18  */
  19  
  20  // security - hide paths
  21  if (!defined('ADODB_DIR')) die();
  22  
  23  function Lens_ParseTest()
  24  {
  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>";
  31  }
  32  
  33  
  34  if (!function_exists('ctype_alnum')) {
  35  	function ctype_alnum($text) {
  36          return preg_match('/^[a-z0-9]*$/i', $text);
  37      }
  38  }
  39  
  40  //Lens_ParseTest();
  41  
  42  /**
  43      Parse arguments, treat "text" (text) and 'text' as quotation marks.
  44      To escape, use "" or '' or ))
  45      
  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`
  49      
  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.
  53  */
  54  function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
  55  {
  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      
  65      while ($pos < $max) {
  66          $ch = substr($args,$pos,1);
  67          switch($ch) {
  68          case ' ':
  69          case "\t":
  70          case "\n":
  71          case "\r":
  72              if (!$quoted) {
  73                  if ($intoken) {
  74                      $intoken = false;
  75                      $tokens[$stmtno][] = implode('',$tokarr);
  76                  }
  77                  break;
  78              }
  79              
  80              $tokarr[] = $ch;
  81              break;
  82          
  83          case '`':
  84              if ($intoken) $tokarr[] = $ch;
  85          case '(':
  86          case ')':    
  87          case '"':
  88          case "'":
  89              
  90              if ($intoken) {
  91                  if (empty($endquote)) {
  92                      $tokens[$stmtno][] = implode('',$tokarr);
  93                      if ($ch == '(') $endquote = ')';
  94                      else $endquote = $ch;
  95                      $quoted = true;
  96                      $intoken = true;
  97                      $tokarr = array();
  98                  } else if ($endquote == $ch) {
  99                      $ch2 = substr($args,$pos+1,1);
 100                      if ($ch2 == $endquote) {
 101                          $pos += 1;
 102                          $tokarr[] = $ch2;
 103                      } else {
 104                          $quoted = false;
 105                          $intoken = false;
 106                          $tokens[$stmtno][] = implode('',$tokarr);
 107                          $endquote = '';
 108                      }
 109                  } else
 110                      $tokarr[] = $ch;
 111                      
 112              }else {
 113              
 114                  if ($ch == '(') $endquote = ')';
 115                  else $endquote = $ch;
 116                  $quoted = true;
 117                  $intoken = true;
 118                  $tokarr = array();
 119                  if ($ch == '`') $tokarr[] = '`';
 120              }
 121              break;
 122              
 123          default:
 124              
 125              if (!$intoken) {
 126                  if ($ch == $endstmtchar) {
 127                      $stmtno += 1;
 128                      $tokens[$stmtno] = array();
 129                      break;
 130                  }
 131              
 132                  $intoken = true;
 133                  $quoted = false;
 134                  $endquote = false;
 135                  $tokarr = array();
 136      
 137              }
 138              
 139              if ($quoted) $tokarr[] = $ch;
 140              else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
 141              else {
 142                  if ($ch == $endstmtchar) {            
 143                      $tokens[$stmtno][] = implode('',$tokarr);
 144                      $stmtno += 1;
 145                      $tokens[$stmtno] = array();
 146                      $intoken = false;
 147                      $tokarr = array();
 148                      break;
 149                  }
 150                  $tokens[$stmtno][] = implode('',$tokarr);
 151                  $tokens[$stmtno][] = $ch;
 152                  $intoken = false;
 153              }
 154          }
 155          $pos += 1;
 156      }
 157      if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
 158      
 159      return $tokens;
 160  }
 161  
 162  
 163  class ADODB_DataDict {
 164      var $connection;
 165      var $debug = false;
 166      var $dropTable = 'DROP TABLE %s';
 167      var $renameTable = 'RENAME TABLE %s TO %s'; 
 168      var $dropIndex = 'DROP INDEX %s';
 169      var $addCol = ' ADD';
 170      var $alterCol = ' ALTER COLUMN';
 171      var $dropCol = ' DROP COLUMN';
 172      var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s';    // table, old-column, new-column, column-definitions (not used by default)
 173      var $nameRegex = '\w';
 174      var $nameRegexBrackets = 'a-zA-Z0-9_\(\)';
 175      var $schema = false;
 176      var $serverInfo = array();
 177      var $autoIncrement = false;
 178      var $dataProvider;
 179      var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changetablesql
 180      var $blobSize = 100;     /// any varchar/char field this size or greater is treated as a blob
 181                              /// in other words, we use a text area for editting.
 182      
 183  	function GetCommentSQL($table,$col)
 184      {
 185          return false;
 186      }
 187      
 188  	function SetCommentSQL($table,$col,$cmt)
 189      {
 190          return false;
 191      }
 192      
 193  	function MetaTables()
 194      {
 195          if (!$this->connection->IsConnected()) return array();
 196          return $this->connection->MetaTables();
 197      }
 198      
 199  	function MetaColumns($tab, $upper=true, $schema=false)
 200      {
 201          if (!$this->connection->IsConnected()) return array();
 202          return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema);
 203      }
 204      
 205  	function MetaPrimaryKeys($tab,$owner=false,$intkey=false)
 206      {
 207          if (!$this->connection->IsConnected()) return array();
 208          return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
 209      }
 210      
 211  	function MetaIndexes($table, $primary = false, $owner = false)
 212      {
 213          if (!$this->connection->IsConnected()) return array();
 214          return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner);
 215      }
 216      
 217  	function MetaType($t,$len=-1,$fieldobj=false)
 218      {
 219          return ADORecordSet::MetaType($t,$len,$fieldobj);
 220      }
 221      
 222  	function NameQuote($name = NULL,$allowBrackets=false)
 223      {
 224          if (!is_string($name)) {
 225              return FALSE;
 226          }
 227          
 228          $name = trim($name);
 229          
 230          if ( !is_object($this->connection) ) {
 231              return $name;
 232          }
 233          
 234          $quote = $this->connection->nameQuote;
 235          
 236          // if name is of the form `name`, quote it
 237          if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
 238              return $quote . $matches[1] . $quote;
 239          }
 240          
 241          // if name contains special characters, quote it
 242          $regex = ($allowBrackets) ? $this->nameRegexBrackets : $this->nameRegex;
 243          
 244          if ( !preg_match('/^[' . $regex . ']+$/', $name) ) {
 245              return $quote . $name . $quote;
 246          }
 247          
 248          return $name;
 249      }
 250      
 251  	function TableName($name)
 252      {
 253          if ( $this->schema ) {
 254              return $this->NameQuote($this->schema) .'.'. $this->NameQuote($name);
 255          }
 256          return $this->NameQuote($name);
 257      }
 258      
 259      // Executes the sql array returned by GetTableSQL and GetIndexSQL
 260  	function ExecuteSQLArray($sql, $continueOnError = true)
 261      {
 262          $rez = 2;
 263          $conn = &$this->connection;
 264          $saved = $conn->debug;
 265          foreach($sql as $line) {
 266              
 267              if ($this->debug) $conn->debug = true;
 268              $ok = $conn->Execute($line);
 269              $conn->debug = $saved;
 270              if (!$ok) {
 271                  if ($this->debug) ADOConnection::outp($conn->ErrorMsg());
 272                  if (!$continueOnError) return 0;
 273                  $rez = 1;
 274              }
 275          }
 276          return $rez;
 277      }
 278      
 279      /*
 280           Returns the actual type given a character code.
 281          
 282          C:  varchar
 283          X:  CLOB (character large object) or largest varchar size if CLOB is not supported
 284          C2: Multibyte varchar
 285          X2: Multibyte CLOB
 286          
 287          B:  BLOB (binary large object)
 288          
 289          D:  Date
 290          T:  Date-time 
 291          L:  Integer field suitable for storing booleans (0 or 1)
 292          I:  Integer
 293          F:  Floating point number
 294          N:  Numeric or decimal number
 295      */
 296      
 297  	function ActualType($meta)
 298      {
 299          return $meta;
 300      }
 301      
 302  	function CreateDatabase($dbname,$options=false)
 303      {
 304          $options = $this->_Options($options);
 305          $sql = array();
 306          
 307          $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
 308          if (isset($options[$this->upperName]))
 309              $s .= ' '.$options[$this->upperName];
 310          
 311          $sql[] = $s;
 312          return $sql;
 313      }
 314      
 315      /*
 316       Generates the SQL to create index. Returns an array of sql strings.
 317      */
 318  	function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
 319      {
 320          if (!is_array($flds)) {
 321              $flds = explode(',',$flds);
 322          }
 323          
 324          foreach($flds as $key => $fld) {
 325              # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
 326              $flds[$key] = $this->NameQuote($fld,$allowBrackets=true);
 327          }
 328          
 329          return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
 330      }
 331      
 332  	function DropIndexSQL ($idxname, $tabname = NULL)
 333      {
 334          return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname)));
 335      }
 336      
 337  	function SetSchema($schema)
 338      {
 339          $this->schema = $schema;
 340      }
 341      
 342  	function AddColumnSQL($tabname, $flds)
 343      {
 344          $tabname = $this->TableName ($tabname);
 345          $sql = array();
 346          list($lines,$pkey) = $this->_GenFields($flds);
 347          $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
 348          foreach($lines as $v) {
 349              $sql[] = $alter . $v;
 350          }
 351          return $sql;
 352      }
 353      
 354      /**
 355       * Change the definition of one column
 356       *
 357       * As some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
 358       * to allow, recreating the table and copying the content over to the new table
 359       * @param string $tabname table-name
 360       * @param string $flds column-name and type for the changed column
 361       * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
 362       * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
 363       * @return array with SQL strings
 364       */
 365  	function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
 366      {
 367          $tabname = $this->TableName ($tabname);
 368          $sql = array();
 369          list($lines,$pkey) = $this->_GenFields($flds);
 370          $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
 371          foreach($lines as $v) {
 372              $sql[] = $alter . $v;
 373          }
 374          return $sql;
 375      }
 376      
 377      /**
 378       * Rename one column
 379       *
 380       * Some DBM's can only do this together with changeing the type of the column (even if that stays the same, eg. mysql)
 381       * @param string $tabname table-name
 382       * @param string $oldcolumn column-name to be renamed
 383       * @param string $newcolumn new column-name
 384       * @param string $flds='' complete column-defintion-string like for AddColumnSQL, only used by mysql atm., default=''
 385       * @return array with SQL strings
 386       */
 387  	function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
 388      {
 389          $tabname = $this->TableName ($tabname);
 390          if ($flds) {
 391              list($lines,$pkey) = $this->_GenFields($flds);
 392              list(,$first) = each($lines);
 393              list(,$column_def) = split("[\t ]+",$first,2);
 394          }
 395          return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
 396      }
 397          
 398      /**
 399       * Drop one column
 400       *
 401       * Some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
 402       * to allow, recreating the table and copying the content over to the new table
 403       * @param string $tabname table-name
 404       * @param string $flds column-name and type for the changed column
 405       * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
 406       * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
 407       * @return array with SQL strings
 408       */
 409  	function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
 410      {
 411          $tabname = $this->TableName ($tabname);
 412          if (!is_array($flds)) $flds = explode(',',$flds);
 413          $sql = array();
 414          $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
 415          foreach($flds as $v) {
 416              $sql[] = $alter . $this->NameQuote($v);
 417          }
 418          return $sql;
 419      }
 420      
 421  	function DropTableSQL($tabname)
 422      {
 423          return array (sprintf($this->dropTable, $this->TableName($tabname)));
 424      }
 425      
 426  	function RenameTableSQL($tabname,$newname)
 427      {
 428          return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
 429      }    
 430      
 431      /*
 432       Generate the SQL to create table. Returns an array of sql strings.
 433      */
 434  	function CreateTableSQL($tabname, $flds, $tableoptions=false)
 435      {
 436          if (!$tableoptions) $tableoptions = array();
 437          
 438          list($lines,$pkey) = $this->_GenFields($flds, true);
 439          
 440          $taboptions = $this->_Options($tableoptions);
 441          $tabname = $this->TableName ($tabname);
 442          $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
 443          
 444          $tsql = $this->_Triggers($tabname,$taboptions);
 445          foreach($tsql as $s) $sql[] = $s;
 446          
 447          return $sql;
 448      }
 449      
 450  	function _GenFields($flds,$widespacing=false)
 451      {
 452          if (is_string($flds)) {
 453              $padding = '     ';
 454              $txt = $flds.$padding;
 455              $flds = array();
 456              $flds0 = Lens_ParseArgs($txt,',');
 457              $hasparam = false;
 458              foreach($flds0 as $f0) {
 459                  $f1 = array();
 460                  foreach($f0 as $token) {
 461                      switch (strtoupper($token)) {
 462                      case 'CONSTRAINT':
 463                      case 'DEFAULT': 
 464                          $hasparam = $token;
 465                          break;
 466                      default:
 467                          if ($hasparam) $f1[$hasparam] = $token;
 468                          else $f1[] = $token;
 469                          $hasparam = false;
 470                          break;
 471                      }
 472                  }
 473                  $flds[] = $f1;
 474                  
 475              }
 476          }
 477          $this->autoIncrement = false;
 478          $lines = array();
 479          $pkey = array();
 480          foreach($flds as $fld) {
 481              $fld = _array_change_key_case($fld);
 482          
 483              $fname = false;
 484              $fdefault = false;
 485              $fautoinc = false;
 486              $ftype = false;
 487              $fsize = false;
 488              $fprec = false;
 489              $fprimary = false;
 490              $fnoquote = false;
 491              $fdefts = false;
 492              $fdefdate = false;
 493              $fconstraint = false;
 494              $fnotnull = false;
 495              $funsigned = false;
 496              
 497              //-----------------
 498              // Parse attributes
 499              foreach($fld as $attr => $v) {
 500                  if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
 501                  else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
 502                  
 503                  switch($attr) {
 504                  case '0':
 505                  case 'NAME':     $fname = $v; break;
 506                  case '1':
 507                  case 'TYPE':     $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
 508                  
 509                  case 'SIZE':     
 510                                  $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
 511                                  if ($dotat === false) $fsize = $v;
 512                                  else {
 513                                      $fsize = substr($v,0,$dotat);
 514                                      $fprec = substr($v,$dotat+1);
 515                                  }
 516                                  break;
 517                  case 'UNSIGNED': $funsigned = true; break;
 518                  case 'AUTOINCREMENT':
 519                  case 'AUTO':    $fautoinc = true; $fnotnull = true; break;
 520                  case 'KEY':
 521                  case 'PRIMARY':    $fprimary = $v; $fnotnull = true; break;
 522                  case 'DEF':
 523                  case 'DEFAULT': $fdefault = $v; break;
 524                  case 'NOTNULL': $fnotnull = $v; break;
 525                  case 'NOQUOTE': $fnoquote = $v; break;
 526                  case 'DEFDATE': $fdefdate = $v; break;
 527                  case 'DEFTIMESTAMP': $fdefts = $v; break;
 528                  case 'CONSTRAINT': $fconstraint = $v; break;
 529                  } //switch
 530              } // foreach $fld
 531              
 532              //--------------------
 533              // VALIDATE FIELD INFO
 534              if (!strlen($fname)) {
 535                  if ($this->debug) ADOConnection::outp("Undefined NAME");
 536                  return false;
 537              }
 538              
 539              $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
 540              $fname = $this->NameQuote($fname);
 541              
 542              if (!strlen($ftype)) {
 543                  if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'");
 544                  return false;
 545              } else {
 546                  $ftype = strtoupper($ftype);
 547              }
 548              
 549              $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
 550              
 551              if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
 552              
 553              if ($fprimary) $pkey[] = $fname;
 554              
 555              // some databases do not allow blobs to have defaults
 556              if ($ty == 'X') $fdefault = false;
 557              
 558              //--------------------
 559              // CONSTRUCT FIELD SQL
 560              if ($fdefts) {
 561                  if (substr($this->connection->databaseType,0,5) == 'mysql') {
 562                      $ftype = 'TIMESTAMP';
 563                  } else {
 564                      $fdefault = $this->connection->sysTimeStamp;
 565                  }
 566              } else if ($fdefdate) {
 567                  if (substr($this->connection->databaseType,0,5) == 'mysql') {
 568                      $ftype = 'TIMESTAMP';
 569                  } else {
 570                      $fdefault = $this->connection->sysDate;
 571                  }
 572              } else if ($fdefault !== false && !$fnoquote)
 573                  if ($ty == 'C' or $ty == 'X' or 
 574                      ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault)))
 575                      if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ') 
 576                          $fdefault = trim($fdefault);
 577                      else if (strtolower($fdefault) != 'null')
 578                          $fdefault = $this->connection->qstr($fdefault);
 579              $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
 580              
 581              if ($widespacing) $fname = str_pad($fname,24);
 582              $lines[$fid] = $fname.' '.$ftype.$suffix;
 583              
 584              if ($fautoinc) $this->autoIncrement = true;
 585          } // foreach $flds
 586          
 587          return array($lines,$pkey);
 588      }
 589      /*
 590           GENERATE THE SIZE PART OF THE DATATYPE
 591              $ftype is the actual type
 592              $ty is the type defined originally in the DDL
 593      */
 594  	function _GetSize($ftype, $ty, $fsize, $fprec)
 595      {
 596          if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
 597              $ftype .= "(".$fsize;
 598              if (strlen($fprec)) $ftype .= ",".$fprec;
 599              $ftype .= ')';
 600          }
 601          return $ftype;
 602      }
 603      
 604      
 605      // return string must begin with space
 606  	function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
 607      {    
 608          $suffix = '';
 609          if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
 610          if ($fnotnull) $suffix .= ' NOT NULL';
 611          if ($fconstraint) $suffix .= ' '.$fconstraint;
 612          return $suffix;
 613      }
 614      
 615  	function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
 616      {
 617          $sql = array();
 618          
 619          if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
 620              $sql[] = sprintf ($this->dropIndex, $idxname);
 621              if ( isset($idxoptions['DROP']) )
 622                  return $sql;
 623          }
 624          
 625          if ( empty ($flds) ) {
 626              return $sql;
 627          }
 628          
 629          $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
 630      
 631          $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
 632          
 633          if ( isset($idxoptions[$this->upperName]) )
 634              $s .= $idxoptions[$this->upperName];
 635          
 636          if ( is_array($flds) )
 637              $flds = implode(', ',$flds);
 638          $s .= '(' . $flds . ')';
 639          $sql[] = $s;
 640          
 641          return $sql;
 642      }
 643      
 644  	function _DropAutoIncrement($tabname)
 645      {
 646          return false;
 647      }
 648      
 649  	function _TableSQL($tabname,$lines,$pkey,$tableoptions)
 650      {
 651          $sql = array();
 652          
 653          if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
 654              $sql[] = sprintf($this->dropTable,$tabname);
 655              if ($this->autoIncrement) {
 656                  $sInc = $this->_DropAutoIncrement($tabname);
 657                  if ($sInc) $sql[] = $sInc;
 658              }
 659              if ( isset ($tableoptions['DROP']) ) {
 660                  return $sql;
 661              }
 662          }
 663          $s = "CREATE TABLE $tabname (\n";
 664          $s .= implode(",\n", $lines);
 665          if (sizeof($pkey)>0) {
 666              $s .= ",\n                 PRIMARY KEY (";
 667              $s .= implode(", ",$pkey).")";
 668          }
 669          if (isset($tableoptions['CONSTRAINTS'])) 
 670              $s .= "\n".$tableoptions['CONSTRAINTS'];
 671          
 672          if (isset($tableoptions[$this->upperName.'_CONSTRAINTS'])) 
 673              $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
 674          
 675          $s .= "\n)";
 676          if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
 677          $sql[] = $s;
 678          
 679          return $sql;
 680      }
 681      
 682      /*
 683          GENERATE TRIGGERS IF NEEDED
 684          used when table has auto-incrementing field that is emulated using triggers
 685      */
 686  	function _Triggers($tabname,$taboptions)
 687      {
 688          return array();
 689      }
 690      
 691      /*
 692          Sanitize options, so that array elements with no keys are promoted to keys
 693      */
 694  	function _Options($opts)
 695      {
 696          if (!is_array($opts)) return array();
 697          $newopts = array();
 698          foreach($opts as $k => $v) {
 699              if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
 700              else $newopts[strtoupper($k)] = $v;
 701          }
 702          return $newopts;
 703      }
 704      
 705      /*
 706      "Florian Buzin [ easywe ]" <florian.buzin#easywe.de>
 707      
 708      This function changes/adds new fields to your table. You don't
 709      have to know if the col is new or not. It will check on its own.
 710      */
 711  	function ChangeTableSQL($tablename, $flds, $tableoptions = false)
 712      {
 713      global $ADODB_FETCH_MODE;
 714      
 715          $save = $ADODB_FETCH_MODE;
 716          $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
 717          if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false);
 718          
 719          // check table exists
 720          $save_handler = $this->connection->raiseErrorFn;
 721          $this->connection->raiseErrorFn = '';
 722          $cols = $this->MetaColumns($tablename);
 723          $this->connection->raiseErrorFn = $save_handler;
 724          
 725          if (isset($savem)) $this->connection->SetFetchMode($savem);
 726          $ADODB_FETCH_MODE = $save;
 727          
 728          if ( empty($cols)) { 
 729              return $this->CreateTableSQL($tablename, $flds, $tableoptions);
 730          }
 731          
 732          if (is_array($flds)) {
 733          // Cycle through the update fields, comparing
 734          // existing fields to fields to update.
 735          // if the Metatype and size is exactly the
 736          // same, ignore - by Mark Newham
 737              $holdflds = array();
 738              foreach($flds as $k=>$v) {
 739                  if ( isset($cols[$k]) && is_object($cols[$k]) ) {
 740                      $c = $cols[$k];
 741                      $ml = $c->max_length;
 742                      $mt = &$this->MetaType($c->type,$ml);
 743                      if ($ml == -1) $ml = '';
 744                      if ($mt == 'X') $ml = $v['SIZE'];
 745                      if (($mt != $v['TYPE']) ||  $ml != $v['SIZE']) {
 746                          $holdflds[$k] = $v;
 747                      }
 748                  } else {
 749                      $holdflds[$k] = $v;
 750                  }        
 751              }
 752              $flds = $holdflds;
 753          }
 754      
 755  
 756          // already exists, alter table instead
 757          list($lines,$pkey) = $this->_GenFields($flds);
 758          $alter = 'ALTER TABLE ' . $this->TableName($tablename);
 759          $sql = array();
 760  
 761          foreach ( $lines as $id => $v ) {
 762              if ( isset($cols[$id]) && is_object($cols[$id]) ) {
 763              
 764                  $flds = Lens_ParseArgs($v,',');
 765                  
 766                  //  We are trying to change the size of the field, if not allowed, simply ignore the request.
 767                  if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) continue;     
 768               
 769                  $sql[] = $alter . $this->alterCol . ' ' . $v;
 770              } else {
 771                  $sql[] = $alter . $this->addCol . ' ' . $v;
 772              }
 773          }
 774          
 775          return $sql;
 776      }
 777  } // class
 778  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7