## Apache Zeta Components Manual :: Docs For Class ezcDbSchemaCommonSqlWriter

### DatabaseSchema::ezcDbSchemaCommonSqlWriter

### Class ezcDbSchemaCommonSqlWriter

An abstract class that implements some common functionality required by multiple database backends.

Source for this file: /DatabaseSchema/src/handlers/common_sql_writer.php

### Implements interfaces:

- ezcDbSchemaDbWriter
- ezcDbSchemaDiffDbWriter

Version: | //autogentag//

### Descendants

Child Class | Description
--- | ---
ezcDbSchemaMysqlWriter | Handler for storing database schemas and applying differences that uses MySQL as backend.
ezcDbSchemaPgsqlWriter | Handler for storing database schemas and applying differences that uses PostgreSQL as backend.
ezcDbSchemaSqliteWriter | Handler for storing database schemas and applying differences that uses SQLite as backend.
ezcDbSchemaOracleWriter | Handler for storing database schemas and applying differences that uses Oracle as backend.

### Member Variables

protected array(string) | `$queries` Stores a list of queries that is generated by the various Database writing backends.
protected ezcDbSchema | `$schema` Stores the schema definition where the generators operate on.

### Method Summary

public array(string) | `convertToDDL( $dbSchema )` Returns an array with SQL DDL statements that creates the database definition in $dbSchema
protected abstract void | `generateAddFieldSql( $tableName , $fieldName , $fieldDefinition )` Adds a "alter table" query to add the field $fieldName to $tableName with the definition $fieldDefinition.
protected abstract void | `generateAddIndexSql( $tableName , $indexName , $indexDefinition )` Adds a "alter table" query to add the index $indexName to the table $tableName with definition $indexDefinition to the internal list of queries
protected abstract void | `generateChangeFieldSql( $tableName , $fieldName , $fieldDefinition )` Adds a "alter table" query to change the field $fieldName to $tableName with the definition $fieldDefinition.
protected void | `generateCreateTableSql( $tableName , $tableDefinition )` Adds a "create table" query for the table $tableName with definition $tableDefinition to the internal list of queries.
protected string | `generateCreateTableSqlStatement( $tableName )` Returns a "CREATE TABLE" SQL statement part for the table $tableName.
protected string | `generateDefault( $type , $value )` Returns an appropriate default value for $type with $value.
protected void | `generateDiffSchemaAsSql( )` Generates queries to upgrade an existing database with the changes stored in $this->diffSchema.
protected void | `generateDiffSchemaTableAsSql( $tableName , $tableDiff )` Generates queries to upgrade a the table $tableName with the differences in $tableDiff.
protected abstract void | `generateDropFieldSql( $tableName , $fieldName )` Adds a "alter table" query to drop the field $fieldName from $tableName.
protected abstract void | `generateDropIndexSql( $tableName , $indexName )` Adds a "alter table" query to remote the index $indexName from the table $tableName to the internal list of queries.
protected abstract void | `generateDropTableSql( $tableName )` Adds a "drop table" query for the table $tableName to the internal list of queries.
protected abstract string | `generateFieldSql( $fieldName , $fieldDefinition )` Returns a column definition for $fieldName with definition $fieldDefinition.
protected void | `generateSchemaAsSql( )` Creates SQL DDL statements from a schema definitin.
public int | `getWriterType( )` Returns what type of schema writer this class implements.
public boolean | `isQueryAllowed( $db , $query )` Checks if the query is allowed.
public void | `saveToDb( $db , $dbSchema )` Creates the tables contained in $schema in the database that is related to $db

### Methods

#### convertToDDL

array(string) convertToDDL( ezcDbSchema $dbSchema )

Returns an array with SQL DDL statements that creates the database definition in $dbSchema

Converts the schema definition contained in $dbSchema to DDL SQL. This SQL can be used to create tables in an existing database according to the definition. The SQL queries are returned as an array.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$dbSchema` | ezcDbSchema |

##### Implementation of:

Method | Description
--- | ---
`ezcDbSchemaDbWriter::convertToDDL()` | Returns an array with SQL DDL statements that creates the database definition in $dbSchema

#### generateAddFieldSql

void generateAddFieldSql( string $tableName , string $fieldName , ezcDbSchemaField $fieldDefinition )

Adds a "alter table" query to add the field $fieldName to $tableName with the definition $fieldDefinition.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |
`$fieldName` | string |
`$fieldDefinition` | ezcDbSchemaField |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateAddFieldSql()` | Adds a "alter table" query to add the field $fieldName to $tableName with the definition $fieldDefinition.
`ezcDbSchemaPgsqlWriter::generateAddFieldSql()` | Adds a "alter table" query to add the field $fieldName to $tableName with the definition $fieldDefinition.
`ezcDbSchemaSqliteWriter::generateAddFieldSql()` | Adds a "alter table" query to add the field $fieldName to $tableName with the definition $fieldDefinition.
`ezcDbSchemaOracleWriter::generateAddFieldSql()` | Adds a "alter table" query to add the field $fieldName to $tableName with the definition $fieldDefinition.

#### generateAddIndexSql

void generateAddIndexSql( string $tableName , string $indexName , ezcDbSchemaIndex $indexDefinition )

Adds a "alter table" query to add the index $indexName to the table $tableName with definition $indexDefinition to the internal list of queries

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |
`$indexName` | string |
`$indexDefinition` | ezcDbSchemaIndex |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateAddIndexSql()` | Adds a "alter table" query to add the index $indexName to the table $tableName with definition $indexDefinition to the internal list of queries
`ezcDbSchemaPgsqlWriter::generateAddIndexSql()` | Adds a "alter table" query to add the index $indexName to the table $tableName with definition $indexDefinition to the internal list of queries
`ezcDbSchemaSqliteWriter::generateAddIndexSql()` | Adds a "create index" query to add the index $indexName to the table $tableName with definition $indexDefinition to the internal list of queries
`ezcDbSchemaOracleWriter::generateAddIndexSql()` | Adds a "alter table" query to add the index $indexName to the table $tableName with definition $indexDefinition to the internal list of queries

#### generateChangeFieldSql

void generateChangeFieldSql( string $tableName , string $fieldName , ezcDbSchemaField $fieldDefinition )

Adds a "alter table" query to change the field $fieldName to $tableName with the definition $fieldDefinition.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |
`$fieldName` | string |
`$fieldDefinition` | ezcDbSchemaField |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateChangeFieldSql()` | Adds a "alter table" query to change the field $fieldName to $tableName with the definition $fieldDefinition.
`ezcDbSchemaPgsqlWriter::generateChangeFieldSql()` | Adds a "alter table" query to change the field $fieldName to $tableName with the definition $fieldDefinition.
`ezcDbSchemaSqliteWriter::generateChangeFieldSql()` | Adds a "alter table" query to change the field $fieldName to $tableName with the definition $fieldDefinition.
`ezcDbSchemaOracleWriter::generateChangeFieldSql()` | Adds a "alter table" query to change the field $fieldName to $tableName with the definition $fieldDefinition.

#### generateCreateTableSql

void generateCreateTableSql( string $tableName , ezcDbSchemaTable $tableDefinition )

Adds a "create table" query for the table $tableName with definition $tableDefinition to the internal list of queries.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |
`$tableDefinition` | ezcDbSchemaTable |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateCreateTableSql()` | Adds a "create table" query for the table $tableName with definition $tableDefinition to the internal list of queries.
`ezcDbSchemaPgsqlWriter::generateCreateTableSql()` | Adds a "create table" query for the table $tableName with definition $tableDefinition to the internal list of queries.
`ezcDbSchemaSqliteWriter::generateCreateTableSql()` | Adds a "create table" query for the table $tableName with definition $tableDefinition to the internal list of queries.
`ezcDbSchemaOracleWriter::generateCreateTableSql()` | Adds a "create table" query for the table $tableName with definition $tableDefinition to the internal list of queries.

#### generateCreateTableSqlStatement

string generateCreateTableSqlStatement( string $tableName )

Returns a "CREATE TABLE" SQL statement part for the table $tableName.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateCreateTableSqlStatement()` | Returns a "CREATE TABLE" SQL statement part for the table $tableName.
`ezcDbSchemaPgsqlWriter::generateCreateTableSqlStatement()` | Returns a "CREATE TABLE" SQL statement part for the table $tableName.
`ezcDbSchemaSqliteWriter::generateCreateTableSqlStatement()` | Returns a "CREATE TABLE" SQL statement part for the table $tableName.

#### generateDefault

string generateDefault( string $type , mixed $value )

Returns an appropriate default value for $type with $value.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$type` | string |
`$value` | mixed |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaSqliteWriter::generateDefault()` | Returns an appropriate default value for $type with $value.
`ezcDbSchemaOracleWriter::generateDefault()` | Returns an appropriate default value for $type with $value.

#### generateDiffSchemaAsSql

void generateDiffSchemaAsSql( )

Generates queries to upgrade an existing database with the changes stored in $this->diffSchema.

This method generates queries to migrate a database to a new version with the changes that are stored in the $this->diffSchema property. It will call different subfunctions for the different types of changes, and those functions will add queries to the internal list of queries that is stored in $this->queries.

#### generateDiffSchemaTableAsSql

void generateDiffSchemaTableAsSql( string $tableName , ezcDbSchemaTableDiff $tableDiff )

Generates queries to upgrade a the table $tableName with the differences in $tableDiff.

This method generates queries to migrate a table to a new version with the changes that are stored in the $tableDiff property. It will call different subfunctions for the different types of changes, and those functions will add queries to the internal list of queries that is stored in $this->queries.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |
`$tableDiff` | ezcDbSchemaTableDiff |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateDiffSchemaTableAsSql()` | Generates queries to upgrade a the table $tableName with the differences in $tableDiff.
`ezcDbSchemaPgsqlWriter::generateDiffSchemaTableAsSql()` | Generates queries to upgrade a the table $tableName with the differences in $tableDiff.
`ezcDbSchemaSqliteWriter::generateDiffSchemaTableAsSql()` | Generates queries to upgrade a the table $tableName with the differences in $tableDiff.
`ezcDbSchemaOracleWriter::generateDiffSchemaTableAsSql()` | Generates queries to upgrade a the table $tableName with the differences in $tableDiff.

#### generateDropFieldSql

void generateDropFieldSql( string $tableName , string $fieldName )

Adds a "alter table" query to drop the field $fieldName from $tableName.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |
`$fieldName` | string |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateDropFieldSql()` | Adds a "alter table" query to drop the field $fieldName from $tableName.
`ezcDbSchemaPgsqlWriter::generateDropFieldSql()` | Adds a "alter table" query to drop the field $fieldName from $tableName.
`ezcDbSchemaSqliteWriter::generateDropFieldSql()` | Adds a "alter table" query to drop the field $fieldName from $tableName.
`ezcDbSchemaOracleWriter::generateDropFieldSql()` | Adds a "alter table" query to drop the field $fieldName from $tableName.

#### generateDropIndexSql

void generateDropIndexSql( string $tableName , string $indexName )

Adds a "alter table" query to remote the index $indexName from the table $tableName to the internal list of queries.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |
`$indexName` | string |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateDropIndexSql()` | Adds a "alter table" query to remote the index $indexName from the table $tableName to the internal list of queries.
`ezcDbSchemaPgsqlWriter::generateDropIndexSql()` | Adds a "alter table" query to remove the index $indexName from the table $tableName to the internal list of queries.
`ezcDbSchemaSqliteWriter::generateDropIndexSql()` | Adds a "alter table" query to revote the index $indexName from the table $tableName to the internal list of queries.
`ezcDbSchemaOracleWriter::generateDropIndexSql()` | Adds a "alter table" query to remote the index $indexName from the table $tableName to the internal list of queries.

#### generateDropTableSql

void generateDropTableSql( string $tableName )

Adds a "drop table" query for the table $tableName to the internal list of queries.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$tableName` | string |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateDropTableSql()` | Adds a "drop table" query for the table $tableName to the internal list of queries.
`ezcDbSchemaPgsqlWriter::generateDropTableSql()` | Adds a "drop table" query for the table $tableName to the internal list of queries.
`ezcDbSchemaSqliteWriter::generateDropTableSql()` | Adds a "drop table" query for the table $tableName to the internal list of queries.
`ezcDbSchemaOracleWriter::generateDropTableSql()` | Adds a "drop table" query for the table $tableName to the internal list of queries.

#### generateFieldSql

string generateFieldSql( string $fieldName , ezcDbSchemaField $fieldDefinition )

Returns a column definition for $fieldName with definition $fieldDefinition.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$fieldName` | string |
`$fieldDefinition` | ezcDbSchemaField |

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::generateFieldSql()` | Returns a column definition for $fieldName with definition $fieldDefinition.
`ezcDbSchemaPgsqlWriter::generateFieldSql()` | Returns a column definition for $fieldName with definition $fieldDefinition.
`ezcDbSchemaSqliteWriter::generateFieldSql()` | Returns a column definition for $fieldName with definition $fieldDefinition.
`ezcDbSchemaOracleWriter::generateFieldSql()` | Returns a column definition for $fieldName with definition $fieldDefinition.

#### generateSchemaAsSql

void generateSchemaAsSql( )

Creates SQL DDL statements from a schema definitin.

Loops over the tables in the schema definition in $this->schema and creates SQL SSL statements for this which it stores internally into the $this->queries array.

#### getWriterType

int getWriterType( )

Returns what type of schema writer this class implements.

This method always returns ezcDbSchema::DATABASE

##### Implementation of:

Method | Description
--- | ---
`ezcDbSchemaWriter::getWriterType()` | Returns what type of schema writer this class implements.

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaMysqlWriter::getWriterType()` | Returns what type of schema writer this class implements.
`ezcDbSchemaSqliteWriter::getWriterType()` | Returns what type of schema writer this class implements.

#### isQueryAllowed

boolean isQueryAllowed( $db , string $query )

Checks if the query is allowed.

Perform testing if table exist for DROP TABLE query to avoid stoping execution while try to drop not existent table.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$db` | ezcDbHandler |
`$query` | string |

##### Implementation of:

Method | Description
--- | ---
`ezcDbSchemaDbWriter::isQueryAllowed()` | Checks if the query is allowed.

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaPgsqlWriter::isQueryAllowed()` | Checks if certain query allowed.
`ezcDbSchemaSqliteWriter::isQueryAllowed()` | Checks if certain query allowed.
`ezcDbSchemaOracleWriter::isQueryAllowed()` | Checks if query allowed.

#### saveToDb

void saveToDb( $db , ezcDbSchema $dbSchema )

Creates the tables contained in $schema in the database that is related to $db

This method takes the table definitions from $schema and will create the tables according to this definition in the database that is references by the $db handler. If tables with the same name as contained in the definitions already exist they will be removed and recreated with the new definition.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$db` | ezcDbHandler |
`$dbSchema` | ezcDbSchema |

##### Implementation of:

Method | Description
--- | ---
`ezcDbSchemaDbWriter::saveToDb()` | Creates the tables contained in $schema in the database that is related to $db

##### Redefined in descendants as:

Method | Description
--- | ---
`ezcDbSchemaPgsqlWriter::saveToDb()` | Creates tables defined in $dbSchema in the database referenced by $db.

Documentation generated by phpDocumentor 1.4.3