2009-03-25 04:18:09 +01:00
|
|
|
<?php
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Abstract database connectivity class.
|
|
|
|
* Sub-classes of this implement the actual database connection libraries
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-08-08 04:55:32 +02:00
|
|
|
abstract class Database {
|
2009-05-11 01:55:38 +02:00
|
|
|
/**
|
|
|
|
* This constant was added in SilverStripe 2.4 to indicate that SQL-queries
|
|
|
|
* should now use ANSI-compatible syntax. The most notable affect of this
|
|
|
|
* change is that table and field names should be escaped with double quotes
|
|
|
|
* and not backticks
|
|
|
|
*/
|
|
|
|
const USE_ANSI_SQL = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* It takes no parameters, and should create the database from the information
|
|
|
|
* specified in the constructor.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return boolean Returns true if successful
|
|
|
|
*/
|
2008-02-25 03:10:37 +01:00
|
|
|
abstract function createDatabase();
|
2007-09-14 03:28:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the connection string from input
|
|
|
|
* @param array $parameters The connection details
|
|
|
|
* @return string $connect The connection string
|
|
|
|
**/
|
2008-02-25 03:10:37 +01:00
|
|
|
abstract function getConnect($parameters);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new table.
|
2009-05-21 07:08:11 +02:00
|
|
|
* @param $tableName The name of the table
|
|
|
|
* @param $fields A map of field names to field types
|
|
|
|
* @param $indexes A map of indexes
|
|
|
|
* @param $options An map of additional options. The available keys are as follows:
|
|
|
|
* - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" for MySQL.
|
|
|
|
* - 'temporary' - If true, then a temporary table will be created
|
|
|
|
* @return The table name generated. This may be different from the table name, for example with temporary tables.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-05-19 05:55:14 +02:00
|
|
|
abstract function createTable($table, $fields = null, $indexes = null, $options = null);
|
2007-08-15 04:50:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2008-11-10 00:56:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the database column name of the given field.
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the tbale the field is in.
|
|
|
|
* @param string $oldName The name of the field to change.
|
|
|
|
* @param string $newName The new name of the field
|
|
|
|
*/
|
|
|
|
abstract function renameField($tableName, $oldName, $newName);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
2008-11-24 00:20:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given table exists in the database
|
|
|
|
*/
|
|
|
|
abstract function hasTable($tableName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the enum values available on the given field
|
|
|
|
*/
|
|
|
|
abstract function enumValuesForField($tableName, $fieldName);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2009-08-08 04:55:32 +02:00
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
/**
|
|
|
|
* 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() {
|
2009-05-07 06:30:54 +02:00
|
|
|
$this->tableList = array();
|
|
|
|
$tables = $this->tableList();
|
|
|
|
foreach($tables as $table) $this->tableList[strtolower($table)] = strtolower($table);
|
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->indexList = null;
|
|
|
|
$this->fieldList = null;
|
|
|
|
$this->schemaUpdateTransaction = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
function endSchemaUpdate() {
|
|
|
|
foreach($this->schemaUpdateTransaction as $tableName => $changes) {
|
|
|
|
switch($changes['command']) {
|
|
|
|
case 'create':
|
2009-05-19 05:55:14 +02:00
|
|
|
$this->createTable($tableName, $changes['newFields'], $changes['newIndexes'], $changes['options']);
|
2007-08-15 04:50:39 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'alter':
|
|
|
|
$this->alterTable($tableName, $changes['newFields'], $changes['newIndexes'],
|
2009-05-19 05:55:14 +02:00
|
|
|
$changes['alteredFields'], $changes['alteredIndexes'], $changes['alteredOptions']);
|
2007-08-15 04:50:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->schemaUpdateTransaction = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transactional schema altering functions - they don't do anyhting except for update schemaUpdateTransaction
|
|
|
|
|
2009-05-19 05:55:14 +02:00
|
|
|
/**
|
|
|
|
* @param string $table
|
|
|
|
* @param string $options
|
|
|
|
*/
|
|
|
|
function transCreateTable($table, $options = null) {
|
|
|
|
$this->schemaUpdateTransaction[$table] = array('command' => 'create', 'newFields' => array(), 'newIndexes' => array(), 'options' => $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $table
|
|
|
|
* @param array $options
|
|
|
|
*/
|
|
|
|
function transAlterTable($table, $options) {
|
|
|
|
$this->transInitTable($table);
|
|
|
|
$this->schemaUpdateTransaction[$table]['alteredOptions'] = $options;
|
2007-08-15 04:50:39 +02:00
|
|
|
}
|
2009-05-19 05:55:14 +02:00
|
|
|
|
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(),
|
2009-05-19 05:55:14 +02:00
|
|
|
'alteredOptions' => ''
|
2007-08-17 06:44:44 +02:00
|
|
|
);
|
|
|
|
}
|
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.
|
2009-05-19 05:55:14 +02:00
|
|
|
* @todo Change detection for CREATE TABLE $options other than "Engine"
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @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
|
2009-03-12 12:11:35 +01:00
|
|
|
* @param string $indexSchema A list of indexes to create. See {@link requireIndex()}
|
2009-05-19 05:55:14 +02:00
|
|
|
* @param array $options
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-05-19 05:55:14 +02:00
|
|
|
function requireTable($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK=true, $options = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!isset($this->tableList[strtolower($table)])) {
|
2009-05-19 05:55:14 +02:00
|
|
|
$this->transCreateTable($table, $options);
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Table $table: created","created");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-05-19 05:55:14 +02:00
|
|
|
$this->checkAndRepairTable($table, $options);
|
|
|
|
|
|
|
|
// Check if options changed
|
2009-08-08 04:55:32 +02:00
|
|
|
if($options && isset($options[get_class($this)])) {
|
2009-05-19 05:55:14 +02:00
|
|
|
$tableOptionsChanged = false;
|
2009-08-08 04:55:32 +02:00
|
|
|
if(preg_match('/ENGINE=([^\s]*)/', $options[get_class($this)], $alteredEngineMatches)) {
|
2009-05-19 05:55:14 +02:00
|
|
|
$alteredEngine = $alteredEngineMatches[1];
|
|
|
|
$tableStatus = DB::query(sprintf(
|
|
|
|
'SHOW TABLE STATUS WHERE "Name" = \'%s\'',
|
|
|
|
$table
|
|
|
|
))->first();
|
|
|
|
$tableOptionsChanged = ($tableStatus['Engine'] != $alteredEngine);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($tableOptionsChanged) {
|
|
|
|
$this->transAlterTable($table, $options);
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2009-02-13 03:37:58 +01:00
|
|
|
//DB ABSTRACTION: we need to convert this to a db-specific version:
|
2009-03-11 22:48:59 +01:00
|
|
|
$this->requireField($table, 'ID', DB::getConn()->IdColumn(false, $hasAutoIncPK));
|
2009-02-13 03:37:58 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Create custom fields
|
|
|
|
if($fieldSchema) {
|
|
|
|
foreach($fieldSchema as $fieldName => $fieldSpec) {
|
|
|
|
$fieldObj = eval(ViewableData::castingObjectCreator($fieldSpec));
|
|
|
|
$fieldObj->setTable($table);
|
|
|
|
$fieldObj->requireField();
|
|
|
|
}
|
2009-05-19 05:55:14 +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[strtolower($table)])) {
|
2008-04-05 03:39:13 +02:00
|
|
|
$suffix = '';
|
|
|
|
while(isset($this->tableList[strtolower("_obsolete_{$table}$suffix")])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$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.
|
2009-03-12 12:11:35 +01:00
|
|
|
*
|
|
|
|
* 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('type' => 'index|unique|fulltext', 'value' => 'FieldA, FieldB'): This gives you full
|
|
|
|
* control over the index.
|
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @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;
|
|
|
|
|
2008-12-17 01:40:24 +01:00
|
|
|
//DB Abstraction: remove this ===true option as a possibility?
|
2007-07-19 12:40:28 +02:00
|
|
|
if($spec === true) {
|
|
|
|
$spec = "($index)";
|
|
|
|
}
|
2008-12-17 01:40:24 +01:00
|
|
|
|
|
|
|
//Indexes specified as arrays cannot be checked with this line: (it flattens out the array)
|
|
|
|
if(!is_array($spec))
|
|
|
|
$spec = ereg_replace(" *, *",",",$spec);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
if(!isset($this->tableList[strtolower($table)])) $newTable = true;
|
|
|
|
|
2009-03-10 03:02:14 +01:00
|
|
|
if(!$newTable && !isset($this->indexList[$table])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->indexList[$table] = $this->indexList($table);
|
|
|
|
}
|
2009-03-17 23:22:46 +01:00
|
|
|
|
2009-03-04 22:54:33 +01:00
|
|
|
//Fix up the index for database purposes
|
|
|
|
$index=DB::getConn()->getDbSqlDefinition($table, $index, null, true);
|
|
|
|
|
2009-03-17 23:22:46 +01:00
|
|
|
//Fix the key for database purposes
|
|
|
|
$index_alt=DB::getConn()->modifyIndex($index, $spec);
|
|
|
|
|
2009-03-10 03:02:14 +01:00
|
|
|
if(!$newTable) {
|
2009-03-23 02:00:33 +01:00
|
|
|
if(isset($this->indexList[$table][$index_alt])) {
|
|
|
|
if(is_array($this->indexList[$table][$index_alt])) {
|
|
|
|
$array_spec = $this->indexList[$table][$index_alt]['spec'];
|
|
|
|
} else {
|
|
|
|
$array_spec = $this->indexList[$table][$index_alt];
|
|
|
|
}
|
2009-03-10 03:02:14 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-17 06:02:40 +01:00
|
|
|
|
|
|
|
if($newTable || !isset($this->indexList[$table][$index_alt])) {
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->transCreateIndex($table, $index, $spec);
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Index $table.$index: created as $spec","created");
|
2009-03-04 22:54:33 +01:00
|
|
|
} else if($array_spec != DB::getConn()->convertIndexSpec($spec)) {
|
2007-08-15 04:50:39 +02:00
|
|
|
$this->transAlterIndex($table, $index, $spec);
|
2008-12-17 01:40:24 +01:00
|
|
|
$spec_msg=DB::getConn()->convertIndexSpec($spec);
|
2009-03-04 22:54:33 +01:00
|
|
|
Database::alteration_message("Index $table.$index: changed to $spec_msg <i style=\"color: #AAA\">(from {$array_spec})</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.
|
2009-01-11 23:20:51 +01:00
|
|
|
* @param array|string $spec The field specification. If passed in array syntax, the specific database
|
|
|
|
* driver takes care of the ALTER TABLE syntax. If passed as a string, its assumed to
|
|
|
|
* be prepared as a direct SQL framgment ready for insertion into ALTER TABLE. In this case you'll
|
|
|
|
* need to take care of database abstraction in your DBField subclass.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function requireField($table, $field, $spec) {
|
2009-02-17 03:49:39 +01:00
|
|
|
//TODO: this is starting to get extremely fragmented.
|
|
|
|
//There are two different versions of $spec floating around, and their content changes depending
|
|
|
|
//on how they are structured. This needs to be tidied up.
|
2009-03-25 04:17:19 +01:00
|
|
|
$fieldValue = null;
|
2007-08-17 06:44:44 +02:00
|
|
|
$newTable = false;
|
2009-01-11 23:20:51 +01:00
|
|
|
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::mark('requireField');
|
2008-11-23 02:01:03 +01:00
|
|
|
|
2009-01-11 23:20:51 +01:00
|
|
|
// backwards compatibility patch for pre 2.4 requireField() calls
|
2009-02-17 03:49:39 +01:00
|
|
|
$spec_orig=$spec;
|
2009-01-11 23:20:51 +01:00
|
|
|
if(!is_string($spec)) {
|
|
|
|
// TODO: This is tempororary
|
|
|
|
$spec['parts']['name'] = $field;
|
2009-02-17 03:49:39 +01:00
|
|
|
$spec_orig['parts']['name'] = $field;
|
2009-01-11 23:20:51 +01:00
|
|
|
//Convert the $spec array into a database-specific string
|
2009-02-17 03:49:39 +01:00
|
|
|
$spec=DB::getConn()->$spec['type']($spec['parts'], true);
|
2009-01-11 23:20:51 +01:00
|
|
|
}
|
2009-02-17 04:54:39 +01:00
|
|
|
|
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.
|
2009-03-12 03:43:10 +01:00
|
|
|
// TODO: move this to the MySQLDatabase file, or drop it altogether?
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$this->supportsCollations()) {
|
|
|
|
$spec = eregi_replace(' *character set [^ ]+( collate [^ ]+)?( |$)','\\2',$spec);
|
|
|
|
}
|
2009-03-12 03:43:10 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-03-03 22:45:54 +01:00
|
|
|
// Get the value of this field.
|
2009-09-17 02:08:57 +02:00
|
|
|
if(is_array($spec)){
|
2009-02-17 03:49:39 +01:00
|
|
|
$specValue=$spec['data_type'];
|
2009-09-17 02:08:57 +02:00
|
|
|
if(isset($spec['precision']))
|
|
|
|
$specValue.='(' . $spec['precision'] . ')';
|
|
|
|
} else $specValue=$spec;
|
2009-03-03 22:45:54 +01:00
|
|
|
|
|
|
|
// We need to get db-specific versions of the ID column:
|
2009-03-12 03:43:10 +01:00
|
|
|
if($spec_orig==DB::getConn()->IdColumn() || $spec_orig==DB::getConn()->IdColumn(true))
|
2009-03-03 22:45:54 +01:00
|
|
|
$specValue=DB::getConn()->IdColumn(true);
|
2009-03-12 03:43:10 +01:00
|
|
|
|
2009-03-10 03:02:14 +01:00
|
|
|
if(!$newTable) {
|
2009-03-25 04:17:19 +01:00
|
|
|
if(isset($this->fieldList[$table][$field])) {
|
|
|
|
if(is_array($this->fieldList[$table][$field])) {
|
|
|
|
$fieldValue = $this->fieldList[$table][$field]['data_type'];
|
|
|
|
} else {
|
|
|
|
$fieldValue = $this->fieldList[$table][$field];
|
|
|
|
}
|
2009-03-10 03:02:14 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-12 03:43:10 +01:00
|
|
|
|
2009-03-03 22:45:54 +01:00
|
|
|
// Get the version of the field as we would create it. This is used for comparison purposes to see if the
|
|
|
|
// existing field is different to what we now want
|
|
|
|
if(is_array($spec_orig)) {
|
2009-02-17 03:49:39 +01:00
|
|
|
$spec_orig=DB::getConn()->$spec_orig['type']($spec_orig['parts']);
|
2009-02-17 04:54:39 +01:00
|
|
|
}
|
|
|
|
|
2009-02-17 03:49:39 +01:00
|
|
|
if($newTable || $fieldValue=='') {
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::mark('createField');
|
2009-02-17 03:49:39 +01:00
|
|
|
|
|
|
|
$this->transCreateField($table, $field, $spec_orig);
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::unmark('createField');
|
2009-02-17 03:49:39 +01:00
|
|
|
Database::alteration_message("Field $table.$field: created as $spec_orig","created");
|
|
|
|
} else if($fieldValue != $specValue) {
|
2008-02-25 03:10:37 +01:00
|
|
|
// If enums are being modified, then we need to fix existing data in the table.
|
|
|
|
// Update any records where the enum is set to a legacy value to be set to the default.
|
|
|
|
// One hard-coded exception is SiteTree - the default for this is Page.
|
2009-02-17 03:49:39 +01:00
|
|
|
|
|
|
|
if(substr($specValue, 0, 4) == "enum") {
|
|
|
|
$newStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","",$spec_orig);
|
2008-12-01 06:03:34 +01:00
|
|
|
$new = preg_split("/'\s*,\s*'/", $newStr);
|
|
|
|
|
2009-02-17 03:49:39 +01:00
|
|
|
$oldStr = preg_replace("/(^enum\s*\(')|('$\).*)/i","", $fieldValue);
|
2008-12-01 06:03:34 +01:00
|
|
|
$old = preg_split("/'\s*,\s*'/", $newStr);
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
$holder = array();
|
|
|
|
foreach($old as $check) {
|
|
|
|
if(!in_array($check, $new)) {
|
|
|
|
$holder[] = $check;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(count($holder)) {
|
2009-02-17 03:49:39 +01:00
|
|
|
$default = explode('default ', $spec_orig);
|
2008-02-25 03:10:37 +01:00
|
|
|
$default = $default[1];
|
|
|
|
if($default == "'SiteTree'") $default = "'Page'";
|
2008-11-23 01:31:06 +01:00
|
|
|
$query = "UPDATE \"$table\" SET $field=$default WHERE $field IN (";
|
2008-02-25 03:10:37 +01:00
|
|
|
for($i=0;$i+1<count($holder);$i++) {
|
|
|
|
$query .= "'{$holder[$i]}', ";
|
|
|
|
}
|
|
|
|
$query .= "'{$holder[$i]}')";
|
|
|
|
DB::query($query);
|
|
|
|
$amount = DB::affectedRows();
|
|
|
|
Database::alteration_message("Changed $amount rows to default value of field $field (Value: $default)");
|
|
|
|
}
|
|
|
|
}
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::mark('alterField');
|
2009-02-17 03:49:39 +01:00
|
|
|
$this->transAlterField($table, $field, $spec_orig);
|
2007-08-15 04:50:39 +02:00
|
|
|
Profiler::unmark('alterField');
|
2009-02-17 03:49:39 +01:00
|
|
|
Database::alteration_message("Field $table.$field: changed to $spec_orig <i style=\"color: #AAA\">(from {$fieldValue})</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
|
|
|
}
|
2008-11-10 00:56:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the given field exists, move it out of the way by renaming it to _obsolete_(fieldname).
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param string $fieldName
|
|
|
|
*/
|
|
|
|
function dontRequireField($table, $fieldName) {
|
|
|
|
$fieldList = $this->fieldList($table);
|
|
|
|
if(array_key_exists($fieldName, $fieldList)) {
|
|
|
|
$suffix = '';
|
|
|
|
while(isset($fieldList[strtolower("_obsolete_{$fieldName}$suffix")])) {
|
|
|
|
$suffix = $suffix ? ($suffix+1) : 2;
|
|
|
|
}
|
|
|
|
$this->renameField($table, $fieldName, "_obsolete_{$fieldName}$suffix");
|
|
|
|
Database::alteration_message("Field $table.$fieldName: renamed to $table._obsolete_{$fieldName}$suffix","obsolete");
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2009-03-25 23:26:03 +01:00
|
|
|
if($manipulation) foreach($manipulation as $table => $writeInfo) {
|
2009-03-11 22:48:59 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($writeInfo['fields']) && $writeInfo['fields']) {
|
2008-11-24 00:20:02 +01:00
|
|
|
$fieldList = $columnList = $valueList = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($writeInfo['fields'] as $fieldName => $fieldVal) {
|
2008-11-23 01:31:06 +01:00
|
|
|
$fieldList[] = "\"$fieldName\" = $fieldVal";
|
2008-11-24 00:20:02 +01:00
|
|
|
$columnList[] = "\"$fieldName\"";
|
|
|
|
|
|
|
|
// Empty strings inserted as null in INSERTs. Replacement of Database::replace_with_null().
|
|
|
|
if($fieldVal === "''") $valueList[] = "null";
|
|
|
|
else $valueList[] = $fieldVal;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!isset($writeInfo['where']) && isset($writeInfo['id'])) {
|
2008-11-24 00:20:02 +01:00
|
|
|
$writeInfo['where'] = "\"ID\" = " . (int)$writeInfo['id'];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch($writeInfo['command']) {
|
|
|
|
case "update":
|
2008-11-24 20:28:46 +01:00
|
|
|
// Test to see if this update query shouldn't, in fact, be an insert
|
|
|
|
if($this->query("SELECT \"ID\" FROM \"$table\" WHERE $writeInfo[where]")->value()) {
|
|
|
|
$fieldList = implode(", ", $fieldList);
|
2009-04-27 03:34:23 +02:00
|
|
|
$sql = "UPDATE \"$table\" SET $fieldList where $writeInfo[where]";
|
2008-11-24 20:28:46 +01:00
|
|
|
$this->query($sql);
|
|
|
|
break;
|
|
|
|
}
|
2008-11-24 00:20:02 +01:00
|
|
|
|
2008-11-24 20:28:46 +01:00
|
|
|
// ...if not, we'll skip on to the insert code
|
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'])) {
|
2008-11-24 10:31:14 +01:00
|
|
|
$columnList[] = "\"ID\"";
|
2008-11-24 00:20:02 +01:00
|
|
|
$valueList[] = (int)$writeInfo['id'];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-03-11 22:48:59 +01:00
|
|
|
|
2008-11-24 00:20:02 +01:00
|
|
|
$columnList = implode(", ", $columnList);
|
|
|
|
$valueList = implode(", ", $valueList);
|
2009-04-27 03:34:23 +02:00
|
|
|
$sql = "INSERT INTO \"$table\" ($columnList) VALUES ($valueList)";
|
2007-07-19 12:40:28 +02:00
|
|
|
$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-10-02 06:46:33 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/** Replaces "\'\'" with "null", recursively walks through the given array.
|
2007-10-02 06:46:33 +02:00
|
|
|
* @param string $array Array where the replacement should happen
|
|
|
|
*/
|
|
|
|
static function replace_with_null(&$array) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$array = ereg_replace('= *\'\'', "= null", $array);
|
2007-10-02 06:46:33 +02:00
|
|
|
|
|
|
|
if(is_array($array)) {
|
|
|
|
foreach($array as $key => $value) {
|
|
|
|
if(is_array($value)) {
|
|
|
|
array_walk($array, array(Database, 'replace_with_null'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
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) {
|
2008-09-16 20:12:07 +02:00
|
|
|
user_error($msg, $errorLevel);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable supression of database messages.
|
|
|
|
*/
|
|
|
|
function quiet() {
|
|
|
|
Database::$supressOutput = true;
|
|
|
|
}
|
|
|
|
|
2007-08-24 05:31:14 +02:00
|
|
|
static function alteration_message($message,$type=""){
|
2008-11-24 20:28:46 +01:00
|
|
|
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="";
|
|
|
|
}
|
2009-04-11 07:00:41 +02:00
|
|
|
echo (Director::is_cli()) ? " * ".strip_tags($message)."\n" : "<li style=\"color: $color\">$message</li>";
|
2008-11-24 20:28:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a SQLQuery object into a SQL statement
|
|
|
|
*/
|
|
|
|
public function sqlQueryToString(SQLQuery $sqlQuery) {
|
|
|
|
if (!$sqlQuery->from) return '';
|
|
|
|
$distinct = $sqlQuery->distinct ? "DISTINCT " : "";
|
|
|
|
if($sqlQuery->delete) {
|
|
|
|
$text = "DELETE ";
|
|
|
|
} else if($sqlQuery->select) {
|
|
|
|
$text = "SELECT $distinct" . implode(", ", $sqlQuery->select);
|
|
|
|
}
|
|
|
|
$text .= " FROM " . implode(" ", $sqlQuery->from);
|
|
|
|
|
|
|
|
if($sqlQuery->where) $text .= " WHERE (" . $sqlQuery->getFilter(). ")";
|
|
|
|
if($sqlQuery->groupby) $text .= " GROUP BY " . implode(", ", $sqlQuery->groupby);
|
|
|
|
if($sqlQuery->having) $text .= " HAVING ( " . implode(" ) AND ( ", $sqlQuery->having) . " )";
|
|
|
|
if($sqlQuery->orderby) $text .= " ORDER BY " . $sqlQuery->orderby;
|
|
|
|
|
|
|
|
if($sqlQuery->limit) {
|
|
|
|
$limit = $sqlQuery->limit;
|
|
|
|
// Pass limit as array or SQL string value
|
|
|
|
if(is_array($limit)) {
|
|
|
|
if(!array_key_exists('limit',$limit)) user_error('SQLQuery::limit(): Wrong format for $limit', E_USER_ERROR);
|
|
|
|
|
|
|
|
if(isset($limit['start']) && is_numeric($limit['start']) && isset($limit['limit']) && is_numeric($limit['limit'])) {
|
|
|
|
$combinedLimit = "$limit[limit] OFFSET $limit[start]";
|
|
|
|
} elseif(isset($limit['limit']) && is_numeric($limit['limit'])) {
|
|
|
|
$combinedLimit = (int)$limit['limit'];
|
|
|
|
} else {
|
|
|
|
$combinedLimit = false;
|
2007-08-24 05:31:14 +02:00
|
|
|
}
|
2008-11-24 20:28:46 +01:00
|
|
|
if(!empty($combinedLimit)) $text .= " LIMIT " . $combinedLimit;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$text .= " LIMIT " . $sqlQuery->limit;
|
2007-08-24 05:31:14 +02:00
|
|
|
}
|
2008-11-24 20:28:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
2007-08-24 05:31:14 +02:00
|
|
|
}
|
|
|
|
|
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()}
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-08-08 04:55:32 +02:00
|
|
|
abstract class Query implements Iterator {
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
2009-05-28 04:06:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to keep track of whether iteration has begun, to prevent unnecessary seeks
|
|
|
|
*/
|
|
|
|
private $queryHasBegun = false;
|
2007-09-15 02:03:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-06-08 02:17:37 +02:00
|
|
|
* Return an array containing all the values from a specific column. If no column is set, then the first will be
|
|
|
|
* returned
|
|
|
|
*
|
|
|
|
* @param string $column
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2009-06-08 02:17:37 +02:00
|
|
|
public function column($column = null) {
|
|
|
|
$result = array();
|
|
|
|
|
2009-08-11 10:51:05 +02:00
|
|
|
while($record = $this->next()) {
|
|
|
|
if($column) $result[] = $record[$column];
|
|
|
|
else $result[] = $record[key($record)];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-08-11 10:51:05 +02:00
|
|
|
|
2009-06-08 02:17:37 +02:00
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array containing all values in the leftmost column, where the keys are the
|
|
|
|
* same as the values.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function keyedColumn() {
|
2009-02-02 00:49:53 +01:00
|
|
|
$column = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($this as $record) {
|
2009-08-11 10:51:05 +02:00
|
|
|
$val = $record[key($record)];
|
2007-07-19 12:40:28 +02:00
|
|
|
$column[$val] = $val;
|
|
|
|
}
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a map from the first column to the second column.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function map() {
|
2009-02-02 00:49:53 +01:00
|
|
|
$column = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
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() {
|
2009-08-11 10:51:05 +02:00
|
|
|
$record = $this->next();
|
|
|
|
if($record) return $record[key($record)];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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() {
|
2009-05-28 04:06:12 +02:00
|
|
|
if($this->queryHasBegun && $this->numRecords() > 0) {
|
|
|
|
$this->queryHasBegun = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
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() {
|
2009-05-28 04:06:12 +02:00
|
|
|
$this->queryHasBegun = true;
|
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() {
|
2009-08-11 10:51:05 +02:00
|
|
|
if(!$this->currentRecord) $this->next();
|
|
|
|
return $this->currentRecord !== false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2009-01-11 23:20:51 +01:00
|
|
|
?>
|