Commit d645b165 authored by romanb's avatar romanb

More refactorings

parent 477199b0
...@@ -1094,6 +1094,16 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -1094,6 +1094,16 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
{ {
return $this->getTable($name)->create(); return $this->getTable($name)->create();
} }
/**
* Creates a new Doctrine_Query object that operates on this connection.
*
* @return Doctrine_Query
*/
public function createQuery()
{
return new Doctrine_Query($this);
}
/** /**
* flush * flush
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
* @version $Revision$ * @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi> * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/ */
class Doctrine_Hydrate extends Doctrine_Locator_Injectable implements Serializable abstract class Doctrine_Hydrate extends Doctrine_Locator_Injectable implements Serializable
{ {
/** /**
* QUERY TYPE CONSTANTS * QUERY TYPE CONSTANTS
...@@ -1284,4 +1284,6 @@ class Doctrine_Hydrate extends Doctrine_Locator_Injectable implements Serializab ...@@ -1284,4 +1284,6 @@ class Doctrine_Hydrate extends Doctrine_Locator_Injectable implements Serializab
{ {
return $this->parts; return $this->parts;
} }
abstract public function getQuery($params = array());
} }
...@@ -450,50 +450,53 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -450,50 +450,53 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$tableAlias = $this->getTableAlias($componentAlias); $tableAlias = $this->getTableAlias($componentAlias);
$table = $this->_aliasMap[$componentAlias]['table']; $table = $this->_aliasMap[$componentAlias]['table'];
if (isset($this->_pendingFields[$componentAlias])) { if ( ! isset($this->_pendingFields[$componentAlias])) {
$fields = $this->_pendingFields[$componentAlias]; return;
}
// check for wildcards $fields = $this->_pendingFields[$componentAlias];
if (in_array('*', $fields)) {
//echo "<br />";Doctrine::dump($table->getColumnNames()); echo "<br />"; // check for wildcards
$fields = $table->getColumnNames(); if (in_array('*', $fields)) {
} else { //echo "<br />";Doctrine::dump($table->getColumnNames()); echo "<br />";
// only auto-add the primary key fields if this query object is not $fields = $table->getFieldNames();
// a subquery of another query object } else {
if ( ! $this->isSubquery) { // only auto-add the primary key fields if this query object is not
$fields = array_unique(array_merge((array) $table->getIdentifier(), $fields)); // a subquery of another query object
} if ( ! $this->isSubquery) {
$fields = array_unique(array_merge((array) $table->getIdentifier(), $fields));
} }
$sql = array(); }
foreach ($fields as $name) {
if (($owner = $table->getColumnOwner($name)) !== null && $sql = array();
$owner !== $table->getComponentName()) { foreach ($fields as $fieldName) {
$columnName = $table->getColumnName($fieldName);
if (($owner = $table->getColumnOwner($columnName)) !== null &&
$owner !== $table->getComponentName()) {
$parent = $this->_conn->getTable($owner);
$parent = $this->_conn->getTable($owner); $columnName = $parent->getColumnName($fieldName);
$name = $parent->getColumnName($name); $parentAlias = $this->getTableAlias($componentAlias . '.' . $parent->getComponentName());
$parentAlias = $this->getTableAlias($componentAlias . '.' . $parent->getComponentName()); $sql[] = $this->_conn->quoteIdentifier($parentAlias . '.' . $columnName)
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
} else {
$sql[] = $this->_conn->quoteIdentifier($parentAlias . '.' . $name) $columnName = $table->getColumnName($fieldName);
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
} else {
$name = $table->getColumnName($name); $sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $columnName)
. ' AS '
$sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $name) . $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
}
} }
$this->_neededTables[] = $tableAlias;
//Doctrine::dump(implode(', ', $sql));
//echo "<br /><br />";
return implode(', ', $sql);
} }
$this->_neededTables[] = $tableAlias;
//Doctrine::dump(implode(', ', $sql));
//echo "<br /><br />";
return implode(', ', $sql);
} }
/** /**
...@@ -1126,8 +1129,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1126,8 +1129,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
return $this->_sql; return $this->_sql;
} }
$parts = $this->_dqlParts;
// reset the state // reset the state
if ( ! $this->isSubquery()) { if ( ! $this->isSubquery()) {
$this->_aliasMap = array(); $this->_aliasMap = array();
...@@ -1140,6 +1141,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1140,6 +1141,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
foreach ($this->_dqlParts as $queryPartName => $queryParts) { foreach ($this->_dqlParts as $queryPartName => $queryParts) {
$this->processQueryPart($queryPartName, $queryParts); $this->processQueryPart($queryPartName, $queryParts);
} }
$params = $this->convertEnums($params); $params = $this->convertEnums($params);
$this->_state = self::STATE_DIRECT; $this->_state = self::STATE_DIRECT;
...@@ -1148,8 +1150,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1148,8 +1150,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$this->preQuery(); $this->preQuery();
$this->_state = self::STATE_CLEAN; $this->_state = self::STATE_CLEAN;
$this->_dqlParts = $parts;
if (empty($this->parts['from'])) { if (empty($this->parts['from'])) {
return false; return false;
} }
...@@ -1205,8 +1205,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1205,8 +1205,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
if ($needsSubQuery) { if ($needsSubQuery) {
$subquery = $this->getLimitSubquery(); $subquery = $this->getLimitSubquery();
// what about composite keys?
$idColumnName = $table->getColumnName($table->getIdentifier());
switch (strtolower($this->_conn->getName())) { switch (strtolower($this->_conn->getName())) {
case 'mysql': case 'mysql':
// mysql doesn't support LIMIT in subqueries // mysql doesn't support LIMIT in subqueries
...@@ -1215,11 +1215,11 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1215,11 +1215,11 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
break; break;
case 'pgsql': case 'pgsql':
// pgsql needs special nested LIMIT subquery // pgsql needs special nested LIMIT subquery
$subquery = 'SELECT doctrine_subquery_alias.' . $table->getIdentifier(). ' FROM (' . $subquery . ') AS doctrine_subquery_alias'; $subquery = 'SELECT doctrine_subquery_alias.' . $idColumnName . ' FROM (' . $subquery . ') AS doctrine_subquery_alias';
break; break;
} }
$field = $this->getTableAlias($rootAlias) . '.' . $table->getIdentifier(); $field = $this->getTableAlias($rootAlias) . '.' . $idColumnName;
// only append the subquery if it actually contains something // only append the subquery if it actually contains something
if ($subquery !== '') { if ($subquery !== '') {
...@@ -1236,7 +1236,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1236,7 +1236,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : ''; $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
if ($modifyLimit) { if ($modifyLimit) {
$q = $this->_conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']); $q = $this->_conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']);
} }
......
...@@ -143,7 +143,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract ...@@ -143,7 +143,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
* *
* @return string the built sql query * @return string the built sql query
*/ */
public function getQuery() public function getQuery($params = array())
{ {
$select = array(); $select = array();
......
...@@ -147,10 +147,9 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -147,10 +147,9 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
$this->_table = $table; $this->_table = $table;
$exists = ( ! $isNewEntry); $exists = ( ! $isNewEntry);
} else { } else {
$class = get_class($this); $class = get_class($this);
// get the table of this class // get the table of this class
$this->_table = Doctrine_Manager::getInstance() $this->_table = Doctrine_Manager::getInstance()->getTable($class);
->getTable($class);
$exists = false; $exists = false;
} }
...@@ -185,7 +184,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -185,7 +184,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
// set the default values for this record // set the default values for this record
$this->assignDefaultValues(); $this->assignDefaultValues();
} else { } else {
$this->_state = Doctrine_Record::STATE_CLEAN; $this->_state = Doctrine_Record::STATE_CLEAN;
if ($count < $this->_table->getColumnCount()) { if ($count < $this->_table->getColumnCount()) {
$this->_state = Doctrine_Record::STATE_PROXY; $this->_state = Doctrine_Record::STATE_PROXY;
...@@ -247,7 +246,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -247,7 +246,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
/** /**
* isValid * isValid
* *
* @return boolean whether or not this record passes all column validations * @return boolean whether or not this record is valid
*/ */
public function isValid() public function isValid()
{ {
...@@ -428,13 +427,13 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -428,13 +427,13 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
* *
* @param array $data data array to be cleaned * @param array $data data array to be cleaned
* @return integer * @return integer
* @todo Better description. What exactly does this "cleaning" involve?
*/ */
public function cleanData(&$data) public function cleanData(&$data)
{ {
$tmp = $data; $tmp = $data;
$data = array(); $data = array();
//Doctrine::dump($this->getTable()->getFieldNames());
foreach ($this->getTable()->getFieldNames() as $fieldName) { foreach ($this->getTable()->getFieldNames() as $fieldName) {
if ( ! isset($tmp[$fieldName])) { if ( ! isset($tmp[$fieldName])) {
$data[$fieldName] = self::$_null; $data[$fieldName] = self::$_null;
...@@ -458,7 +457,6 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -458,7 +457,6 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
{ {
$this->_values = array_merge($this->_values, $this->cleanData($data)); $this->_values = array_merge($this->_values, $this->cleanData($data));
$this->_data = array_merge($this->_data, $data); $this->_data = array_merge($this->_data, $data);
//Doctrine::dump($this->_data);
$this->prepareIdentifiers(true); $this->prepareIdentifiers(true);
} }
...@@ -638,8 +636,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count ...@@ -638,8 +636,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
} }
if ($this->_state === Doctrine_Record::STATE_TCLEAN || if ($this->_state === Doctrine_Record::STATE_TCLEAN ||
$this->_state === Doctrine_Record::STATE_CLEAN) { $this->_state === Doctrine_Record::STATE_CLEAN) {
$this->_modified = array(); $this->_modified = array();
} }
......
...@@ -956,8 +956,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable ...@@ -956,8 +956,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
$fieldName = $parts[0]; $fieldName = $parts[0];
} }
$name = strtolower($parts[0]); $name = strtolower($parts[0]);
$this->_columnNames[$fieldName] = $name; if ($prepend) {
$this->_fieldNames[$name] = $fieldName; $this->_columnNames = array_merge(array($fieldName => $name), $this->_columnNames);
$this->_fieldNames = array_merge(array($name => $fieldName), $this->_fieldNames);
} else {
$this->_columnNames[$fieldName] = $name;
$this->_fieldNames[$name] = $fieldName;
}
if ($length == null) { if ($length == null) {
switch ($type) { switch ($type) {
......
...@@ -96,12 +96,12 @@ class Doctrine_Export_Record_TestCase extends Doctrine_UnitTestCase ...@@ -96,12 +96,12 @@ class Doctrine_Export_Record_TestCase extends Doctrine_UnitTestCase
public function testExportModelFromDirectory() public function testExportModelFromDirectory()
{ {
Doctrine::createTablesFromModels(dirname(__FILE__) . DIRECTORY_SEPARATOR .'..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'export'); Doctrine::createTablesFromModels(dirname(__FILE__) . DIRECTORY_SEPARATOR .'..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'export');
$this->assertEqual($this->adapter->pop(), 'COMMIT'); $this->assertEqual($this->adapter->pop(), 'COMMIT');
$this->assertEqual($this->adapter->pop(), 'ALTER TABLE cms__category_languages ADD FOREIGN KEY (category_id) REFERENCES cms__category(id) ON DELETE CASCADE'); $this->assertEqual($this->adapter->pop(), 'ALTER TABLE cms__category_languages ADD FOREIGN KEY (category_id) REFERENCES cms__category(id) ON DELETE CASCADE');
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE cms__category_languages (id BIGINT AUTO_INCREMENT, name TEXT, category_id BIGINT, language_id BIGINT, INDEX index_category_idx (category_id), INDEX index_language_idx (language_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = INNODB'); $this->assertEqual($this->adapter->pop(), 'CREATE TABLE cms__category_languages (id BIGINT AUTO_INCREMENT, name TEXT, category_id BIGINT, language_id BIGINT, INDEX index_category_idx (category_id), INDEX index_language_idx (language_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = INNODB');
$this->assertEqual($this->adapter->pop(), 'CREATE TABLE cms__category (id BIGINT AUTO_INCREMENT, created DATETIME, parent BIGINT, position MEDIUMINT, active BIGINT, INDEX index_parent_idx (parent), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = INNODB'); $this->assertEqual($this->adapter->pop(), 'CREATE TABLE cms__category (id BIGINT AUTO_INCREMENT, created DATETIME, parent BIGINT, position MEDIUMINT, active BIGINT, INDEX index_parent_idx (parent), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = INNODB');
$this->assertEqual($this->adapter->pop(), 'BEGIN TRANSACTION'); $this->assertEqual($this->adapter->pop(), 'BEGIN TRANSACTION');
} }
......
...@@ -97,7 +97,7 @@ class Doctrine_Hydrate_Mock extends Doctrine_Hydrate ...@@ -97,7 +97,7 @@ class Doctrine_Hydrate_Mock extends Doctrine_Hydrate
{ {
$this->data = $data; $this->data = $data;
} }
public function getQuery() public function getQuery($params = array())
{ {
} }
......
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