4 @version v5.21.0 2021-02-27
5 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
6 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
11 Set tabs to 4 for best viewing.
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
18 class ADODB2_firebird
extends ADODB_DataDict
{
20 var $databaseType = 'firebird';
21 var $seqField = false;
22 var $seqPrefix = 's_';
23 var $blobSize = 40000;
24 var $renameColumn = 'ALTER TABLE %s ALTER %s TO %s';
25 var $alterCol = ' ALTER';
26 var $dropCol = ' DROP';
28 function ActualType($meta)
31 case 'C': return 'VARCHAR';
33 case 'X': return 'BLOB SUB_TYPE TEXT';
35 case 'C2': return 'VARCHAR(32765)'; // up to 32K
36 case 'X2': return 'VARCHAR(4096)';
38 case 'V': return 'CHAR';
39 case 'C1': return 'CHAR(1)';
41 case 'B': return 'BLOB';
43 case 'D': return 'DATE';
45 case 'T': return 'TIMESTAMP';
47 case 'L': return 'SMALLINT';
48 case 'I': return 'INTEGER';
49 case 'I1': return 'SMALLINT';
50 case 'I2': return 'SMALLINT';
51 case 'I4': return 'INTEGER';
52 case 'I8': return 'BIGINT';
54 case 'F': return 'DOUBLE PRECISION';
55 case 'N': return 'DECIMAL';
61 function NameQuote($name = NULL,$allowBrackets=false)
63 if (!is_string($name)) {
69 if ( !is_object($this->connection
) ) {
73 $quote = $this->connection
->nameQuote
;
75 // if name is of the form `name`, quote it
76 if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
77 return $quote . $matches[1] . $quote;
80 // if name contains special characters, quote it
81 if ( !preg_match('/^[' . $this->nameRegex
. ']+$/', $name) ) {
82 return $quote . $name . $quote;
85 return $quote . $name . $quote;
88 function CreateDatabase($dbname, $options=false)
90 $options = $this->_Options($options);
93 $sql[] = "DECLARE EXTERNAL FUNCTION LOWER CSTRING(80) RETURNS CSTRING(80) FREE_IT ENTRY_POINT 'IB_UDF_lower' MODULE_NAME 'ib_udf'";
98 function _DropAutoIncrement($t)
100 if (strpos($t,'.') !== false) {
101 $tarr = explode('.',$t);
102 return 'DROP GENERATOR '.$tarr[0].'."s_'.$tarr[1].'"';
104 return 'DROP GENERATOR s_'.$t;
108 function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
112 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
113 if ($fnotnull) $suffix .= ' NOT NULL';
114 if ($fautoinc) $this->seqField
= $fname;
115 $fconstraint = preg_replace("/``/", "\"", $fconstraint);
116 if ($fconstraint) $suffix .= ' '.$fconstraint;
122 Generate the SQL to create table. Returns an array of sql strings.
124 function CreateTableSQL($tabname, $flds, $tableoptions=array())
126 list($lines,$pkey,$idxs) = $this->_GenFields($flds, true);
127 // genfields can return FALSE at times
128 if ($lines == null) $lines = array();
130 $taboptions = $this->_Options($tableoptions);
131 $tabname = $this->TableName ($tabname);
132 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
134 if ($this->autoIncrement
&& !isset($taboptions['DROP']))
135 { $tsql = $this->_Triggers($tabname,$taboptions);
136 foreach($tsql as $s) $sql[] = $s;
139 if (is_array($idxs)) {
140 foreach($idxs as $idx => $idxdef) {
141 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
142 $sql = array_merge($sql, $sql_idxs);
151 CREATE or replace TRIGGER jaddress_insert
152 before insert on jaddress
155 IF ( NEW."seqField" IS NULL OR NEW."seqField" = 0 ) THEN
156 NEW."seqField" = GEN_ID("GEN_tabname", 1);
159 function _Triggers($tabname,$tableoptions)
161 if (!$this->seqField
) return array();
163 $tab1 = preg_replace( '/"/', '', $tabname );
165 $t = strpos($tab1,'.');
166 if ($t !== false) $tab = substr($tab1,$t+
1);
168 $seqField = $this->seqField
;
169 $seqname = $this->schema
.'.'.$this->seqPrefix
.$tab;
170 $trigname = $this->schema
.'.t_'.$this->seqPrefix
.$tab;
172 $seqField = $this->seqField
;
173 $seqname = $this->seqPrefix
.$tab1;
174 $trigname = 't_'.$seqname;
177 if (isset($tableoptions['DROP']))
178 { $sql[] = "DROP GENERATOR $seqname";
180 elseif (isset($tableoptions['REPLACE']))
181 { $sql[] = "DROP GENERATOR \"$seqname\"";
182 $sql[] = "CREATE GENERATOR \"$seqname\"";
183 $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";
186 { $sql[] = "CREATE GENERATOR $seqname";
187 $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";
190 $this->seqField
= false;
195 * Change the definition of one column
197 * As some DBM's can't do that on there own, you need to supply the complete definition of the new table,
198 * to allow, recreating the table and copying the content over to the new table
199 * @param string $tabname table-name
200 * @param string $flds column-name and type for the changed column
201 * @param string $tableflds='' complete definition of the new table, eg. for postgres, default ''
202 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
203 * @return array with SQL strings
205 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
207 $tabname = $this->TableName ($tabname);
209 list($lines,$pkey,$idxs) = $this->_GenFields($flds);
210 // genfields can return FALSE at times
211 if ($lines == null) $lines = array();
212 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol
. ' ';
213 foreach($lines as $v) {
214 $sql[] = $alter . $v;
216 if (is_array($idxs)) {
217 foreach($idxs as $idx => $idxdef) {
218 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
219 $sql = array_merge($sql, $sql_idxs);