Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezsqliteschema.php
Go to the documentation of this file.
1 <?php
2
3 class eZSQLiteSchema extends eZDBSchemaInterface
4 {
5 function __construct( $params )
6 {
7 parent::__construct( $params );
8 }
9
13 function schema( $params = array() )
14 {
15 $params = array_merge( array( 'meta_data' => false,
16 'format' => 'generic' ),
17 $params );
18 $schema = array();
19
20 if ( $this->Schema === false )
21 {
22 $tableArray = $this->DBInstance->arrayQuery( "SELECT name FROM sqlite_master WHERE type='table'" );
23
24 foreach( $tableArray as $tableNameArray )
25 {
26 $table_name = current( $tableNameArray );
27 if ( !isset( $params['table_include'] ) or
28 ( is_array( $params['table_include'] ) and
29 in_array( $table_name, $params['table_include'] ) ) )
30 {
31 $schema_table['name'] = $table_name;
32 $schema_table['fields'] = $this->fetchTableFields( $table_name, $params );
33 $schema_table['indexes'] = $this->fetchTableIndexes( $table_name, $params );
34
35 $schema[$table_name] = $schema_table;
36 }
37 }
38 $this->transformSchema( $schema, $params['format'] == 'local' );
39 ksort( $schema );
40 $this->Schema = $schema;
41 }
42 else
43 {
44 $this->transformSchema( $this->Schema, $params['format'] == 'local' );
45 $schema = $this->Schema;
46 }
47 return $schema;
48 }
49
55 private function fetchTableFields( $table, $params )
56 {
57 $fields = array();
58
59 $resultArray = $this->DBInstance->arrayQuery( "PRAGMA table_info($table)" );
60
61 foreach( $resultArray as $row )
62 {
63 $field = array();
64 $field['type'] = $this->parseType ( $row['Type'], $field['length'] );
65 if ( !$field['length'] )
66 {
67 unset( $field['length'] );
68 }
69 $field['not_null'] = 0;
70 if ( $row['Null'] != 'YES' )
71 {
72 $field['not_null'] = '1';
73 }
74 $field['default'] = false;
75 if ( !$field['not_null'] )
76 {
77 if ( $row['Default'] === null )
78 $field['default'] = null;
79 else
80 $field['default'] = (string)$row['Default'];
81 }
82 else
83 {
84 $field['default'] = (string)$row['Default'];
85 }
86
87 $numericTypes = array( 'float', 'int' );
88 $blobTypes = array( 'tinytext', 'text', 'mediumtext', 'longtext' );
89 $charTypes = array( 'varchar', 'char' );
90 if ( in_array( $field['type'], $charTypes ) )
91 {
92 if ( !$field['not_null'] )
93 {
94 if ( $field['default'] === null )
95 {
96 $field['default'] = null;
97 }
98 else if ( $field['default'] === false )
99 {
100 $field['default'] = '';
101 }
102 }
103 }
104 else if ( in_array( $field['type'], $numericTypes ) )
105 {
106 if ( $field['default'] === false )
107 {
108 if ( $field['not_null'] )
109 {
110 $field['default'] = 0;
111 }
112 }
113 else if ( $field['type'] == 'int' )
114 {
115 if ( $field['not_null'] or
116 is_numeric( $field['default'] ) )
117 $field['default'] = (int)$field['default'];
118 }
119 else if ( $field['type'] == 'float' or
120 is_numeric( $field['default'] ) )
121 {
122 if ( $field['not_null'] or
123 is_numeric( $field['default'] ) )
124 $field['default'] = (float)$field['default'];
125 }
126 }
127 else if ( in_array( $field['type'], $blobTypes ) )
128 {
129 // We do not want default for blobs.
130 $field['default'] = false;
131 }
132
133 if ( str_contains( $row['Extra'], 'auto_increment' ) !== false )
134 {
135 unset( $field['length'] );
136 $field['not_null'] = 0;
137 $field['default'] = false;
138 $field['type'] = 'auto_increment';
139 }
140
141 if ( !$field['not_null'] )
142 unset( $field['not_null'] );
143
144 $fields[$row['Field']] = $field;
145 }
146 ksort( $fields );
147
148 return $fields;
149 }
150
154 private function fetchTableIndexes( $table, $params )
155 {
156 $metaData = false;
157 if ( isset( $params['meta_data'] ) )
158 {
159 $metaData = $params['meta_data'];
160 }
161
162 $indexes = array();
163
164 $resultArray = $this->DBInstance->arrayQuery( "PRAGMA index_info( $table )" );
165
166 foreach( $resultArray as $row )
167 {
168 $kn = $row['Key_name'];
169
170 if ( $kn == 'PRIMARY' )
171 {
172 $indexes[$kn]['type'] = 'primary';
173 }
174 else
175 {
176 $indexes[$kn]['type'] = $row['Non_unique'] ? 'non-unique' : 'unique';
177 }
178
179 $indexFieldDef = array( 'name' => $row['Column_name'] );
180
181 // Include length if one is defined
182 if ( $row['Sub_part'] )
183 {
184 $indexFieldDef['mysql:length'] = (int)$row['Sub_part'];
185 }
186
187 // Check if we have any entries other than 'name', if not we skip the array definition
188 if ( count( array_diff( array_keys( $indexFieldDef ), array( 'name' ) ) ) == 0 )
189 {
190 $indexFieldDef = $indexFieldDef['name'];
191 }
192 $indexes[$kn]['fields'][$row['Seq_in_index'] - 1] = $indexFieldDef;
193 }
194 ksort( $indexes );
195
196 return $indexes;
197 }
198
199 function parseType( $type_info, &$length_info )
200 {
201 preg_match ( "@([a-z ]*)(\‍(([0-9]*|[0-9]*,[0-9]*)\‍))?@", $type_info, $matches );
202 if ( isset( $matches[3] ) )
203 {
204 $length_info = $matches[3];
205 if ( is_numeric( $length_info ) )
206 $length_info = (int)$length_info;
207 }
208 return $matches[1];
209 }
210
214 function generateAddIndexSql( $table_name, $index_name, $def, $params, $isEmbedded = false )
215 {
216 $diffFriendly = isset( $params['diff_friendly'] ) ? $params['diff_friendly'] : false;
217 $sql = '';
218
219 // Will be set to true when primary key is inside CREATE TABLE
220 if ( !$isEmbedded )
221 {
222 $sql .= "CREATE ";
223 $sql .= " ";
224 }
225
226 switch ( $def['type'] )
227 {
228 case 'primary':
229 {
230 $sql .= 'PRIMARY KEY';
231 } break;
232
233 case 'non-unique':
234 {
235 $sql .= "INDEX $index_name";
236 } break;
237
238 case 'unique':
239 {
240 $sql .= "UNIQUE INDEX $index_name";
241 } break;
242 }
243
244 if ( !$isEmbedded )
245 {
246 $sql .= " ON $table_name ";
247 }
248
249 $sql .= $diffFriendly ? " (\n " : " ( " ;
250 $fields = $def['fields'];
251 $i = 0;
252 foreach ( $fields as $fieldDef )
253 {
254 if ( $i > 0 )
255 {
256 $sql .= $diffFriendly ? ",\n " : ', ';
257 }
258 if ( is_array( $fieldDef ) )
259 {
260 $sql .= $fieldDef['name'];
261 if ( isset( $fieldDef['sqlite:length'] ) )
262 {
263 if ( $diffFriendly )
264 {
265 $sql .= "(\n";
266 $sql .= " " . str_repeat( ' ', strlen( $fieldDef['name'] ) );
267 }
268 else
269 {
270 $sql .= "( ";
271 }
272 $sql .= $fieldDef['sqlite:length'];
273 if ( $diffFriendly )
274 {
275 $sql .= ")";
276 }
277 else
278 {
279 $sql .= " )";
280 }
281 }
282 }
283 else
284 {
285 $sql .= $fieldDef;
286 }
287 ++$i;
288 }
289 $sql .= $diffFriendly ? "\n)" : " )";
290
291 if ( !$isEmbedded )
292 {
293 return $sql . ";\n";
294 }
295 return $sql;
296 }
297
301 function generateFieldDef( $field_name, $def, &$skip_primary, $params = null )
302 {
303 $diffFriendly = isset( $params['diff_friendly'] ) ? $params['diff_friendly'] : false;
304
305 $sql_def = $field_name . ' ';
306 $defaultText = "DEFAULT";
307
308 if ( $def['type'] != 'auto_increment' )
309 {
310 $defList = array();
311 $type = $def['type'];
312 if ( $type === 'int' )
313 {
314 $type = 'INTEGER';
315 }
316 if ( isset( $def['length'] ) )
317 {
318 $type .= "({$def['length']})";
319 }
320 $defList[] = $type;
321 if ( $type !== 'int' && isset( $def['not_null'] ) && ( $def['not_null'] ) )
322 {
323 $defList[] = 'NOT NULL';
324 }
325 if ( array_key_exists( 'default', $def ) )
326 {
327 if ( $def['default'] === null )
328 {
329 $defList[] = "$defaultText NULL";
330 }
331 else if ( $def['default'] !== false )
332 {
333 $defList[] = "$defaultText '{$def['default']}'";
334 }
335 }
336 else if ( $def['type'] == 'varchar' )
337 {
338 $defList[] = "$defaultText ''";
339 }
340 $sql_def .= join( $diffFriendly ? "\n " : " ", $defList );
341 $skip_primary = false;
342 }
343 else
344 {
345 $incrementText = ""; /*"PRIMARY KEY"*/
346 if ( $diffFriendly )
347 {
348 $sql_def .= "INTEGER\n $incrementText";
349 }
350 else
351 {
352 $sql_def .= "INTEGER $incrementText";
353 }
354 $skip_primary = true;
355 }
356 return $sql_def;
357 }
358
363 function generateTableSchema( $tableName, $table, $params )
364 {
365 return $this->generateTableSQL( $tableName, $table, $params, false, false );
366 }
367
372 function generateTableSQLList( $tableName, $table, $params, $separateTypes )
373 {
374 return $this->generateTableSQL( $tableName, $table, $params, true, $separateTypes );
375 }
376
384 function generateTableSQL( $tableName, $tableDef, $params, $asArray, $separateTypes = false )
385 {
386 $diffFriendly = isset( $params['diff_friendly'] ) ? $params['diff_friendly'] : false;
387 $mysqlCompatible = isset( $params['compatible_sql'] ) ? $params['compatible_sql'] : false;
388
389 if ( $asArray )
390 {
391 if ( $separateTypes )
392 {
393 $sqlList = array( 'tables' => array() );
394 }
395 else
396 {
397 $sqlList = array();
398 }
399 }
400
401 $sql = '';
402 $skip_pk = false;
403 $sql_fields = array();
404 $sql .= "CREATE TABLE $tableName (\n";
405
406 $fields = $tableDef['fields'];
407
408 foreach ( $fields as $field_name => $field_def )
409 {
410 $sql_fields[] = ' ' . self::generateFieldDef( $field_name, $field_def, $skip_pk_flag, $params );
411 if ( $skip_pk_flag )
412 {
413 $skip_pk = true;
414 }
415 }
416
417 // Make sure the order is as defined by 'offset'
418 $indexes = $tableDef['indexes'];
419
420 // We need to add all keys in table definition
421 foreach ( $indexes as $index_name => $index_def )
422 {
423 if ( $index_def['type'] == 'primary' )
424 {
425 $sql_fields[] = ( $diffFriendly ? '' : ' ' ) . self::generateAddIndexSql( $tableName, $index_name, $index_def, $params, true );
426 }
427 }
428 $sql .= join( ",\n", $sql_fields );
429 $sql .= "\n)";
430
431 // Add some extra table options if they are required
432 $extraOptions = array();
433 if ( isset( $params['table_type'] ) and $params['table_type'] )
434 {
435 $typeName = $this->tableStorageTypeName( $params['table_type'] );
436 if ( $typeName )
437 {
438 $extraOptions[] = "TYPE=" . $typeName;
439 }
440 }
441
442 if ( isset( $tableDef['options'] ) )
443 {
444 foreach( $tableDef['options'] as $optionType => $optionValue )
445 {
446 $optionText = $this->generateTableOption( $tableName, $tableDef, $optionType, $optionValue, $params );
447 if ( $optionText )
448 $extraOptions[] = $optionText;
449 }
450 }
451
452 if ( count( $extraOptions ) > 0 )
453 {
454 $sql .= " " . implode( $diffFriendly ? "\n" : " ", $extraOptions );
455 }
456
457 if ( $asArray )
458 {
459 if ( $separateTypes )
460 {
461 $sqlList['tables'][] = $sql . ";";
462 }
463 else
464 {
465 $sqlList[] = $sql . ";";
466 }
467
468 foreach ( $indexes as $index_name => $index_def )
469 {
470 if ( $index_def['type'] != 'primary' )
471 {
472 $sqlList[] = ( $diffFriendly ? '' : ' ' ) . self::generateAddIndexSql( $tableName, $index_name, $index_def, $params, false );
473 }
474 }
475 }
476 else
477 {
478 $sql .= ";\n";
479
480 if ( $mysqlCompatible )
481 {
482 $sql .= "\n\n\n";
483 }
484
485 foreach ( $indexes as $index_name => $index_def )
486 {
487 if ( $index_def['type'] != 'primary' )
488 {
489 $sql .= ( $diffFriendly ? '' : ' ' ) . self::generateAddIndexSql( $tableName, $index_name, $index_def, $params, false ) . "\n\n\n";
490 }
491 }
492 }
493
494 return $asArray ? $sqlList : $sql;
495 }
496
500 function generateDropTable( $table, $params )
501 {
502 return "DROP TABLE $table;\n";
503 }
504
508 function isMultiInsertSupported()
509 {
510 return false;
511 }
512
516 function escapeSQLString( $value )
517 {
518 if ( $this->DBInstance instanceof eZDBInterface )
519 {
520 return $this->DBInstance->escapeString( $value );
521 }
522
523 return $value;
524 }
525
529 function schemaType()
530 {
531 return 'sqlite';
532 }
533
537 function schemaName()
538 {
539 return 'SQLite';
540 }
541 }
542
543 ?>
eZDBInterface
Definition: ezdbinterface.php:20
eZDBSchemaInterface
This class provide interface for DB schema library.
Definition: ezdbschemainterface.php:62
eZDBSchemaInterface\$Schema
$Schema
Definition: ezdbschemainterface.php:1373
eZDBSchemaInterface\transformSchema
transformSchema(&$schema, $toLocal)
Definition: ezdbschemainterface.php:1046
eZSQLiteSchema
Definition: ezsqliteschema.php:4
eZSQLiteSchema\fetchTableFields
fetchTableFields( $table, $params)
Definition: ezsqliteschema.php:55
eZSQLiteSchema\escapeSQLString
escapeSQLString( $value)
Definition: ezsqliteschema.php:516
eZSQLiteSchema\generateAddIndexSql
generateAddIndexSql( $table_name, $index_name, $def, $params, $isEmbedded=false)
Definition: ezsqliteschema.php:214
eZSQLiteSchema\generateTableSQL
generateTableSQL( $tableName, $tableDef, $params, $asArray, $separateTypes=false)
Definition: ezsqliteschema.php:384
eZSQLiteSchema\__construct
__construct( $params)
Definition: ezsqliteschema.php:5
eZSQLiteSchema\generateFieldDef
generateFieldDef( $field_name, $def, &$skip_primary, $params=null)
Definition: ezsqliteschema.php:301
eZSQLiteSchema\schemaType
schemaType()
Definition: ezsqliteschema.php:529
eZSQLiteSchema\schemaName
schemaName()
Definition: ezsqliteschema.php:537
eZSQLiteSchema\isMultiInsertSupported
isMultiInsertSupported()
Definition: ezsqliteschema.php:508
eZSQLiteSchema\generateDropTable
generateDropTable( $table, $params)
Definition: ezsqliteschema.php:500
eZSQLiteSchema\generateTableSQLList
generateTableSQLList( $tableName, $table, $params, $separateTypes)
Definition: ezsqliteschema.php:372
eZSQLiteSchema\parseType
parseType( $type_info, &$length_info)
Definition: ezsqliteschema.php:199
eZSQLiteSchema\schema
schema( $params=array())
Definition: ezsqliteschema.php:13
eZSQLiteSchema\fetchTableIndexes
fetchTableIndexes( $table, $params)
Definition: ezsqliteschema.php:154
eZSQLiteSchema\generateTableSchema
generateTableSchema( $tableName, $table, $params)
Definition: ezsqliteschema.php:363
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.