ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilDBPdoPostgresFieldDefinition.php
Go to the documentation of this file.
1<?php
2require_once('class.ilDBPdoFieldDefinition.php');
3
10{
11
15 protected $options = array(
16 'default_text_field_length' => 4096,
17 'decimal_places' => 2,
18 );
19
24 public function getTypeDeclaration($field)
25 {
26 $db = $this->getDBInstance();
27
28 switch ($field['type'])
29 {
30 case 'text':
31 $length = !empty($field['length']) ? $field['length'] : $this->options['default_text_field_length'];
32 $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
33 $fixed = false; // FSX we do not want to have fixed lengths
34
35 return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(' . $this->options['default_text_field_length']
36 . ')') : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
37 case 'clob':
38 return 'TEXT';
39 case 'blob':
40 return 'BYTEA';
41 case 'integer':
42 if (!empty($field['autoincrement']))
43 {
44 if (!empty($field['length']))
45 {
46 $length = $field['length'];
47 if ($length > 4)
48 {
49 return 'BIGSERIAL PRIMARY KEY';
50 }
51 }
52
53 return 'SERIAL PRIMARY KEY';
54 }
55 if (!empty($field['length']))
56 {
57 $length = $field['length'];
58 if ($length <= 2)
59 {
60 return 'SMALLINT';
61 } elseif ($length == 3 || $length == 4)
62 {
63 return 'INT';
64 } elseif ($length > 4)
65 {
66 return 'BIGINT';
67 }
68 }
69
70 return 'INT';
71 case 'boolean':
72 return 'BOOLEAN';
73 case 'date':
74 return 'DATE';
75 case 'time':
76 return 'TIME without time zone';
77 case 'timestamp':
78 return 'TIMESTAMP without time zone';
79 case 'float':
80 return 'FLOAT8';
81 case 'decimal':
82 $length = !empty($field['length']) ? $field['length'] : 18;
83 $scale = !empty($field['scale']) ? $field['scale'] : $this->options['decimal_places'];
84
85 return 'NUMERIC(' . $length . ',' . $scale . ')';
86 }
87 }
88
89
96 protected function getIntegerDeclaration($name, $field)
97 {
98 $db = $this->getDBInstance();
99
100 if (!empty($field['unsigned']))
101 {
102 $db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer";
103 }
104 if (!empty($field['autoincrement']))
105 {
106 $name = $db->quoteIdentifier($name, true);
107
108 return $name . ' ' . $this->getTypeDeclaration($field);
109 }
110 $default = '';
111 if (array_key_exists('default', $field))
112 {
113 if ($field['default'] === '')
114 {
115 $field['default'] = empty($field['notnull']) ? null : 0;
116 }
117 $default = ' DEFAULT ' . $this->quote($field['default'], 'integer');
118 } elseif (empty($field['notnull']))
119 {
120 $default = ' DEFAULT NULL';
121 }
122
123 $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
124 $name = $db->quoteIdentifier($name, true);
125
126 return $name . ' ' . $this->getTypeDeclaration($field) . $default . $notnull;
127 }
128
129
135 protected function mapNativeDatatypeInternal($field)
136 {
137 $db_type = strtolower($field['type']);
138 $length = $field['length'];
139 $type = array();
140 $unsigned = $fixed = null;
141 switch ($db_type)
142 {
143 case 'smallint':
144 case 'int2':
145 $type[] = 'integer';
146 $unsigned = false;
147 $length = 2;
148 if ($length == '2')
149 {
150 $type[] = 'boolean';
151 if (preg_match('/^(is|has)/', $field['name']))
152 {
153 $type = array_reverse($type);
154 }
155 }
156 break;
157 case 'int':
158 case 'int4':
159 case 'integer':
160 case 'serial':
161 case 'serial4':
162 $type[] = 'integer';
163 $unsigned = false;
164 $length = 4;
165 break;
166 case 'bigint':
167 case 'int8':
168 case 'bigserial':
169 case 'serial8':
170 $type[] = 'integer';
171 $unsigned = false;
172 $length = 8;
173 break;
174 case 'bool':
175 case 'boolean':
176 $type[] = 'boolean';
177 $length = null;
178 break;
179 case 'text':
180 case 'varchar':
181 $fixed = false;
182 case 'unknown':
183 case 'char':
184 case 'bpchar':
185 $type[] = 'text';
186 if ($length == '1')
187 {
188 $type[] = 'boolean';
189 if (preg_match('/^(is|has)/', $field['name']))
190 {
191 $type = array_reverse($type);
192 }
193 } elseif (strstr($db_type, 'text'))
194 {
195 $type[] = 'clob';
196 }
197 if ($fixed !== false)
198 {
199 $fixed = true;
200 }
201 break;
202 case 'date':
203 $type[] = 'date';
204 $length = null;
205 break;
206 case 'datetime':
207 case 'timestamp':
208 $type[] = 'timestamp';
209 $length = null;
210 break;
211 case 'time':
212 $type[] = 'time';
213 $length = null;
214 break;
215 case 'float':
216 case 'float8':
217 case 'double':
218 case 'real':
219 $type[] = 'float';
220 break;
221 case 'decimal':
222 case 'money':
223 case 'numeric':
224 $type[] = 'decimal';
225 if ($field['scale'])
226 {
227 $length = $length . ',' . $field['scale'];
228 }
229 break;
230 case 'tinyblob':
231 case 'mediumblob':
232 case 'longblob':
233 case 'blob':
234 case 'bytea':
235 $type[] = 'blob';
236 $length = null;
237 break;
238 case 'oid':
239 $type[] = 'blob';
240 $type[] = 'clob';
241 $length = null;
242 break;
243 case 'year':
244 $type[] = 'integer';
245 $type[] = 'date';
246 $length = null;
247 break;
248 default:
249 throw new ilDatabaseException('unknown database attribute type: ' . $db_type);
250 }
251
252 if ((int)$length <= 0)
253 {
254 $length = null;
255 }
256
257 return array( $type, $length, $unsigned, $fixed );
258 }
259}
An exception for terminatinating execution or to throw for unit testing.
Class ilDBPdoFieldDefinition.
quote($value, $type=null, $quote=true, $escape_wildcards=false)
Class ilDBPdoPostgresFieldDefinition.
Class ilDatabaseException.