home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / adodb-datadict.inc.php < prev    next >
Encoding:
PHP Script  |  2005-01-24  |  20.7 KB  |  771 lines

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