Sgss_Collection_Pdo_Map
Represents an associated collection in database table

Description

This is an implementation of the Sgss_Collection_Map_Interface using database.

The database schema to be used with this class requires two columns of text datatype. One column represents the keys in this map, and another represents the values associated with those keys. The key column must have either primary key or unique constraint to avoid duplication.

Keys and values will be serialized when stored into database, so that those retain its datatype or class when retrieved. Therefore, any elements (keys 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 (
     key_column_name      TEXT PRIMARY KEY
     value_column_name    TEXT
 );

The first parameter of the constructor specifies the database to be used with this map. This can be either config array or PDO object. The second parameter specifies table name, the third and fourth specifies name of the columns to store keys and values.

  1.  <?php
  2.  require_once 'Sgss/Collection/Pdo/Map.php';
  3.  
  4.  $map new Sgss_Collection_Pdo_Map(
  5.      array('dns'      => 'mysql:dbname=testdb;host=127.0.0.1',
  6.            'username' => 'dbuser',
  7.            'password' => 'dbpass'),
  8.      'table_name''key_column_name''value_column_name'
  9.  );

is equivalent to:

  1.  <?php
  2.  require_once 'Sgss/Collection/Pdo/Map.php';
  3.  
  4.  $db new PDO('mysql:dbname=testdb;host=127.0.0.1''dbuser''dbpass');
  5.  $map new Sgss_Collection_Pdo_Map(
  6.      $db'table_name''key_column_name''value_column_name'
  7.  );

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.

Class diagram:
Sgss_Collection_Map_Abstract
 + Sgss_Collection_Pdo_Map
Used by:
Sgss_Font_Cache_Record::init()
Author:
Matsuda Shota
Copyright:
(c) 2007-2008 Matsuda Shota
Usedby:
Sgss_Font_Cache_Record::init()
License:
http://creativecommons.org/licenses/GPL/2.0/
Located in:
/Collection/Pdo/Map.php (line 106)

Class overview

Variables

protected array $_config

Config to connect to the database

protected PDO $_db

Database handle used with this map

protected string $_driver

Driver name of the database handle

protected boolean $_isPrepared

Represents whether the prepared statements were created

protected string $_key

Name of the column containing keys that already exists in the database

protected string $_table

Name of the table that already exists in the database

protected string $_value

Name of the column containing values that already exists in the database

Methods

__construct (array|PDO $config, string $table, string $key, string $value)

The constructor

boolean clear ()

Removes all the keys and the associated values in this map

mixed|null get (mixed $key)

Returns the value associated with the specified key

boolean has (mixed $key)

Determines whether this map contains the associated value with the specified key

boolean hasValue (mixed $value)

Determines whether this map contains the specified value

boolean put (mixed $key, mixed $value)

Inserts or replaces the association between the specified key and value into this map

boolean remove (mixed $key)

Removes the specified key and the associated value from this map

int size ()

Returns the number of associations between key and value in this map

array toArray ()

Converts this map to an associated array

array toKeyArray ()

Returns an array containing all the keys in this map

array toValueArray ()

Returns an array containing all the values in this map

protected void _connect ()

Connects to the database

protected void _invalidateStatements ()

Marks the prepared statements as no longer working

protected void _prepareStatements ()

Creates the prepared statements used with the database handle

protected string _quoteName (string $name)

Quotes the specified table or column name

protected scalar _sopite (mixed $obj, [boolean $key = false])

Converts the specified object to the corresponding scalar value

protected mixed _waken (scalar $scalar, [boolean $key = false])

Converts the specified scalar value to the corresponding object

array __sleep ()
void __wakeup ()
Inherited from Sgss_Collection_Map_Abstract:
count(), hasAll(), hasAllValues(), isEmpty(), offsetExists(), offsetGet(), offsetSet(), offsetUnset(), putAll(), removeAll(), set(), __get(), __isset(), __set(), __unset()

Variable detail

$_config 

Config to connect to the database

Signature:
protected array $_config = array(...)


$_db 

Database handle used with this map

Signature:
protected PDO $_db


$_driver 

Driver name of the database handle

Signature:
protected string $_driver = ''


$_isPrepared 

Represents whether the prepared statements were created

Signature:
protected boolean $_isPrepared = false


$_key 

Name of the column containing keys that already exists in the database

Signature:
protected string $_key


$_table 

Name of the table that already exists in the database

Signature:
protected string $_table


$_value 

Name of the column containing values that already exists in the database

Signature:
protected string $_value


Method detail

__construct

The constructor

Parameters:
  • array|PDO $config

    Config to connect to the database, or PDO object used with the map

  • string $table

    Name of the table that already exists in the database

  • string $key

    Name of the column containing keys that already exists in the database

  • string $value

    Name of the column containing the values that already exists in the database

Signature:
public __construct (array|PDO $config, string $table, string $key, string $value)


clear

Removes all the keys and the associated values in this map

Returns:
boolean -- True when the operation changed this map
Uses:
Sgss_Collection_Pdo_Map::size()
Related subject:
Sgss_Collection_Map_Interface::clear()
Signature:
public boolean clear ()


get

Returns the value associated with the specified key

Parameters:
  • mixed $key

    Key associated with the value to be retrieved

Returns:
mixed|null -- Retrieved value or null if this map does not contain the key
Uses:
Sgss_Collection_Pdo_Map::_sopite(), Sgss_Collection_Pdo_Map::_waken()
Related subject:
Sgss_Collection_Map_Interface::get()
Signature:
public mixed|null get (mixed $key)


has

Determines whether this map contains the associated value with the specified key

Parameters:
  • mixed $key

    Key to determine

Returns:
boolean -- True when this map contains the associated value
Uses:
Sgss_Collection_Pdo_Map::_sopite()
Related subject:
Sgss_Collection_Map_Interface::has()
Signature:
public boolean has (mixed $key)


hasValue

Determines whether this map contains the specified value

Parameters:
  • mixed $value

    Value to determine

Returns:
boolean -- True when this map contains the value
Uses:
Sgss_Collection_Pdo_Map::_sopite()
Related subject:
Sgss_Collection_Map_Interface::hasValue()
Signature:
public boolean hasValue (mixed $value)


put

Inserts or replaces the association between the specified key and value into this map

Parameters:
  • mixed $key

    Key to be inserted

  • mixed $value

    Value to be inserted and associated with the key

Returns:
boolean -- True when the operation changed this map
Uses:
Sgss_Collection_Pdo_Map::_sopite()
Related subject:
Sgss_Collection_Map_Interface::put()
Signature:
public boolean put (mixed $key, mixed $value)


remove

Removes the specified key and the associated value from this map

Parameters:
  • mixed $key

    Key associated with the value to be removed

Returns:
boolean -- True when the operation changed this list
Uses:
Sgss_Collection_Pdo_Map::_sopite()
Related subject:
Sgss_Collection_Map_Interface::remove()
Signature:
public boolean remove (mixed $key)


size

Returns the number of associations between key and value in this map

Returns:
int -- The number of associations in this map
Used by:
Sgss_Collection_Pdo_Map::clear()
Related subject:
Sgss_Collection_Map_Interface::size()
Signature:
public int size ()


toArray

Converts this map to an associated array

Returns:
array -- Associated array representing this map
Uses:
Sgss_Collection_Pdo_Map::_waken()
Related subject:
Sgss_Collection_Map_Interface::toArray()
Signature:
public array toArray ()


toKeyArray

Returns an array containing all the keys in this map

Returns:
array -- Array containing all the keys in this map
Uses:
Sgss_Collection_Pdo_Map::_waken()
Related subject:
Sgss_Collection_Map_Interface::toKeyArray()
Signature:
public array toKeyArray ()


toValueArray

Returns an array containing all the values in this map

Returns:
array -- Array containing all the values in this map
Uses:
Sgss_Collection_Pdo_Map::_waken()
Related subject:
Sgss_Collection_Map_Interface::toValueArray()
Signature:
public array toValueArray ()


_connect 

Connects to the database

Throws:
  • Sgss_Collection_Map_Exception

    When unable to connect to the database

Used by:
Sgss_Collection_Pdo_Map::__wakeup()
Signature:
protected void _connect ()


_invalidateStatements 

Marks the prepared statements as no longer working

Signature:
protected void _invalidateStatements ()


_prepareStatements 

Creates the prepared statements used with the database handle

Throws:
  • Sgss_Collection_Map_Exception

    When unable to prepare statements

Signature:
protected void _prepareStatements ()


_quoteName 

Quotes the specified table or column name

Parameters:
  • string $name

    Column or table name to be quoted

Returns:
string -- Quoted string
Signature:
protected string _quoteName (string $name)


_sopite 

Converts the specified object to the corresponding scalar value

Parameters:
  • mixed $obj

    Object to be converted to scalar

  • boolean $key

    Whether the object is used as the key

Returns:
scalar -- Scalar value corresponding to the object
Throws:
  • Sgss_Collection_Map_Exception

    When the object is or contains unconvertable to scalar (resource or internal class)

Uses:
serialize()
Used by:
Sgss_Collection_Pdo_Map::remove(), Sgss_Collection_Pdo_Map::get(), Sgss_Collection_Pdo_Map::hasValue(), Sgss_Collection_Pdo_Map::put(), Sgss_Collection_Pdo_Map::has()
Signature:
protected scalar _sopite (mixed $obj, [boolean $key = false])


_waken 

Converts the specified scalar value to the corresponding object

Parameters:
  • scalar $scalar

    Scalar value to be converted to object

  • boolean $key

    Whether the converted object is to be used as the key

Returns:
mixed -- Object corresponding to the scalar
Uses:
unserialize()
Used by:
Sgss_Collection_Pdo_Map::toArray(), Sgss_Collection_Pdo_Map::toKeyArray(), Sgss_Collection_Pdo_Map::get(), Sgss_Collection_Pdo_Map::toValueArray()
Signature:
protected mixed _waken (scalar $scalar, [boolean $key = false])


__sleep

Throws:
  • Sgss_Collection_Map_Exception

    When this map is unserializable

Signature:
public array __sleep ()


__wakeup

Uses:
Sgss_Collection_Pdo_Map::_connect()
Signature:
public void __wakeup ()