MDL-15635 sqlite fixes - by Andrei Bautu

This commit is contained in:
skodak 2008-08-30 18:46:18 +00:00
parent 1c50df9f24
commit a47c84c4bb
3 changed files with 227 additions and 67 deletions

View file

@ -52,15 +52,9 @@ class sqlite_sql_generator extends sql_generator {
public $sequence_name = 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'; //Particular name for inline sequences in this generator public $sequence_name = 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'; //Particular name for inline sequences in this generator
public $unsigned_allowed = false; // To define in the generator must handle unsigned information public $unsigned_allowed = false; // To define in the generator must handle unsigned information
public $enum_inline_code = true; //Does the generator need to add inline code in the column definition
public $enum_extra_code = false; //Does the generator need to add extra code to generate code for the enums in the table public $enum_extra_code = false; //Does the generator need to add extra code to generate code for the enums in the table
public $add_after_clause = true; // Does the generator need to add the after clause for fields
public $concat_character = null; //Characters to be used as concatenation operator. If not defined
//MySQL CONCAT function will be use
public $alter_column_sql = 'ALTER TABLE TABLENAME MODIFY COLUMN COLUMNSPECS'; //The SQL template to alter columns
public $drop_index_sql = 'ALTER TABLE TABLENAME DROP INDEX INDEXNAME'; //SQL sentence to drop one index public $drop_index_sql = 'ALTER TABLE TABLENAME DROP INDEX INDEXNAME'; //SQL sentence to drop one index
//TABLENAME, INDEXNAME are dinamically replaced //TABLENAME, INDEXNAME are dinamically replaced
@ -167,13 +161,114 @@ class sqlite_sql_generator extends sql_generator {
return $dbtype; return $dbtype;
} }
/**
* Function to emulate full ALTER TABLE which SQLite does not support.
* The function can be used to drop a column ($xmldb_delete_field != null and
* $xmldb_add_field == null), add a column ($xmldb_delete_field == null and
* $xmldb_add_field != null), change/rename a column ($xmldb_delete_field == null
* and $xmldb_add_field == null).
* @param xmldb_table $xmldb_table table to change
* @param xmldb_field $xmldb_add_field column to create/modify (full specification is required)
* @param xmldb_field $xmldb_delete_field column to delete/modify (only name field is required)
* @return array of strings (SQL statements to alter the table structure)
*/
protected function getAlterTableSchema($xmldb_table, $xmldb_add_field=NULL, $xmldb_delete_field=NULL) {
/// Get the quoted name of the table and field
$tablename = $this->getTableName($xmldb_table);
$oldname = $xmldb_delete_field ? $xmldb_delete_field->getName() : NULL;
$newname = $xmldb_add_field ? $xmldb_add_field->getName() : NULL;
if($xmldb_delete_field) {
$xmldb_table->deleteField($oldname);
}
if($xmldb_add_field) {
$xmldb_table->addField($xmldb_add_field);
}
if($oldname) {
// alter indexes
$indexes = $xmldb_table->getIndexes();
foreach($indexes as $index) {
$fields = $index->getFields();
$i = array_search($oldname, $fields);
if($i!==FALSE) {
if($newname) {
$fields[$i] = $newname;
} else {
unset($fields[$i]);
}
$xmldb_table->deleteIndex($index->getName());
if(count($fields)) {
$index->setFields($fields);
$xmldb_table->addIndex($index);
}
}
}
// alter keys
$keys = $xmldb_table->getKeys();
foreach($keys as $key) {
$fields = $key->getFields();
$reffields = $key->getRefFields();
$i = array_search($oldname, $fields);
if($i!==FALSE) {
if($newname) {
$fields[$i] = $newname;
} else {
unset($fields[$i]);
unset($reffields[$i]);
}
$xmldb_table->deleteKey($key->getName());
if(count($fields)) {
$key->setFields($fields);
$key->setRefFields($fields);
$xmldb_table->addkey($key);
}
}
}
}
// prepare data copy
$fields = $xmldb_table->getFields();
foreach ($fields as $key => $field) {
$fieldname = $field->getName();
if($fieldname == $newname && $oldname && $oldname != $newname) {
// field rename operation
$fields[$key] = $this->getEncQuoted($oldname) . ' AS ' . $this->getEncQuoted($newname);
} else {
$fields[$key] = $this->getEncQuoted($field->getName());
}
}
$fields = implode(',', $fields);
$results[] = 'BEGIN TRANSACTION';
$results[] = 'CREATE TEMPORARY TABLE temp_data AS SELECT * FROM ' . $tablename;
$results[] = 'DROP TABLE ' . $tablename;
$results = array_merge($results, $this->getCreateTableSQL($xmldb_table));
$results[] = 'INSERT INTO ' . $tablename . ' SELECT ' . $fields . ' FROM temp_data';
$results[] = 'DROP TABLE temp_data';
$results[] = 'COMMIT';
return $results;
}
/**
* Given one xmldb_table and one xmldb_field, return the SQL statements needded to alter the field in the table
*/
public function getAlterFieldSQL($xmldb_table, $xmldb_field, $skip_type_clause = NULL, $skip_default_clause = NULL, $skip_notnull_clause = NULL) {
return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field);
}
/**
* Given one xmldb_table and one xmldb_key, return the SQL statements needded to add the key to the table
* note that undelying indexes will be added as parametrised by $xxxx_keys and $xxxx_index parameters
*/
public function getAddKeySQL($xmldb_table, $xmldb_key) {
$xmldb_table->addKey($xmldb_key);
return $this->getAlterTableSchema($xmldb_table);
}
/** /**
* Given one xmldb_table and one xmldb_field, return the SQL statements needded to create its enum * Given one xmldb_table and one xmldb_field, return the SQL statements needded to create its enum
* (usually invoked from getModifyEnumSQL() * (usually invoked from getModifyEnumSQL()
*/ */
public function getCreateEnumSQL($xmldb_table, $xmldb_field) { public function getCreateEnumSQL($xmldb_table, $xmldb_field) {
/// For MySQL, just alter the field return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field);
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
} }
/** /**
@ -181,8 +276,7 @@ class sqlite_sql_generator extends sql_generator {
* (usually invoked from getModifyEnumSQL() * (usually invoked from getModifyEnumSQL()
*/ */
public function getDropEnumSQL($xmldb_table, $xmldb_field) { public function getDropEnumSQL($xmldb_table, $xmldb_field) {
/// For MySQL, just alter the field return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field);
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
} }
/** /**
@ -190,9 +284,7 @@ class sqlite_sql_generator extends sql_generator {
* (usually invoked from getModifyDefaultSQL() * (usually invoked from getModifyDefaultSQL()
*/ */
public function getCreateDefaultSQL($xmldb_table, $xmldb_field) { public function getCreateDefaultSQL($xmldb_table, $xmldb_field) {
/// Just a wrapper over the getAlterFieldSQL() function for MySQL that return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field);
/// is capable of handling defaults
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
} }
/** /**
@ -201,20 +293,55 @@ class sqlite_sql_generator extends sql_generator {
* SQLite is pretty diferent from the standard to justify this oveloading * SQLite is pretty diferent from the standard to justify this oveloading
*/ */
public function getRenameFieldSQL($xmldb_table, $xmldb_field, $newname) { public function getRenameFieldSQL($xmldb_table, $xmldb_field, $newname) {
$oldfield = clone($xmldb_field);
$xmldb_field->setName($newname);
return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $oldfield);
}
// TODO: Add code to rename column /**
* Given one xmldb_table and one xmldb_index, return the SQL statements needded to rename the index in the table
*/
function getRenameIndexSQL($xmldb_table, $xmldb_index, $newname) {
/// Get the real index name
$dbindexname = $this->mdb->get_manager()->find_index_name($xmldb_table, $xmldb_index);
$xmldb_index->setName($newname);
$results = array('DROP INDEX ' . $dbindexname);
$results = array_merge($results, $this->getCreateIndexSQL($xmldb_table, $xmldb_index));
return $results;
}
/// Need a clone of xmldb_field to perform the change leaving original unmodified /**
$xmldb_field_clone = clone($xmldb_field); * Given one xmldb_table and one xmldb_key, return the SQL statements needded to rename the key in the table
* Experimental! Shouldn't be used at all!
*/
public function getRenameKeySQL($xmldb_table, $xmldb_key, $newname) {
$xmldb_table->deleteKey($xmldb_key->getName());
$xmldb_key->setName($newname);
$xmldb_table->addkey($xmldb_key);
return $this->getAlterTableSchema($xmldb_table);
}
/// Change the name of the field to perform the change /**
$xmldb_field_clone->setName($xmldb_field_clone->getName() . ' ' . $newname); * Given one xmldb_table and one xmldb_field, return the SQL statements needded to drop the field from the table
*/
public function getDropFieldSQL($xmldb_table, $xmldb_field) {
return $this->getAlterTableSchema($xmldb_table, NULL, $xmldb_field);
}
$fieldsql = $this->getFieldSQL($xmldb_field_clone); /**
* Given one xmldb_table and one xmldb_index, return the SQL statements needded to drop the index from the table
*/
public function getDropIndexSQL($xmldb_table, $xmldb_index) {
$xmldb_table->deleteIndex($xmldb_index->getName());
return $this->getAlterTableSchema($xmldb_table);
}
$sql = 'ALTER TABLE ' . $this->getTableName($xmldb_table) . ' CHANGE ' . $fieldsql; /**
* Given one xmldb_table and one xmldb_index, return the SQL statements needded to drop the index from the table
return array($sql); */
public function getDropKeySQL($xmldb_table, $xmldb_key) {
$xmldb_table->deleteKey($xmldb_key->getName());
return $this->getAlterTableSchema($xmldb_table);
} }
/** /**
@ -222,16 +349,16 @@ class sqlite_sql_generator extends sql_generator {
* (usually invoked from getModifyDefaultSQL() * (usually invoked from getModifyDefaultSQL()
*/ */
public function getDropDefaultSQL($xmldb_table, $xmldb_field) { public function getDropDefaultSQL($xmldb_table, $xmldb_field) {
/// Just a wrapper over the getAlterFieldSQL() function for MySQL that return $this->getAlterTableSchema($xmldb_table, $xmldb_field, $xmldb_field);
/// is capable of handling defaults
return $this->getAlterFieldSQL($xmldb_table, $xmldb_field);
} }
/** /**
* Given one XMLDB Field, return its enum SQL * Given one XMLDB Field, return its enum SQL
*/ */
public function getEnumSQL($xmldb_field) { public function getEnumSQL($xmldb_field) {
return 'enum'; // Enum values are between /*LISTSTART*/ and /*LISTEND*/ so that
// get_columns can easily find them
return 'enum CHECK (' . $this->getEncQuoted($xmldb_field->getName()) . ' IN (/*LISTSTART*/' . implode(',', $xmldb_field->getEnumValues()) . '/*LISTEND*/))';
} }
/** /**
@ -247,15 +374,28 @@ class sqlite_sql_generator extends sql_generator {
* Optionally the function allows one xmldb_field to be specified in * Optionally the function allows one xmldb_field to be specified in
* order to return only the check constraints belonging to one field. * order to return only the check constraints belonging to one field.
* Each element contains the name of the constraint and its description * Each element contains the name of the constraint and its description
* If no check constraints are found, returns an empty array * If no check constraints are found, returns an empty array.
* MySQL doesn't have check constraints in this implementation, but
* we return them based on the enum fields in the table
*/ */
public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) { public function getCheckConstraintsFromDB($xmldb_table, $xmldb_field = null) {
$tablename = $xmldb_table->getName($xmldb_table);
// TODO: add code for constraints // Fetch all the columns in the table
if (!$columns = $this->mdb->get_columns($tablename, false)) {
return array(); return array();
} }
$results = array();
$filter = $xmldb_field ? $xmldb_field->getName() : NULL;
// Iterate over columns searching for enums
foreach ($columns as $key => $column) {
// Enum found, let's add it to the constraints list
if (!empty($column->enums) && (!$filter || $column->name == $filter)) {
$result = new object;
$result->name = $key;
$result->description = implode(', ', $column->enums);
$results[$key] = $result;
}
}
return $results;
}
/** /**
* Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg) * Given one object name and it's type (pk, uk, fk, ck, ix, uix, seq, trg)

View file

@ -183,6 +183,7 @@ abstract class pdo_moodle_database extends moodle_database {
$this->debug_query($sql); $this->debug_query($sql);
} }
$this->pdb->exec($sql); $this->pdb->exec($sql);
$this->reset_columns();
return true; return true;
} catch (PDOException $ex) { } catch (PDOException $ex) {
$this->lastError = $ex->getMessage(); $this->lastError = $ex->getMessage();
@ -404,11 +405,16 @@ abstract class pdo_moodle_database extends moodle_database {
$value = (int)$value; // prevent "false" problems $value = (int)$value; // prevent "false" problems
} }
if (!empty($column->enums)) { if (!empty($column->enums)) {
// workaround for problem with wrong enums
if (is_null($value) and !$column->not_null) {
// ok - nulls allowed
} else {
if (!in_array((string)$value, $column->enums)) { if (!in_array((string)$value, $column->enums)) {
debugging('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.'); debugging('Enum value '.s($value).' not allowed in field '.$field.' table '.$table.'.');
return false; return false;
} }
} }
}
$cleaned[$field] = $value; $cleaned[$field] = $value;
} }

View file

@ -96,7 +96,7 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
} else { } else {
$path = $CFG->dataroot; $path = $CFG->dataroot;
} }
$path = ltrim($path, '\\/') . '/'; $path = rtrim($path, '\\/').'/';
if (!empty($this->dbuser)) { if (!empty($this->dbuser)) {
$path .= $this->dbuser.'_'; $path .= $this->dbuser.'_';
} }
@ -110,7 +110,7 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
*/ */
public function get_tables() { public function get_tables() {
$tables = array(); $tables = array();
$sql = 'SELECT name FROM sqlite_master WHERE type="table" ORDER BY name'; $sql = 'SELECT name FROM sqlite_master WHERE type="table" UNION ALL SELECT name FROM sqlite_temp_master WHERE type="table" ORDER BY name';
if ($this->debug) { if ($this->debug) {
$this->debug_query($sql); $this->debug_query($sql);
} }
@ -207,7 +207,7 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
// datatype in the CREATE TABLE command. We try to guess which type is used here // datatype in the CREATE TABLE command. We try to guess which type is used here
switch(substr($columninfo['type'], 0, 3)) { switch(substr($columninfo['type'], 0, 3)) {
case 'int': // int integer case 'int': // int integer
if($columninfo['primary_key'] && preg_match('/' . $columninfo['name'] . '\W*integer\W*primary\W*key\W*autoincrement/im', $createsql)) { if ($columninfo['primary_key'] && preg_match('/'.$columninfo['name'].'\W+integer\W+primary\W+key\W+autoincrement/im', $createsql)) {
$columninfo['meta_type'] = 'R'; $columninfo['meta_type'] = 'R';
$columninfo['auto_increment'] = true; $columninfo['auto_increment'] = true;
} else { } else {
@ -224,6 +224,16 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
case 'cha': // char case 'cha': // char
$columninfo['meta_type'] = 'C'; $columninfo['meta_type'] = 'C';
break; break;
case 'enu': // enums
if (preg_match('|'.$columninfo['name'].'\W+in\W+\(/\*liststart\*/(.*?)/\*listend\*/\)|im', $createsql, $tmp)) {
$tmp = explode(',', $tmp[1]);
foreach($tmp as $value) {
$columninfo['enums'][] = trim($value, '\'"');
}
unset($tmp);
}
$columninfo['meta_type'] = 'C';
break;
case 'tex': // text case 'tex': // text
case 'clo': // clob case 'clo': // clob
$columninfo['meta_type'] = 'X'; $columninfo['meta_type'] = 'X';
@ -246,6 +256,10 @@ class sqlite3_pdo_moodle_database extends pdo_moodle_database {
$columninfo['meta_type'] = 'D'; $columninfo['meta_type'] = 'D';
break; break;
} }
if ($columninfo['has_default'] && ($columninfo['meta_type'] == 'X' || $columninfo['meta_type']== 'C')) {
// trim extra quotes from text default values
$columninfo['default_value'] = substr($columninfo['default_value'], 1, -1);
}
$columns[$columninfo['name']] = new database_column_info($columninfo); $columns[$columninfo['name']] = new database_column_info($columninfo);
} }