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