PHP Class Gdn_DatabaseStructure, vanilla

Mostra file Open project: vanilla/vanilla Class Usage Examples

Public Properties

Property Type Description
$CaptureOnly Whether or not to only capture the sql, rather than execute it. When this property is true then a property called CapturedSql will be added to this class which is an array of all the Sql statements.
$Database The instance of the database singleton.

Protected Properties

Property Type Description
$_CharacterEncoding The character encoding to set as default for the table being created.
$_Columns array
$_DatabasePrefix string
$_ExistingColumns The existing columns in the database.
$_TableExists Whether or not this table exists in the database.
$_TableName The name of the table to create or modify.
$_TableStorageEngine The name of the storage engine for this table.

Public Methods

Method Description
__construct ( string $Database = null ) The constructor for this class. Automatically fills $this->ClassName.
column ( string $Name, mixed $Type, boolean $NullDefault = false, string $KeyType = false ) Defines a column to be added to $this->Table().
columnExists ( string $ColumnName ) : boolean Returns whether or not a column exists in the database.
columnTypeString ( mixed $Column ) : string Return the definition string for a column.
columns ( $Name = '' ) : array An associative array of $ColumnName => $ColumnProperties columns for the table.
databasePrefix ( string $DatabasePrefix = '' ) Gets and/or sets the database prefix.
drop ( ) Drops $this->Table() from the database.
dropColumn ( string $Name ) Drops $Name column from $this->Table().
engine ( $Engine, boolean $CheckAvailability = true )
existingColumns ( ) : array Gets the column definitions for the columns in the database.
get ( string $TableName = '' ) : Gdn_DatabaseStructure Load the schema for this table from the database.
getAlterTableThreshold ( ) : integer Get the alter table threshold.
getIssues ( ) : array Get a list of issues that occurred during the last call to {@link Gdn_DatabaseStructure::set()}.
getRowCountEstimate ( string $tableName ) : integer | null Get the estimated number of rows in a table.
primaryKey ( string $Name, string $Type = 'int' ) : Gdn_DatabaseStructure Defines a primary key column on a table.
query ( string $sql, boolean $checkTreshold = false ) : Gdn_Dataset Send a query to the database and return the result.
renameColumn ( string $OldName, string $NewName ) Renames a column in $this->Table().
renameTable ( string $OldName, string $NewName, boolean $UsePrefix = false ) Renames a table in the database.
reset ( ) : Gdn_DatabaseStructure Reset the internal state of this object so that it can be reused.
set ( boolean $Explicit = false, boolean $Drop = false ) Creates the table and columns specified with $this->Table() and $this->Column(). If no table or columns have been specified, this method will throw a fatal error.
setAlterTableThreshold ( integer $alterTableThreshold ) : Gdn_MySQLStructure Set the alterTableThreshold.
table ( string $Name = '', string $CharacterEncoding = '' ) Specifies the name of the table to create or modify.
tableExists ( $TableName = null ) : boolean Whether or not the table exists in the database.
tableName ( ) : string Returns the name of the table being defined in this object.
types ( string $Class = 'all' ) Gets an array of type names allowed in the structure.
view ( string $Name, string $Query ) Specifies the name of the view to create or modify.

Protected Methods

Method Description
_create ( ) Creates the table defined with $this->Table() and $this->Column().
_createColumn ( $Name, $Type, $Null, $Default, $KeyType ) : stdClass
_modify ( boolean $Explicit = false ) Modifies $this->Table() with the columns specified with $this->Column().
addIssue ( string $message, string $sql ) : Gdn_DatabaseStructure Add an issue to the issues list.
executeQuery ( string $sql, boolean $checkThreshold = false ) : boolean Send a query to the database and return the result.

Method Details

__construct() public method

The constructor for this class. Automatically fills $this->ClassName.
public __construct ( string $Database = null )
$Database string

_create() protected method

Creates the table defined with $this->Table() and $this->Column().
protected _create ( )

_createColumn() protected method

protected _createColumn ( $Name, $Type, $Null, $Default, $KeyType ) : stdClass
$Name
$Type
$Null
$Default
$KeyType
return stdClass

_modify() protected method

Modifies $this->Table() with the columns specified with $this->Column().
protected _modify ( boolean $Explicit = false )
$Explicit boolean If TRUE, this method will remove any columns from the table that were not defined with $this->Column().

addIssue() protected method

Add an issue to the issues list.
protected addIssue ( string $message, string $sql ) : Gdn_DatabaseStructure
$message string A human readable string for the issue.
$sql string The SQL that didn't happen.
return Gdn_DatabaseStructure Returns **this** for chaining.

column() public method

Defines a column to be added to $this->Table().
public column ( string $Name, mixed $Type, boolean $NullDefault = false, string $KeyType = false )
$Name string The name of the column to create.
$Type mixed The data type of the column to be created. Types with a length speecifty the length in barackets. * If an array of values is provided, the type will be set as "enum" and the array will be assigned as the column's Enum property. * If an array of two values is specified then a "set" or "enum" can be specified (ex. array('set', array('Short', 'Tall', 'Fat', 'Skinny')))
$NullDefault boolean Whether or not nulls are allowed, if not a default can be specified. * TRUE: Nulls are allowed. * FALSE: Nulls are not allowed. * Any other value: Nulls are not allowed, and the specified value will be used as the default.
$KeyType string What type of key is this column on the table? Options are primary, key, and FALSE (not a key).

columnExists() public method

Returns whether or not a column exists in the database.
public columnExists ( string $ColumnName ) : boolean
$ColumnName string The name of the column to check.
return boolean

columnTypeString() public method

Return the definition string for a column.
public columnTypeString ( mixed $Column ) : string
$Column mixed The column to get the type string from. - object: The column as returned by the database schema. The properties looked at are Type, Length, and Precision. - string
return string The type definition string.

columns() public method

An associative array of $ColumnName => $ColumnProperties columns for the table.
public columns ( $Name = '' ) : array
return array

databasePrefix() public method

Gets and/or sets the database prefix.
public databasePrefix ( string $DatabasePrefix = '' )
$DatabasePrefix string

drop() public method

Drops $this->Table() from the database.
public drop ( )

dropColumn() public method

Drops $Name column from $this->Table().
public dropColumn ( string $Name )
$Name string The name of the column to drop from $this->Table().

engine() public method

public engine ( $Engine, boolean $CheckAvailability = true )
$Engine
$CheckAvailability boolean

executeQuery() protected method

Send a query to the database and return the result.
protected executeQuery ( string $sql, boolean $checkThreshold = false ) : boolean
$sql string The sql to execute.
$checkThreshold boolean Whether or not to check the alter table threshold before altering the table.
return boolean Whether or not the query succeeded.

existingColumns() public method

Gets the column definitions for the columns in the database.
public existingColumns ( ) : array
return array

get() public method

Load the schema for this table from the database.
public get ( string $TableName = '' ) : Gdn_DatabaseStructure
$TableName string The name of the table to get or blank to get the schema for the current table.
return Gdn_DatabaseStructure $this

getAlterTableThreshold() public method

The alter table threshold is the maximum estimated rows a table can have where alter tables are allowed.
public getAlterTableThreshold ( ) : integer
return integer Returns the threshold as an integer. A value of zero means no threshold.

getIssues() public method

Get a list of issues that occurred during the last call to {@link Gdn_DatabaseStructure::set()}.
public getIssues ( ) : array
return array Returns an array of issues.

getRowCountEstimate() public method

Get the estimated number of rows in a table.
public getRowCountEstimate ( string $tableName ) : integer | null
$tableName string The name of the table to look up, without its prefix.
return integer | null Returns the estimated number of rows or **null** if the information doesn't exist.

primaryKey() public method

Defines a primary key column on a table.
public primaryKey ( string $Name, string $Type = 'int' ) : Gdn_DatabaseStructure
$Name string The name of the column.
$Type string The data type of the column.
return Gdn_DatabaseStructure $this.

query() public method

Send a query to the database and return the result.
Deprecation: since 2.3. Was incorrectly public. Replaced by executeQuery().
public query ( string $sql, boolean $checkTreshold = false ) : Gdn_Dataset
$sql string The sql to execute.
$checkTreshold boolean Should not be used
return Gdn_Dataset

renameColumn() public method

Renames a column in $this->Table().
public renameColumn ( string $OldName, string $NewName )
$OldName string The name of the column to be renamed.
$NewName string The new name for the column being renamed.

renameTable() public method

Renames a table in the database.
public renameTable ( string $OldName, string $NewName, boolean $UsePrefix = false )
$OldName string The name of the table to be renamed.
$NewName string The new name for the table being renamed.
$UsePrefix boolean A boolean value indicating if $this->_DatabasePrefix should be prefixed before $OldName and $NewName.

reset() public method

Reset the internal state of this object so that it can be reused.
public reset ( ) : Gdn_DatabaseStructure
return Gdn_DatabaseStructure $this

set() public method

Creates the table and columns specified with $this->Table() and $this->Column(). If no table or columns have been specified, this method will throw a fatal error.
public set ( boolean $Explicit = false, boolean $Drop = false )
$Explicit boolean If TRUE, and the table specified with $this->Table() already exists, this method will remove any columns from the table that were not defined with $this->Column().
$Drop boolean If TRUE, and the table specified with $this->Table() already exists, this method will drop the table before attempting to re-create it.

setAlterTableThreshold() public method

Set the alterTableThreshold.
public setAlterTableThreshold ( integer $alterTableThreshold ) : Gdn_MySQLStructure
$alterTableThreshold integer
return Gdn_MySQLStructure Returns `$this` for fluent calls.

table() public method

Specifies the name of the table to create or modify.
public table ( string $Name = '', string $CharacterEncoding = '' )
$Name string The name of the table.
$CharacterEncoding string The default character encoding to specify for this table.

tableExists() public method

Whether or not the table exists in the database.
public tableExists ( $TableName = null ) : boolean
return boolean

tableName() public method

Returns the name of the table being defined in this object.
public tableName ( ) : string
return string

types() public method

Gets an array of type names allowed in the structure.
public types ( string $Class = 'all' )
$Class string The class of types to get. Valid values are: - int: Integer types. - float: Floating point types. - decimal: Precise decimal types. - numeric: float, int and decimal. - string: String types. - date: Date types. - length: Types that have a length. - precision: Types that have a precision. - other: Types that don't fit into any other category on their own. - all: All recognized types.

view() public method

Specifies the name of the view to create or modify.
public view ( string $Name, string $Query )
$Name string The name of the view.
$Query string Query to create as the view. Typically this can be generated with the $Database object.

Property Details

$CaptureOnly public_oe property

Whether or not to only capture the sql, rather than execute it. When this property is true then a property called CapturedSql will be added to this class which is an array of all the Sql statements.
public $CaptureOnly

$Database public_oe property

The instance of the database singleton.
public $Database

$_CharacterEncoding protected_oe property

The character encoding to set as default for the table being created.
protected $_CharacterEncoding

$_Columns protected_oe property

protected array $_Columns
return array

$_DatabasePrefix protected_oe property

protected string $_DatabasePrefix
return string

$_ExistingColumns protected_oe property

The existing columns in the database.
protected $_ExistingColumns

$_TableExists protected_oe property

Whether or not this table exists in the database.
protected $_TableExists

$_TableName protected_oe property

The name of the table to create or modify.
protected $_TableName

$_TableStorageEngine protected_oe property

The name of the storage engine for this table.
protected $_TableStorageEngine