2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract database connectivity class.
|
|
|
|
* Sub-classes of this implement the actual database connection libraries
|
|
|
|
*/
|
|
|
|
abstract class Database extends Object {
|
|
|
|
/**
|
|
|
|
* Connection object to the database.
|
|
|
|
* @param resource
|
|
|
|
*/
|
|
|
|
static $globalConn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is false, then information about database operations
|
|
|
|
* will be displayed, eg creation of tables.
|
|
|
|
* @param boolean
|
|
|
|
*/
|
|
|
|
public static $supressOutput = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the given SQL query.
|
|
|
|
* This abstract function must be defined by subclasses as part of the actual implementation.
|
|
|
|
* It should return a subclass of Query as the result.
|
|
|
|
* @param string $sql The SQL query to execute
|
|
|
|
* @param int $errorLevel The level of error reporting to enable for the query
|
|
|
|
* @return Query
|
|
|
|
*/
|
|
|
|
abstract function query($sql, $errorLevel = E_USER_ERROR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the autogenerated ID from the previous INSERT query.
|
|
|
|
* @return int
|
|
|
|
*/
|
2007-09-14 03:35:54 +02:00
|
|
|
abstract function getGeneratedID($table);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the connection to the database is active.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
abstract function isActive();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the database and connect to it. This can be called if the
|
|
|
|
* initial database connection is not successful because the database
|
|
|
|
* does not exist.
|
2007-09-14 03:28:16 +02:00
|
|
|
* @param string $connect Connection string
|
|
|
|
* @param string $username Database username
|
|
|
|
* @param string $password Database Password
|
|
|
|
* @param string $database Database to which to create
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean Returns true if successful
|
|
|
|
*/
|
2007-09-14 03:28:16 +02:00
|
|
|
abstract function createDatabase($connect, $username, $password, $database);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the connection string from input
|
|
|
|
* @param array $parameters The connection details
|
|
|
|
* @return string $connect The connection string
|
|
|
|
**/
|
|
|
|
abstract function getConnect($parameters);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new table.
|
|
|
|
* The table will have a single field - the integer key ID.
|
|
|
|
* @param string $table Name of table to create.
|
|
|
|
*/
|
2007-08-15 04:50:39 +02:00
|
|
|
abstract function createTable($table, $fields = null, $indexes = null);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alter a table's schema.
|
|
|
|
*/
|
2007-09-14 03:44:34 +02:00
|
|
|
abstract function alterTable($table, $newFields = null, $newIndexes = null, $alteredFields = null, $alteredIndexes = null);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a table.
|
|
|
|
* @param string $oldTableName The old table name.
|
|
|
|
* @param string $newTableName The new table name.
|
|
|
|
*/
|
|
|
|
abstract function renameTable($oldTableName, $newTableName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new field on a table.
|
|
|
|
* @param string $table Name of the table.
|
|
|
|
* @param string $field Name of the field to add.
|
|
|
|
* @param string $spec The field specification, eg 'INTEGER NOT NULL'
|
|
|
|
*/
|
|
|
|
abstract function createField($table, $field, $spec);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all the fields for the given table.
|
|
|
|
* Returns a map of field name => field spec.
|
|
|
|
* @param string $table The table name.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected abstract function fieldList($table);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of all tables in the database.
|
|
|
|
* The table names will be in lower case.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected abstract function tableList();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The table list, generated by the tableList() function.
|
|
|
|
* Used by the requireTable() function.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $tableList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The field list, generated by the fieldList() function.
|
|
|
|
* An array of maps of field name => field spec, indexed
|
|
|
|
* by table name.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fieldList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The index list for each table, generated by the indexList() function.
|
|
|
|
* An map from table name to an array of index names.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $indexList;
|
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Large array structure that represents a schema update transaction
|
|
|
|
*/
|
|
|
|
protected $schemaUpdateTransaction;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a schema-updating transaction.
|
|
|
|
* All calls to requireTable/Field/Index will keep track of the changes requested, but not actually do anything.
|
|
|
|
* Once
|
|
|
|
*/
|
|
|
|
function beginSchemaUpdate() {
|
|
|
|
$this->tableList = $this->tableList();
|
|
|
|
$this->indexList = null;
|
|
|
|
$this->fieldList = null;
|
|
|
|
$this->schemaUpdateTransaction = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
function endSchemaUpdate() {
|
|
|
|
foreach($this->schemaUpdateTransaction as $tableName => $changes) {
|
|
|
|
switch($changes['command']) {
|
|
|
|
case 'create':
|
|
|
|
$this->createTable($tableName, $changes['newFields'], $changes['newIndexes']);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'alter':
|
|
|
|
$this->alterTable($tableName, $changes['newFields'], $changes['newIndexes'],
|
|
|
|
$changes['alteredFields'], $changes['alteredIndexes']);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->schemaUpdateTransaction = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transactional schema altering functions - they don't do anyhting except for update schemaUpdateTransaction
|
|
|
|
|
|
|
|
function transCreateTable($table) {
|
2007-08-16 08:33:41 +02:00
|
|
|
$this->schemaUpdateTransaction[$table] = array('command' => 'create', 'newFields' => array(), 'newIndexes' => array());
|
2007-08-15 04:50:39 +02:00
|
|
|
}
|
|
|
|
function transCreateField($table, $field, $schema) {
|
2007-08-16 08:33:41 +02:00
|
|
|
$this->transInitTable($table);
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->schemaUpdateTransaction[$table]['newFields'][$field] = $schema;
|
|
|
|
}
|
|
|
|
function transCreateIndex($table, $index, $schema) {
|
2007-08-21 05:40:09 +02:00
|
|
|
$this->transInitTable($table);
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->schemaUpdateTransaction[$table]['newIndexes'][$index] = $schema;
|
|
|
|
}
|
|
|
|
function transAlterField($table, $field, $schema) {
|
2007-08-21 05:40:09 +02:00
|
|
|
$this->transInitTable($table);
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->schemaUpdateTransaction[$table]['alteredFields'][$field] = $schema;
|
|
|
|
}
|
|
|
|
function transAlterIndex($table, $index, $schema) {
|
2007-08-21 05:40:09 +02:00
|
|
|
$this->transInitTable($table);
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->schemaUpdateTransaction[$table]['alteredIndexes'][$index] = $schema;
|
|
|
|
}
|
|
|
|
|
2007-08-16 08:33:41 +02:00
|
|
|
/**
|
|
|
|
* Handler for the other transXXX methods - mark the given table as being altered
|
|
|
|
* if it doesn't already exist
|
|
|
|
*/
|
|
|
|
protected function transInitTable($table) {
|
2007-08-17 06:44:44 +02:00
|
|
|
if(!isset($this->schemaUpdateTransaction[$table])) {
|
|
|
|
$this->schemaUpdateTransaction[$table] = array(
|
|
|
|
'command' => 'alter',
|
|
|
|
'newFields' => array(),
|
|
|
|
'newIndexes' => array(),
|
|
|
|
'alteredFields' => array(),
|
|
|
|
'alteredIndexes' => array(),
|
|
|
|
);
|
|
|
|
}
|
2007-08-16 08:33:41 +02:00
|
|
|
}
|
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Generate the following table in the database, modifying whatever already exists
|
|
|
|
* as necessary.
|
|
|
|
* @param string $table The name of the table
|
|
|
|
* @param string $fieldSchema A list of the fields to create, in the same form as DataObject::$db
|
|
|
|
* @param string $indexSchema A list of indexes to create. The keys of the array are the names of the index.
|
|
|
|
* The values of the array can be one of:
|
|
|
|
* - true: Create a single column index on the field named the same as the index.
|
|
|
|
* - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full
|
|
|
|
* control over the index.
|
|
|
|
*/
|
|
|
|
function requireTable($table, $fieldSchema = null, $indexSchema = null) {
|
|
|
|
if(!isset($this->tableList[strtolower($table)])) {
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->transCreateTable($table);
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Table $table: created","created");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
|
|
|
$this->checkAndRepairTable($table);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create custom fields
|
|
|
|
if($fieldSchema) {
|
|
|
|
foreach($fieldSchema as $fieldName => $fieldSpec) {
|
|
|
|
$fieldObj = eval(ViewableData::castingObjectCreator($fieldSpec));
|
|
|
|
$fieldObj->setTable($table);
|
|
|
|
$fieldObj->requireField();
|
|
|
|
}
|
2007-08-15 04:50:39 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Create custom indexes
|
|
|
|
if($indexSchema) {
|
|
|
|
foreach($indexSchema as $indexName => $indexDetails) {
|
|
|
|
$this->requireIndex($table, $indexName, $indexDetails);
|
|
|
|
}
|
2007-09-15 02:03:12 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* If the given table exists, move it out of the way by renaming it to _obsolete_(tablename).
|
|
|
|
* @param string $table The table name.
|
|
|
|
*/
|
|
|
|
function dontRequireTable($table) {
|
|
|
|
if(!isset($this->tableList)) $this->tableList = $this->tableList();
|
|
|
|
if(isset($this->tableList[strtolower($table)])) {
|
|
|
|
while($this->tableList[strtolower("_obsolete_{$table}$suffix")]) {
|
|
|
|
$suffix = $suffix ? ($suffix+1) : 2;
|
2007-09-15 02:03:12 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->renameTable($table, "_obsolete_{$table}$suffix");
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Table $table: renamed to _obsolete_{$table}$suffix","obsolete");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the given index in the database, modifying whatever already exists as necessary.
|
|
|
|
* @param string $table The table name.
|
|
|
|
* @param string $index The index name.
|
|
|
|
* @param string|boolean $spec The specification of the index. See requireTable() for more information.
|
|
|
|
*/
|
|
|
|
function requireIndex($table, $index, $spec) {
|
2007-08-17 06:44:44 +02:00
|
|
|
$newTable = false;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($spec === true) {
|
|
|
|
$spec = "($index)";
|
|
|
|
}
|
|
|
|
$spec = ereg_replace(" *, *",",",$spec);
|
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
if(!isset($this->tableList[strtolower($table)])) $newTable = true;
|
|
|
|
|
|
|
|
if(!$newTable && !isset($this->indexList[$table])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->indexList[$table] = $this->indexList($table);
|
|
|
|
}
|
2007-08-15 04:50:39 +02:00
|
|
|
if($newTable || !isset($this->indexList[$table][$index])) {
|
|
|
|
$this->transCreateIndex($table, $index, $spec);
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Index $table.$index: created as $spec","created");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else if($this->indexList[$table][$index] != $spec) {
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->transAlterIndex($table, $index, $spec);
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Index $table.$index: changed to $spec <i style=\"color: #AAA\">(from {$this->indexList[$table][$index]})</i>","changed");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the given field on the table, modifying whatever already exists as necessary.
|
|
|
|
* @param string $table The table name.
|
|
|
|
* @param string $field The field name.
|
|
|
|
* @param string $spec The field specification.
|
|
|
|
*/
|
|
|
|
function requireField($table, $field, $spec) {
|
2007-08-17 06:44:44 +02:00
|
|
|
$newTable = false;
|
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::mark('requireField');
|
2007-07-19 12:40:28 +02:00
|
|
|
// Collations didn't come in until MySQL 4.1. Anything earlier will throw a syntax error if you try and use
|
|
|
|
// collations.
|
|
|
|
if(!$this->supportsCollations()) {
|
|
|
|
$spec = eregi_replace(' *character set [^ ]+( collate [^ ]+)?( |$)','\\2',$spec);
|
|
|
|
}
|
2007-08-15 04:50:39 +02:00
|
|
|
if(!isset($this->tableList[strtolower($table)])) $newTable = true;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
if(!$newTable && !isset($this->fieldList[$table])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->fieldList[$table] = $this->fieldList($table);
|
|
|
|
}
|
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
if($newTable || !isset($this->fieldList[$table][$field])) {
|
|
|
|
Profiler::mark('createField');
|
|
|
|
$this->transCreateField($table, $field, $spec);
|
|
|
|
Profiler::unmark('createField');
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Field $table.$field: created as $spec","created");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else if($this->fieldList[$table][$field] != $spec) {
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::mark('alterField');
|
|
|
|
$this->transAlterField($table, $field, $spec);
|
|
|
|
Profiler::unmark('alterField');
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Field $table.$field: changed to $spec <i style=\"color: #AAA\">(from {$this->fieldList[$table][$field]})</i>","changed");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::unmark('requireField');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a complex manipulation on the database.
|
|
|
|
* A manipulation is an array of insert / or update sequences. The keys of the array are table names,
|
|
|
|
* and the values are map containing 'command' and 'fields'. Command should be 'insert' or 'update',
|
|
|
|
* and fields should be a map of field names to field values, including quotes. The field value can
|
|
|
|
* also be a SQL function or similar.
|
|
|
|
* @param array $manipulation
|
|
|
|
*/
|
|
|
|
function manipulate($manipulation) {
|
|
|
|
foreach($manipulation as $table => $writeInfo) {
|
|
|
|
if(isset($writeInfo['fields']) && $writeInfo['fields']) {
|
|
|
|
$fieldList = array();
|
|
|
|
foreach($writeInfo['fields'] as $fieldName => $fieldVal) {
|
|
|
|
$fieldList[] = "`$fieldName` = $fieldVal";
|
|
|
|
}
|
|
|
|
$fieldList = implode(", ", $fieldList);
|
|
|
|
|
|
|
|
if(!isset($writeInfo['where']) && isset($writeInfo['id'])) {
|
|
|
|
$writeInfo['where'] = "ID = $writeInfo[id]";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($writeInfo['command']) {
|
|
|
|
case "update":
|
|
|
|
$sql = "update `$table` SET $fieldList where $writeInfo[where]";
|
|
|
|
$this->query($sql);
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// If numAffectedRecord = 0, then we want to run instert instead
|
|
|
|
if(!$this->affectedRows()) {
|
|
|
|
if(!isset($writeInfo['fields']['ID']) && isset($writeInfo['id'])) {
|
|
|
|
$fieldList .= ", ID = $writeInfo[id]";
|
|
|
|
}
|
|
|
|
$sql = "insert into `$table` SET $fieldList";
|
2007-09-15 02:03:12 +02:00
|
|
|
$this->query($sql, null);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
break;
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
case "insert":
|
|
|
|
if(!isset($writeInfo['fields']['ID']) && isset($writeInfo['id'])) {
|
|
|
|
$fieldList .= ", ID = $writeInfo[id]";
|
|
|
|
}
|
|
|
|
$sql = "insert into `$table` SET $fieldList";
|
|
|
|
$this->query($sql);
|
|
|
|
break;
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
default:
|
|
|
|
$sql = null;
|
|
|
|
user_error("Database::manipulate() Can't recognise command '$writeInfo[command]'", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-15 02:03:12 +02:00
|
|
|
/**
|
2007-07-19 12:40:28 +02:00
|
|
|
* Error handler for database errors.
|
|
|
|
* All database errors will call this function to report the error. It isn't a static function;
|
|
|
|
* it will be called on the object itself and as such can be overridden in a subclass.
|
|
|
|
* @todo hook this into a more well-structured error handling system.
|
|
|
|
* @param string $msg The error message.
|
|
|
|
* @param int $errorLevel The level of the error to throw.
|
|
|
|
*/
|
|
|
|
function databaseError($msg, $errorLevel = E_USER_ERROR) {
|
|
|
|
user_error("DATABASE ERROR: $msg", $errorLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable supression of database messages.
|
|
|
|
*/
|
|
|
|
function quiet() {
|
|
|
|
Database::$supressOutput = true;
|
|
|
|
}
|
|
|
|
|
2007-08-24 05:31:14 +02:00
|
|
|
static function alteration_message($message,$type=""){
|
|
|
|
if(!Database::$supressOutput) {
|
|
|
|
$color = "";
|
|
|
|
switch ($type){
|
|
|
|
case "created":
|
|
|
|
$color = "green";
|
|
|
|
break;
|
|
|
|
case "obsolete":
|
|
|
|
$color = "red";
|
|
|
|
break;
|
|
|
|
case "error":
|
|
|
|
$color = "red";
|
|
|
|
break;
|
|
|
|
case "deleted":
|
|
|
|
$color = "red";
|
|
|
|
break;
|
|
|
|
case "changed":
|
|
|
|
$color = "blue";
|
|
|
|
break;
|
|
|
|
case "repaired":
|
|
|
|
$color = "blue";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$color="";
|
|
|
|
}
|
|
|
|
echo "<li style=\"color: $color\">$message</li>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract query-result class.
|
|
|
|
* Once again, this should be subclassed by an actual database implementation. It will only
|
|
|
|
* ever be constructed by a subclass of Database. The result of a database query - an iteratable object that's returned by DB::Query
|
|
|
|
*
|
|
|
|
* Primarily, the Query class takes care of the iterator plumbing, letting the subclasses focusing
|
|
|
|
* on providing the specific data-access methods that are required: {@link nextRecord()}, {@link numRecords()}
|
|
|
|
* and {@link seek()}
|
|
|
|
*/
|
|
|
|
abstract class Query extends Object implements Iterator {
|
|
|
|
/**
|
|
|
|
* The current record in the interator.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $currentRecord = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of the current row in the interator.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $rowNum = -1;
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return an array containing all values in the leftmost column.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function column() {
|
|
|
|
foreach($this as $record) {
|
|
|
|
$column[] = reset($record);
|
|
|
|
}
|
|
|
|
return isset($column) ? $column : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array containing all values in the leftmost column, where the keys are the
|
|
|
|
* same as the values.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function keyedColumn() {
|
|
|
|
foreach($this as $record) {
|
|
|
|
$val = reset($record);
|
|
|
|
$column[$val] = $val;
|
|
|
|
}
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a map from the first column to the second column.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function map() {
|
|
|
|
foreach($this as $record) {
|
|
|
|
$key = reset($record);
|
|
|
|
$val = next($record);
|
|
|
|
$column[$key] = $val;
|
|
|
|
}
|
|
|
|
return $column;
|
|
|
|
}
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the next record in the iterator.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function record() {
|
|
|
|
return $this->next();
|
|
|
|
}
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the first column of the first record.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function value() {
|
|
|
|
foreach($this as $record) {
|
|
|
|
return reset($record);
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-08-16 08:33:41 +02:00
|
|
|
/**
|
|
|
|
* Return an HTML table containing the full result-set
|
|
|
|
*/
|
|
|
|
public function table() {
|
|
|
|
$first = true;
|
|
|
|
$result = "<table>\n";
|
|
|
|
|
|
|
|
foreach($this as $record) {
|
|
|
|
if($first) {
|
|
|
|
$result .= "<tr>";
|
|
|
|
foreach($record as $k => $v) {
|
|
|
|
$result .= "<th>" . Convert::raw2xml($k) . "</th> ";
|
|
|
|
}
|
|
|
|
$result .= "</tr> \n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result .= "<tr>";
|
|
|
|
foreach($record as $k => $v) {
|
|
|
|
$result .= "<td>" . Convert::raw2xml($v) . "</td> ";
|
|
|
|
}
|
|
|
|
$result .= "</tr> \n";
|
|
|
|
|
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($first) return "No records found";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Iterator function implementation. Rewind the iterator to the first item and return it.
|
|
|
|
* Makes use of {@link seek()} and {@link numRecords()}, takes care of the plumbing.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rewind() {
|
|
|
|
if($this->numRecords() > 0) {
|
|
|
|
return $this->seek(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator function implementation. Return the current item of the iterator.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function current() {
|
|
|
|
if(!$this->currentRecord) {
|
|
|
|
return $this->next();
|
|
|
|
} else {
|
|
|
|
return $this->currentRecord;
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Iterator function implementation. Return the first item of this iterator.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function first() {
|
|
|
|
$this->rewind();
|
|
|
|
return $this->current();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator function implementation. Return the row number of the current item.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function key() {
|
|
|
|
return $this->rowNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator function implementation. Return the next record in the iterator.
|
|
|
|
* Makes use of {@link nextRecord()}, takes care of the plumbing.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function next() {
|
2007-09-15 02:03:12 +02:00
|
|
|
$this->currentRecord = $this->nextRecord();
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->rowNum++;
|
|
|
|
return $this->currentRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterator function implementation. Check if the iterator is pointing to a valid item.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function valid() {
|
|
|
|
return $this->current() !== false;
|
|
|
|
}
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the next record in the query result.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract function nextRecord();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the total number of items in the query result.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
abstract function numRecords();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to a specific row number in the query result and return the record.
|
|
|
|
* @param int $rowNum Tow number to go to.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract function seek($rowNum);
|
|
|
|
}
|
|
|
|
|
2007-09-15 02:03:12 +02:00
|
|
|
?>
|