CmsDbCreator - 2gathr/SimpleDB GitHub Wiki
CmsDbCreator
A class allowing to CmsDb-format a database and to create or alter CmsDb-formatted tables.
Overview
CmsDbCreator {
public static bool formatDatabase (SimpleDb $db)
public static CmsDbCreator createTable (CmsDb $db, string $name)
public static CmsDbCreator alterTable (CmsDb $db, string $name)
public static bool dropTable (CmsDb $db, string $name)
public CmsDbCreator addColumn (string $col_name, string $column_definition[, bool $auto_confirm = false[, int $keys = 0]])
public int addKey (int $types, array $columns)
public int addKey (int $types, string $column)
public CmsDbCreator removeKey (int $key_id)
}
CmsDbCreator::addColumn()
public CmsDbCreator CmsDbCreator::addColumn (string $col_name, string $column_definition[, bool $auto_confirm = false[, int $keys = 0]])
Adds a column to the table.
Parameters
- $col_name The name of the new column. It can only be a combination of letters or digits or the underscore character, that is, a Perl "word". It must not be longer than 28 characters, may not be one of the following: "key", "key_col", "state" and cannot end with "_view".
- $column_definition The definition of the column's data type, default value etc. according to the MySQL documentation (see "column_definition").
- $auto_confirm If set to true, any change to this column will directly be reflected in the CmsDbAdv-formatted table as retrieved with a
SELECTquery, regardless of the change being confirmed or not. - $keys A set of constraints for the column. The possible values are equal to the ones for $types of CmsDbCreator::addKey().
Return Values
Returns the CmsDbAdvCreator object on success in order to allow method chaining.
Examples
$db = new CmsDb ('example.com', 'database', 'user', 'secret');
CmsDbCreator::createTable ($db, 'user')
-> addColumn ('name', 'varchar(50) NOT NULL', true)
-> addColumn ('email', 'varchar(254) NOT NULL', false, CmsDbAdv::KEY_UNIQUE)
-> addColumn ('password', 'varchar(50) NOT NULL', false, CmsDbAdv::KEY_NEW)
-> addColumn ('time_registered', 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP', true);
CmsDbCreator::addKey()
public int CmsDbCreator::addKey (int $types, array $columns)
public int CmsDbCreator::addKey (int $types, string $column)
Adds constraints to the table.
Parameters
-
$types A flag-like combination of one or more constraint types. Following types are available:
CmsDb::KEY_UNIQUEAll cuurent and not yet confirmed values or combinations of values in the specified columns must be unique.CmsDb::KEY_NEWAll values (no matter if confirmed, unconfirmed or undone) must be unique for each ID. This is for example useful for passwords to prevent users from reusing old ones. Only one column is allowed for this constraint type.CmsDb::KEY_DISTINCTAll values ever entered must be unique. Only one column is allowed for this constraint type.
A new type can always be added. Just open a ticket.
A combination of multiple types can be used the following way:
$creator -> addKey (CmsDb::KEY_NEW | CmsDb::KEY_UNIQUE, 'column1')
-
$columns An array containing the names of the columns for which the constraint should be added.
OR
-
$column A single column name.
Return Values
Returns the ID of the added constraint, which can be used to delete the constraint with CmsDbCreator::removeKey().