This is an implementation of the Sgss_Collection_Table_Interface using database.
The database schema to be used with this class requires at least two columns of text datatype. One column represents the row identifiers in this table, and another represents the values associated with those identifiers. The row identifier column must have either primary key or unique constraint to avoid duplication.
Row identifiers and values will be serialized when stored into database, so that those retain its datatype or class when retrieved. Therefore, any elements (identifiers and values) must be serializable, in other words, not contain any resource object. The columns must have enough length to store serialized elements.
A simple query to create such table follows:
CREATE TABLE table_name (
row_column_name TEXT PRIMARY KEY
column_name_1 TEXT
column_name_2 TEXT
...
column_name_N TEXT
);The first parameter of the constructor specifies the database to be used with this table. This can be either config array or PDO object. The second parameter specifies table name, the third specifies name of the columns to store row identifiers, and the fourth specifies an array of value column names.
<?phprequire_once 'Sgss/Collection/Pdo/Table.php';array('dns' => 'mysql:dbname=testdb;host=127.0.0.1','username' => 'dbuser','password' => 'dbpass'),'table_name', 'row_column_name',array('column_name_1', 'column_name_2', ..., 'column_name_N'));is equivalent to:
<?phprequire_once 'Sgss/Collection/Pdo/Table.php';$db = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass');$db, 'table_name', 'key_column_name',array('column_name_1', 'column_name_2', ..., 'column_name_N'));Do not specify quoted name in those parameters because all the names will be automatically quoted, otherwise it will indicate different table or column from what you intended.
The Sgss_Collection_Table_Interface defines three methods to retrieve table row: retrieveRow(), createRow(), and getRow(). The differences between those are in the existence of row. The retrieveRow() returns a row that already exists in the table, and the createRow() returns a non-existing row. For example:
<?php// Assume this represents an empty table.// This is not inserted into the table at this time.$row->foo = 'foo';$row->bar = 'bar';// Actually insert into the table.$row->insert();// This already exists in the table.// Not need to call insert().$row->foo = 'foo';$row->bar = 'bar';// Remove this row from the table.$row->remove();The getRow() returns both of the aboves depending on whether the row to be retrieved exists in the table. Thus if the existence is undetermined, you should call insert() or remove() of the returned row every time applying changes or removing it.
<?php$row->foo = 'foo';$row->bar = 'bar';// Make sure to call this.$row->insert();Sgss_Collection_Table_Abstract
+ Sgss_Collection_Pdo_Table
|
$_columns
Column names defined in the database schema |
|
$_config
Config to connect to the database |
|
$_db
Database handle used with this table |
|
$_driver
Driver name of the database handle |
|
$_isPrepared
Represents whether the prepared statements were created |
|
$_row
Name of the column containing row identifiers that already exists in the database |
|
$_table
Name of the table that already exists in the database |
|
__construct (array|PDO $config, string $table, string $row, array $columns)
The constructor |
boolean |
clearRows ()
Removes all the rows in this table |
Sgss_Collection_TableRow_Interface|null |
createRow (mixed $row)
Creates and returns the row object associated with the specified identifier, which is prepared to be inserted into this table |
int |
getRowCount ()
Returns the number of rows in this table |
boolean |
hasRow (mixed $row)
Determines this table contains the row associated with the specified identifier |
boolean |
removeRow (mixed $row)
Removes the row associated with the specified identifier from this table |
Sgss_Collection_TableRow_Interface|null |
retrieveRow (mixed $row)
Retrieves and returns the row object associated with the specified identifier in this table |
array |
toArray ()
Converts this table to an associated array |
array |
toColumnArray ()
Returns an array containing all the column names in this table |
array |
toRowArray ()
Returns an array containing all the keys associated with the rows in this table |
|
_connect ()
Connects to the database |
|
_invalidateStatements ()
Marks the prepared statements as no longer working |
|
_prepareStatements ()
Creates the prepared statements used with the database handle |
|
_quoteName (string|array $name)
Quotes the specified table or column name |
|
_sopite (mixed $obj, [boolean $key = false])
Converts the specified object to the corresponding scalar value |
|
_waken (scalar $scalar, [boolean $key = false])
Converts the specified scalar value to the corresponding object |
array |
__sleep ()
|
void |
__wakeup ()
|
Column names defined in the database schema
protected array $_columns
Config to connect to the database
protected array $_config = array(...)
Database handle used with this table
protected PDO $_db
Driver name of the database handle
protected string $_driver = ''
Represents whether the prepared statements were created
protected boolean $_isPrepared = false
Name of the column containing row identifiers that already exists in the database
protected string $_row
Name of the table that already exists in the database
protected string $_table
The constructor
array|PDO $config
Config to connect to the database, or PDO object used with the table
string $table
Name of the table that already exists in the database
string $row
Name of the column containing row identifiers that already exists in the database
array $columns
Column names defined in the database schema
When the $columns contains no element
public __construct (array|PDO $config, string $table, string $row, array $columns)
Removes all the rows in this table
public boolean clearRows ()
Creates and returns the row object associated with the specified identifier, which is prepared to be inserted into this table
mixed $row
Identifier to be associated with the prepared row
public Sgss_Collection_TableRow_Interface|null createRow (mixed $row)
Returns the number of rows in this table
public int getRowCount ()
Determines this table contains the row associated with the specified identifier
mixed $row
Identifier with which the row to determine is associated
public boolean hasRow (mixed $row)
Removes the row associated with the specified identifier from this table
mixed $row
Identifier with which the row to be removed is associated
public boolean removeRow (mixed $row)
Retrieves and returns the row object associated with the specified identifier in this table
mixed $row
Identifier with which the row is associated
public Sgss_Collection_TableRow_Interface|null retrieveRow (mixed $row)
Converts this table to an associated array
public array toArray ()
Returns an array containing all the column names in this table
public array toColumnArray ()
Returns an array containing all the keys associated with the rows in this table
public array toRowArray ()
Connects to the database
When unable to connect to the database
protected void _connect ()
Marks the prepared statements as no longer working
protected void _invalidateStatements ()
Creates the prepared statements used with the database handle
When unable to prepare statements
protected void _prepareStatements ()
Quotes the specified table or column name
string|array $name
Column or table name or its array to be quoted
protected string|array _quoteName (string|array $name)
Converts the specified object to the corresponding scalar value
mixed $obj
Object to be converted to scalar
boolean $key
Whether the object is used as the key
When the object is or contains unconvertable to scalar (resource or internal class)
protected scalar _sopite (mixed $obj, [boolean $key = false])
Converts the specified scalar value to the corresponding object
scalar $scalar
Scalar value to be converted to object
boolean $key
Whether the converted object is to be used as the key
protected mixed _waken (scalar $scalar, [boolean $key = false])
When this table is unserializable
public array __sleep ()
public void __wakeup ()