Commit f8b96636 authored by zYne's avatar zYne

Ported index/sequence name fixing and common manager functionality from MDB2

parent 311131b5
......@@ -28,6 +28,7 @@
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Lukas Smith <smith@pooteeweet.org> (MDB2 library)
*/
abstract class Doctrine_Connection extends Doctrine_Configurable implements Countable, IteratorAggregate {
/**
......@@ -167,6 +168,34 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$this->supported[$feature] === 'emulated' ||
$this->supported[$feature]);
}
/**
* Removes any formatting in an sequence name using the 'seqname_format' option
*
* @param string $sqn string that containts name of a potential sequence
* @return string name of the sequence with possible formatting removed
*/
public function fixSequenceName($sqn) {
$seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
$seq_name = preg_replace($seq_pattern, '\\1', $sqn);
if ($seq_name && ! strcasecmp($sqn, $db->getSequenceName($seq_name))) {
return $seq_name;
}
return $sqn;
}
/**
* Removes any formatting in an index name using the 'idxname_format' option
*
* @param string $idx string that containts name of anl index
* @return string name of the index with possible formatting removed
*/
public function fixIndexName($idx) {
$idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';
$idx_name = preg_replace($idx_pattern, '\\1', $idx);
if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {
return $idx_name;
}
return $idx;
}
/**
* returns a datadict object
*
......
......@@ -21,14 +21,14 @@
/**
* Doctrine_Export
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Export {
/**
* @var Doctrine_Connection $conn Doctrine_Connection object
......@@ -43,6 +43,15 @@ class Doctrine_Export {
$this->conn = $conn;
$this->dbh = $conn->getDBH();
}
/**
* drop an existing database
*
* @param string $name name of the database that should be dropped
* @return void
*/
public function dropDatabase($database) {
throw new Doctrine_Export_Exception('Drop database not supported by this driver.');
}
/**
* dropTable
*
......@@ -53,9 +62,143 @@ class Doctrine_Export {
public function dropTable($table) {
$this->dbh->query('DROP TABLE '.$table);
}
/**
* drop existing index
*
* @param string $table name of table that should be used in method
* @param string $name name of the index to be dropped
* @return void
*/
public function dropIndex($table, $name) {
$name = $db->quoteIdentifier($db->getIndexName($name), true);
return $db->exec("DROP INDEX $name");
}
/**
* drop existing constraint
*
* @param string $table name of table that should be used in method
* @param string $name name of the constraint to be dropped
* @param string $primary hint if the constraint is primary
* @return void
*/
public function dropConstraint($table, $name, $primary = false) {
$table = $db->quoteIdentifier($table, true);
$name = $db->quoteIdentifier($db->getIndexName($name), true);
return $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");
}
/**
* drop existing sequence
*
* @param string $seq_name name of the sequence to be dropped
* @return void
*/
public function dropSequence($name) {
throw new Doctrine_Export_Exception('Drop sequence not supported by this driver.');
}
/**
* create a new database
*
* @param string $name name of the database that should be created
* @return void
*/
public function createDatabase($database) {
throw new Doctrine_Export_Exception('Create database not supported by this driver.');
}
/**
* create a new table
*
* @param string $name Name of the database that should be created
* @param array $fields Associative array that contains the definition of each field of the new table
* The indexes of the array entries are the names of the fields of the table an
* the array entry values are associative arrays like those that are meant to be
* passed with the field definitions to get[Type]Declaration() functions.
* array(
* 'id' => array(
* 'type' => 'integer',
* 'unsigned' => 1
* 'notnull' => 1
* 'default' => 0
* ),
* 'name' => array(
* 'type' => 'text',
* 'length' => 12
* ),
* 'password' => array(
* 'type' => 'text',
* 'length' => 12
* )
* );
* @param array $options An associative array of table options:
*
* @return void
*/
public function createTable($name, $fields, $options = array()) {
if ( ! $name)
throw new Doctrine_Export_Exception('no valid table name specified');
if (empty($fields))
throw new Doctrine_Export_Exception('no fields specified for table '.$name);
$query_fields = $this->getFieldDeclarationList($fields);
if (!empty($options['primary'])) {
$query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
}
$name = $db->quoteIdentifier($name, true);
$query = "CREATE TABLE $name ($query_fields)";
return $db->exec($query);
}
/**
* create sequence
*
* @param string $seq_name name of the sequence to be created
* @param string $start start value of the sequence; default is 1
* @return void
*/
public function createSequence($seq_name, $start = 1) {
throw new Doctrine_Export_Exception('Create sequence not supported by this driver.');
}
/**
* [[ borrowed from PEAR MDB2 ]]
* create a constraint on a table
*
* @param string $table name of the table on which the constraint is to be created
* @param string $name name of the constraint to be created
* @param array $definition associative array that defines properties of the constraint to be created.
* Currently, only one property named FIELDS is supported. This property
* is also an associative with the names of the constraint fields as array
* constraints. Each entry of this array is set to another type of associative
* array that specifies properties of the constraint that are specific to
* each field.
*
* Example
* array(
* 'fields' => array(
* 'user_name' => array(),
* 'last_login' => array()
* )
* )
* @return void
*/
public function createConstraint($table, $name, $definition) {
$table = $db->quoteIdentifier($table, true);
$name = $db->quoteIdentifier($db->getIndexName($name), true);
$query = "ALTER TABLE $table ADD CONSTRAINT $name";
if (!empty($definition['primary'])) {
$query.= ' PRIMARY KEY';
} elseif (!empty($definition['unique'])) {
$query.= ' UNIQUE';
}
$fields = array();
foreach (array_keys($definition['fields']) as $field) {
$fields[] = $db->quoteIdentifier($field, true);
}
$query .= ' ('. implode(', ', $fields) . ')';
return $db->exec($query);
}
/**
* Get the stucture of a field into an array
*
*
......@@ -88,7 +231,7 @@ class Doctrine_Export {
* @throws PDOException
* @return void
*/
function createIndex($table, $name, array $definition) {
public function createIndex($table, $name, array $definition) {
$table = $this->conn->quoteIdentifier($table);
$name = $this->conn->quoteIdentifier($name);
......@@ -100,6 +243,123 @@ class Doctrine_Export {
$query .= ' ('. implode(', ', $fields) . ')';
return $this->dbh->query($query);
}
/**
* alter an existing table
*
* @param string $name name of the table that is intended to be changed.
* @param array $changes associative array that contains the details of each type
* of change that is intended to be performed. The types of
* changes that are currently supported are defined as follows:
*
* name
*
* New name for the table.
*
* add
*
* Associative array with the names of fields to be added as
* indexes of the array. The value of each entry of the array
* should be set to another associative array with the properties
* of the fields to be added. The properties of the fields should
* be the same as defined by the MDB2 parser.
*
*
* remove
*
* Associative array with the names of fields to be removed as indexes
* of the array. Currently the values assigned to each entry are ignored.
* An empty array should be used for future compatibility.
*
* rename
*
* Associative array with the names of fields to be renamed as indexes
* of the array. The value of each entry of the array should be set to
* another associative array with the entry named name with the new
* field name and the entry named Declaration that is expected to contain
* the portion of the field declaration already in DBMS specific SQL code
* as it is used in the CREATE TABLE statement.
*
* change
*
* Associative array with the names of the fields to be changed as indexes
* of the array. Keep in mind that if it is intended to change either the
* name of a field and any other properties, the change array entries
* should have the new names of the fields as array indexes.
*
* The value of each entry of the array should be set to another associative
* array with the properties of the fields to that are meant to be changed as
* array entries. These entries should be assigned to the new values of the
* respective properties. The properties of the fields should be the same
* as defined by the MDB2 parser.
*
* Example
* array(
* 'name' => 'userlist',
* 'add' => array(
* 'quota' => array(
* 'type' => 'integer',
* 'unsigned' => 1
* )
* ),
* 'remove' => array(
* 'file_limit' => array(),
* 'time_limit' => array()
* ),
* 'change' => array(
* 'name' => array(
* 'length' => '20',
* 'definition' => array(
* 'type' => 'text',
* 'length' => 20,
* ),
* )
* ),
* 'rename' => array(
* 'sex' => array(
* 'name' => 'gender',
* 'definition' => array(
* 'type' => 'text',
* 'length' => 1,
* 'default' => 'M',
* ),
* )
* )
* )
*
* @param boolean $check indicates whether the function should just check if the DBMS driver
* can perform the requested table alterations if the value is true or
* actually perform them otherwise.
* @return void
*/
public function alterTable($name, $changes, $check) {
throw new Doctrine_Export_Exception('Alter table not supported by this driver.');
}
/**
* Get declaration of a number of field in bulk
*
* @param string $fields a multidimensional associative array.
* The first dimension determines the field name, while the second
* dimension is keyed with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
*
* default
* Boolean value to be used as default for this field.
*
* notnull
* Boolean flag that indicates whether this field is constrained
* to not be set to null.
*
* @return string
*/
public function getFieldDeclarationList(array $fields) {
foreach ($fields as $field_name => $field) {
$query = $db->getDeclaration($field['type'], $field_name, $field);
$query_fields[] = $query;
}
return implode(', ', $query_fields);
}
/**
* export
*/
......
......@@ -34,6 +34,7 @@ class Doctrine_Expression_Sqlite extends Doctrine_Expression {
/**
* Returns the md5 sum of the data that SQLite's md5() function receives.
*
* @param mixed $data
* @return string
*/
public static function md5Impl($data) {
......@@ -42,6 +43,8 @@ class Doctrine_Expression_Sqlite extends Doctrine_Expression {
/**
* Returns the modules of the data that SQLite's mod() function receives.
*
* @param integer $dividend
* @param integer $divisor
* @return string
*/
public static function modImpl($dividend, $divisor) {
......@@ -49,7 +52,7 @@ class Doctrine_Expression_Sqlite extends Doctrine_Expression {
}
/**
* Returns a concattenation of the data that SQLite's concat() function receives.
* Returns a concatenation of the data that SQLite's concat() function receives.
*
* @return string
*/
......@@ -57,6 +60,30 @@ class Doctrine_Expression_Sqlite extends Doctrine_Expression {
$args = func_get_args();
return join( '', $args );
}
/**
* locate
* returns the position of the first occurrence of substring $substr in string $str that
* SQLite's locate() function receives
*
* @param string $substr literal string to find
* @param string $str literal string
* @return string
*/
public static function locateImpl($substr, $str) {
return strpos($str, $substr);
}
public static function sha1($str) {
return sha1($str);
}
public static function ltrim($str) {
return ltrim($str);
}
public static function rtrim($str) {
return rtrim($str);
}
public static function trim($str) {
return trim($str);
}
/**
* returns the regular expression operator
*
......@@ -65,6 +92,19 @@ class Doctrine_Expression_Sqlite extends Doctrine_Expression {
public function regexp() {
return 'RLIKE';
}
/**
* soundex
* Returns a string to call a function to compute the
* soundex encoding of a string
*
* The string "?000" is returned if the argument is NULL.
*
* @param string $value
* @return string SQL soundex function with given parameter
*/
public function soundex($value) {
return 'SOUNDEX(' . $value . ')';
}
/**
* Return string to call a variable with the current timestamp inside an SQL statement
* There are three special variables for current date and time.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment