PHP Classes

File: DATA/MySQL5/DefaultIndexingStrategy.php

Recommend this page to a friend!
  Classes of Martin Alterisio   DATA   DATA/MySQL5/DefaultIndexingStrategy.php   Download  
File: DATA/MySQL5/DefaultIndexingStrategy.php
Role: Class source
Content type: text/plain
Description: Default strategy for mapping an associative array index to rows in a database table.
Class: DATA
Access data stored in MySQL tables like arrays
Author: By
Last change: + anonymous access
Date: 17 years ago
Size: 2,630 bytes
 

Contents

Class file image Download
<?php
/**
 * @package DATA_MySQL5
 */

/**
 * Default strategy for mapping an associative array
 * index to rows in a database table.
 *
 * Either the primary key is used, or an uniquely indexed
 * field if the primary key is auto-numeric and there is
 * only one unique index.
 */
class DATA_MySQL5_DefaultIndexingStrategy implements DATA_AssociativeIndexingStrategy {
   
/**
     * Stores table name on construction for future operations.
     * @var string
     */
   
protected $table;
   
   
/**
     * Constructor.
     *
     * @param string $table The table name.
     */
   
public function __construct($table) {
       
$this->table = $table;
    }
   
    public function
isSingleRowIndexing() {
        return
true;
    }
   
   
/**
     * Returns the field used as index according to the offset supplied in the array access.
     *
     * Throws {@link DATA_PrimaryKeyNeeded}, {@link DATA_PrimaryKeyTooLarge}.
     *
     * @return string Index field name.
     */
   
protected function getIndexField() {
       
$keys = DATA_MySQL5_Schema::getPrimaryKey($this->table);
        if (
count($keys) == 0) throw new DATA_PrimaryKeyNeeded($this->table);
        if (
count($keys) > 1) throw new DATA_PrimaryKeyTooLarge($this->table);
       
$uniqueFields = DATA_MySQL5_Schema::getUniqueFields($this->table);
        if (
count($uniqueFields) == 1
        
&& DATA_MySQL5_Schema::isAutoIncrement($this->table, $keys[0])) {
            return
$uniqueFields[0];
        } else {
            return
$keys[0];
        }
    }
   
   
/**
     * Returns inboxed version of the row offset provided.
     *
     * Throws {@link DATA_PrimaryKeyNeeded}, {@link DATA_PrimaryKeyTooLarge}.
     *
     * @param string|DATA_SQLType $row The row offset.
     * @return DATA_SQLType Inboxed row offset.
     */
   
public function inboxRowOffset($row) {
       
$indexField = $this->getIndexField();
        try {
            return
DATA_MySQL5_Schema::getSQLTypeFactory($this->table, $indexField)->inbox($row);
        } catch (
DATA_SQLTypeConstraintFailed $exception) {
           
$exception->setTable($this->table);
           
$exception->setField($indexField);
            throw
$exception;
        }
    }
   
    public function
buildWhereConditions($row) {
       
$indexField = $this->getIndexField();
        return
"`{$indexField}` = " . DATA_MySQL5_Access::prepareData($row);
    }
   
    public function
getAdditionalInsertFields($row) {
        return array(
           
$this->getIndexField() => $row
       
);
    }
}
?>