3f2444951ab29deb04e615a08f3530150f65e6c4
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / datadict / datadict-db2.inc.php
1 <?php
2
3 /**
4 V4.81 3 May 2006 (c) 2000-2006 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 */
12 // security - hide paths
13 if (!defined('ADODB_DIR')) die();
14
15 class ADODB2_db2 extends ADODB_DataDict {
16
17 var $databaseType = 'db2';
18 var $seqField = false;
19
20 function ActualType($meta)
21 {
22 switch($meta) {
23 case 'C': return 'VARCHAR';
24 case 'XL': return 'CLOB';
25 case 'X': return 'VARCHAR(3600)';
26
27 case 'C2': return 'VARCHAR'; // up to 32K
28 case 'X2': return 'VARCHAR(3600)'; // up to 32000, but default page size too small
29
30 case 'B': return 'BLOB';
31
32 case 'D': return 'DATE';
33 case 'T': return 'TIMESTAMP';
34
35 case 'L': return 'SMALLINT';
36 case 'I': return 'INTEGER';
37 case 'I1': return 'SMALLINT';
38 case 'I2': return 'SMALLINT';
39 case 'I4': return 'INTEGER';
40 case 'I8': return 'BIGINT';
41
42 case 'F': return 'DOUBLE';
43 case 'N': return 'DECIMAL';
44 default:
45 return $meta;
46 }
47 }
48
49 // return string must begin with space
50 function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
51 {
52 $suffix = '';
53 if ($fautoinc) return ' GENERATED ALWAYS AS IDENTITY'; # as identity start with
54 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
55 if ($fnotnull) $suffix .= ' NOT NULL';
56 if ($fconstraint) $suffix .= ' '.$fconstraint;
57 return $suffix;
58 }
59
60 function AlterColumnSQL($tabname, $flds)
61 {
62 if ($this->debug) ADOConnection::outp("AlterColumnSQL not supported");
63 return array();
64 }
65
66
67 function DropColumnSQL($tabname, $flds)
68 {
69 if ($this->debug) ADOConnection::outp("DropColumnSQL not supported");
70 return array();
71 }
72
73
74 function ChangeTableSQL($tablename, $flds, $tableoptions = false)
75 {
76
77 /**
78 Allow basic table changes to DB2 databases
79 DB2 will fatally reject changes to non character columns
80
81 */
82
83 $validTypes = array("CHAR","VARC");
84 $invalidTypes = array("BIGI","BLOB","CLOB","DATE", "DECI","DOUB", "INTE", "REAL","SMAL", "TIME");
85 // check table exists
86 $cols = &$this->MetaColumns($tablename);
87 if ( empty($cols)) {
88 return $this->CreateTableSQL($tablename, $flds, $tableoptions);
89 }
90
91 // already exists, alter table instead
92 list($lines,$pkey) = $this->_GenFields($flds);
93 $alter = 'ALTER TABLE ' . $this->TableName($tablename);
94 $sql = array();
95
96 foreach ( $lines as $id => $v ) {
97 if ( isset($cols[$id]) && is_object($cols[$id]) ) {
98 /**
99 If the first field of $v is the fieldname, and
100 the second is the field type/size, we assume its an
101 attempt to modify the column size, so check that it is allowed
102 $v can have an indeterminate number of blanks between the
103 fields, so account for that too
104 */
105 $vargs = explode(' ' , $v);
106 // assume that $vargs[0] is the field name.
107 $i=0;
108 // Find the next non-blank value;
109 for ($i=1;$i<sizeof($vargs);$i++)
110 if ($vargs[$i] != '')
111 break;
112
113 // if $vargs[$i] is one of the following, we are trying to change the
114 // size of the field, if not allowed, simply ignore the request.
115 if (in_array(substr($vargs[$i],0,4),$invalidTypes))
116 continue;
117 // insert the appropriate DB2 syntax
118 if (in_array(substr($vargs[$i],0,4),$validTypes)) {
119 array_splice($vargs,$i,0,array('SET','DATA','TYPE'));
120 }
121
122 // Now Look for the NOT NULL statement as this is not allowed in
123 // the ALTER table statement. If it is in there, remove it
124 if (in_array('NOT',$vargs) && in_array('NULL',$vargs)) {
125 for ($i=1;$i<sizeof($vargs);$i++)
126 if ($vargs[$i] == 'NOT')
127 break;
128 array_splice($vargs,$i,2,'');
129 }
130 $v = implode(' ',$vargs);
131 $sql[] = $alter . $this->alterCol . ' ' . $v;
132 } else {
133 $sql[] = $alter . $this->addCol . ' ' . $v;
134 }
135 }
136
137 return $sql;
138 }
139
140 }
141
142
143 ?>