Schema
+ Inheritance diagram for Schema:

Description

Schema control and metadata.

The column definition constants are two bytes each, used in bitwise composition.

  • The high-byte (<I_CONST>) is used for the specific primary index type.
  • The low-byte (<T_CONST>) is used for the specific storage type.
    • The final bit 0x01 flags NOT NULL
  • The literal values may change in the future, do not hard code them.
  • The values may expand to use a total of 4 or 8 bytes to accommodate more stuff.

Definition constants are never returned by this class' methods. The methods can only receive them.

Public Properties

const DATETIME_FORMAT = 'Y-m-d H:i:s'
 
const I_PRIMARY = 0xfe00
 <I_CONST>: One or more columns compose the primary key. More...
 
const T_AUTOINCREMENT = self::I_AUTOINCREMENT | self::T_INT
 <T_CONST>: Column is the primary key and auto-increments (8-byte signed integer). More...
 
const T_BLOB = 0x03
 <T_CONST>: Arbitrary binary data up to 4GiB. More...
 
const T_BLOB_NULL = 0x02
 
const T_BOOL = 0xff
 <T_CONST>: Boolean analog (numeric). More...
 
const T_BOOL_NULL = 0xfe
 
const T_CONST_NAMES
 Maps storage types to T_CONST names. More...
 
const T_DATETIME = 0xf9
 <T_CONST>: Native DATETIME type, stored as YYYY-MM-DD hh:mm:ss UTC. More...
 
const T_DATETIME_NULL = 0xf8
 
const T_FLOAT = 0xfb
 <T_CONST>: 8-byte IEEE floating point number. More...
 
const T_FLOAT_NULL = 0xfa
 
const T_INT = 0xfd
 <T_CONST>: 8-byte signed integer. More...
 
const T_INT_NULL = 0xfc
 
const T_STRING = 0xf7
 <T_CONST>: UTF-8 up to 255 bytes. More...
 
const T_STRING_NULL = 0xf6
 
const T_TEXT = 0x05
 <T_CONST>: UTF-8 up to 64KiB. More...
 
const T_TEXT_NULL = 0x04
 

Protected Properties

int[] $colDefs
 
DB $db
 
Table[] $tables = []
 
const COLUMN_DEFINITIONS
 Driver-specific schema phrases. More...
 
const I_AUTOINCREMENT = self::I_PRIMARY | 0x0100
 Partial definition for T_AUTOINCREMENT, use that instead. More...
 
const I_MASK = 0xff00
 Higher byte mask (column index type). More...
 
const PHP_TYPES
 Maps column types reported by the database into PHP native/annotated types. More...
 
const T_MASK = 0xff
 Lower-byte mask (column storage type). More...
 
const T_STRICT = 0x01
 Flags whether a type is NOT NULL More...
 

Public Static Methods

magic static self factory (DB $db)
 

Public Methods

 __construct (DB $db)
 
$this addColumn (string $table, string $column, int $type=self::T_STRING_NULL)
 ALTER TABLE $table ADD COLUMN $column ... if it doesn't exist. More...
 
$this addUniqueKey (string $table, array $columns)
 Driver-appropriate constraint creation. More...
 
$this createTable (string $table, array $columns, array $foreign=[])
 CREATE TABLE $table ... More...
 
$this dropTable (string $table)
 DROP TABLE IF EXISTS $table More...
 
$this dropUniqueKey (string $table, array $columns)
 Driver-appropriate constraint deletion. More...
 
array[] getColumnInfo (string $table)
 Returns column metadata in an associative array. More...
 
DB getDb ()
 
string getForeignKeyName (string $table, string $column)
 FK_TABLE__COLUMN More...
 
string getPrimaryKeyName (string $table, array $columns)
 PK_TABLE__COLUMN__COLUMN__COLUMN More...
 
null Table getTable (string $name)
 
string getUniqueKeyName (string $table, array $columns)
 UQ_TABLE__COLUMN__COLUMN__COLUMN More...
 
bool hasColumn (string $table, string $column)
 
bool hasUniqueKey (string $table, array $columns)
 
bool offsetExists ($table)
 Whether a table exists. More...
 
null Table offsetGet ($table)
 Returns a table by name. More...
 
 offsetSet ($offset, $value)
 
 offsetUnset ($offset)
 
$this renameTable (string $oldName, string $newName)
 ALTER TABLE $oldName RENAME TO $newName More...
 

Protected Methods

int[] sortColumns (array $types)
 Sorts according to index priority, storage size/complexity, and name. More...
 

Properties

◆ COLUMN_DEFINITIONS

const COLUMN_DEFINITIONS
protected

Driver-specific schema phrases.

◆ I_AUTOINCREMENT

const I_AUTOINCREMENT = self::I_PRIMARY | 0x0100
protected

Partial definition for T_AUTOINCREMENT, use that instead.

◆ I_MASK

const I_MASK = 0xff00
protected

Higher byte mask (column index type).

◆ I_PRIMARY

const I_PRIMARY = 0xfe00

<I_CONST>: One or more columns compose the primary key.

◆ PHP_TYPES

const PHP_TYPES
protected
Initial value:
= [
'BOOLEAN' => 'bool',
'BIGINT' => 'int',
'INTEGER' => 'int',
'DOUBLE PRECISION' => 'float',
'VARCHAR(255)' => 'string',
'TEXT' => 'String',
'BLOB' => 'STRING',
'LONGBLOB' => 'STRING',
'DATETIME' => 'DateTime',
]

Maps column types reported by the database into PHP native/annotated types.

This is used by Schema::getColumnInfo()

◆ T_AUTOINCREMENT

const T_AUTOINCREMENT = self::I_AUTOINCREMENT | self::T_INT

<T_CONST>: Column is the primary key and auto-increments (8-byte signed integer).

◆ T_BLOB

const T_BLOB = 0x03

<T_CONST>: Arbitrary binary data up to 4GiB.

◆ T_BOOL

const T_BOOL = 0xff

<T_CONST>: Boolean analog (numeric).

◆ T_CONST_NAMES

const T_CONST_NAMES
Initial value:
= [
'bool' => 'T_BOOL',
'DateTime' => 'T_DATETIME',
'float' => 'T_FLOAT',
'int' => 'T_INT',
'string' => 'T_STRING',
'String' => 'T_TEXT',
'STRING' => 'T_BLOB',
]

Maps storage types to T_CONST names.

Resolved storage types in Record are keys here.

This is also used when generating migrations on the command-line.

◆ T_DATETIME

const T_DATETIME = 0xf9

<T_CONST>: Native DATETIME type, stored as YYYY-MM-DD hh:mm:ss UTC.

◆ T_FLOAT

const T_FLOAT = 0xfb

<T_CONST>: 8-byte IEEE floating point number.

◆ T_INT

const T_INT = 0xfd

<T_CONST>: 8-byte signed integer.

◆ T_MASK

const T_MASK = 0xff
protected

Lower-byte mask (column storage type).

◆ T_STRICT

const T_STRICT = 0x01
protected

Flags whether a type is NOT NULL

◆ T_STRING

const T_STRING = 0xf7

<T_CONST>: UTF-8 up to 255 bytes.

◆ T_TEXT

const T_TEXT = 0x05

<T_CONST>: UTF-8 up to 64KiB.

Constructor

◆ __construct()

__construct ( DB  $db)
Parameters
DB$db

Methods

◆ addColumn()

$this addColumn ( string  $table,
string  $column,
int  $type = self::T_STRING_NULL 
)

ALTER TABLE $table ADD COLUMN $column ... if it doesn't exist.

Parameters
string$table
string$column
int$type
Returns
$this

◆ addUniqueKey()

$this addUniqueKey ( string  $table,
array  $columns 
)

Driver-appropriate constraint creation.

Parameters
string$table
string[]$columns
Returns
$this

◆ createTable()

$this createTable ( string  $table,
array  $columns,
array  $foreign = [] 
)

CREATE TABLE $table ...

At least one column must be given.

Parameters
string$table
int[]$columns[ name => <I_CONST> | <T_CONST> ]
Column[]$foreign[ column name => <External Column> ]
Returns
$this

◆ dropTable()

$this dropTable ( string  $table)

DROP TABLE IF EXISTS $table

Parameters
string$table
Returns
$this

◆ dropUniqueKey()

$this dropUniqueKey ( string  $table,
array  $columns 
)

Driver-appropriate constraint deletion.

Parameters
string$table
string[]$columns
Returns
$this

◆ factory()

magic static self factory ( DB  $db)
static
Returns
self

◆ getColumnInfo()

array [] getColumnInfo ( string  $table)

Returns column metadata in an associative array.

Elements are:

  • name
  • type: PHP native/annotated type (as a string)
  • nullable: boolean

The returned type can be used to get a T_CONST name from Schema::T_CONST_NAMES

Parameters
string$table
string$column
Returns
array[] Keyed by name.

◆ getDb()

DB getDb ( )
Returns
DB

◆ getForeignKeyName()

string getForeignKeyName ( string  $table,
string  $column 
)
final

FK_TABLE__COLUMN

Parameters
string$table
string$column
Returns
string

◆ getPrimaryKeyName()

string getPrimaryKeyName ( string  $table,
array  $columns 
)
final

PK_TABLE__COLUMN__COLUMN__COLUMN

Parameters
string$table
string[]$columns
Returns
string

◆ getTable()

null Table getTable ( string  $name)
Parameters
string$name
Returns
null|Table

◆ getUniqueKeyName()

string getUniqueKeyName ( string  $table,
array  $columns 
)
final

UQ_TABLE__COLUMN__COLUMN__COLUMN

Parameters
string$table
string[]$columns
Returns
string

◆ hasColumn()

bool hasColumn ( string  $table,
string  $column 
)
Parameters
string$table
string$column
Returns
bool

◆ hasUniqueKey()

bool hasUniqueKey ( string  $table,
array  $columns 
)
Parameters
string$table
string[]$columns
Returns
bool

◆ offsetExists()

bool offsetExists (   $table)
final

Whether a table exists.

Parameters
string$table
Returns
bool

◆ offsetGet()

null Table offsetGet (   $table)

Returns a table by name.

Parameters
string$table
Returns
null|Table

◆ offsetSet()

offsetSet (   $offset,
  $value 
)
final
Parameters
$offset
$value
Exceptions
LogicException

◆ offsetUnset()

offsetUnset (   $offset)
final
Parameters
$offset
Exceptions
LogicException

◆ renameTable()

$this renameTable ( string  $oldName,
string  $newName 
)

ALTER TABLE $oldName RENAME TO $newName

Parameters
string$oldName
string$newName
Returns
$this

◆ sortColumns()

int [] sortColumns ( array  $types)
protected

Sorts according to index priority, storage size/complexity, and name.

Parameters
int[]$types
Returns
int[]