root/vtigercrm/branches/4.2.4.1/adodb/adodb-datadict.inc.php

Revision 5749, 22.1 kB (checked in by jeffk, 4 years ago)

refs #757. revert adodb-4.81 to adodb-4.72 to fix installation-time breakage.

  • Property svn:mime-type set to text/plain
Line 
1 <?php
2 /**
3   V4.72 21 Feb 2006  (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved.
4   Released under both BSD license and Lesser GPL library license.
5   Whenever there is any discrepancy between the two licenses,
6   the BSD license will take precedence.
7        
8   Set tabs to 4 for best viewing.
9  
10         DOCUMENTATION:
11        
12                 See adodb/tests/test-datadict.php for docs and examples.
13 */
14
15 /*
16         Test script for parser
17 */
18
19 // security - hide paths
20 if (!defined('ADODB_DIR')) die();
21
22 function Lens_ParseTest()
23 {
24 $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";
25 print "<p>$str</p>";
26 $a= Lens_ParseArgs($str);
27 print "<pre>";
28 print_r($a);
29 print "</pre>";
30 }
31
32
33 if (!function_exists('ctype_alnum')) {
34         function ctype_alnum($text) {
35                 return preg_match('/^[a-z0-9]*$/i', $text);
36         }
37 }
38
39 //Lens_ParseTest();
40
41 /**
42         Parse arguments, treat "text" (text) and 'text' as quotation marks.
43         To escape, use "" or '' or ))
44        
45         Will read in "abc def" sans quotes, as: abc def
46         Same with 'abc def'.
47         However if `abc def`, then will read in as `abc def`
48        
49         @param endstmtchar    Character that indicates end of statement
50         @param tokenchars     Include the following characters in tokens apart from A-Z and 0-9
51         @returns 2 dimensional array containing parsed tokens.
52 */
53 function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
54 {
55         $pos = 0;
56         $intoken = false;
57         $stmtno = 0;
58         $endquote = false;
59         $tokens = array();
60         $tokens[$stmtno] = array();
61         $max = strlen($args);
62         $quoted = false;
63         $tokarr = array();
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         // temporary for debuging vtiger - GS
260
261         function println($msg)
262         {
263                 require_once('include/logging.php');
264                 $log1 =& LoggerManager::getLogger('VT');
265                 if(is_array($msg))
266                 {
267                         $log1->fatal("Install ->".print_r($msg,true));
268                 }
269                 else
270                 {
271                         $log1->fatal("Install ->".$msg);
272                 }
273                 return $msg;
274         }
275
276
277 //----------------
278
279        
280         // Executes the sql array returned by GetTableSQL and GetIndexSQL
281         function ExecuteSQLArray($sql, $continueOnError = true)
282         {
283                 $rez = 2;
284                 $conn = &$this->connection;
285                 $saved = $conn->debug;
286                 foreach($sql as $line) {
287                        
288                         if ($this->debug) $conn->debug = true;
289                         $this->println($line);
290                         $ok = $conn->Execute($line);
291                         $conn->debug = $saved;
292                         if (!$ok) {
293                                 $this->println("Table Creation Error: Query Failed");
294                                 $this->println(" ");
295                                 if ($this->debug)
296                                 {
297
298                                         $this->println("InstallError: ".$conn->ErrorMsg());
299                                         ADOConnection::outp($conn->ErrorMsg());
300                                 }
301                                 if (!$continueOnError) return 0;
302                                 $rez = 1;
303                         }
304                 }
305                 return $rez;
306         }
307        
308         /*
309                 Returns the actual type given a character code.
310                
311                 C:  varchar
312                 X:  CLOB (character large object) or largest varchar size if CLOB is not supported
313                 C2: Multibyte varchar
314                 X2: Multibyte CLOB
315                
316                 B:  BLOB (binary large object)
317                
318                 D:  Date
319                 T:  Date-time
320                 L:  Integer field suitable for storing booleans (0 or 1)
321                 I:  Integer
322                 F:  Floating point number
323                 N:  Numeric or decimal number
324         */
325        
326         function ActualType($meta)
327         {
328                 return $meta;
329         }
330        
331         function CreateDatabase($dbname,$options=false)
332         {
333                 $options = $this->_Options($options);
334                 $sql = array();
335                
336                 $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
337                 if (isset($options[$this->upperName]))
338                         $s .= ' '.$options[$this->upperName];
339                
340                 $sql[] = $s;
341                 return $sql;
342         }
343        
344         /*
345          Generates the SQL to create index. Returns an array of sql strings.
346         */
347         function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
348         {
349                 if (!is_array($flds)) {
350                         $flds = explode(',',$flds);
351                 }
352                
353                 foreach($flds as $key => $fld) {
354                         # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
355                         $flds[$key] = $this->NameQuote($fld,$allowBrackets=true);
356                 }
357                
358                 return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
359         }
360        
361         function DropIndexSQL ($idxname, $tabname = NULL)
362         {
363                 return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname)));
364         }
365        
366         function SetSchema($schema)
367         {
368                 $this->schema = $schema;
369         }
370        
371         function AddColumnSQL($tabname, $flds)
372         {
373                 $tabname = $this->TableName ($tabname);
374                 $sql = array();
375                 list($lines,$pkey) = $this->_GenFields($flds);
376                 $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
377                 foreach($lines as $v) {
378                         $sql[] = $alter . $v;
379                 }
380                 return $sql;
381         }
382        
383         /**
384          * Change the definition of one column
385          *
386          * As some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
387          * to allow, recreating the table and copying the content over to the new table
388          * @param string $tabname table-name
389          * @param string $flds column-name and type for the changed column
390          * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
391          * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
392          * @return array with SQL strings
393          */
394         function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
395         {
396                 $tabname = $this->TableName ($tabname);
397                 $sql = array();
398                 list($lines,$pkey) = $this->_GenFields($flds);
399                 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
400                 foreach($lines as $v) {
401                         $sql[] = $alter . $v;
402                 }
403                 return $sql;
404         }
405        
406         /**
407          * Rename one column
408          *
409          * Some DBM's can only do this together with changeing the type of the column (even if that stays the same, eg. mysql)
410          * @param string $tabname table-name
411          * @param string $oldcolumn column-name to be renamed
412          * @param string $newcolumn new column-name
413          * @param string $flds='' complete column-defintion-string like for AddColumnSQL, only used by mysql atm., default=''
414          * @return array with SQL strings
415          */
416         function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
417         {
418                 $tabname = $this->TableName ($tabname);
419                 if ($flds) {
420                         list($lines,$pkey) = $this->_GenFields($flds);
421                         list(,$first) = each($lines);
422                         list(,$column_def) = split("[\t ]+",$first,2);
423                 }
424                 return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
425         }
426                
427         /**
428          * Drop one column
429          *
430          * Some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
431          * to allow, recreating the table and copying the content over to the new table
432          * @param string $tabname table-name
433          * @param string $flds column-name and type for the changed column
434          * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
435          * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
436          * @return array with SQL strings
437          */
438         function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
439         {
440                 $tabname = $this->TableName ($tabname);
441                 if (!is_array($flds)) $flds = explode(',',$flds);
442                 $sql = array();
443                 $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
444                 foreach($flds as $v) {
445                         $sql[] = $alter . $this->NameQuote($v);
446                 }
447                 return $sql;
448         }
449        
450         function DropTableSQL($tabname)
451         {
452                 return array (sprintf($this->dropTable, $this->TableName($tabname)));
453         }
454        
455         function RenameTableSQL($tabname,$newname)
456         {
457                 return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
458         }       
459        
460         /*
461          Generate the SQL to create table. Returns an array of sql strings.
462         */
463         function CreateTableSQL($tabname, $flds, $tableoptions=false)
464         {
465                 if (!$tableoptions) $tableoptions = array();
466                
467                 list($lines,$pkey) = $this->_GenFields($flds, true);
468                
469                 $taboptions = $this->_Options($tableoptions);
470                 $tabname = $this->TableName ($tabname);
471                 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
472                
473                 $tsql = $this->_Triggers($tabname,$taboptions);
474                 foreach($tsql as $s) $sql[] = $s;
475                
476                 return $sql;
477         }
478        
479         function _GenFields($flds,$widespacing=false)
480         {
481                 if (is_string($flds)) {
482                         $padding = '     ';
483                         $txt = $flds.$padding;
484                         $flds = array();
485                         $flds0 = Lens_ParseArgs($txt,',');
486                         $hasparam = false;
487                         foreach($flds0 as $f0) {
488                                 $f1 = array();
489                                 foreach($f0 as $token) {
490                                         switch (strtoupper($token)) {
491                                         case 'CONSTRAINT':
492                                         case 'DEFAULT':
493                                                 $hasparam = $token;
494                                                 break;
495                                         default:
496                                                 if ($hasparam) $f1[$hasparam] = $token;
497                                                 else $f1[] = $token;
498                                                 $hasparam = false;
499                                                 break;
500                                         }
501                                 }
502                                 $flds[] = $f1;
503                                
504                         }
505                 }
506                 $this->autoIncrement = false;
507                 $lines = array();
508                 $pkey = array();
509                 foreach($flds as $fld) {
510                         $fld = _array_change_key_case($fld);
511                
512                         $fname = false;
513                         $fdefault = false;
514                         $fautoinc = false;
515                         $ftype = false;
516                         $fsize = false;
517                         $fprec = false;
518                         $fprimary = false;
519                         $fnoquote = false;
520                         $fdefts = false;
521                         $fdefdate = false;
522                         $fconstraint = false;
523                         $fnotnull = false;
524                         $funsigned = false;
525                        
526                         //-----------------
527                         // Parse attributes
528                         foreach($fld as $attr => $v) {
529                                 if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
530                                 else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
531                                
532                                 switch($attr) {
533                                 case '0':
534                                 case 'NAME':    $fname = $v; break;
535                                 case '1':
536                                 case 'TYPE':    $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
537                                
538                                 case 'SIZE':   
539                                                                 $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
540                                                                 if ($dotat === false) $fsize = $v;
541                                                                 else {
542                                                                         $fsize = substr($v,0,$dotat);
543                                                                         $fprec = substr($v,$dotat+1);
544                                                                 }
545                                                                 break;
546                                 case 'UNSIGNED': $funsigned = true; break;
547                                 case 'AUTOINCREMENT':
548                                 case 'AUTO':    $fautoinc = true; $fnotnull = true; break;
549                                 case 'KEY':
550                                 case 'PRIMARY': $fprimary = $v; $fnotnull = true; break;
551                                 case 'DEF':
552                                 case 'DEFAULT': $fdefault = $v; break;
553                                 case 'NOTNULL': $fnotnull = $v; break;
554                                 case 'NOQUOTE': $fnoquote = $v; break;
555                                 case 'DEFDATE': $fdefdate = $v; break;
556                                 case 'DEFTIMESTAMP': $fdefts = $v; break;
557                                 case 'CONSTRAINT': $fconstraint = $v; break;
558                                 } //switch
559                         } // foreach $fld
560                        
561                         //--------------------
562                         // VALIDATE FIELD INFO
563                         if (!strlen($fname)) {
564                                 if ($this->debug) ADOConnection::outp("Undefined NAME");
565                                 return false;
566                         }
567                        
568                         $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
569                         $fname = $this->NameQuote($fname);
570                        
571                         if (!strlen($ftype)) {
572                                 if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'");
573                                 return false;
574                         } else {
575                                 $ftype = strtoupper($ftype);
576                         }
577                        
578                         $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
579                        
580                         if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
581                        
582                         if ($fprimary) $pkey[] = $fname;
583                        
584                         // some databases do not allow blobs to have defaults
585                         if ($ty == 'X') $fdefault = false;
586                        
587                         //--------------------
588                         // CONSTRUCT FIELD SQL
589                         if ($fdefts) {
590                                 if (substr($this->connection->databaseType,0,5) == 'mysql') {
591                                         $ftype = 'TIMESTAMP';
592                                 } else {
593                                         $fdefault = $this->connection->sysTimeStamp;
594                                 }
595                         } else if ($fdefdate) {
596                                 if (substr($this->connection->databaseType,0,5) == 'mysql') {
597                                         $ftype = 'TIMESTAMP';
598                                 } else {
599                                         $fdefault = $this->connection->sysDate;
600                                 }
601                         } else if ($fdefault !== false && !$fnoquote)
602                                 if ($ty == 'C' or $ty == 'X' or
603                                         ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault)))
604                                         if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
605                                                 $fdefault = trim($fdefault);
606                                         else if (strtolower($fdefault) != 'null')
607                                                 $fdefault = $this->connection->qstr($fdefault);
608                         $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
609                        
610                         if ($widespacing) $fname = str_pad($fname,24);
611                         $lines[$fid] = $fname.' '.$ftype.$suffix;
612                        
613                         if ($fautoinc) $this->autoIncrement = true;
614                 } // foreach $flds
615                
616                 return array($lines,$pkey);
617         }
618         /*
619                  GENERATE THE SIZE PART OF THE DATATYPE
620                         $ftype is the actual type
621                         $ty is the type defined originally in the DDL
622         */
623         function _GetSize($ftype, $ty, $fsize, $fprec)
624         {
625                 if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
626                         $ftype .= "(".$fsize;
627                         if (strlen($fprec)) $ftype .= ",".$fprec;
628                         $ftype .= ')';
629                 }
630                 return $ftype;
631         }
632        
633        
634         // return string must begin with space
635         function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
636         {       
637                 $suffix = '';
638                 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
639                 if ($fnotnull) $suffix .= ' NOT NULL';
640                 if ($fconstraint) $suffix .= ' '.$fconstraint;
641                 return $suffix;
642         }
643        
644         function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
645         {
646                 $sql = array();
647                
648                 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
649                         $sql[] = sprintf ($this->dropIndex, $idxname);
650                         if ( isset($idxoptions['DROP']) )
651                                 return $sql;
652                 }
653                
654                 if ( empty ($flds) ) {
655                         return $sql;
656                 }
657                
658                 $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
659        
660                 $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
661                
662                 if ( isset($idxoptions[$this->upperName]) )
663                         $s .= $idxoptions[$this->upperName];
664                
665                 if ( is_array($flds) )
666                         $flds = implode(', ',$flds);
667                 $s .= '(' . $flds . ')';
668                 $sql[] = $s;
669                
670                 return $sql;
671         }
672        
673         function _DropAutoIncrement($tabname)
674         {
675                 return false;
676         }
677        
678         function _TableSQL($tabname,$lines,$pkey,$tableoptions)
679         {
680                 $sql = array();
681                
682                 if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
683                         $sql[] = sprintf($this->dropTable,$tabname);
684                         if ($this->autoIncrement) {
685                                 $sInc = $this->_DropAutoIncrement($tabname);
686                                 if ($sInc) $sql[] = $sInc;
687                         }
688                         if ( isset ($tableoptions['DROP']) ) {
689                                 return $sql;
690                         }
691                 }
692                 $s = "CREATE TABLE $tabname (\n";
693                 $s .= implode(",\n", $lines);
694                 if (sizeof($pkey)>0) {
695                         $s .= ",\n                 PRIMARY KEY (";
696                         $s .= implode(", ",$pkey).")";
697                 }
698                 if (isset($tableoptions['CONSTRAINTS']))
699                         $s .= "\n".$tableoptions['CONSTRAINTS'];
700                
701                 if (isset($tableoptions[$this->upperName.'_CONSTRAINTS']))
702                         $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
703                
704                 $s .= "\n)";
705                 if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
706                 $sql[] = $s;
707                
708                 return $sql;
709         }
710        
711         /*
712                 GENERATE TRIGGERS IF NEEDED
713                 used when table has auto-incrementing field that is emulated using triggers
714         */
715         function _Triggers($tabname,$taboptions)
716         {
717                 return array();
718         }
719        
720         /*
721                 Sanitize options, so that array elements with no keys are promoted to keys
722         */
723         function _Options($opts)
724         {
725                 if (!is_array($opts)) return array();
726                 $newopts = array();
727                 foreach($opts as $k => $v) {
728                         if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
729                         else $newopts[strtoupper($k)] = $v;
730                 }
731                 return $newopts;
732         }
733        
734         /*
735         "Florian Buzin [ easywe ]" <florian.buzin#easywe.de>
736        
737         This function changes/adds new fields to your table. You don't
738         have to know if the col is new or not. It will check on its own.
739         */
740         function ChangeTableSQL($tablename, $flds, $tableoptions = false, $forceAlter = false) // GS Fix for constraint impl - forceAlter
741         {
742         global $ADODB_FETCH_MODE;
743        
744                 $save = $ADODB_FETCH_MODE;
745                 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
746                 if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false);
747                
748                 // check table exists
749                 $save_handler = $this->connection->raiseErrorFn;
750                 $this->connection->raiseErrorFn = '';
751                 $cols = $this->MetaColumns($tablename);
752                 $this->connection->raiseErrorFn = $save_handler;
753                
754                 if (isset($savem)) $this->connection->SetFetchMode($savem);
755                 $ADODB_FETCH_MODE = $save;
756                
757                 if ( $forceAlter == false && empty($cols)) { // GS Fix for constraint impl
758                         return $this->CreateTableSQL($tablename, $flds, $tableoptions);
759                 }
760                
761                 if (is_array($flds)) {
762                 // Cycle through the update fields, comparing
763                 // existing fields to fields to update.
764                 // if the Metatype and size is exactly the
765                 // same, ignore - by Mark Newham
766                         $holdflds = array();
767                         foreach($flds as $k=>$v) {
768                                 if ( isset($cols[$k]) && is_object($cols[$k]) ) {
769                                         // If already not allowing nulls, then don't change
770                                         $obj = $cols[$k];
771                                         if (isset($obj->not_null) && $obj->not_null)
772                                                 $v = str_replace('NOT NULL','',$v);
773
774                                         $c = $cols[$k];
775                                         $ml = $c->max_length;
776                                         $mt = $this->MetaType($c->type,$ml);
777                                         if ($ml == -1) $ml = '';
778                                         if ($mt == 'X') $ml = $v['SIZE'];
779                                         if (($mt != $v['TYPE']) ||  $ml != $v['SIZE']) {
780                                                 $holdflds[$k] = $v;
781                                         }
782                                 } else {
783                                         $holdflds[$k] = $v;
784                                 }               
785                         }
786                         $flds = $holdflds;
787                 }
788        
789
790                 // already exists, alter table instead
791                 list($lines,$pkey) = $this->_GenFields($flds);
792                 $alter = 'ALTER TABLE ' . $this->TableName($tablename);
793                 $sql = array();
794
795                 foreach ( $lines as $id => $v ) {
796                         if ( isset($cols[$id]) && is_object($cols[$id]) ) {
797                        
798                                 $flds = Lens_ParseArgs($v,',');
799                                
800                                 //  We are trying to change the size of the field, if not allowed, simply ignore the request.
801                                 if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) continue;         
802                        
803                                 $sql[] = $alter . $this->alterCol . ' ' . $v;
804                         } else {
805                                 $sql[] = $alter . $this->addCol . ' ' . $v;
806                         }
807                 }
808                
809                 // GS Fix for constraint impl -- start
810                 if($forceAlter == false) return $sql;
811                 $sqlarray = array();
812
813                 $alter .= implode(",\n", $sql);
814                 if (sizeof($pkey)>0) {
815                         $alter .= ",\n PRIMARY KEY (";
816                         $alter .= implode(", ",$pkey).")";
817                 }
818                
819                 if (isset($tableoptions['CONSTRAINTS']))
820                         $alter .= "\n".$tableoptions['CONSTRAINTS'];
821
822                 if (isset($tableoptions[$this->upperName.'_CONSTRAINTS']))
823                         $alter .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
824
825                 if (isset($tableoptions[$this->upperName])) $alter .= $tableoptions[$this->upperName];
826                         $sqlarray[] = $alter;
827
828                
829                 $taboptions = $this->_Options($tableoptions);
830                 $tsql = $this->_Triggers($this->TableName($tablename),$taboptions);
831                 foreach($tsql as $s) $sqlarray[] = $s;
832
833                 // GS Fix for constraint impl -- end
834                
835                 return $sqlarray;
836         }
837 } // class
838 ?>
Note: See TracBrowser for help on using the browser.