DatabaseSchema: ezcDbSchema
[ ]
[ ]
[ ]
[ ]
[ ]
Class: ezcDbSchema
|
ezcDbSchema is the main class for schema operations. [
source]
ezcDbSchema represents the schema itself and provide proxy methods to the handlers that are able to load/save schemas from/to files, databases or other sources/destinations, depending on available schema handlers.
A database schema is a definition of all the tables inside a database, including field definitions and indexes.
The available builtin handlers are currently for MySQL, XML files and PHP arrays.
The following example shows you how you can load a database schema from the PHP format and store it into the XML format.
1. $schema = ezcDbSchema::createFromFile( 'array', 'file.php' );
2. $schema->writeToFile( 'xml', 'file.xml' );
The following example shows how you can load a database schema from the XML format and store it into a database.
1. $db = ezcDbFactory::create( 'mysql://user:password@host/database' );
2. $schema = ezcDbSchema::createFromFile( 'xml', 'file.php' );
3. $schema->writeToDb( $db );
Constants
DATABASE
= 2
|
Used by reader and writer classes to inform that it implements a database based handler. |
FILE
= 1
|
Used by reader and writer classes to inform that it implements a file based handler. |
Member Variables
|
public ezcDbSchemaOptions |
$options
Contains the options that are used by creating new schemas. |
|
public array(string) |
$supportedTypes
= array( 'integer', 'boolean', 'float', 'decimal', 'timestamp', 'time', 'date', 'text', 'blob', 'clob' )
A list of all the supported database filed types |
Method Summary
|
public array(string) |
convertToDDL(
$db )
Returns the $db specific SQL queries that would create the tables defined in the schema. |
|
public static void |
createFromDb(
$db )
Factory method to create a ezcDbSchema object from the database $db. |
|
public static void |
createFromFile(
$format, $file )
Factory method to create a ezcDbSchema object from the file $file with the format $format. |
|
public static ezcDbSchemaField |
createNewField(
$fieldType, $fieldLength, $fieldNotNull, $fieldDefault, $fieldAutoIncrement, $fieldUnsigned )
Returns an object to represent a table's field in the schema. |
|
public static ezcDbSchemaIndex |
createNewIndex(
$fields, $primary, $unique )
Returns an object to represent a table's field in the schema. |
|
public static ezcDbSchemaIndexField |
createNewIndexField(
[$sorting = null] )
Returns an object to represent a table's field in the schema. |
|
public static ezcDbSchemaTable |
createNewTable(
$fields, $indexes )
Returns an object to represent a table in the schema. |
|
public ezcDbSchema |
__construct(
$schema, [$data = array()] )
Constructs a new ezcDbSchema object with schema definition $schema. |
|
public array |
getData(
)
Returns the internal data. |
|
public array(string=>ezcDbSchemaTable) |
&getSchema(
)
Returns the internal schema by reference. |
|
public static void |
setOptions(
$options )
Associates an option object with this static class. |
|
public void |
writeToDb(
$db )
Creates the tables defined in the schema into the database specified through $db. |
|
public void |
writeToFile(
$format, $file )
Writes the schema to the file $file in format $format. |
Methods
convertToDDL
Returns the $db specific SQL queries that would create the tables defined in the schema.
The database type can be given as both a database handler (instanceof ezcDbHandler) or the name of the database as string as retrieved through calling getName() on the database handler object.
Parameters
Throws
| Class | Description |
ezcDbSchemaInvalidWriterClassException |
if the handler associated with the $format is not a database schema writer. |
See also:
ezcDbHandler::getName().
createFromDb
void createFromDb(
$db )
Factory method to create a ezcDbSchema object from the database $db.
Parameters
Throws
| Class | Description |
ezcDbSchemaInvalidReaderClassException |
if the handler associated with the $format is not a database schema reader. |
createFromFile
void createFromFile(
string
$format, string
$file )
Factory method to create a ezcDbSchema object from the file $file with the format $format.
Parameters
| Name |
Type |
Description |
$format |
string |
|
$file |
string |
|
Throws
| Class | Description |
ezcDbSchemaInvalidReaderClassException |
if the handler associated with the $format is not a file schema reader. |
createNewField
ezcDbSchemaField createNewField(
string
$fieldType, integer
$fieldLength, bool
$fieldNotNull, mixed
$fieldDefault, bool
$fieldAutoIncrement, bool
$fieldUnsigned )
Returns an object to represent a table's field in the schema.
Parameters
| Name |
Type |
Description |
$fieldType |
string |
|
$fieldLength |
integer |
|
$fieldNotNull |
bool |
|
$fieldDefault |
mixed |
|
$fieldAutoIncrement |
bool |
|
$fieldUnsigned |
bool |
|
createNewIndex
ezcDbSchemaIndex createNewIndex(
array(string=>ezcDbSchemaIndexField)
$fields, bool
$primary, bool
$unique )
Returns an object to represent a table's field in the schema.
Parameters
| Name |
Type |
Description |
$fields |
array(string=>ezcDbSchemaIndexField) |
|
$primary |
bool |
|
$unique |
bool |
|
createNewIndexField
Returns an object to represent a table's field in the schema.
Parameters
| Name |
Type |
Description |
$sorting |
int |
|
createNewTable
ezcDbSchemaTable createNewTable(
array(string=>ezcDbSchemaField)
$fields, array(string=>ezcDbSchemaIndex)
$indexes )
Returns an object to represent a table in the schema.
Parameters
| Name |
Type |
Description |
$fields |
array(string=>ezcDbSchemaField) |
|
$indexes |
array(string=>ezcDbSchemaIndex) |
|
__construct
ezcDbSchema __construct(
$schema, [array
$data = array()] )
Constructs a new ezcDbSchema object with schema definition $schema.
Parameters
| Name |
Type |
Description |
$schema |
array(ezcDbSchemaTable) |
|
$data |
array |
|
getData
array getData(
)
Returns the internal data.
This data is not used anywhere though.
getSchema
array(string=>ezcDbSchemaTable) &getSchema(
)
Returns the internal schema by reference.
The method returns an array where the key is the table name, and the value the table definition stored in a ezcDbSchemaTable struct.
setOptions
Associates an option object with this static class.
Parameters
writeToDb
void writeToDb(
$db )
Creates the tables defined in the schema into the database specified through $db.
Parameters
Throws
| Class | Description |
ezcDbSchemaInvalidWriterClassException |
if the handler associated with the $format is not a database schema writer. |
writeToFile
void writeToFile(
string
$format, string
$file )
Writes the schema to the file $file in format $format.
Parameters
| Name |
Type |
Description |
$format |
string |
Available formats are at least: 'array' and 'xml'. |
$file |
string |
|
Throws
| Class | Description |
ezcDbSchemaInvalidWriterClassException |
if the handler associated with the $format is not a file schema writer. |
Last updated: Mon, 30 Mar 2009