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
{
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
......
......@@ -32,7 +32,7 @@
* @version $Revision$
* @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
......@@ -1284,4 +1284,6 @@ class Doctrine_Hydrate extends Doctrine_Locator_Injectable implements Serializab
{
return $this->parts;
}
abstract public function getQuery($params = array());
}
......@@ -450,50 +450,53 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$tableAlias = $this->getTableAlias($componentAlias);
$table = $this->_aliasMap[$componentAlias]['table'];
if (isset($this->_pendingFields[$componentAlias])) {
$fields = $this->_pendingFields[$componentAlias];
if ( ! isset($this->_pendingFields[$componentAlias])) {
return;
}
// check for wildcards
if (in_array('*', $fields)) {
//echo "<br />";Doctrine::dump($table->getColumnNames()); echo "<br />";
$fields = $table->getColumnNames();
} else {
// only auto-add the primary key fields if this query object is not
// a subquery of another query object
if ( ! $this->isSubquery) {
$fields = array_unique(array_merge((array) $table->getIdentifier(), $fields));
}
$fields = $this->_pendingFields[$componentAlias];
// check for wildcards
if (in_array('*', $fields)) {
//echo "<br />";Doctrine::dump($table->getColumnNames()); echo "<br />";
$fields = $table->getFieldNames();
} else {
// only auto-add the primary key fields if this query object is not
// 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 &&
$owner !== $table->getComponentName()) {
}
$sql = array();
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)
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
} else {
$columnName = $table->getColumnName($fieldName);
$name = $table->getColumnName($name);
$sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $name)
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $name);
}
$sql[] = $this->_conn->quoteIdentifier($tableAlias . '.' . $columnName)
. ' AS '
. $this->_conn->quoteIdentifier($tableAlias . '__' . $columnName);
}
$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
return $this->_sql;
}
$parts = $this->_dqlParts;
// reset the state
if ( ! $this->isSubquery()) {
$this->_aliasMap = array();
......@@ -1140,6 +1141,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
foreach ($this->_dqlParts as $queryPartName => $queryParts) {
$this->processQueryPart($queryPartName, $queryParts);
}
$params = $this->convertEnums($params);
$this->_state = self::STATE_DIRECT;
......@@ -1148,8 +1150,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$this->preQuery();
$this->_state = self::STATE_CLEAN;
$this->_dqlParts = $parts;
if (empty($this->parts['from'])) {
return false;
}
......@@ -1205,8 +1205,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
if ($needsSubQuery) {
$subquery = $this->getLimitSubquery();
// what about composite keys?
$idColumnName = $table->getColumnName($table->getIdentifier());
switch (strtolower($this->_conn->getName())) {
case 'mysql':
// mysql doesn't support LIMIT in subqueries
......@@ -1215,11 +1215,11 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
break;
case 'pgsql':
// 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;
}
$field = $this->getTableAlias($rootAlias) . '.' . $table->getIdentifier();
$field = $this->getTableAlias($rootAlias) . '.' . $idColumnName;
// only append the subquery if it actually contains something
if ($subquery !== '') {
......@@ -1236,7 +1236,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']) : '';
if ($modifyLimit) {
$q = $this->_conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']);
}
......
......@@ -143,7 +143,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
*
* @return string the built sql query
*/
public function getQuery()
public function getQuery($params = array())
{
$select = array();
......
......@@ -147,10 +147,9 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
$this->_table = $table;
$exists = ( ! $isNewEntry);
} else {
$class = get_class($this);
$class = get_class($this);
// get the table of this class
$this->_table = Doctrine_Manager::getInstance()
->getTable($class);
$this->_table = Doctrine_Manager::getInstance()->getTable($class);
$exists = false;
}
......@@ -185,7 +184,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
// set the default values for this record
$this->assignDefaultValues();
} else {
$this->_state = Doctrine_Record::STATE_CLEAN;
$this->_state = Doctrine_Record::STATE_CLEAN;
if ($count < $this->_table->getColumnCount()) {
$this->_state = Doctrine_Record::STATE_PROXY;
......@@ -247,7 +246,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
/**
* isValid
*
* @return boolean whether or not this record passes all column validations
* @return boolean whether or not this record is valid
*/
public function isValid()
{
......@@ -428,13 +427,13 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
*
* @param array $data data array to be cleaned
* @return integer
* @todo Better description. What exactly does this "cleaning" involve?
*/
public function cleanData(&$data)
{
$tmp = $data;
$data = array();
//Doctrine::dump($this->getTable()->getFieldNames());
foreach ($this->getTable()->getFieldNames() as $fieldName) {
if ( ! isset($tmp[$fieldName])) {
$data[$fieldName] = self::$_null;
......@@ -458,7 +457,6 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
{
$this->_values = array_merge($this->_values, $this->cleanData($data));
$this->_data = array_merge($this->_data, $data);
//Doctrine::dump($this->_data);
$this->prepareIdentifiers(true);
}
......@@ -638,8 +636,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
}
if ($this->_state === Doctrine_Record::STATE_TCLEAN ||
$this->_state === Doctrine_Record::STATE_CLEAN) {
$this->_state === Doctrine_Record::STATE_CLEAN) {
$this->_modified = array();
}
......
......@@ -956,8 +956,13 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
$fieldName = $parts[0];
}
$name = strtolower($parts[0]);
$this->_columnNames[$fieldName] = $name;
$this->_fieldNames[$name] = $fieldName;
if ($prepend) {
$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) {
switch ($type) {
......
......@@ -96,12 +96,12 @@ class Doctrine_Export_Record_TestCase extends Doctrine_UnitTestCase
public function testExportModelFromDirectory()
{
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(), '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 (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');
}
......
......@@ -97,7 +97,7 @@ class Doctrine_Hydrate_Mock extends Doctrine_Hydrate
{
$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