Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdbschemainterface.php
Go to the documentation of this file.
1 <?php
61 class eZDBSchemaInterface
62 {
68 public function __construct( $params )
69 {
70 $this->DBInstance = $params['instance'];
71 $this->Schema = false;
72 $this->Data = false;
73 if ( isset( $params['schema'] ) )
74 $this->Schema = $params['schema'];
75 if ( isset( $params['data'] ) )
76 $this->Data = $params['data'];
77 }
78
88 function schema( $params = array() )
89 {
90 }
91
107 function data( $schema = false, $tableNameList = false, $params = array() )
108 {
109 $params = array_merge( array( 'meta_data' => false,
110 'format' => 'generic',
111 'offset' => false,
112 'limit' => false,
113 'force_read' => false ),
114 $params );
115
116 if ( $this->Data === false || $params['force_read'] )
117 {
118 if ( $schema === false )
119 $schema = $this->schema( $params );
120
121 // We need to transform schema to local format for data to be fetched correctly.
122 if ( $schema['_info']['format'] == 'generic' )
123 $this->transformSchema( $schema, true );
124
125 $data = array();
126 foreach ( $schema as $tableName => $tableInfo )
127 {
128 // Skip the information array, this is not a table
129 if ( $tableName == '_info' )
130 continue;
131
132 if ( is_array( $tableNameList ) and
133 !in_array( $tableName, $tableNameList ) )
134 continue;
135
136 $tableEntry = $this->fetchTableData( $tableInfo, $params['offset'], $params['limit'] );
137 if ( count( $tableEntry['rows'] ) > 0 )
138 $data[$tableName] = $tableEntry;
139 }
140 $this->transformData( $data, $params['format'] == 'local' );
141 ksort( $data );
142 $this->Data = $data;
143 }
144 else
145 {
146 $this->transformData( $this->Data, $params['format'] == 'local' );
147 $data = $this->Data;
148 }
149
150 return $data;
151 }
152
159 function validate()
160 {
161 return false;
162 }
163
179 function fetchTableData( $tableInfo, $offset = false, $limit = false )
180 {
181 if ( count( $tableInfo['fields'] ) == 0 )
182 return false;
183
184 $tableName = $tableInfo['name'];
185 $fields = array_keys( $tableInfo['fields'] );
186 $fieldText = implode( ', ', $fields );
187 $rows = $this->DBInstance->arrayQuery( "SELECT $fieldText FROM $tableName", array( 'offset' => $offset, 'limit' => $limit ) );
188 $resultArray = array();
190 foreach ( $rows as $row )
191 {
192 $rowData = array();
193 foreach ( $tableInfo['fields'] as $fieldName => $field )
194 {
195 if ( $field['type'] == 'char' )
196 {
197 $rowData[$fieldName] = str_pad( $row[$fieldName], $field['length'], ' ' );
198 }
199 else
200 {
201 $rowData[$fieldName] = $row[$fieldName];
202 }
203 }
204 $resultArray[] = array_values( $rowData );
205 }
206 return array( 'fields' => $fields,
207 'rows' => $resultArray );
208 }
209
217 function writeUpgradeFile( $differences, $filename, $params = array() )
218 {
219 $params = array_merge( array( 'schema' => true,
220 'data' => false,
221 'allow_multi_insert' => false,
222 'diff_friendly' => false ),
223 $params );
224 $fp = @fopen( $filename, 'w' );
225 if ( $fp )
226 {
227 fputs( $fp, $this->generateUpgradeFile( $differences, $params ) );
228 fclose( $fp );
229 return true;
230 }
231 else
232 {
233 return false;
234 }
235 }
236
243 function writeSQLSchemaFile( $filename, $params = array() )
244 {
245 $params = array_merge( array( 'schema' => true,
246 'data' => false,
247 'allow_multi_insert' => false,
248 'diff_friendly' => false,
249 'offset' => false,
250 'limit' => false ),
251 $params );
252 $includeSchema = $params['schema'];
253 $includeData = $params['data'];
254 $fp = @fopen( $filename, 'w' );
255 if ( $fp )
256 {
257 $schema = $this->schema( $params );
258 $this->transformSchema( $schema, true );
259 if ( $includeSchema )
260 {
261 fputs( $fp, $this->generateSchemaFile( $schema, $params ) );
262 }
263 if ( $includeData )
264 {
265 $data = $this->data( $schema, false, array( 'offset' => $params['offset'], 'limit' => $params['limit'] ) );
266 $this->transformData( $data, true );
267 fputs( $fp, $this->generateDataFile( $schema, $data, $params ) );
268 }
269 fclose( $fp );
270 return true;
271 }
272 else
273 {
274 return false;
275 }
276 }
277
283 function writeSerializedSchemaFile( $filename, $params = array() )
284 {
285 $params = array_merge( array( 'schema' => true,
286 'data' => false,
287 'offset' => false,
288 'limit' => false ),
289 $params );
290 $includeSchema = $params['schema'];
291 $includeData = $params['data'];
292 $fp = @fopen( $filename, 'w' );
293 if ( $fp )
294 {
295 $schema = $this->schema( $params );
296 if ( $includeSchema and $includeData )
297 {
298 fputs( $fp, serialize( array( 'schema' => $schema,
299 'data' => $this->data( $schema, false, array( 'offset' => $params['offset'], 'limit' => $params['limit'] ) ) ) ) );
300 }
301 else if ( $includeSchema )
302 {
303 fputs( $fp, serialize( $schema ) );
304 }
305 else if ( $includeData )
306 {
307 fputs( $fp, serialize( $this->data( $schema, false, array( 'offset' => $params['offset'], 'limit' => $params['limit'] ) ) ) );
308 }
309 fclose( $fp );
310 return true;
311 }
312 else
313 {
314 return false;
315 }
316 }
317
323 function writeArraySchemaFile( $filename, $params = array() )
324 {
325 $params = array_merge( array( 'schema' => true,
326 'data' => false,
327 'offset' => false,
328 'limit' => false ),
329 $params );
330 $includeSchema = $params['schema'];
331 $includeData = $params['data'];
332 $fp = @fopen( $filename, 'w' );
333 if ( $fp )
334 {
335 $schema = $this->schema( $params );
336 fputs( $fp, '<?' . 'php' . "\n" );
337 if ( $includeSchema )
338 {
339 fputs( $fp, "// This array contains the database schema\n" );
340 if ( isset( $schema['_info'] ) )
341 {
342 // push the _info member at bottom of array
343 $info = $schema['_info'];
344 unset( $schema['_info'] );
345 $schema['_info'] = $info;
346 }
347 fputs( $fp, '$schema = ' . var_export( $schema, true ) . ";\n" );
348 }
349 if ( $includeData )
350 {
351 $data = $this->data( $schema, false, array( 'offset' => $params['offset'], 'limit' => $params['limit'] ) );
352 fputs( $fp, "// This array contains the database data\n" );
353 if ( isset( $data['_info'] ) )
354 {
355 // push the _info member at bottom of array
356 $info = $data['_info'];
357 unset( $data['_info'] );
358 $data['_info'] = $info;
359 }
360 fputs( $fp, '$data = ' . var_export( $data, true ) . ";\n" );
361 }
362 fputs( $fp, "\n" . '?>' );
363 fclose( $fp );
364 return true;
365 }
366 else
367 {
368 return false;
369 }
370 }
371
380 function insertSchema( $params = array() )
381 {
382 $params = array_merge( array( 'schema' => true,
383 'data' => false ),
384 $params );
385
386 if ( !is_object( $this->DBInstance ) )
387 {
388 eZDebug::writeError( "No database instance is available, cannot insert", __METHOD__ );
389 return false;
390 }
391
392 $oldOutputSQL = $this->DBInstance->OutputSQL;
393 $this->DBInstance->OutputSQL = false;
394
395 $includeSchema = $params['schema'];
396 $includeData = $params['data'];
397 $params['format'] = 'local';
398 $schema = $this->schema( $params );
399 if ( $includeSchema )
400 {
401 foreach ( $schema as $tableName => $table )
402 {
403 // Skip the information array, this is not a table
404 if ( $tableName == '_info' )
405 continue;
406
407 $sqlList = $this->generateTableSQLList( $tableName, $table, $params, false );
408 foreach ( $sqlList as $sql )
409 {
410 if ( !$this->DBInstance->query( $sql ) )
411 {
412 eZDebug::writeError( "Failed inserting the SQL:\n$sql" );
413 return false;
414 }
415 }
416 }
417 }
418 if ( $includeData )
419 {
420 $data = $this->data( $schema, false, array( 'format' => 'local' ) );
421
422 $this->DBInstance->begin();
423
424 foreach ( $schema as $tableName => $table )
425 {
426 // Skip the information array, this is not a table
427 if ( $tableName == '_info' )
428 continue;
429
430 if ( !isset( $data[$tableName] ) )
431 {
432 continue;
433 }
434
435 $sqlList = $this->generateTableInsertSQLList( $tableName, $table, $data[$tableName], $params, false );
436 foreach ( $sqlList as $sql )
437 {
438 if ( !$this->DBInstance->query( $sql ) )
439 {
440 eZDebug::writeError( "Failed inserting the SQL:\n$sql" );
441 $this->DBInstance->rollback();
442 return false;
443 }
444 }
445 }
446
447 $this->DBInstance->commit();
448
449 // Update sequences for databases that require this
450 if ( method_exists( $this->DBInstance, 'correctSequenceValues' ) )
451 {
452 $status = $this->DBInstance->correctSequenceValues();
453 if ( !$status )
454 return false;
455 }
456 }
457 $this->DBInstance->OutputSQL = $oldOutputSQL;
458 return true;
459 }
460
466 function generateDataFile( $schema, $data, $params )
467 {
468 $params = array_merge( array( 'allow_multi_insert' => false,
469 'diff_friendly' => false ),
470 $params );
471 $sql = '';
472
473 $i = 0;
474 foreach ( $schema as $tableName => $tableDef )
475 {
476 // Skip the info structure, this is not a table
477 if ( $tableName == '_info' )
478 continue;
479
480 if ( !isset( $data[$tableName] ) )
481 continue;
482 if ( $i > 0 )
483 $sql .= "\n\n";
484 $dataEntries = $data[$tableName];
485 $sql .= $this->generateTableInsert( $tableName, $tableDef, $dataEntries, $params );
486 ++$i;
487 }
488
489 return $sql;
490 }
491
497 function generateSchemaFile( $schema, $params = array() )
498 {
499 $sql = '';
500
501 $i = 0;
502 foreach ( $schema as $table => $tableDef )
503 {
504 // Skip the info structure, this is not a table
505 if ( $table == '_info' )
506 continue;
507
508 if ( $i > 0 )
509 $sql .= "\n\n";
510 $sql .= $this->generateTableSchema( $table, $tableDef, $params );
511 ++$i;
512 }
513
514 return $sql;
515 }
516
520 function generateUpgradeFile( $differences, $params = array() )
521 {
522 $params = array_merge( array( 'schema' => true,
523 'data' => false,
524 'allow_multi_insert' => false,
525 'diff_friendly' => false ),
526 $params );
527 $sql = '';
528
529 /* Loop over all 'table_changes' */
530 if ( isset( $differences['table_changes'] ) )
531 {
532 foreach ( $differences['table_changes'] as $table => $table_diff )
533 {
534 if ( isset ( $table_diff['added_fields'] ) )
535 {
536 foreach ( $table_diff['added_fields'] as $field_name => $added_field )
537 {
538 $this->appendSQLComments( $added_field, $sql );
539 $sql .= $this->generateAddFieldSql( $table, $field_name, $added_field, $params );
540 }
541 }
542
543 if ( isset ( $table_diff['changed_fields'] ) )
544 {
545 foreach ( $table_diff['changed_fields'] as $field_name => $changed_field )
546 {
547 $changed_field_def = $changed_field['field-def'];
548 $diffPrams = array_merge( $params, array( 'different-options' => $changed_field['different-options'] ) );
549 $this->appendSQLComments( $changed_field_def, $sql );
550 $sql .= $this->generateAlterFieldSql( $table, $field_name, $changed_field_def, $diffPrams );
551 unset( $diffPrams );
552 }
553 }
554 if ( isset ( $table_diff['removed_fields'] ) )
555 {
556 foreach ( $table_diff['removed_fields'] as $field_name => $removed_field)
557 {
558 $this->appendSQLComments( $removed_field, $sql );
559 $sql .= $this->generateDropFieldSql( $table, $field_name, $params );
560 }
561 }
562
563 if ( isset ( $table_diff['removed_indexes'] ) )
564 {
565 foreach ( $table_diff['removed_indexes'] as $index_name => $removed_index)
566 {
567 $this->appendSQLComments( $removed_index, $sql );
568 $sql .= $this->generateDropIndexSql( $table, $index_name, $removed_index, $params );
569 }
570 }
571 if ( isset ( $table_diff['added_indexes'] ) )
572 {
573 foreach ( $table_diff['added_indexes'] as $index_name => $added_index)
574 {
575 $this->appendSQLComments( $added_index, $sql );
576 $sql .= $this->generateAddIndexSql( $table, $index_name, $added_index, $params );
577 }
578 }
579
580 if ( isset ( $table_diff['changed_indexes'] ) )
581 {
582 foreach ( $table_diff['changed_indexes'] as $index_name => $changed_index )
583 {
584 //eZDebug::writeDebug( $changed_index, "changed index $index_name" );
585 $this->appendSQLComments( $changed_index, $sql );
586 $sql .= $this->generateDropIndexSql( $table, $index_name, $changed_index, $params );
587 $sql .= $this->generateAddIndexSql( $table, $index_name, $changed_index, $params );
588 //eZDebug::writeDebug( 'qqq' );
589 }
590 }
591 }
592 }
593 if ( isset( $differences['new_tables'] ) )
594 {
595 foreach ( $differences['new_tables'] as $table => $table_def )
596 {
597 $this->appendSQLComments( $table_def, $sql );
598 $sql .= $this->generateTableSchema( $table, $table_def, $params );
599 }
600 }
601 if ( isset( $differences['removed_tables'] ) )
602 {
603 foreach ( $differences['removed_tables'] as $table => $table_def )
604 {
605 $this->appendSQLComments( $table_def, $sql );
606 $sql .= $this->generateDropTable( $table, $params );
607 }
608 }
609 return $sql;
610 }
611
635 function generateTableSQLList( $tableName, $table, $params, $separateTypes )
636 {
637 return false;
638 }
639
651 function generateTableSchema( $tableName, $table, $params )
652 {
653 return false;
654 }
655
661 function generateTableInsert( $tableName, $tableDef, $dataEntries, $params )
662 {
663 return join( "\n", $this->generateTableInsertSQLList( $tableName, $tableDef, $dataEntries, $params, true ) );
664 }
665
669 function generateTableInsertSQLList( $tableName, $tableDef, $dataEntries, $params, $withClosure = true )
670 {
671 $diffFriendly = isset( $params['diff_friendly'] ) ? $params['diff_friendly'] : false;
672 $multiInsert = ( isset( $params['allow_multi_insert'] ) and $params['allow_multi_insert'] ) ? $this->isMultiInsertSupported() : false;
673
674 // Make sure we don't generate SQL when there are no rows
675 if ( count( $dataEntries['rows'] ) == 0 )
676 return '';
677
678 $sqlList = array();
679 $sql = '';
680 $defText = '';
681 $entryIndex = 0;
682 foreach ( $dataEntries['fields'] as $fieldName )
683 {
684 if ( !isset( $tableDef['fields'][$fieldName] ) )
685 continue;
686 if ( $entryIndex == 0 )
687 {
688 if ( $diffFriendly )
689 {
690 $defText .= " ";
691 }
692 }
693 else
694 {
695 if ( $diffFriendly )
696 {
697 $defText .= ",\n ";
698 }
699 else
700 {
701 $defText .= ", ";
702 }
703 }
704 $defText .= $fieldName;
705 ++$entryIndex;
706 }
707
708 if ( $multiInsert )
709 {
710 if ( $diffFriendly )
711 {
712 $sql .= "INSERT INTO $tableName (\n $defText\n)\nVALUES\n";
713 }
714 else
715 {
716 $sql .= "INSERT INTO $tableName ($defText) VALUES ";
717 }
718 }
719 $insertIndex = 0;
720 foreach ( $dataEntries['rows'] as $row )
721 {
722 if ( $multiInsert and $insertIndex > 0 )
723 {
724 if ( $diffFriendly )
725 $sql .= "\n,\n";
726 else
727 $sql .= ", ";
728 }
729 $dataText = '';
730 $entryIndex = 0;
731 foreach ( $dataEntries['fields'] as $fieldName )
732 {
733 if ( !isset( $tableDef['fields'][$fieldName] ) )
734 continue;
735 if ( $entryIndex == 0 )
736 {
737 if ( $diffFriendly )
738 {
739 $dataText .= " ";
740 }
741 }
742 else
743 {
744 if ( $diffFriendly )
745 {
746 $dataText .= ",\n ";
747 }
748 else
749 {
750 $dataText .= ",";
751 }
752 }
753 $dataText .= $this->generateDataValueTextSQL( $tableDef['fields'][$fieldName], $row[$entryIndex] );
754 ++$entryIndex;
755 }
756 if ( $multiInsert )
757 {
758 if ( $diffFriendly )
759 {
760 $sql .= "(\n $dataText\n)";
761 }
762 else
763 {
764 $sql .= "($dataText)";
765 }
766 ++$insertIndex;
767 }
768 else
769 {
770 if ( $diffFriendly )
771 {
772 $sqlList[] = "INSERT INTO $tableName (\n$defText\n) VALUES (\n$dataText\n)" . ( $withClosure ? ";" : "" );
773 }
774 else
775 {
776 $sqlList[] = "INSERT INTO $tableName ($defText) VALUES ($dataText)" . ( $withClosure ? ";" : "" );
777 }
778 }
779 }
780 if ( $multiInsert )
781 {
782 if ( $withClosure )
783 $sql .= "\n;";
784 $sqlList[] = $sql;
785 }
786 return $sqlList;
787 }
788
792 function generateDataValueTextSQL( $fieldDef, $value )
793 {
794 if ( $fieldDef['type'] == 'auto_increment' or
795 $fieldDef['type'] == 'int' or
796 $fieldDef['type'] == 'float' )
797 {
798 if ( $value === null or
799 $value === false )
800 return "NULL";
801 $value = (int)$value;
802 $value = (string)$value;
803 return $value;
804 }
805 else if ( is_string( $value ) )
806 {
807 return "'" . $this->escapeSQLString( $value ) . "'";
808 }
809 else
810 {
811 if ( $value === null or
812 $value === false )
813 return "NULL";
814 return (string)$value;
815 }
816 }
817
823 function escapeSQLString( $value )
824 {
825 return $value;
826 }
827
832 function generateAlterFieldSql( $table_name, $field_name, $def, $params )
833 {
834 }
835
840 function generateAddFieldSql( $table_name, $field_name, $def, $params )
841 {
842 }
843
847 function generateDropFieldSql( $table_name, $field_name, $params )
848 {
849 $sql = "ALTER TABLE $table_name DROP COLUMN $field_name";
850
851 return $sql . ";\n";
852 }
853
858 function appendSQLComments( $def, &$sql )
859 {
860 if ( isset( $def['comments'] ) )
861 {
862 if ( count( $def['comments'] ) > 0 )
863 $sql .= "\n";
864 foreach ( $def['comments'] as $comment )
865 {
866 $commentLines = explode( "\n", $comment );
867 foreach ( $commentLines as $commentLine )
868 {
869 $sql .= '-- ' . $commentLine . "\n";
870 }
871 }
872 return true;
873 }
874 return false;
875 }
876
882 function isMultiInsertSupported()
883 {
884 return false;
885 }
886
894 function schemaType()
895 {
896 }
897
905 function schemaName()
906 {
907 }
908
914 function loadSchemaTransformationRules( $schemaType )
915 {
916 $ini = eZINI::instance( 'dbschema.ini' );
917
918 if ( !$ini )
919 {
920 eZDebug::writeError( "Error loading $schemaType schema transformation rules" );
921 return false;
922 }
923
924 $transformationRules = array();
925
926 if ( $ini->hasVariable( $schemaType, 'ColumnNameTranslation' ) )
927 $transformationRules['column-name'] = $ini->variable( $schemaType, 'ColumnNameTranslation' );
928
929 if ( $ini->hasVariable( $schemaType, 'ColumnTypeTranslation' ) )
930 {
931 $transformationRules['column-type'] = $ini->variable( $schemaType, 'ColumnTypeTranslation' );
932
933 // substitute values like "type1;type2" with an appropriate arrays
934 if ( is_array( $transformationRules['column-type'] ) )
935 {
936 foreach ( $transformationRules['column-type'] as $key => $val )
937 {
938 $types = explode( ';', $val );
939 $transformationRules['column-type'][$key] = $types;
940 }
941 }
942 }
943
944 $indexTranslations = array();
945 if ( $ini->hasVariable( $schemaType, 'IndexTranslation' ) )
946 {
947 $translations = $ini->variable( $schemaType, 'IndexTranslation' );
948 foreach ( $translations as $combinedName => $translation )
949 {
950 list( $tableName, $indexName ) = explode( '.', $combinedName );
951 $indexTranslations[$tableName][$indexName] = array();
952 $fields = explode( ';', $translation );
953 foreach ( $fields as $field )
954 {
955 $entries = explode( '.', $field );
956 $fieldName = $entries[0];
957 $fieldData = array();
958 for ( $i = 1; $i < count( $entries ); ++$i )
959 {
960 list( $metaName, $metaValue ) = explode( '/', $entries[$i], 2 );
961 if ( is_numeric( $metaValue ) )
962 $metaValue = (int)$metaValue;
963 $fieldData[$metaName] = $metaValue;
964 }
965 $indexTranslations[$tableName][$indexName][$fieldName] = $fieldData;
966 }
967 }
968 }
969 $transformationRules['index-field'] = $indexTranslations;
970
971 $tableTranslations = array();
972 if ( $ini->hasVariable( $schemaType, 'TableOptionTranslation' ) )
973 {
974 $translations = $ini->variable( $schemaType, 'TableOptionTranslation' );
975 foreach ( $translations as $tableName => $optionTexts )
976 {
977 $tableTranslations[$tableName] = array();
978 $options = explode( ';', $optionTexts );
979 $optionData = array();
980 foreach ( $options as $option )
981 {
982 list( $metaName, $metaValue ) = explode( '/', $option, 2 );
983 if ( is_numeric( $metaValue ) )
984 $metaValue = (int)$metaValue;
985 $optionData[$metaName] = $metaValue;
986 $tableTranslations[$tableName] = $optionData;
987 }
988 }
989 }
990 $transformationRules['table-option'] = $tableTranslations;
991
992 if ( $ini->hasVariable( $schemaType, 'IndexNameTranslation' ) )
993 {
994 $tmpIdxNameTranslations = $ini->variable( $schemaType, 'IndexNameTranslation' );
995
996 if ( is_array( $tmpIdxNameTranslations ) )
997 {
998 foreach ( $tmpIdxNameTranslations as $key => $val )
999 {
1000 list( $tableName, $genericIdxName ) = explode( '.', $key );
1001 $localIdxName = $val;
1002 if ( !$tableName || !$genericIdxName || !$localIdxName )
1003 {
1004 eZDebug::writeWarning( "Malformed index name translation rule: $key => $val" );
1005 continue;
1006 }
1007 $transformationRules['index-name'][] = array( $tableName, $genericIdxName, $localIdxName );
1008
1009 }
1010 }
1011 unset( $tmpIdxNameTranslations );
1012 }
1013
1014 if ( $ini->hasVariable( $schemaType, 'ColumnOptionTranslations' ) )
1015 {
1016 $transformationRules['column-option'] = array();
1017 foreach( $ini->variable( $schemaType, 'ColumnOptionTranslations' ) as $key => $val )
1018 {
1019 list( $tableName, $colName ) = explode( '.', $key );
1020 $colOptOverride = $val;
1021 if ( !$tableName || !$colName || !$colOptOverride )
1022 {
1023 eZDebug::writeWarning( "Malformed column option translation rule: $key => $val" );
1024 continue;
1025 }
1026 $transformationRules['column-option'][] = array( $tableName, $colName, $colOptOverride );
1027 }
1028 }
1029
1030 // prevent PHP warnings when cycling through the rules
1031 foreach ( array( 'column-name', 'column-type', 'column-option', 'index-name' ) as $rulesType )
1032 {
1033 if( !isset( $transformationRules[$rulesType] ) )
1034 $transformationRules[$rulesType] = array();
1035 }
1036
1037 return $transformationRules;
1038 }
1039
1046 function transformSchema( &$schema, /* bool */ $toLocal )
1047 {
1048 // Check if it is already in correct format
1049 if ( isset( $schema['_info']['format'] ) )
1050 {
1051 if ( $schema['_info']['format'] == ( $toLocal ? 'local' : 'generic' ) )
1052 return true;
1053 }
1054
1055 // Set the new format it will get
1056 $schema['_info']['format'] = $toLocal ? 'local' : 'generic';
1057
1058 // load the schema transformation rules
1059 $schemaType = $this->schemaType();
1060 $schemaTransformationRules = eZDBSchemaInterface::loadSchemaTransformationRules( $schemaType );
1061 if ( $schemaTransformationRules === false )
1062 return false;
1063
1064 // transform column names
1065 foreach ( $schemaTransformationRules['column-name'] as $key => $val )
1066 {
1067 list( $tableName, $genericColName ) = explode( '.', $key );
1068 $localColName = $val;
1069
1070 if ( $toLocal )
1071 {
1072 $searchColName = $genericColName;
1073 $replacementColName = $localColName;
1074 }
1075 else
1076 {
1077 $searchColName = $localColName;
1078 $replacementColName = $genericColName;
1079 }
1080
1081 if ( !isset( $schema[$tableName] ) )
1082 continue;
1083
1084 // transform column names in tables
1085 if ( isset( $schema[$tableName]['fields'][$searchColName] ) )
1086 {
1087 $schema[$tableName]['fields'][$replacementColName] = $schema[$tableName]['fields'][$searchColName];
1088 unset( $schema[$tableName]['fields'][$searchColName] );
1089 ksort( $schema[$tableName]['fields'] );
1090 eZDebugSetting::writeDebug( 'lib-dbschema-transformation', '',
1091 "transformed table column name $tableName.$searchColName to $replacementColName" );
1092 }
1093
1094
1095 // transform column names in indexes
1096 foreach ( $schema[$tableName]['indexes'] as $indexName => $indexSchema )
1097 {
1098 if ( ( $key = array_search( $searchColName, $indexSchema['fields'] ) ) !== false )
1099 {
1100 $schema[$tableName]['indexes'][$indexName]['fields'][$key] = $replacementColName;
1101 eZDebugSetting::writeDebug( 'lib-dbschema-transformation', '',
1102 "transformed index columnn name $indexName.$searchColName to $replacementColName" );
1103 }
1104 }
1105 }
1106
1107 // tranform column types
1108 foreach ( $schemaTransformationRules['column-type'] as $key => $val )
1109 {
1110 list( $tableName, $colName ) = explode( '.', $key );
1111 list( $genericType, $localType ) = $val;
1112
1113 if ( !isset( $schema[$tableName] ) )
1114 continue;
1115
1116 preg_match( '/(\w+)\‍((\d+)\‍)/', $localType, $matches );
1117 $localLength = ( count($matches) == 3 ) ? $matches[2] : null;
1118 if ( count($matches) == 3 )
1119 $localType = $matches[1];
1120
1121 preg_match( '/(\w+)\‍((\d+)\‍)/', $genericType, $matches );
1122 $genericLength = ( count($matches) == 3 ) ? $matches[2] : null;
1123 if ( count($matches) == 3 )
1124 $genericType = $matches[1];
1125
1126 if ( !isset( $schema[$tableName]['fields'][$colName] ) )
1127 continue;
1128
1129 $fieldSchema = $schema[$tableName]['fields'][$colName];
1130
1131 if ( $toLocal )
1132 {
1133 $searchType = $genericType;
1134 $searchLength = $genericLength;
1135 $replacementType = $localType;
1136 $replacementLength = $localLength;
1137 }
1138 else // to generic
1139 {
1140 $searchType = $localType;
1141 $searchLength = $localLength;
1142 $replacementType = $genericType;
1143 $replacementLength = $genericLength;
1144 }
1145
1146 $fieldSchema['type'] = $replacementType;
1147 if ( $replacementLength !== null )
1148 {
1149 $fieldSchema['length'] = $replacementLength;
1150 }
1151 else
1152 {
1153 unset( $fieldSchema['length'] );
1154 }
1155
1156 eZDebugSetting::writeDebug( 'lib-dbschema-transformation', '',
1157 "transformed table column type $schemaType:$tableName.$colName from $searchType to $replacementType" );
1158
1159 $schema[$tableName]['fields'][$colName] = $fieldSchema;
1160 }
1161
1162 // Find indexes that needs to be fixed
1163 foreach ( $schemaTransformationRules['index-field'] as $tableName => $indexes )
1164 {
1165 foreach ( $indexes as $indexName => $fields )
1166 {
1167 if ( !isset( $schema[$tableName]['indexes'][$indexName]['fields'] ) )
1168 continue;
1169
1170 $newFields = array();
1171 foreach ( $schema[$tableName]['indexes'][$indexName]['fields'] as $indexField )
1172 {
1173 if ( !is_array( $indexField ) )
1174 {
1175 $indexField = array( 'name' => $indexField );
1176 }
1177 $fieldName = $indexField['name'];
1178 if ( isset( $fields[$fieldName] ) )
1179 {
1180 if ( $toLocal )
1181 {
1182 $indexField = array_merge( $indexField,
1183 $fields[$fieldName] );
1184 }
1185 else
1186 {
1187 foreach ( $fields[$fieldName] as $removeName => $removeValue )
1188 {
1189 unset( $indexField[$removeName] );
1190 }
1191 }
1192 }
1193
1194 // Check if we have any entries other than 'name', if not we skip the array definition
1195 if ( count( array_diff( array_keys( $indexField ), array( 'name' ) ) ) == 0 )
1196 {
1197 $indexField = $indexField['name'];
1198 }
1199 $newFields[] = $indexField;
1200 }
1201 $schema[$tableName]['indexes'][$indexName]['fields'] = $newFields;
1202 }
1203 }
1204
1205 // Find tables that needs to fix their options
1206 foreach ( $schemaTransformationRules['table-option'] as $tableName => $options )
1207 {
1208 if ( !isset( $schema[$tableName] ) )
1209 continue;
1210
1211 if ( !isset( $schema[$tableName]['options'] ) )
1212 {
1213 if ( $toLocal )
1214 $schema[$tableName]['options'] = $options;
1215 }
1216 else
1217 {
1218 if ( $toLocal )
1219 {
1220 $schema[$tableName]['options'] = array_merge( $schema[$tableName]['options'], $options );
1221 }
1222 else
1223 {
1224 foreach ( $options as $optionName => $optionValue )
1225 {
1226 unset( $schema[$tableName]['options'][$optionName] );
1227 }
1228 }
1229 }
1230 }
1231
1232 // Transform index names
1233 foreach ( $schemaTransformationRules['index-name'] as $idxTransRule )
1234 {
1235 list( $tableName, $genericIdxName, $localIdxName ) = $idxTransRule;
1236
1237 if ( $toLocal )
1238 {
1239 $searchIdxName = $genericIdxName;
1240 $replacementIdxName = $localIdxName;
1241 }
1242 else
1243 {
1244 $searchIdxName = $localIdxName;
1245 $replacementIdxName = $genericIdxName;
1246 }
1247
1248 if ( !isset( $schema[$tableName] ) )
1249 continue;
1250
1251 if ( isset( $schema[$tableName]['indexes'][$searchIdxName] ) )
1252 {
1253 eZDebugSetting::writeDebug( 'lib-dbschema-transformation', '',
1254 "replaced $tableName.$searchIdxName => $replacementIdxName" );
1255 $schema[$tableName]['indexes'][$replacementIdxName] = $schema[$tableName]['indexes'][$searchIdxName];
1256 unset( $schema[$tableName]['indexes'][$searchIdxName] );
1257 ksort( $schema[$tableName]['indexes'] );
1258 }
1259
1260 }
1261
1262 // Transform table column options
1263 foreach ( $schemaTransformationRules['column-option'] as $colOptTransRule )
1264 {
1265 list( $tableName, $colName, $colOptOverride ) = $colOptTransRule;
1266
1267 if ( !isset( $schema[$tableName] ) || !isset( $schema[$tableName]['fields'][$colName] ) )
1268 continue;
1269
1270 $fieldSchema = $schema[$tableName]['fields'][$colName];
1271
1272 switch ( $colOptOverride )
1273 {
1274 case 'null':
1275 {
1276 if ( $toLocal )
1277 {
1278 // remove "NOT NULL" requirement
1279 unset( $fieldSchema['not_null'] );
1280 eZDebugSetting::writeDebug( 'lib-dbschema-transformation', '',
1281 "transformed table column option: $schemaType:$tableName.$colName set to NULL" );
1282 }
1283 else
1284 {
1285 // add "NOT NULL" requirement
1286 $fieldSchema['not_null'] = '1';
1287 eZDebugSetting::writeDebug( 'lib-dbschema-transformation', '',
1288 "transformed table column option: $schemaType:$tableName.$colName set to NOT NULL" );
1289 }
1290
1291 // FIXME: ugly hack preserving keys order in the field schema array, just to be diff-friendly
1292 {
1293 $tmp = $fieldSchema['default'];
1294 unset( $fieldSchema['default'] );
1295 $fieldSchema['default'] = $tmp;
1296 }
1297 } break;
1298 default:
1299 {
1300 eZDebug::writeWarning( "Column option override '$colOptOverride' is not supported" );
1301 } break;
1302 }
1303
1304 $schema[$tableName]['fields'][$colName] = $fieldSchema;
1305 }
1306
1307
1308 return true;
1309 }
1310
1317 function transformData( &$data, /* bool */ $toLocal )
1318 {
1319 // Check if it is already in correct format
1320 if ( isset( $data['_info']['format'] ) )
1321 {
1322 if ( $data['_info']['format'] == ( $toLocal ? 'local' : 'generic' ) )
1323 return true;
1324 }
1325
1326 // Set the new format it will get
1327 $data['_info']['format'] = $toLocal ? 'local' : 'generic';
1328
1329 // load the schema transformation rules
1330 $schemaType = $this->schemaType();
1331 $schemaTransformationRules = eZDBSchemaInterface::loadSchemaTransformationRules( $schemaType );
1332 if ( $schemaTransformationRules === false )
1333 return false;
1334
1335 // transform column names
1336 foreach ( $schemaTransformationRules['column-name'] as $key => $val )
1337 {
1338 list( $tableName, $genericColName ) = explode( '.', $key );
1339 $localColName = $val;
1340
1341 if ( $toLocal )
1342 {
1343 $searchColName = $genericColName;
1344 $replacementColName = $localColName;
1345 }
1346 else
1347 {
1348 $searchColName = $localColName;
1349 $replacementColName = $genericColName;
1350 }
1351
1352 if ( !isset( $data[$tableName] ) )
1353 continue;
1354
1355 // transform column names in tables
1356 $fieldsData = $data[$tableName]['fields'];
1357 foreach ( $fieldsData as $key => $fieldName )
1358 {
1359 if ( $searchColName == $fieldName )
1360 {
1361 $data[$tableName]['fields'][$key] = $replacementColName;
1362 eZDebugSetting::writeDebug( 'lib-dbschema-data-transformation', '',
1363 "transformed table column name $tableName.$searchColName to $replacementColName" );
1364 }
1365 }
1366 }
1367
1368 return true;
1369 }
1370
1372 public $DBInstance;
1373 public $Schema;
1374 public $Data;
1375 }
1376
1377 ?>
eZDBSchemaInterface
This class provide interface for DB schema library.
Definition: ezdbschemainterface.php:62
eZDBSchemaInterface\generateDataFile
generateDataFile( $schema, $data, $params)
Definition: ezdbschemainterface.php:466
eZDBSchemaInterface\writeSQLSchemaFile
writeSQLSchemaFile( $filename, $params=array())
Definition: ezdbschemainterface.php:243
eZDBSchemaInterface\__construct
__construct( $params)
Definition: ezdbschemainterface.php:68
eZDBSchemaInterface\transformData
transformData(&$data, $toLocal)
Definition: ezdbschemainterface.php:1317
eZDBSchemaInterface\generateTableInsertSQLList
generateTableInsertSQLList( $tableName, $tableDef, $dataEntries, $params, $withClosure=true)
Definition: ezdbschemainterface.php:669
eZDBSchemaInterface\$Data
$Data
Definition: ezdbschemainterface.php:1374
eZDBSchemaInterface\generateTableSQLList
generateTableSQLList( $tableName, $table, $params, $separateTypes)
Definition: ezdbschemainterface.php:635
eZDBSchemaInterface\validate
validate()
Definition: ezdbschemainterface.php:159
eZDBSchemaInterface\generateUpgradeFile
generateUpgradeFile( $differences, $params=array())
Definition: ezdbschemainterface.php:520
eZDBSchemaInterface\writeSerializedSchemaFile
writeSerializedSchemaFile( $filename, $params=array())
Definition: ezdbschemainterface.php:283
eZDBSchemaInterface\schemaType
schemaType()
Definition: ezdbschemainterface.php:894
eZDBSchemaInterface\$Schema
$Schema
Definition: ezdbschemainterface.php:1373
eZDBSchemaInterface\generateAddFieldSql
generateAddFieldSql( $table_name, $field_name, $def, $params)
Definition: ezdbschemainterface.php:840
eZDBSchemaInterface\isMultiInsertSupported
isMultiInsertSupported()
Definition: ezdbschemainterface.php:882
eZDBSchemaInterface\schema
schema( $params=array())
Definition: ezdbschemainterface.php:88
eZDBSchemaInterface\writeUpgradeFile
writeUpgradeFile( $differences, $filename, $params=array())
Definition: ezdbschemainterface.php:217
eZDBSchemaInterface\generateTableSchema
generateTableSchema( $tableName, $table, $params)
Definition: ezdbschemainterface.php:651
eZDBSchemaInterface\generateDataValueTextSQL
generateDataValueTextSQL( $fieldDef, $value)
Definition: ezdbschemainterface.php:792
eZDBSchemaInterface\generateAlterFieldSql
generateAlterFieldSql( $table_name, $field_name, $def, $params)
Definition: ezdbschemainterface.php:832
eZDBSchemaInterface\generateTableInsert
generateTableInsert( $tableName, $tableDef, $dataEntries, $params)
Definition: ezdbschemainterface.php:661
eZDBSchemaInterface\transformSchema
transformSchema(&$schema, $toLocal)
Definition: ezdbschemainterface.php:1046
eZDBSchemaInterface\insertSchema
insertSchema( $params=array())
Definition: ezdbschemainterface.php:380
eZDBSchemaInterface\generateDropFieldSql
generateDropFieldSql( $table_name, $field_name, $params)
Definition: ezdbschemainterface.php:847
eZDBSchemaInterface\data
data( $schema=false, $tableNameList=false, $params=array())
Definition: ezdbschemainterface.php:107
eZDBSchemaInterface\appendSQLComments
appendSQLComments( $def, &$sql)
Definition: ezdbschemainterface.php:858
eZDBSchemaInterface\fetchTableData
fetchTableData( $tableInfo, $offset=false, $limit=false)
Definition: ezdbschemainterface.php:179
eZDBSchemaInterface\schemaName
schemaName()
Definition: ezdbschemainterface.php:905
eZDBSchemaInterface\$DBInstance
$DBInstance
eZDB instance
Definition: ezdbschemainterface.php:1372
eZDBSchemaInterface\escapeSQLString
escapeSQLString( $value)
Definition: ezdbschemainterface.php:823
eZDBSchemaInterface\generateSchemaFile
generateSchemaFile( $schema, $params=array())
Definition: ezdbschemainterface.php:497
eZDBSchemaInterface\writeArraySchemaFile
writeArraySchemaFile( $filename, $params=array())
Definition: ezdbschemainterface.php:323
eZDBSchemaInterface\loadSchemaTransformationRules
loadSchemaTransformationRules( $schemaType)
Definition: ezdbschemainterface.php:914
eZDebugSetting\writeDebug
static writeDebug( $conditionName, $string, $label="")
Definition: ezdebugsetting.php:127
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.