Commit 3bc748c5 authored by lsmith's avatar lsmith

- s/\$db/\$this->conn

- turned raiseError() calls into throw Exception
- MDB2 style query*() conversion to Doctrine style fetch*()
- removed PEAR::isError() calls
parent b0f3a5d7
......@@ -71,7 +71,7 @@ class Doctrine_Export_Firebird extends Doctrine_Export
if (is_null($start)) {
$this->conn->beginTransaction();
$query = 'SELECT MAX(' . $this->conn->quoteIdentifier($name, true) . ') FROM ' . $this->conn->quoteIdentifier($table, true);
$start = $this->db->queryOne($query, 'integer');
$start = $this->conn->fetchOne($query, 'integer');
++$start;
$result = $this->createSequence($table, $start);
......@@ -110,23 +110,10 @@ class Doctrine_Export_Firebird extends Doctrine_Export
$result = $this->dropSequence($table);
/**
if (PEAR::isError($result)) {
return $db->raiseError(null, null, null,
'sequence for autoincrement PK could not be dropped', __FUNCTION__);
}
*/
//remove autoincrement trigger associated with the table
$table = $this->conn->getDbh()->quote(strtoupper($table));
$trigger_name = $this->conn->getDbh()->quote(strtoupper($table) . '_AUTOINCREMENT_PK');
$result = $this->conn->exec("DELETE FROM RDB\$TRIGGERS WHERE UPPER(RDB\$RELATION_NAME)=$table AND UPPER(RDB\$TRIGGER_NAME)=$trigger_name");
/**
if (PEAR::isError($result)) {
return $db->raiseError(null, null, null,
'trigger for autoincrement PK could not be dropped', __FUNCTION__);
}
*/
$table = $this->conn->quote(strtoupper($table));
$trigger_name = $this->conn->quote(strtoupper($table) . '_AUTOINCREMENT_PK');
return $this->conn->exec("DELETE FROM RDB\$TRIGGERS WHERE UPPER(RDB\$RELATION_NAME)=$table AND UPPER(RDB\$TRIGGER_NAME)=$trigger_name");
}
/**
* create a new table
......@@ -190,15 +177,12 @@ class Doctrine_Export_Firebird extends Doctrine_Export
foreach ($changes as $change_name => $change) {
switch ($change_name) {
case 'notnull':
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'it is not supported changes to field not null constraint', __FUNCTION__);
throw new Doctrine_DataDict_Firebird_Exception('it is not supported changes to field not null constraint');
case 'default':
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'it is not supported changes to field default value', __FUNCTION__);
throw new Doctrine_DataDict_Firebird_Exception('it is not supported changes to field default value');
case 'length':
/*
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'it is not supported changes to field default length', __FUNCTION__);
return throw new Doctrine_DataDict_Firebird_Exception('it is not supported changes to field default length');
*/
case 'unsigned':
case 'type':
......@@ -206,11 +190,10 @@ class Doctrine_Export_Firebird extends Doctrine_Export
case 'definition':
break;
default:
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
'it is not supported change of type' . $change_name, __FUNCTION__);
throw new Doctrine_DataDict_Firebird_Exception('it is not supported change of type' . $change_name);
}
}
return MDB2_OK;
return true;
}
/**
* drop an existing table
......@@ -326,18 +309,15 @@ class Doctrine_Export_Firebird extends Doctrine_Export
break;
case 'change':
foreach ($changes['change'] as $field) {
if (PEAR::isError($err = $this->checkSupportedChanges($field))) {
return $err;
}
$this->checkSupportedChanges($field);
}
break;
default:
return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
'change type ' . $change_name . ' not yet supported', __FUNCTION__);
throw new Doctrine_DataDict_Firebird_Exception('change type ' . $change_name . ' not yet supported');
}
}
if ($check) {
return MDB2_OK;
return true;
}
$query = '';
if (!empty($changes['add']) && is_array($changes['add'])) {
......@@ -345,7 +325,7 @@ class Doctrine_Export_Firebird extends Doctrine_Export
if ($query) {
$query.= ', ';
}
$query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field, $name);
$query.= 'ADD ' . $this->conn->getDeclaration($field['type'], $field_name, $field, $name);
}
}
......@@ -354,7 +334,7 @@ class Doctrine_Export_Firebird extends Doctrine_Export
if ($query) {
$query.= ', ';
}
$field_name = $db->quoteIdentifier($field_name, true);
$field_name = $this->conn->quoteIdentifier($field_name, true);
$query.= 'DROP ' . $field_name;
}
}
......@@ -364,32 +344,30 @@ class Doctrine_Export_Firebird extends Doctrine_Export
if ($query) {
$query.= ', ';
}
$field_name = $db->quoteIdentifier($field_name, true);
$query.= 'ALTER ' . $field_name . ' TO ' . $db->quoteIdentifier($field['name'], true);
$field_name = $this->conn->quoteIdentifier($field_name, true);
$query.= 'ALTER ' . $field_name . ' TO ' . $this->conn->quoteIdentifier($field['name'], true);
}
}
if (!empty($changes['change']) && is_array($changes['change'])) {
// missing support to change DEFAULT and NULLability
foreach ($changes['change'] as $field_name => $field) {
if (PEAR::isError($err = $this->checkSupportedChanges($field))) {
return $err;
}
$this->checkSupportedChanges($field);
if ($query) {
$query.= ', ';
}
$db->loadModule('Datatype', null, true);
$field_name = $db->quoteIdentifier($field_name, true);
$query.= 'ALTER ' . $field_name.' TYPE ' . $db->datatype->getTypeDeclaration($field['definition']);
$this->conn->loadModule('Datatype', null, true);
$field_name = $this->conn->quoteIdentifier($field_name, true);
$query.= 'ALTER ' . $field_name.' TYPE ' . $this->conn->datatype->getTypeDeclaration($field['definition']);
}
}
if (!strlen($query)) {
return MDB2_OK;
return false;
}
$name = $db->quoteIdentifier($name, true);
$result = $db->exec("ALTER TABLE $name $query");
$name = $this->conn->quoteIdentifier($name, true);
$result = $this->conn->exec("ALTER TABLE $name $query");
$this->_silentCommit();
return $result;
}
......@@ -529,7 +507,7 @@ class Doctrine_Export_Firebird extends Doctrine_Export
public function dropSequence($seq_name)
{
$sequence_name = $this->conn->getSequenceName($seq_name);
$sequence_name = $this->conn->getDbh()->quote($sequence_name);
$sequence_name = $this->conn->quote($sequence_name);
$query = "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)=$sequence_name";
return $this->conn->exec($query);
}
......
......@@ -42,14 +42,14 @@ class Doctrine_Export_Mssql extends Doctrine_Export
*/
public function createDatabase($name)
{
$name = $db->quoteIdentifier($name, true);
$name = $this->conn->quoteIdentifier($name, true);
$query = "CREATE DATABASE $name";
if ($db->options['database_device']) {
$query.= ' ON '.$db->options['database_device'];
$query.= $db->options['database_size'] ? '=' .
$db->options['database_size'] : '';
if ($this->conn->options['database_device']) {
$query.= ' ON '.$this->conn->options['database_device'];
$query.= $this->conn->options['database_size'] ? '=' .
$this->conn->options['database_size'] : '';
}
return $db->standaloneQuery($query, null, true);
return $this->conn->standaloneQuery($query, null, true);
}
/**
* drop an existing database
......@@ -59,8 +59,8 @@ class Doctrine_Export_Mssql extends Doctrine_Export
*/
public function dropDatabase($name)
{
$name = $db->quoteIdentifier($name, true);
return $db->standaloneQuery("DROP DATABASE $name", null, true);
$name = $this->conn->quoteIdentifier($name, true);
return $this->conn->standaloneQuery("DROP DATABASE $name", null, true);
}
/**
* alter an existing table
......@@ -162,7 +162,7 @@ class Doctrine_Export_Mssql extends Doctrine_Export
case 'rename':
case 'change':
default:
return $db->raiseError(Doctrine::ERR_CANNOT_ALTER, null, null,
return $this->conn->raiseError(Doctrine::ERR_CANNOT_ALTER, null, null,
'alterTable: change type "'.$change_name.'" not yet supported');
}
}
......@@ -188,7 +188,7 @@ class Doctrine_Export_Mssql extends Doctrine_Export
}
if (!$query) {
return MDB2_OK;
return false;
}
$name = $this->conn->quoteIdentifier($name, true);
......@@ -203,29 +203,25 @@ class Doctrine_Export_Mssql extends Doctrine_Export
*/
public function createSequence($seq_name, $start = 1)
{
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
$seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
$sequence_name = $this->conn->quoteIdentifier($this->conn->getSequenceName($seq_name), true);
$seqcol_name = $this->conn->quoteIdentifier($this->conn->options['seqcol_name'], true);
$query = "CREATE TABLE $sequence_name ($seqcol_name " .
"INT PRIMARY KEY CLUSTERED IDENTITY($start,1) NOT NULL)";
$res = $db->exec($query);
$res = $this->conn->exec($query);
if ($start == 1) {
return true;
}
$query = 'SET IDENTITY_INSERT $sequence_name ON ' .
'INSERT INTO $sequence_name (' . $seqcol_name . ') VALUES ( ' . $start . ')';
$res = $db->exec($query);
$result = $db->exec("DROP TABLE $sequence_name");
if (PEAR::isError($result)) {
return $db->raiseError($result, null, null,
'createSequence: could not drop inconsistent sequence table');
try {
$query = 'SET IDENTITY_INSERT $sequence_name ON ' .
'INSERT INTO $sequence_name (' . $seqcol_name . ') VALUES ( ' . $start . ')';
$res = $this->conn->exec($query);
} catch (Exception $e) {
$result = $this->conn->exec("DROP TABLE $sequence_name");
}
return $db->raiseError($res, null, null,
'createSequence: could not create sequence table');
return true;
}
/**
* This function drops an existing sequence
......@@ -235,7 +231,7 @@ class Doctrine_Export_Mssql extends Doctrine_Export
*/
public function dropSequence($seqName)
{
$sequenceName = $db->quoteIdentifier($db->getSequenceName($seqName), true);
$sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($seqName), true);
return $this->conn->exec('DROP TABLE ' . $sequenceName);
}
}
......@@ -306,11 +306,11 @@ class Doctrine_Export_Mysql extends Doctrine_Export
}
if ( ! $query) {
return MDB2_OK;
return false;
}
$name = $this->conn->quoteIdentifier($name, true);
return $this->dbh->query("ALTER TABLE $name $query");
return $this->conn->exec("ALTER TABLE $name $query");
}
/**
* create sequence
......@@ -328,7 +328,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
. strlen($this->dbh->options['default_table_type']) ? ' TYPE = '
. $this->dbh->options['default_table_type'] : '';
$res = $this->dbh->query($query);
$res = $this->conn->exec($query);
if ($start == 1)
return true;
......@@ -336,11 +336,11 @@ class Doctrine_Export_Mysql extends Doctrine_Export
$query = 'INSERT INTO ' . $sequenceName
. ' (' . $seqcol_name . ') VALUES (' . ($start-1) . ')';
$res = $this->dbh->query($query);
$res = $this->conn->exec($query);
// Handle error
try {
$result = $this->dbh->query('DROP TABLE ' . $sequenceName);
$result = $this->conn->exec('DROP TABLE ' . $sequenceName);
} catch(Exception $e) {
throw new Doctrine_Export_Mysql_Exception('could not drop inconsistent sequence table');
}
......
......@@ -53,19 +53,16 @@ class Doctrine_Export_Oracle extends Doctrine_Export
? ' DEFAULT TABLESPACE '.$this->conn->options['default_tablespace'] : '';
$query = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password . $tablespace;
$result = $this->conn->query($query);
$result = $this->conn->exec($query);
$query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
$result = $this->conn->query($query);
if (PEAR::isError($result)) {
try {
$query = 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE, CREATE TRIGGER TO ' . $username;
$result = $this->conn->exec($query);
} catch (Exception $e) {
$query = 'DROP USER '.$username.' CASCADE';
$result2 = $this->conn->query($query);
if (PEAR::isError($result2)) {
return $this->conn->raiseError($result2, null, null,
'could not setup the database user', __FUNCTION__);
}
return $result;
$result2 = $this->conn->exec($query);
}
return true;
}
/**
* drop an existing database
......@@ -83,7 +80,7 @@ class Doctrine_Export_Oracle extends Doctrine_Export
$username = sprintf($this->conn->getAttribute(Doctrine::ATTR_DB_NAME_FORMAT), $name);
return $this->conn->query('DROP USER ' . $username . ' CASCADE');
return $this->conn->exec('DROP USER ' . $username . ' CASCADE');
}
/**
* add an autoincrement sequence + trigger
......@@ -104,32 +101,18 @@ class Doctrine_Export_Oracle extends Doctrine_Export
);
$result = $this->createConstraint($table, $index_name, $definition);
/**
if (PEAR::isError($result)) {
return $this->conn->raiseError($result, null, null,
'primary key for autoincrement PK could not be created', __FUNCTION__);
}
*/
if (is_null($start)) {
$this->conn->beginTransaction();
$query = 'SELECT MAX(' . $this->conn->quoteIdentifier($name, true) . ') FROM ' . $this->conn->quoteIdentifier($table, true);
$start = $this->db->queryOne($query, 'integer');
if (PEAR::isError($start)) {
return $start;
}
$start = $this->conn->fetchOne($query);
++$start;
$result = $this->createSequence($table, $start);
$this->conn->commit();
} else {
$result = $this->createSequence($table, $start);
}
/**
if (PEAR::isError($result)) {
return $this->conn->raiseError($result, null, null,
'sequence for autoincrement PK could not be created', __FUNCTION__);
}
*/
$sequence_name = $this->conn->getSequenceName($table);
$trigger_name = $this->conn->quoteIdentifier($table . '_AI_PK', true);
$table = $this->conn->quoteIdentifier($table, true);
......@@ -350,13 +333,12 @@ END;
case 'rename':
break;
default:
return $this->conn->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
'change type "'.$changeName.'" not yet supported', __FUNCTION__);
throw new Doctrine_Export_Oracle_Exception('change type "'.$changeName.'" not yet supported');
}
}
if ($check) {
return MDB2_OK;
return false;
}
$name = $this->conn->quoteIdentifier($name, true);
......@@ -367,9 +349,6 @@ END;
$fields[] = $this->conn->getDeclaration($field['type'], $field_name, $field);
}
$result = $this->conn->exec("ALTER TABLE $name ADD (". implode(', ', $fields).')');
if (PEAR::isError($result)) {
return $result;
}
}
if (!empty($changes['change']) && is_array($changes['change'])) {
......@@ -378,9 +357,6 @@ END;
$fields[] = $field_name. ' ' . $this->conn->getDeclaration($field['definition']['type'], '', $field['definition']);
}
$result = $this->conn->exec("ALTER TABLE $name MODIFY (". implode(', ', $fields).')');
if (PEAR::isError($result)) {
return $result;
}
}
if (!empty($changes['rename']) && is_array($changes['rename'])) {
......@@ -388,9 +364,6 @@ END;
$field_name = $this->conn->quoteIdentifier($field_name, true);
$query = "ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->conn->quoteIdentifier($field['name']);
$result = $this->conn->exec($query);
if (PEAR::isError($result)) {
return $result;
}
}
}
......@@ -400,17 +373,11 @@ END;
$fields[] = $this->conn->quoteIdentifier($field_name, true);
}
$result = $this->conn->exec("ALTER TABLE $name DROP COLUMN ". implode(', ', $fields));
if (PEAR::isError($result)) {
return $result;
}
}
if (!empty($changes['name'])) {
$change_name = $this->conn->quoteIdentifier($changes['name'], true);
$result = $this->conn->exec("ALTER TABLE $name RENAME TO ".$change_name);
if (PEAR::isError($result)) {
return $result;
}
}
}
/**
......
......@@ -167,8 +167,8 @@ class Doctrine_Export_Pgsql extends Doctrine_Export
if (!empty($changes['add']) && is_array($changes['add'])) {
foreach ($changes['add'] as $field_name => $field) {
$query = 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
$this->dbh->query("ALTER TABLE $name $query");
$query = 'ADD ' . $this->conn->getDeclaration($field['type'], $field_name, $field);
$this->conn->exec("ALTER TABLE $name $query");
}
}
......@@ -176,7 +176,7 @@ class Doctrine_Export_Pgsql extends Doctrine_Export
foreach ($changes['remove'] as $field_name => $field) {
$field_name = $this->conn->quoteIdentifier($field_name, true);
$query = 'DROP ' . $field_name;
$this->dbh->query("ALTER TABLE $name $query");
$this->conn->exec("ALTER TABLE $name $query");
}
}
......@@ -184,21 +184,21 @@ class Doctrine_Export_Pgsql extends Doctrine_Export
foreach ($changes['change'] as $field_name => $field) {
$field_name = $this->conn->quoteIdentifier($field_name, true);
if (!empty($field['type'])) {
$server_info = $db->getServerVersion();
$server_info = $this->conn->getServerVersion();
if (is_array($server_info) && $server_info['major'] < 8) {
throw new Doctrine_Export_Pgsql_Exception('changing column type for "'.$change_name.'\" requires PostgreSQL 8.0 or above');
}
$query = "ALTER $field_name TYPE ".$db->datatype->getTypeDeclaration($field['definition']);
$this->dbh->query("ALTER TABLE $name $query");
$query = "ALTER $field_name TYPE ".$this->conn->datatype->getTypeDeclaration($field['definition']);
$this->conn->exec("ALTER TABLE $name $query");
}
if (array_key_exists('default', $field)) {
$query = "ALTER $field_name SET DEFAULT ".$db->quote($field['definition']['default'], $field['definition']['type']);
$this->dbh->query("ALTER TABLE $name $query");
$query = "ALTER $field_name SET DEFAULT ".$this->conn->quote($field['definition']['default'], $field['definition']['type']);
$this->conn->exec("ALTER TABLE $name $query");
}
if (!empty($field['notnull'])) {
$query = "ALTER $field_name ".($field['definition']['notnull'] ? "SET" : "DROP").' NOT NULL';
$this->dbh->query("ALTER TABLE $name $query");
$this->conn->exec("ALTER TABLE $name $query");
}
}
......@@ -207,14 +207,14 @@ class Doctrine_Export_Pgsql extends Doctrine_Export
if (!empty($changes['rename']) && is_array($changes['rename'])) {
foreach ($changes['rename'] as $field_name => $field) {
$field_name = $this->conn->quoteIdentifier($field_name, true);
$this->dbh->query("ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->conn->quoteIdentifier($field['name'], true));
$this->conn->exec("ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->conn->quoteIdentifier($field['name'], true));
}
}
$name = $this->conn->quoteIdentifier($name, true);
if (!empty($changes['name'])) {
$change_name = $this->conn->quoteIdentifier($changes['name'], true);
$this->dbh->query("ALTER TABLE $name RENAME TO ".$change_name);
$this->conn->exec("ALTER TABLE $name RENAME TO ".$change_name);
}
}
}
......
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