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(); /** * 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); /** * 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; /** * 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) { $this->schemaUpdateTransaction[$table] = array('command' => 'create', 'newFields' => array(), 'newIndexes' => array()); } function transCreateField($table, $field, $schema) { $this->transInitTable($table); $this->schemaUpdateTransaction[$table]['newFields'][$field] = $schema; } function transCreateIndex($table, $index, $schema) { $this->transInitTable($table); $this->schemaUpdateTransaction[$table]['newIndexes'][$index] = $schema; } function transAlterField($table, $field, $schema) { $this->transInitTable($table); $this->schemaUpdateTransaction[$table]['alteredFields'][$field] = $schema; } function transAlterIndex($table, $index, $schema) { $this->transInitTable($table); $this->schemaUpdateTransaction[$table]['alteredIndexes'][$index] = $schema; } /** * Handler for the other transXXX methods - mark the given table as being altered * if it doesn't already exist */ protected function transInitTable($table) { if(!isset($this->schemaUpdateTransaction[$table])) { $this->schemaUpdateTransaction[$table] = array( 'command' => 'alter', 'newFields' => array(), 'newIndexes' => array(), 'alteredFields' => array(), 'alteredIndexes' => array(), ); } } /** * 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)])) { $this->transCreateTable($table); Database::alteration_message("Table $table: created","created"); } else { $this->checkAndRepairTable($table); } // Create custom fields if($fieldSchema) { foreach($fieldSchema as $fieldName => $fieldSpec) { $fieldObj = eval(ViewableData::castingObjectCreator($fieldSpec)); $fieldObj->setTable($table); $fieldObj->requireField(); } } // Create custom indexes if($indexSchema) { foreach($indexSchema as $indexName => $indexDetails) { $this->requireIndex($table, $indexName, $indexDetails); } } } /** * 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)])) { $suffix = ''; while(isset($this->tableList[strtolower("_obsolete_{$table}$suffix")])) { $suffix = $suffix ? ($suffix+1) : 2; } $this->renameTable($table, "_obsolete_{$table}$suffix"); Database::alteration_message("Table $table: renamed to _obsolete_{$table}$suffix","obsolete"); } } /** * 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) { $newTable = false; if($spec === true) { $spec = "($index)"; } $spec = ereg_replace(" *, *",",",$spec); if(!isset($this->tableList[strtolower($table)])) $newTable = true; if(!$newTable && !isset($this->indexList[$table])) { $this->indexList[$table] = $this->indexList($table); } if($newTable || !isset($this->indexList[$table][$index])) { $this->transCreateIndex($table, $index, $spec); Database::alteration_message("Index $table.$index: created as $spec","created"); } else if($this->indexList[$table][$index] != $spec) { $this->transAlterIndex($table, $index, $spec); Database::alteration_message("Index $table.$index: changed to $spec (from {$this->indexList[$table][$index]})","changed"); } } /** * 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) { $newTable = false; Profiler::mark('requireField'); // TODO: This is tempororary $spec['parts']['name'] = $field; //Convert the $spec array into a database-specific string $spec=DB::getConn()->$spec['type']($spec['parts']); // 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); } if(!isset($this->tableList[strtolower($table)])) $newTable = true; if(!$newTable && !isset($this->fieldList[$table])) { $this->fieldList[$table] = $this->fieldList($table); } if($newTable || !isset($this->fieldList[$table][$field])) { Profiler::mark('createField'); $this->transCreateField($table, $field, $spec); Profiler::unmark('createField'); Database::alteration_message("Field $table.$field: created as $spec","created"); } else if($this->fieldList[$table][$field] != $spec) { // 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. if(substr($spec, 0, 4) == "enum") { $new = substr($spec, 5); $old = substr($this->fieldList[$table][$field], 5); $new = substr($new, 0, strpos($new, ')')); $old = substr($old, 0, strpos($old, ')')); $new = str_replace("'", '', $new); $old = str_replace("'", '', $old); $new = explode(',', $new); $old = explode(',', $old); $holder = array(); foreach($old as $check) { if(!in_array($check, $new)) { $holder[] = $check; } } if(count($holder)) { $default = explode('default ', $spec); $default = $default[1]; if($default == "'SiteTree'") $default = "'Page'"; $query = "UPDATE \"$table\" SET $field=$default WHERE $field IN ("; for($i=0;$i+1transAlterField($table, $field, $spec); Profiler::unmark('alterField'); Database::alteration_message("Field $table.$field: changed to $spec (from {$this->fieldList[$table][$field]})","changed"); } Profiler::unmark('requireField'); } /** * 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"); } } /** * 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 = $columnList = $valueList = array(); foreach($writeInfo['fields'] as $fieldName => $fieldVal) { $fieldList[] = "\"$fieldName\" = $fieldVal"; $columnList[] = "\"$fieldName\""; // Empty strings inserted as null in INSERTs. Replacement of Database::replace_with_null(). if($fieldVal === "''") $valueList[] = "null"; else $valueList[] = $fieldVal; } if(!isset($writeInfo['where']) && isset($writeInfo['id'])) { $writeInfo['where'] = "\"ID\" = " . (int)$writeInfo['id']; } switch($writeInfo['command']) { case "update": // 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); $sql = "update \"$table\" SET $fieldList where $writeInfo[where]"; $this->query($sql); break; } // ...if not, we'll skip on to the insert code case "insert": if(!isset($writeInfo['fields']['ID']) && isset($writeInfo['id'])) { $columnList[] = "\"ID\""; $valueList[] = (int)$writeInfo['id']; } $columnList = implode(", ", $columnList); $valueList = implode(", ", $valueList); $sql = "insert into \"$table\" ($columnList) VALUES ($valueList)"; $this->query($sql); break; default: $sql = null; user_error("Database::manipulate() Can't recognise command '$writeInfo[command]'", E_USER_ERROR); } } } } /** Replaces "\'\'" with "null", recursively walks through the given array. * @param string $array Array where the replacement should happen */ static function replace_with_null(&$array) { $array = ereg_replace('= *\'\'', "= null", $array); if(is_array($array)) { foreach($array as $key => $value) { if(is_array($value)) { array_walk($array, array(Database, 'replace_with_null')); } } } return $array; } /** * 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($msg, $errorLevel); } /** * Enable supression of database messages. */ function quiet() { Database::$supressOutput = true; } 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 "
  • $message
  • "; } } /** * 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; } if(!empty($combinedLimit)) $text .= " LIMIT " . $combinedLimit; } else { $text .= " LIMIT " . $sqlQuery->limit; } } return $text; } } /** * 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()} * @package sapphire * @subpackage model */ 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; /** * 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; } /** * Returns the next record in the iterator. * @return array */ public function record() { return $this->next(); } /** * Returns the first column of the first record. * @return string */ public function value() { foreach($this as $record) { return reset($record); } } /** * Return an HTML table containing the full result-set */ public function table() { $first = true; $result = "\n"; foreach($this as $record) { if($first) { $result .= ""; foreach($record as $k => $v) { $result .= " "; } $result .= " \n"; } $result .= ""; foreach($record as $k => $v) { $result .= " "; } $result .= " \n"; $first = false; } if($first) return "No records found"; return $result; } /** * 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; } } /** * 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() { $this->currentRecord = $this->nextRecord(); $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; } /** * 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); } ?>
    " . Convert::raw2xml($k) . "
    " . Convert::raw2xml($v) . "