Zeta Components - high quality PHP components

Zeta Components Manual :: Docs For Class ezcDbSchema

DatabaseSchema::ezcDbSchema

Class ezcDbSchema

ezcDbSchema is the main class for schema operations.

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 );

Example that shows how to make a comparison between a file on disk and a database, and how to apply the changes.

  1.      $xmlSchema = ezcDbSchema::createFromFile( 'xml', 'wanted-schema.xml' );
  2.      $dbSchema = ezcDbSchema::createFromDb( $db );
  3.      $diff = ezcDbSchemaComparator::compareSchemas( $xmlSchema, $dbSchema );
  4.      $diff->applyToDb( $db );

Source for this file: /DatabaseSchema/src/schema.php

Version:   //autogentag//

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 static ezcDbSchemaOptions $options
Contains the options that are used by creating new schemas.
public static array(string) $supportedTypes
A list of all the supported database filed types

Method Summary

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 static void setOptions( $options )
Associates an option object with this static class.
public array(string) convertToDDL( $db )
Returns the $db specific SQL queries that would create the tables defined 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 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

createFromDb

static void createFromDb( $db )

Factory method to create a ezcDbSchema object from the database $db.

Parameters:
Name Type Description
$db ezcDbHandler
Exceptions:
Type Description
ezcDbSchemaInvalidReaderClassException if the handler associated with the $format is not a database schema reader.

createFromFile

static 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
Exceptions:
Type Description
ezcDbSchemaInvalidReaderClassException if the handler associated with the $format is not a file schema reader.

createNewField

static 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

static 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

static ezcDbSchemaIndexField createNewIndexField( [int $sorting = null] )

Returns an object to represent a table's field in the schema.

Parameters:
Name Type Description
$sorting int

createNewTable

static 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)

setOptions

static void setOptions( ezcDbSchemaOptions $options )

Associates an option object with this static class.

Parameters:
Name Type Description
$options ezcDbSchemaOptions

convertToDDL

array(string) convertToDDL( string|ezcDbHandler $db )

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:
Name Type Description
$db string|ezcDbHandler
Exceptions:
Type Description
ezcDbSchemaInvalidWriterClassException if the handler associated with the $format is not a database schema writer.

__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.

writeToDb

void writeToDb( $db )

Creates the tables defined in the schema into the database specified through $db.

Parameters:
Name Type Description
$db ezcDbHandler
Exceptions:
Type 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
Exceptions:
Type Description
ezcDbSchemaInvalidWriterClassException if the handler associated with the $format is not a file schema writer.
Documentation generated by phpDocumentor 1.4.3