Class SchemaDialect
Base class for schema implementations.
This class contains methods that are common across the various SQL dialects.
Provides methods for performing schema reflection. Results will be in the form of structured arrays. The structure of each result will be documented in this class. Subclasses are free to include additional data that is not documented.
Constants
-
stringCURRENT_DATE ¶'CURRENT_DATE'Default value for a date column that should be set to the current date.
-
array<string, string>CURRENT_DATE_TIME_DEFAULTS ¶[TableSchemaInterface::TYPE_DATE => self::CURRENT_DATE, TableSchemaInterface::TYPE_TIME => self::CURRENT_TIME]Column types that accept the
CURRENT_DATE/CURRENT_TIMEdefaults, mapped to their keyword. -
stringCURRENT_TIME ¶'CURRENT_TIME'Default value for a time column that should be set to the current time.
Property Summary
-
$driver protected
Cake\Database\DriverThe driver instance being used.
Method Summary
-
__construct() public
Constructor
This constructor will connect the driver so that methods like columnSql() and others will fail when the driver has not been connected.
-
addConstraintSql() abstract public
Generate the SQL queries needed to add foreign key constraints to the table
-
applyTypeSpecificColumnConversion() protected
Tries to use a matching database type to convert a SQL column definition to an abstract type definition.
-
columnDefinitionSql() abstract public
Create a SQL snippet for a column based on the array shape that
describeColumns()creates. -
columnSql() abstract public
Generate the SQL fragment for a single column in a table.
-
constraintSql() abstract public
Generate the SQL fragments for defining table constraints.
-
convertConstraintColumns() protected
Convert foreign key constraints references to a valid stringified list
-
convertOnClause() protected
Convert string on clauses to the abstract ones.
-
createTableSql() abstract public
Generate the SQL to create a table.
-
currentDateTimeDefault() protected
Get the keyword when a date or time column defaults to
CURRENT_DATEorCURRENT_TIME.Matching is case-insensitive and accepts an empty
()suffix. -
describe() public
Get the column metadata for a table.
The name can include a database schema name in the form 'schema.table'.
-
describeCheckConstraints() public
Get a list of check constraint metadata as an array.
Each item in the array will contain the following keys:
- name - The name of the constraint.
- expression - The check constraint expression as a SQL fragment.
-
describeColumns() abstract public
Get a list of column metadata as a array
Each item in the array will contain the following:
- name : the name of the column.
- type : the abstract type of the column.
- length : the length of the column.
- default : the default value of the column or null.
- null : boolean indicating whether the column can be null.
- comment : the column comment or null.
Additionally the
autoIncrementkey will be set for columns that are a primary key. -
describeForeignKeys() abstract public
Get a list of constraint metadata as a array
Each item in the array will contain the following:
- name : The name of the constraint
- type : the type of the constraint. Generally
foreign. - columns : the columns in the constraint on the.
- references : A list of the table + all columns in the referenced table
- update : The update action or null
- delete : The delete action or null
-
describeIndexes() abstract public
Get a list of index metadata as a array
Each item in the array will contain the following:
- name : the name of the index.
- type : the type of the index. One of
unique,index,primary. - columns : the columns in the index.
- length : the length of the index if applicable.
-
describeOptions() abstract public
Get platform specific options
No keys are guaranteed to be present as they are database driver dependent.
-
dropConstraintSql() abstract public
Generate the SQL queries needed to drop foreign key constraints from the table
-
dropTableSql() public
Generate the SQL to drop a table.
-
foreignOnClause() protected
Generate an ON clause for a foreign key.
-
getTypeSpecificColumnSql() protected
Tries to use a matching database type to generate the SQL fragment for a single column in a table.
-
hasColumn() public
Check if a table has a column with a given name.
-
hasForeignKey() public
Check if a table has a foreign key with a given name.
-
hasIndex() public
Check if a table has an index with a given name.
-
hasTable() public
Check if a table exists
-
indexSql() abstract public
Generate the SQL fragment for a single index in a table.
-
listTables() public
Get the list of tables and views available in the current connection.
-
listTablesSql() abstract protected
Generate the SQL to list the tables.
-
listTablesWithoutViews() public
Get the list of tables, excluding any views, available in the current connection.
-
listTablesWithoutViewsSql() abstract protected
Generate the SQL to list the tables, excluding all views.
-
truncateTableSql() abstract public
Generate the SQL to truncate a table.
Method Detail
__construct() ¶ public
__construct(Cake\Database\Driver $driver)
Constructor
This constructor will connect the driver so that methods like columnSql() and others will fail when the driver has not been connected.
Parameters
-
Cake\Database\Driver$driver The driver to use.
addConstraintSql() ¶ abstract public
addConstraintSql(Cake\Database\Schema\TableSchema $schema): array
Generate the SQL queries needed to add foreign key constraints to the table
Parameters
-
Cake\Database\Schema\TableSchema$schema The table instance the foreign key constraints are.
Returns
arraySQL fragment.
applyTypeSpecificColumnConversion() ¶ protected
applyTypeSpecificColumnConversion(string $columnType, array $definition): array<string, mixed>|null
Tries to use a matching database type to convert a SQL column definition to an abstract type definition.
Parameters
-
string$columnType The column type.
-
array$definition The column definition.
Returns
array<string, mixed>|nullArray of column information, or null
in case no corresponding type was found or the type didn't provide custom column information.
columnDefinitionSql() ¶ abstract public
columnDefinitionSql(array $column): string
Create a SQL snippet for a column based on the array shape
that describeColumns() creates.
Parameters
-
array$column The column metadata
Returns
stringGenerated SQL fragment for a column
columnSql() ¶ abstract public
columnSql(Cake\Database\Schema\TableSchema $schema, string $name): string
Generate the SQL fragment for a single column in a table.
Parameters
-
Cake\Database\Schema\TableSchema$schema The table instance the column is in.
-
string$name The name of the column.
Returns
stringSQL fragment.
constraintSql() ¶ abstract public
constraintSql(Cake\Database\Schema\TableSchema $schema, string $name): string
Generate the SQL fragments for defining table constraints.
Parameters
-
Cake\Database\Schema\TableSchema$schema The table instance the column is in.
-
string$name The name of the column.
Returns
stringSQL fragment.
convertConstraintColumns() ¶ protected
convertConstraintColumns(array<string>|string $references): string
Convert foreign key constraints references to a valid stringified list
Parameters
-
array<string>|string$references The referenced columns of a foreign key constraint statement
Returns
stringconvertOnClause() ¶ protected
convertOnClause(string $clause): string
Convert string on clauses to the abstract ones.
Parameters
-
string$clause The on clause to convert.
Returns
stringcreateTableSql() ¶ abstract public
createTableSql(Cake\Database\Schema\TableSchema $schema, array<string> $columns, array<string> $constraints, array<string> $indexes): array<string>
Generate the SQL to create a table.
Parameters
-
Cake\Database\Schema\TableSchema$schema Table instance.
-
array<string>$columns The columns to go inside the table.
-
array<string>$constraints The constraints for the table.
-
array<string>$indexes The indexes for the table.
Returns
array<string>SQL statements to create a table.
currentDateTimeDefault() ¶ protected
currentDateTimeDefault(array<string, mixed> $column): string|null
Get the keyword when a date or time column defaults to CURRENT_DATE or CURRENT_TIME.
Matching is case-insensitive and accepts an empty () suffix.
Parameters
-
array<string, mixed>$column The column metadata.
Returns
string|nullOne of the CURRENT_* constants, or null when the default is not one of them.
describe() ¶ public
describe(string $name): Cake\Database\Schema\TableSchemaInterface
Get the column metadata for a table.
The name can include a database schema name in the form 'schema.table'.
Parameters
-
string$name The name of the table to describe.
Returns
Cake\Database\Schema\TableSchemaInterfaceObject with column metadata.
Throws
Cake\Database\Exception\DatabaseExceptionwhen table cannot be described.
describeCheckConstraints() ¶ public
describeCheckConstraints(string $tableName): array
Get a list of check constraint metadata as an array.
Each item in the array will contain the following keys:
- name - The name of the constraint.
- expression - The check constraint expression as a SQL fragment.
Parameters
-
string$tableName The name of the table to describe options on.
Returns
arraydescribeColumns() ¶ abstract public
describeColumns(string $tableName): array
Get a list of column metadata as a array
Each item in the array will contain the following:
- name : the name of the column.
- type : the abstract type of the column.
- length : the length of the column.
- default : the default value of the column or null.
- null : boolean indicating whether the column can be null.
- comment : the column comment or null.
Additionally the autoIncrement key will be set for columns that are a primary key.
Parameters
-
string$tableName The name of the table to describe columns on.
Returns
arraydescribeForeignKeys() ¶ abstract public
describeForeignKeys(string $tableName): array
Get a list of constraint metadata as a array
Each item in the array will contain the following:
- name : The name of the constraint
- type : the type of the constraint. Generally
foreign. - columns : the columns in the constraint on the.
- references : A list of the table + all columns in the referenced table
- update : The update action or null
- delete : The delete action or null
Parameters
-
string$tableName The name of the table to describe foreign keys on.
Returns
arraydescribeIndexes() ¶ abstract public
describeIndexes(string $tableName): array
Get a list of index metadata as a array
Each item in the array will contain the following:
- name : the name of the index.
- type : the type of the index. One of
unique,index,primary. - columns : the columns in the index.
- length : the length of the index if applicable.
Parameters
-
string$tableName The name of the table to describe indexes on.
Returns
arraydescribeOptions() ¶ abstract public
describeOptions(string $tableName): array
Get platform specific options
No keys are guaranteed to be present as they are database driver dependent.
Parameters
-
string$tableName The name of the table to describe options on.
Returns
arraydropConstraintSql() ¶ abstract public
dropConstraintSql(Cake\Database\Schema\TableSchema $schema): array
Generate the SQL queries needed to drop foreign key constraints from the table
Parameters
-
Cake\Database\Schema\TableSchema$schema The table instance the foreign key constraints are.
Returns
arraySQL fragment.
dropTableSql() ¶ public
dropTableSql(Cake\Database\Schema\TableSchema $schema): array
Generate the SQL to drop a table.
Parameters
-
Cake\Database\Schema\TableSchema$schema Schema instance
Returns
arraySQL statements to drop a table.
foreignOnClause() ¶ protected
foreignOnClause(string $on): string
Generate an ON clause for a foreign key.
Parameters
-
string$on The on clause
Returns
stringgetTypeSpecificColumnSql() ¶ protected
getTypeSpecificColumnSql(string $columnType, Cake\Database\Schema\TableSchemaInterface $schema, string $column): string|null
Tries to use a matching database type to generate the SQL fragment for a single column in a table.
Parameters
-
string$columnType The column type.
-
Cake\Database\Schema\TableSchemaInterface$schema The table schema instance the column is in.
-
string$column The name of the column.
Returns
string|nullAn SQL fragment, or null in case no corresponding type was found or the type didn't provide
custom column SQL.
hasColumn() ¶ public
hasColumn(string $tableName, string $columnName): bool
Check if a table has a column with a given name.
Parameters
-
string$tableName The name of the table
-
string$columnName The name of the column
Returns
boolhasForeignKey() ¶ public
hasForeignKey(string $tableName, array<string> $columns = [], string $name = null): bool
Check if a table has a foreign key with a given name.
Parameters
-
string$tableName The name of the table
-
array<string>$columns optional The columns in the foreign key. Specific ordering matters.
-
string$name optional The name of the foreign key to match on. Can be used alone, or with $columns to match keys more precisely.
Returns
boolhasIndex() ¶ public
hasIndex(string $tableName, array<string> $columns = [], string $name = null): bool
Check if a table has an index with a given name.
Parameters
-
string$tableName The name of the table
-
array<string>$columns optional The columns in the index. Specific ordering matters.
-
string$name optional The name of the index to match on. Can be used alone, or with $columns to match indexes more precisely.
Returns
boolhasTable() ¶ public
hasTable(string $tableName, string|null $schema = null): bool
Check if a table exists
Parameters
-
string$tableName The name of the table
-
string|null$schema optional The schema look for table in. If null the default schema is used.
Returns
boolindexSql() ¶ abstract public
indexSql(Cake\Database\Schema\TableSchema $schema, string $name): string
Generate the SQL fragment for a single index in a table.
Parameters
-
Cake\Database\Schema\TableSchema$schema The table object the column is in.
-
string$name The name of the column.
Returns
stringSQL fragment.
listTables() ¶ public
listTables(string|null $schema = null): array<string>
Get the list of tables and views available in the current connection.
Parameters
-
string|null$schema optional The schema to get the tables for. If null the default schema is used.
Returns
array<string>The list of tables and views in the connected database/schema.
listTablesSql() ¶ abstract protected
listTablesSql(array<string, mixed> $config): array{0: string, 1: array}
Generate the SQL to list the tables.
Parameters
-
array<string, mixed>$config The connection configuration to use for getting tables from.
Returns
array{0: string, 1: array}An array of (sql, params) to execute.
listTablesWithoutViews() ¶ public
listTablesWithoutViews(): array<string>
Get the list of tables, excluding any views, available in the current connection.
Returns
array<string>The list of tables in the connected database/schema.
listTablesWithoutViewsSql() ¶ abstract protected
listTablesWithoutViewsSql(array<string, mixed> $config): array{0: string, 1: array}
Generate the SQL to list the tables, excluding all views.
Parameters
-
array<string, mixed>$config The connection configuration to use for getting tables from.
Returns
array{0: string, 1: array}An array of (sql, params) to execute.
truncateTableSql() ¶ abstract public
truncateTableSql(Cake\Database\Schema\TableSchema $schema): array
Generate the SQL to truncate a table.
Parameters
-
Cake\Database\Schema\TableSchema$schema Table instance.
Returns
arraySQL statements to truncate a table.