ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 case 'text':
30 $length = !empty($field['length']) ? $field['length'] : $this->options['default_text_field_length'];
31 $fixed = !empty($field['fixed']) ? $field['fixed'] : false;
32 $fixed = false; // FSX we do not want to have fixed lengths
33
34 return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(' . $this->options['default_text_field_length']
35 . ')') : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
36 case 'clob':
37 return 'TEXT';
38 case 'blob':
39 return 'BYTEA';
40 case 'integer':
41 if (!empty($field['autoincrement'])) {
42 if (!empty($field['length'])) {
43 $length = $field['length'];
44 if ($length > 4) {
45 return 'BIGSERIAL PRIMARY KEY';
46 }
47 }
48
49 return 'SERIAL PRIMARY KEY';
50 }
51 if (!empty($field['length'])) {
52 $length = $field['length'];
53 if ($length <= 2) {
54 return 'SMALLINT';
55 } elseif ($length == 3 || $length == 4) {
56 return 'INT';
57 } elseif ($length > 4) {
58 return 'BIGINT';
59 }
60 }
61
62 return 'INT';
63 case 'boolean':
64 return 'BOOLEAN';
65 case 'date':
66 return 'DATE';
67 case 'time':
68 return 'TIME without time zone';
69 case 'timestamp':
70 return 'TIMESTAMP without time zone';
71 case 'float':
72 return 'FLOAT8';
73 case 'decimal':
74 $length = !empty($field['length']) ? $field['length'] : 18;
75 $scale = !empty($field['scale']) ? $field['scale'] : $this->options['decimal_places'];
76
77 return 'NUMERIC(' . $length . ',' . $scale . ')';
78 }
79 }
80
81
88 protected function getIntegerDeclaration($name, $field)
89 {
90 $db = $this->getDBInstance();
91
92 if (!empty($field['unsigned'])) {
93 $db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer";
94 }
95 if (!empty($field['autoincrement'])) {
96 $name = $db->quoteIdentifier($name, true);
97
98 return $name . ' ' . $this->getTypeDeclaration($field);
99 }
100 $default = '';
101 if (array_key_exists('default', $field)) {
102 if ($field['default'] === '') {
103 $field['default'] = empty($field['notnull']) ? null : 0;
104 }
105 $default = ' DEFAULT ' . $this->quote($field['default'], 'integer');
106 } elseif (empty($field['notnull'])) {
107 $default = ' DEFAULT NULL';
108 }
109
110 $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
111 $name = $db->quoteIdentifier($name, true);
112
113 return $name . ' ' . $this->getTypeDeclaration($field) . $default . $notnull;
114 }
115
116
122 protected function mapNativeDatatypeInternal($field)
123 {
124 $db_type = strtolower($field['type']);
125 $length = $field['length'];
126 $type = array();
127 $unsigned = $fixed = null;
128 switch ($db_type) {
129 case 'smallint':
130 case 'int2':
131 $type[] = 'integer';
132 $unsigned = false;
133 $length = 2;
134 if ($length == '2') {
135 $type[] = 'boolean';
136 if (preg_match('/^(is|has)/', $field['name'])) {
137 $type = array_reverse($type);
138 }
139 }
140 break;
141 case 'int':
142 case 'int4':
143 case 'integer':
144 case 'serial':
145 case 'serial4':
146 $type[] = 'integer';
147 $unsigned = false;
148 $length = 4;
149 break;
150 case 'bigint':
151 case 'int8':
152 case 'bigserial':
153 case 'serial8':
154 $type[] = 'integer';
155 $unsigned = false;
156 $length = 8;
157 break;
158 case 'bool':
159 case 'boolean':
160 $type[] = 'boolean';
161 $length = null;
162 break;
163 case 'text':
164 case 'varchar':
165 $fixed = false;
166 // no break
167 case 'unknown':
168 case 'char':
169 case 'bpchar':
170 $type[] = 'text';
171 if ($length == '1') {
172 $type[] = 'boolean';
173 if (preg_match('/^(is|has)/', $field['name'])) {
174 $type = array_reverse($type);
175 }
176 } elseif (strstr($db_type, 'text')) {
177 $type[] = 'clob';
178 }
179 if ($fixed !== false) {
180 $fixed = true;
181 }
182 break;
183 case 'date':
184 $type[] = 'date';
185 $length = null;
186 break;
187 case 'datetime':
188 case 'timestamp':
189 $type[] = 'timestamp';
190 $length = null;
191 break;
192 case 'time':
193 $type[] = 'time';
194 $length = null;
195 break;
196 case 'float':
197 case 'float8':
198 case 'double':
199 case 'real':
200 $type[] = 'float';
201 break;
202 case 'decimal':
203 case 'money':
204 case 'numeric':
205 $type[] = 'decimal';
206 if ($field['scale']) {
207 $length = $length . ',' . $field['scale'];
208 }
209 break;
210 case 'tinyblob':
211 case 'mediumblob':
212 case 'longblob':
213 case 'blob':
214 case 'bytea':
215 $type[] = 'blob';
216 $length = null;
217 break;
218 case 'oid':
219 $type[] = 'blob';
220 $type[] = 'clob';
221 $length = null;
222 break;
223 case 'year':
224 $type[] = 'integer';
225 $type[] = 'date';
226 $length = null;
227 break;
228 default:
229 throw new ilDatabaseException('unknown database attribute type: ' . $db_type);
230 }
231
232 if ((int) $length <= 0) {
233 $length = null;
234 }
235
236 return array( $type, $length, $unsigned, $fixed );
237 }
238}
$default
Definition: build.php:20
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.
$type