1 |
efrain |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Data Dictionary for DB2.
|
|
|
4 |
*
|
|
|
5 |
* This file is part of ADOdb, a Database Abstraction Layer library for PHP.
|
|
|
6 |
*
|
|
|
7 |
* @package ADOdb
|
|
|
8 |
* @link https://adodb.org Project's web site and documentation
|
|
|
9 |
* @link https://github.com/ADOdb/ADOdb Source code and issue tracker
|
|
|
10 |
*
|
|
|
11 |
* The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
|
|
|
12 |
* and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
|
|
|
13 |
* any later version. This means you can use it in proprietary products.
|
|
|
14 |
* See the LICENSE.md file distributed with this source code for details.
|
|
|
15 |
* @license BSD-3-Clause
|
|
|
16 |
* @license LGPL-2.1-or-later
|
|
|
17 |
*
|
|
|
18 |
* @copyright 2000-2013 John Lim
|
|
|
19 |
* @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
// security - hide paths
|
|
|
23 |
if (!defined('ADODB_DIR')) die();
|
|
|
24 |
|
|
|
25 |
class ADODB2_db2 extends ADODB_DataDict {
|
|
|
26 |
|
|
|
27 |
var $databaseType = 'db2';
|
|
|
28 |
var $seqField = false;
|
|
|
29 |
var $dropCol = 'ALTER TABLE %s DROP COLUMN %s';
|
|
|
30 |
|
|
|
31 |
public $blobAllowsDefaultValue = true;
|
|
|
32 |
public $blobAllowsNotNull = true;
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
function ActualType($meta)
|
|
|
36 |
{
|
|
|
37 |
$meta = strtoupper($meta);
|
|
|
38 |
|
|
|
39 |
/*
|
|
|
40 |
* Add support for custom meta types. We do this
|
|
|
41 |
* first, that allows us to override existing types
|
|
|
42 |
*/
|
|
|
43 |
if (isset($this->connection->customMetaTypes[$meta]))
|
|
|
44 |
return $this->connection->customMetaTypes[$meta]['actual'];
|
|
|
45 |
|
|
|
46 |
switch($meta) {
|
|
|
47 |
case 'C': return 'VARCHAR';
|
|
|
48 |
case 'XL': return 'CLOB';
|
|
|
49 |
case 'X': return 'VARCHAR(3600)';
|
|
|
50 |
|
|
|
51 |
case 'C2': return 'VARCHAR'; // up to 32K
|
|
|
52 |
case 'X2': return 'VARCHAR(3600)'; // up to 32000, but default page size too small
|
|
|
53 |
|
|
|
54 |
case 'B': return 'BLOB';
|
|
|
55 |
|
|
|
56 |
case 'D': return 'DATE';
|
|
|
57 |
case 'TS':
|
|
|
58 |
case 'T': return 'TIMESTAMP';
|
|
|
59 |
|
|
|
60 |
case 'L': return 'SMALLINT';
|
|
|
61 |
case 'I': return 'INTEGER';
|
|
|
62 |
case 'I1': return 'SMALLINT';
|
|
|
63 |
case 'I2': return 'SMALLINT';
|
|
|
64 |
case 'I4': return 'INTEGER';
|
|
|
65 |
case 'I8': return 'BIGINT';
|
|
|
66 |
|
|
|
67 |
case 'F': return 'DOUBLE';
|
|
|
68 |
case 'N': return 'DECIMAL';
|
|
|
69 |
default:
|
|
|
70 |
return $meta;
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// return string must begin with space
|
|
|
75 |
function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
|
|
|
76 |
{
|
|
|
77 |
$suffix = '';
|
|
|
78 |
if ($fautoinc) return ' GENERATED ALWAYS AS IDENTITY'; # as identity start with
|
|
|
79 |
if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
|
|
|
80 |
if ($fnotnull) $suffix .= ' NOT NULL';
|
|
|
81 |
if ($fconstraint) $suffix .= ' '.$fconstraint;
|
|
|
82 |
return $suffix;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
function alterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
|
|
|
86 |
{
|
|
|
87 |
$tabname = $this->TableName ($tabname);
|
|
|
88 |
$sql = array();
|
|
|
89 |
list($lines,$pkey,$idxs) = $this->_GenFields($flds);
|
|
|
90 |
// genfields can return FALSE at times
|
|
|
91 |
if ($lines == null) $lines = array();
|
|
|
92 |
$alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
|
|
|
93 |
|
|
|
94 |
$dataTypeWords = array('SET','DATA','TYPE');
|
|
|
95 |
|
|
|
96 |
foreach($lines as $v)
|
|
|
97 |
{
|
|
|
98 |
/*
|
|
|
99 |
* We must now post-process the line to insert the 'SET DATA TYPE'
|
|
|
100 |
* text into the alter statement
|
|
|
101 |
*/
|
|
|
102 |
$e = explode(' ',$v);
|
|
|
103 |
|
|
|
104 |
array_splice($e,1,0,$dataTypeWords);
|
|
|
105 |
|
|
|
106 |
$v = implode(' ',$e);
|
|
|
107 |
|
|
|
108 |
$sql[] = $alter . $v;
|
|
|
109 |
}
|
|
|
110 |
if (is_array($idxs))
|
|
|
111 |
{
|
|
|
112 |
foreach($idxs as $idx => $idxdef) {
|
|
|
113 |
$sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
|
|
|
114 |
$sql = array_merge($sql, $sql_idxs);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
}
|
|
|
118 |
return $sql;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
function dropColumnSql($tabname, $flds, $tableflds='',$tableoptions='')
|
|
|
124 |
{
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
$tabname = $this->connection->getMetaCasedValue($tabname);
|
|
|
128 |
$flds = $this->connection->getMetaCasedValue($flds);
|
|
|
129 |
|
|
|
130 |
if (ADODB_ASSOC_CASE == ADODB_ASSOC_CASE_NATIVE )
|
|
|
131 |
{
|
|
|
132 |
/*
|
|
|
133 |
* METACASE_NATIVE
|
|
|
134 |
*/
|
|
|
135 |
$tabname = $this->connection->nameQuote . $tabname . $this->connection->nameQuote;
|
|
|
136 |
$flds = $this->connection->nameQuote . $flds . $this->connection->nameQuote;
|
|
|
137 |
}
|
|
|
138 |
$sql = sprintf($this->dropCol,$tabname,$flds);
|
|
|
139 |
return (array)$sql;
|
|
|
140 |
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
|
|
|
144 |
function changeTableSQL($tablename, $flds, $tableoptions = false, $dropOldFields=false)
|
|
|
145 |
{
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
Allow basic table changes to DB2 databases
|
|
|
149 |
DB2 will fatally reject changes to non character columns
|
|
|
150 |
|
|
|
151 |
*/
|
|
|
152 |
|
|
|
153 |
$validTypes = array("CHAR","VARC");
|
|
|
154 |
$invalidTypes = array("BIGI","BLOB","CLOB","DATE", "DECI","DOUB", "INTE", "REAL","SMAL", "TIME");
|
|
|
155 |
// check table exists
|
|
|
156 |
|
|
|
157 |
|
|
|
158 |
$cols = $this->metaColumns($tablename);
|
|
|
159 |
if ( empty($cols)) {
|
|
|
160 |
return $this->createTableSQL($tablename, $flds, $tableoptions);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// already exists, alter table instead
|
|
|
164 |
list($lines,$pkey) = $this->_GenFields($flds);
|
|
|
165 |
$alter = 'ALTER TABLE ' . $this->tableName($tablename);
|
|
|
166 |
$sql = array();
|
|
|
167 |
|
|
|
168 |
foreach ( $lines as $id => $v ) {
|
|
|
169 |
/*
|
|
|
170 |
* If the metaCasing was NATIVE the col returned with nameQuotes
|
|
|
171 |
* around the field. We need to remove this for the metaColumn
|
|
|
172 |
* match
|
|
|
173 |
*/
|
|
|
174 |
$id = str_replace($this->connection->nameQuote,'',$id);
|
|
|
175 |
if ( isset($cols[$id]) && is_object($cols[$id]) ) {
|
|
|
176 |
/**
|
|
|
177 |
If the first field of $v is the fieldname, and
|
|
|
178 |
the second is the field type/size, we assume its an
|
|
|
179 |
attempt to modify the column size, so check that it is allowed
|
|
|
180 |
$v can have an indeterminate number of blanks between the
|
|
|
181 |
fields, so account for that too
|
|
|
182 |
*/
|
|
|
183 |
$vargs = explode(' ' , $v);
|
|
|
184 |
// assume that $vargs[0] is the field name.
|
|
|
185 |
$i=0;
|
|
|
186 |
// Find the next non-blank value;
|
|
|
187 |
for ($i=1;$i<sizeof($vargs);$i++)
|
|
|
188 |
if ($vargs[$i] != '')
|
|
|
189 |
break;
|
|
|
190 |
|
|
|
191 |
// if $vargs[$i] is one of the following, we are trying to change the
|
|
|
192 |
// size of the field, if not allowed, simply ignore the request.
|
|
|
193 |
if (in_array(substr($vargs[$i],0,4),$invalidTypes))
|
|
|
194 |
continue;
|
|
|
195 |
// insert the appropriate DB2 syntax
|
|
|
196 |
if (in_array(substr($vargs[$i],0,4),$validTypes)) {
|
|
|
197 |
array_splice($vargs,$i,0,array('SET','DATA','TYPE'));
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
// Now Look for the NOT NULL statement as this is not allowed in
|
|
|
201 |
// the ALTER table statement. If it is in there, remove it
|
|
|
202 |
if (in_array('NOT',$vargs) && in_array('NULL',$vargs)) {
|
|
|
203 |
for ($i=1;$i<sizeof($vargs);$i++)
|
|
|
204 |
if ($vargs[$i] == 'NOT')
|
|
|
205 |
break;
|
|
|
206 |
array_splice($vargs,$i,2,'');
|
|
|
207 |
}
|
|
|
208 |
$v = implode(' ',$vargs);
|
|
|
209 |
$sql[] = $alter . $this->alterCol . ' ' . $v;
|
|
|
210 |
} else {
|
|
|
211 |
$sql[] = $alter . $this->addCol . ' ' . $v;
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
return $sql;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
}
|