Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdbschema.php
Go to the documentation of this file.
1 <?php
18 class eZDbSchema
19 {
27 static function instance( $params = false )
28 {
29 if ( is_object( $params ) )
30 {
31 $db = $params;
32 $params = array( 'instance' => $db );
33 }
34
35 if ( !isset( $params['instance'] ) )
36 {
37 $db = eZDB::instance();
38 $params['instance'] = $db;
39 }
40
41 $db = $params['instance'];
42
43 if ( !isset( $params['type'] ) )
44 $params['type'] = $db->databaseName();
45 if ( !isset( $params['schema'] ) )
46 $params['schema'] = false;
47
48 $dbname = $params['type'];
49
50 /* Load the database schema handler INI stuff */
51 $ini = eZINI::instance( 'dbschema.ini' );
52 $schemaPaths = $ini->variable( 'SchemaSettings', 'SchemaPaths' );
53 $schemaHandlerClasses = $ini->variable( 'SchemaSettings', 'SchemaHandlerClasses' );
54
55 /* Check if we have a handler */
56 if ( !isset( $schemaPaths[$dbname] ) or !isset( $schemaHandlerClasses[$dbname] ) )
57 {
58 eZDebug::writeError( "No schema handler for database type: $dbname", __METHOD__ );
59 return false;
60 }
61
62 /* Include the schema file and instantiate it */
63 require_once( $schemaPaths[$dbname] );
64 return new $schemaHandlerClasses[$dbname]( $params );
65 }
66
70 static function read( $filename, $returnArray = false )
71 {
72 $fd = @fopen( $filename, 'rb' );
73 if ( $fd )
74 {
75 $buf = fread( $fd, 100 );
76 fclose( $fd );
77 if ( preg_match( '#^<\?' . "php#", $buf ) )
78 {
79 include( $filename );
80 if ( $returnArray )
81 {
82 $params = array();
83 if ( isset( $schema ) )
84 $params['schema'] = $schema;
85 if ( isset( $data ) )
86 $params['data'] = $data;
87 return $params;
88 }
89 else
90 {
91 return $schema;
92 }
93 }
94 else if ( preg_match( '#a:[0-9]+:{#', $buf ) )
95 {
96 return unserialize( file_get_contents( $filename ) );
97 }
98 else
99 {
100 eZDebug::writeError( "Unknown format for file $filename" );
101 return false;
102 }
103 }
104 return false;
105 }
106
110 static function readArray( $filename )
111 {
112 $schema = false;
113 include( $filename );
114 return $schema;
115 }
116
120 static function generateUpgradeFile( $differences )
121 {
122 $diff = var_export( $differences, true );
123 return ( "<?php \n\$diff = \n" . $diff . ";\nreturn \$diff;\n?>\n" );
124 }
125
129 static function writeUpgradeFile( $differences, $filename )
130 {
131 $fp = @fopen( $filename, 'w' );
132 if ( $fp )
133 {
134 fputs( $fp, eZDbSchema::generateUpgradeFile( $differences ) );
135 fclose( $fp );
136 return true;
137 }
138 else
139 {
140 return false;
141 }
142 }
143
148 static function merge( $schema1, $schema2 )
149 {
150 $merged = $schema1;
151 foreach( $schema2 as $tablename => $tabledef )
152 {
153 if ( $tablename != '_info' )
154 {
155 $merged[$tablename] = $tabledef;
156 }
157 }
158 return $merged;
159 }
160 }
161 ?>
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDbSchema
A factory for schema handlers.
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.