source: trunk/phpgwapi/inc/adodb/adodb-datadict.inc.php @ 2

Revision 2, 16.1 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3/**
4  V4.51 29 July 2004  (c) 2000-2004 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
21if (!defined('ADODB_DIR')) die();
22
23function 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";
26print "<p>$str</p>";
27$a= Lens_ParseArgs($str);
28print "<pre>";
29print_r($a);
30print "</pre>";
31}
32
33if (!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*/
53function 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       
64        while ($pos < $max) {
65                $ch = substr($args,$pos,1);
66                switch($ch) {
67                case ' ':
68                case "\t":
69                case "\n":
70                case "\r":
71                        if (!$quoted) {
72                                if ($intoken) {
73                                        $intoken = false;
74                                        $tokens[$stmtno][] = implode('',$tokarr);
75                                }
76                                break;
77                        }
78                       
79                        $tokarr[] = $ch;
80                        break;
81               
82                case '`':
83                        if ($intoken) $tokarr[] = $ch;
84                case '(':
85                case ')':       
86                case '"':
87                case "'":
88                       
89                        if ($intoken) {
90                                if (empty($endquote)) {
91                                        $tokens[$stmtno][] = implode('',$tokarr);
92                                        if ($ch == '(') $endquote = ')';
93                                        else $endquote = $ch;
94                                        $quoted = true;
95                                        $intoken = true;
96                                        $tokarr = array();
97                                } else if ($endquote == $ch) {
98                                        $ch2 = substr($args,$pos+1,1);
99                                        if ($ch2 == $endquote) {
100                                                $pos += 1;
101                                                $tokarr[] = $ch2;
102                                        } else {
103                                                $quoted = false;
104                                                $intoken = false;
105                                                $tokens[$stmtno][] = implode('',$tokarr);
106                                                $endquote = '';
107                                        }
108                                } else
109                                        $tokarr[] = $ch;
110                                       
111                        }else {
112                       
113                                if ($ch == '(') $endquote = ')';
114                                else $endquote = $ch;
115                                $quoted = true;
116                                $intoken = true;
117                                $tokarr = array();
118                                if ($ch == '`') $tokarr[] = '`';
119                        }
120                        break;
121                       
122                default:
123                       
124                        if (!$intoken) {
125                                if ($ch == $endstmtchar) {
126                                        $stmtno += 1;
127                                        $tokens[$stmtno] = array();
128                                        break;
129                                }
130                       
131                                $intoken = true;
132                                $quoted = false;
133                                $endquote = false;
134                                $tokarr = array();
135       
136                        }
137                       
138                        if ($quoted) $tokarr[] = $ch;
139                        else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
140                        else {
141                                if ($ch == $endstmtchar) {                     
142                                        $tokens[$stmtno][] = implode('',$tokarr);
143                                        $stmtno += 1;
144                                        $tokens[$stmtno] = array();
145                                        $intoken = false;
146                                        $tokarr = array();
147                                        break;
148                                }
149                                $tokens[$stmtno][] = implode('',$tokarr);
150                                $tokens[$stmtno][] = $ch;
151                                $intoken = false;
152                        }
153                }
154                $pos += 1;
155        }
156       
157        return $tokens;
158}
159
160
161class ADODB_DataDict {
162        var $connection;
163        var $debug = false;
164        var $dropTable = 'DROP TABLE %s';
165        var $dropIndex = 'DROP INDEX %s';
166        var $addCol = ' ADD';
167        var $alterCol = ' ALTER COLUMN';
168        var $dropCol = ' DROP COLUMN';
169        var $nameRegex = '\w';
170        var $schema = false;
171        var $serverInfo = array();
172        var $autoIncrement = false;
173        var $dataProvider;
174        var $blobSize = 100;    /// any varchar/char field this size or greater is treated as a blob
175                                                        /// in other words, we use a text area for editting.
176       
177        function GetCommentSQL($table,$col)
178        {
179                return false;
180        }
181       
182        function SetCommentSQL($table,$col,$cmt)
183        {
184                return false;
185        }
186       
187        function &MetaTables()
188        {
189                return $this->connection->MetaTables();
190        }
191       
192        function &MetaColumns($tab, $upper=true, $schema=false)
193        {
194                return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema);
195        }
196       
197        function &MetaPrimaryKeys($tab,$owner=false,$intkey=false)
198        {
199                return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
200        }
201       
202        function &MetaIndexes($table, $primary = false, $owner = false)
203        {
204                return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner);
205        }
206       
207        function MetaType($t,$len=-1,$fieldobj=false)
208        {
209                return ADORecordSet::MetaType($t,$len,$fieldobj);
210        }
211       
212        function NameQuote($name = NULL)
213        {
214                if (!is_string($name)) {
215                        return FALSE;
216                }
217               
218                $name = trim($name);
219               
220                if ( !is_object($this->connection) ) {
221                        return $name;
222                }
223               
224                $quote = $this->connection->nameQuote;
225               
226                // if name is of the form `name`, quote it
227                if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
228                        return $quote . $matches[1] . $quote;
229                }
230               
231                // if name contains special characters, quote it
232                if ( !preg_match('/^[' . $this->nameRegex . ']+$/', $name) ) {
233                        return $quote . $name . $quote;
234                }
235               
236                return $name;
237        }
238       
239        function TableName($name)
240        {
241                if ( $this->schema ) {
242                        return $this->NameQuote($this->schema) .'.'. $this->NameQuote($name);
243                }
244                return $this->NameQuote($name);
245        }
246       
247        // Executes the sql array returned by GetTableSQL and GetIndexSQL
248        function ExecuteSQLArray($sql, $continueOnError = true)
249        {
250                $rez = 2;
251                $conn = &$this->connection;
252                $saved = $conn->debug;
253                foreach($sql as $line) {
254                       
255                        if ($this->debug) $conn->debug = true;
256                        $ok = $conn->Execute($line);
257                        $conn->debug = $saved;
258                        if (!$ok) {
259                                if ($this->debug) ADOConnection::outp($conn->ErrorMsg());
260                                if (!$continueOnError) return 0;
261                                $rez = 1;
262                        }
263                }
264                return $rez;
265        }
266       
267        /*
268                Returns the actual type given a character code.
269               
270                C:  varchar
271                X:  CLOB (character large object) or largest varchar size if CLOB is not supported
272                C2: Multibyte varchar
273                X2: Multibyte CLOB
274               
275                B:  BLOB (binary large object)
276               
277                D:  Date
278                T:  Date-time
279                L:  Integer field suitable for storing booleans (0 or 1)
280                I:  Integer
281                F:  Floating point number
282                N:  Numeric or decimal number
283        */
284       
285        function ActualType($meta)
286        {
287                return $meta;
288        }
289       
290        function CreateDatabase($dbname,$options=false)
291        {
292                $options = $this->_Options($options);
293                $sql = array();
294               
295                $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
296                if (isset($options[$this->upperName]))
297                        $s .= ' '.$options[$this->upperName];
298               
299                $sql[] = $s;
300                return $sql;
301        }
302       
303        /*
304         Generates the SQL to create index. Returns an array of sql strings.
305        */
306        function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
307        {
308                if (!is_array($flds)) {
309                        $flds = explode(',',$flds);
310                }
311               
312                foreach($flds as $key => $fld) {
313                        $flds[$key] = $this->NameQuote($fld);
314                }
315               
316                return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
317        }
318       
319        function DropIndexSQL ($idxname, $tabname = NULL)
320        {
321                return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname)));
322        }
323       
324        function SetSchema($schema)
325        {
326                $this->schema = $schema;
327        }
328       
329        function AddColumnSQL($tabname, $flds)
330        {
331                $tabname = $this->TableName ($tabname);
332                $sql = array();
333                list($lines,$pkey) = $this->_GenFields($flds);
334                $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
335                foreach($lines as $v) {
336                        $sql[] = $alter . $v;
337                }
338                return $sql;
339        }
340       
341        function AlterColumnSQL($tabname, $flds)
342        {
343                $tabname = $this->TableName ($tabname);
344                $sql = array();
345                list($lines,$pkey) = $this->_GenFields($flds);
346                $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
347                foreach($lines as $v) {
348                        $sql[] = $alter . $v;
349                }
350                return $sql;
351        }
352       
353        function DropColumnSQL($tabname, $flds)
354        {
355                $tabname = $this->TableName ($tabname);
356                if (!is_array($flds)) $flds = explode(',',$flds);
357                $sql = array();
358                $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
359                foreach($flds as $v) {
360                        $sql[] = $alter . $this->NameQuote($v);
361                }
362                return $sql;
363        }
364       
365        function DropTableSQL($tabname)
366        {
367                return array (sprintf($this->dropTable, $this->TableName($tabname)));
368        }
369       
370        /*
371         Generate the SQL to create table. Returns an array of sql strings.
372        */
373        function CreateTableSQL($tabname, $flds, $tableoptions=false)
374        {
375                if (!$tableoptions) $tableoptions = array();
376               
377                list($lines,$pkey) = $this->_GenFields($flds);
378               
379                $taboptions = $this->_Options($tableoptions);
380                $tabname = $this->TableName ($tabname);
381                $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
382               
383                $tsql = $this->_Triggers($tabname,$taboptions);
384                foreach($tsql as $s) $sql[] = $s;
385               
386                return $sql;
387        }
388       
389        function _GenFields($flds)
390        {
391                if (is_string($flds)) {
392                        $padding = '     ';
393                        $txt = $flds.$padding;
394                        $flds = array();
395                        $flds0 = Lens_ParseArgs($txt,',');
396                        $hasparam = false;
397                        foreach($flds0 as $f0) {
398                                $f1 = array();
399                                foreach($f0 as $token) {
400                                        switch (strtoupper($token)) {
401                                        case 'CONSTRAINT':
402                                        case 'DEFAULT':
403                                                $hasparam = $token;
404                                                break;
405                                        default:
406                                                if ($hasparam) $f1[$hasparam] = $token;
407                                                else $f1[] = $token;
408                                                $hasparam = false;
409                                                break;
410                                        }
411                                }
412                                $flds[] = $f1;
413                               
414                        }
415                }
416                $this->autoIncrement = false;
417                $lines = array();
418                $pkey = array();
419                foreach($flds as $fld) {
420                        $fld = _array_change_key_case($fld);
421               
422                        $fname = false;
423                        $fdefault = false;
424                        $fautoinc = false;
425                        $ftype = false;
426                        $fsize = false;
427                        $fprec = false;
428                        $fprimary = false;
429                        $fnoquote = false;
430                        $fdefts = false;
431                        $fdefdate = false;
432                        $fconstraint = false;
433                        $fnotnull = false;
434                        $funsigned = false;
435                       
436                        //-----------------
437                        // Parse attributes
438                        foreach($fld as $attr => $v) {
439                                if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
440                                else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
441                               
442                                switch($attr) {
443                                case '0':
444                                case 'NAME':    $fname = $v; break;
445                                case '1':
446                                case 'TYPE':    $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
447                               
448                                case 'SIZE':   
449                                                                $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
450                                                                if ($dotat === false) $fsize = $v;
451                                                                else {
452                                                                        $fsize = substr($v,0,$dotat);
453                                                                        $fprec = substr($v,$dotat+1);
454                                                                }
455                                                                break;
456                                case 'UNSIGNED': $funsigned = true; break;
457                                case 'AUTOINCREMENT':
458                                case 'AUTO':    $fautoinc = true; $fnotnull = true; break;
459                                case 'KEY':
460                                case 'PRIMARY': $fprimary = $v; $fnotnull = true; break;
461                                case 'DEF':
462                                case 'DEFAULT': $fdefault = $v; break;
463                                case 'NOTNULL': $fnotnull = $v; break;
464                                case 'NOQUOTE': $fnoquote = $v; break;
465                                case 'DEFDATE': $fdefdate = $v; break;
466                                case 'DEFTIMESTAMP': $fdefts = $v; break;
467                                case 'CONSTRAINT': $fconstraint = $v; break;
468                                } //switch
469                        } // foreach $fld
470                       
471                        //--------------------
472                        // VALIDATE FIELD INFO
473                        if (!strlen($fname)) {
474                                if ($this->debug) ADOConnection::outp("Undefined NAME");
475                                return false;
476                        }
477                       
478                        $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
479                        $fname = $this->NameQuote($fname);
480                       
481                        if (!strlen($ftype)) {
482                                if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'");
483                                return false;
484                        } else {
485                                $ftype = strtoupper($ftype);
486                        }
487                       
488                        $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
489                       
490                        if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
491                       
492                        if ($fprimary) $pkey[] = $fname;
493                       
494                        // some databases do not allow blobs to have defaults
495                        if ($ty == 'X') $fdefault = false;
496                       
497                        //--------------------
498                        // CONSTRUCT FIELD SQL
499                        if ($fdefts) {
500                                if (substr($this->connection->databaseType,0,5) == 'mysql') {
501                                        $ftype = 'TIMESTAMP';
502                                } else {
503                                        $fdefault = $this->connection->sysTimeStamp;
504                                }
505                        } else if ($fdefdate) {
506                                if (substr($this->connection->databaseType,0,5) == 'mysql') {
507                                        $ftype = 'TIMESTAMP';
508                                } else {
509                                        $fdefault = $this->connection->sysDate;
510                                }
511                        } else if (strlen($fdefault) && !$fnoquote)
512                                if ($ty == 'C' or $ty == 'X' or
513                                        ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault)))
514                                        if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
515                                                $fdefault = trim($fdefault);
516                                        else if (strtolower($fdefault) != 'null')
517                                                $fdefault = $this->connection->qstr($fdefault);
518                        $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
519                       
520                        $fname = str_pad($fname,16);
521                        $lines[$fid] = $fname.' '.$ftype.$suffix;
522                       
523                        if ($fautoinc) $this->autoIncrement = true;
524                } // foreach $flds
525               
526                return array($lines,$pkey);
527        }
528        /*
529                 GENERATE THE SIZE PART OF THE DATATYPE
530                        $ftype is the actual type
531                        $ty is the type defined originally in the DDL
532        */
533        function _GetSize($ftype, $ty, $fsize, $fprec)
534        {
535                if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
536                        $ftype .= "(".$fsize;
537                        if (strlen($fprec)) $ftype .= ",".$fprec;
538                        $ftype .= ')';
539                }
540                return $ftype;
541        }
542       
543       
544        // return string must begin with space
545        function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
546        {       
547                $suffix = '';
548                if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
549                if ($fnotnull) $suffix .= ' NOT NULL';
550                if ($fconstraint) $suffix .= ' '.$fconstraint;
551                return $suffix;
552        }
553       
554        function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
555        {
556                $sql = array();
557               
558                if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
559                        $sql[] = sprintf ($this->dropIndex, $idxname);
560                        if ( isset($idxoptions['DROP']) )
561                                return $sql;
562                }
563               
564                if ( empty ($flds) ) {
565                        return $sql;
566                }
567               
568                $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
569       
570                $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
571               
572                if ( isset($idxoptions[$this->upperName]) )
573                        $s .= $idxoptions[$this->upperName];
574               
575                if ( is_array($flds) )
576                        $flds = implode(', ',$flds);
577                $s .= '(' . $flds . ')';
578                $sql[] = $s;
579               
580                return $sql;
581        }
582       
583        function _DropAutoIncrement($tabname)
584        {
585                return false;
586        }
587       
588        function _TableSQL($tabname,$lines,$pkey,$tableoptions)
589        {
590                $sql = array();
591               
592                if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
593                        $sql[] = sprintf($this->dropTable,$tabname);
594                        if ($this->autoIncrement) {
595                                $sInc = $this->_DropAutoIncrement($tabname);
596                                if ($sInc) $sql[] = $sInc;
597                        }
598                        if ( isset ($tableoptions['DROP']) ) {
599                                return $sql;
600                        }
601                }
602                $s = "CREATE TABLE $tabname (\n";
603                $s .= implode(",\n", $lines);
604                if (sizeof($pkey)>0) {
605                        $s .= ",\n                 PRIMARY KEY (";
606                        $s .= implode(", ",$pkey).")";
607                }
608                if (isset($tableoptions['CONSTRAINTS']))
609                        $s .= "\n".$tableoptions['CONSTRAINTS'];
610               
611                if (isset($tableoptions[$this->upperName.'_CONSTRAINTS']))
612                        $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
613               
614                $s .= "\n)";
615                if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
616                $sql[] = $s;
617               
618                return $sql;
619        }
620       
621        /*
622                GENERATE TRIGGERS IF NEEDED
623                used when table has auto-incrementing field that is emulated using triggers
624        */
625        function _Triggers($tabname,$taboptions)
626        {
627                return array();
628        }
629       
630        /*
631                Sanitize options, so that array elements with no keys are promoted to keys
632        */
633        function _Options($opts)
634        {
635                if (!is_array($opts)) return array();
636                $newopts = array();
637                foreach($opts as $k => $v) {
638                        if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
639                        else $newopts[strtoupper($k)] = $v;
640                }
641                return $newopts;
642        }
643       
644        /*
645        "Florian Buzin [ easywe ]" <florian.buzin@easywe.de>
646       
647        This function changes/adds new fields to your table. You don't
648        have to know if the col is new or not. It will check on its own.
649        */
650        function ChangeTableSQL($tablename, $flds, $tableoptions = false)
651        {
652                // check table exists
653                $cols = &$this->MetaColumns($tablename);
654                if ( empty($cols)) {
655                        return $this->CreateTableSQL($tablename, $flds, $tableoptions);
656                }
657               
658                // already exists, alter table instead
659                list($lines,$pkey) = $this->_GenFields($flds);
660                $alter = 'ALTER TABLE ' . $this->TableName($tablename);
661                $sql = array();
662               
663                foreach ( $lines as $id => $v ) {
664                        if ( isset($cols[$id]) && is_object($cols[$id]) ) {
665                                $sql[] = $alter . $this->alterCol . ' ' . $v;
666                        } else {
667                                $sql[] = $alter . $this->addCol . ' ' . $v;
668                        }
669                }
670               
671                return $sql;
672        }
673} // class
674?>
Note: See TracBrowser for help on using the repository browser.