]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/datadict/datadict-firebird.inc.php
Upgrade adodb
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / datadict / datadict-firebird.inc.php
1 <?php
2
3 /**
4   V5.18 3 Sep 2012  (c) 2000-2012 John Lim (jlim#natsoft.com). 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 */
12
13 class ADODB2_firebird extends ADODB_DataDict {
14         
15         var $databaseType = 'firebird';
16         var $seqField = false;
17         var $seqPrefix = 'gen_';
18         var $blobSize = 40000;  
19         
20         function ActualType($meta)
21         {
22                 switch($meta) {
23                 case 'C': return 'VARCHAR';
24                 case 'XL': return 'VARCHAR(32000)'; 
25                 case 'X': return 'VARCHAR(4000)'; 
26                 
27                 case 'C2': return 'VARCHAR'; // up to 32K
28                 case 'X2': return 'VARCHAR(4000)';
29                 
30                 case 'B': return 'BLOB';
31                         
32                 case 'D': return 'DATE';
33                 case 'TS':
34                 case 'T': return 'TIMESTAMP';
35                 
36                 case 'L': return 'SMALLINT';
37                 case 'I': return 'INTEGER';
38                 case 'I1': return 'SMALLINT';
39                 case 'I2': return 'SMALLINT';
40                 case 'I4': return 'INTEGER';
41                 case 'I8': return 'INTEGER';
42                 
43                 case 'F': return 'DOUBLE PRECISION';
44                 case 'N': return 'DECIMAL';
45                 default:
46                         return $meta;
47                 }
48         }
49         
50         function NameQuote($name = NULL)
51         {
52                 if (!is_string($name)) {
53                         return FALSE;
54                 }
55                 
56                 $name = trim($name);
57                 
58                 if ( !is_object($this->connection) ) {
59                         return $name;
60                 }
61                 
62                 $quote = $this->connection->nameQuote;
63                 
64                 // if name is of the form `name`, quote it
65                 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
66                         return $quote . $matches[1] . $quote;
67                 }
68                 
69                 // if name contains special characters, quote it
70                 if ( !preg_match('/^[' . $this->nameRegex . ']+$/', $name) ) {
71                         return $quote . $name . $quote;
72                 }
73                 
74                 return $quote . $name . $quote;
75         }
76
77         function CreateDatabase($dbname, $options=false)
78         {
79                 $options = $this->_Options($options);
80                 $sql = array();
81                 
82                 $sql[] = "DECLARE EXTERNAL FUNCTION LOWER CSTRING(80) RETURNS CSTRING(80) FREE_IT ENTRY_POINT 'IB_UDF_lower' MODULE_NAME 'ib_udf'";
83                 
84                 return $sql;
85         }
86         
87         function _DropAutoIncrement($t)
88         {
89                 if (strpos($t,'.') !== false) {
90                         $tarr = explode('.',$t);
91                         return 'DROP GENERATOR '.$tarr[0].'."gen_'.$tarr[1].'"';
92                 }
93                 return 'DROP GENERATOR "GEN_'.$t;
94         }
95         
96
97         function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
98         {
99                 $suffix = '';
100                 
101                 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
102                 if ($fnotnull) $suffix .= ' NOT NULL';
103                 if ($fautoinc) $this->seqField = $fname;
104                 if ($fconstraint) $suffix .= ' '.$fconstraint;
105                 
106                 return $suffix;
107         }
108         
109 /*
110 CREATE or replace TRIGGER jaddress_insert
111 before insert on jaddress
112 for each row
113 begin
114 IF ( NEW."seqField" IS NULL OR NEW."seqField" = 0 ) THEN
115   NEW."seqField" = GEN_ID("GEN_tabname", 1);
116 end;
117 */
118         function _Triggers($tabname,$tableoptions)
119         {       
120                 if (!$this->seqField) return array();
121                 
122                 $tab1 = preg_replace( '/"/', '', $tabname );
123                 if ($this->schema) {
124                         $t = strpos($tab1,'.');
125                         if ($t !== false) $tab = substr($tab1,$t+1);
126                         else $tab = $tab1;
127                         $seqField = $this->seqField;
128                         $seqname = $this->schema.'.'.$this->seqPrefix.$tab;
129                         $trigname = $this->schema.'.trig_'.$this->seqPrefix.$tab;
130                 } else {
131                         $seqField = $this->seqField;
132                         $seqname = $this->seqPrefix.$tab1;
133                         $trigname = 'trig_'.$seqname;
134                 }
135                 if (isset($tableoptions['REPLACE']))
136                 { $sql[] = "DROP GENERATOR \"$seqname\"";
137                   $sql[] = "CREATE GENERATOR \"$seqname\"";
138                   $sql[] = "ALTER TRIGGER \"$trigname\" BEFORE INSERT OR UPDATE AS BEGIN IF ( NEW.$seqField IS NULL OR NEW.$seqField = 0 ) THEN NEW.$seqField = GEN_ID(\"$seqname\", 1); END";
139                 }
140                 else
141                 { $sql[] = "CREATE GENERATOR \"$seqname\"";
142                   $sql[] = "CREATE TRIGGER \"$trigname\" FOR $tabname BEFORE INSERT OR UPDATE AS BEGIN IF ( NEW.$seqField IS NULL OR NEW.$seqField = 0 ) THEN NEW.$seqField = GEN_ID(\"$seqname\", 1); END";
143                 }
144                 
145                 $this->seqField = false;
146                 return $sql;
147         }
148
149 }
150
151
152 ?>