Commit 17b452b2 authored by doctrine's avatar doctrine

new component: Doctrine_RawSql

parent 4df1a9d1
......@@ -12,10 +12,6 @@ class Doctrine_Cache_Query_Sqlite implements Countable {
* @var PDO $dbh database handler
*/
private $dbh;
/**
* @var array $fetched an array of fetched primary keys
*/
private $fetched = array();
/**
* constructor
*
......
......@@ -434,7 +434,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* @param Doctrine_Query $query
* @param integer $key
*/
public function populate(Doctrine_Query $query) {
public function populate(Doctrine_Hydrate $query) {
$name = $this->table->getComponentName();
if($this instanceof Doctrine_Collection_Immediate ||
......
......@@ -47,7 +47,7 @@ class Doctrine_Form implements Iterator {
} elseif($length <= 4000) {
$elements[$column] = "<textarea name='data[$column]' cols=40 rows=10>".$this->record->get($column)."</textarea>\n";
} else {
$elements[$column] = "<textarea name='data[$column]' cols=60 rows=25>".$this->record->get($column)."</textarea>\n";
$elements[$column] = "<textarea name='data[$column]' cols=80 rows=25>".$this->record->get($column)."</textarea>\n";
}
}
return $elements[$column];
......
This diff is collapsed.
This diff is collapsed.
<?php
class Doctrine_RawSql extends Doctrine_Hydrate {
/**
* @var array $fields
*/
private $fields;
/**
* __call
* method overloader
*
* @param string $name
* @param array $args
* @return Doctrine_RawSql
*/
public function __call($name, $args) {
if( ! isset($this->parts[$name]))
throw new Doctrine_Exception("Unknown overload method");
if($name == 'select') {
preg_match_all('/{([^}{]*)}/U', $args[0], $m);
$this->fields = $m[1];
$this->parts["select"] = array();
} else
$this->parts[$name][] = $args[0];
return $this;
}
/**
* parseQuery
*
* @param string $query
* @return void
*/
public function parseQuery($query) {
preg_match_all('/{([^}{]*)}/U', $query, $m);
$this->fields = $m[1];
$this->clear();
$e = explode(" ", $query);
foreach($e as $k => $part):
$low = strtolower($part);
switch(strtolower($part)):
case "select":
case "from":
case "where":
case "limit":
case "offset":
case "having":
$p = $low;
$parts[$low] = array();
break;
case "order":
case "group":
$i = ($k + 1);
if(isset($e[$i]) && strtolower($e[$i]) === "by") {
$p = $part;
$parts[$low] = array();
} else
$parts[$p][] = $part;
break;
case "by":
continue;
default:
$parts[$p][] = $part;
endswitch;
endforeach;
$this->parts = $parts;
$this->parts["select"] = array();
}
/**
* getQuery
*
*
* @return string
*/
public function getQuery() {
$q = array();
foreach($this->fields as $field) {
$e = explode(".", $field);
if( ! isset($e[1]))
throw new Doctrine_Exception("All selected fields in Sql query must be in format tableAlias.fieldName");
if( ! isset($this->tables[$e[0]])) {
try {
$this->addComponent($e[0], ucwords($e[0]));
} catch(Doctrine_Exception $e) {
throw new Doctrine_Exception("The associated component for tablealias $e[0] couldn't be found.");
}
}
if($e[1] == '*') {
foreach($this->tables[$e[0]]->getColumnNames() as $name) {
$field = $e[0].".".$name;
$this->parts["select"][$field] = $field." AS ".$e[0]."__".$name;
}
} else {
$field = $e[0].".".$e[1];
$this->parts["select"][$field] = $field." AS ".$e[0]."__".$e[1];
}
}
// force-add all primary key fields
foreach($this->tableAliases as $alias) {
foreach($this->tables[$alias]->getPrimaryKeys() as $key) {
$field = $alias.".".$key;
if( ! isset($this->parts["select"][$field]))
$this->parts["select"][$field] = $field." AS ".$alias."__".$key;
}
}
$q[] = "SELECT ".implode(', ', $this->parts['select']);
$string = $this->applyInheritance();
if( ! empty($string))
$this->parts["where"][] = $string;
$copy = $this->parts;
unset($copy['select']);
foreach($copy as $name => $part) {
if(empty($part))
continue;
$q[] = strtoupper($name).' '.implode(' ',$part);
}
return implode(' ', $q);
}
/**
* getFields
*
* @return array
*/
public function getFields() {
return $this->fields;
}
/**
* addComponent
*
* @param string $tableAlias
* @param string $componentName
* @return void
*/
public function addComponent($tableAlias, $componentName) {
$e = explode(".", $componentName);
$currPath = '';
foreach($e as $k => $component) {
$currPath .= '.'.$component;
if($k == 0)
$currPath = substr($currPath,1);
if(isset($this->tableAliases[$currPath]))
$alias = $this->tableAliases[$currPath];
else
$alias = $tableAlias;
$table = $this->session->getTable($component);
$this->tables[$alias] = $table;
$this->fetchModes[$alias] = Doctrine::FETCH_IMMEDIATE;
$this->tableAliases[$currPath] = $alias;
if($k !== 0)
$this->joins[$alias] = $prevAlias;
$prevAlias = $alias;
$prevPath = $currPath;
}
}
}
?>
......@@ -67,11 +67,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
/**
* @var integer $id the primary keys of this object
*/
protected $id = array();
protected $id = array();
/**
* @var array $data the record data
*/
protected $data = array();
protected $data = array();
/**
* @var integer $state the state of this record
* @see STATE_* constants
......@@ -80,23 +80,27 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
/**
* @var array $modified an array containing properties that have been modified
*/
protected $modified = array();
protected $modified = array();
/**
* @var array $collections the collections this record is in
*/
private $collections = array();
private $collections = array();
/**
* @var mixed $references an array containing all the references
* @var array $references an array containing all the references
*/
private $references = array();
private $references = array();
/**
* @var mixed $originals an array containing all the original references
* @var array $originals an array containing all the original references
*/
private $originals = array();
private $originals = array();
/**
* @var array $filters
*/
private $filters = array();
/**
* @var integer $index this index is used for creating object identifiers
*/
private static $index = 1;
private static $index = 1;
/**
* @var Doctrine_Null $null a Doctrine_Null object used for extremely fast
* null value testing
......@@ -1108,6 +1112,20 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
break;
endswitch;
}
/**
* filterRelated
* lazy initializes a new filter instance for given related component
*
* @param $componentName name of the related component
* @return Doctrine_Filter
*/
final public function filterRelated($componentName) {
if( ! isset($this->filters[$componentName])) {
$this->filters[$componentName] = new Doctrine_Filter($componentName);
}
return $this->filters[$componentName];
}
/**
* sets enumerated value array for given field
*
......
......@@ -67,34 +67,35 @@ class Doctrine_Relation {
/**
* @return string the relation alias
*/
public function getAlias() {
final public function getAlias() {
return $this->alias;
}
/**
* @return integer the relation type, either 0 or 1
*/
public function getType() {
final public function getType() {
return $this->type;
}
/**
* @return object Doctrine_Table foreign factory object
*/
public function getTable() {
final public function getTable() {
return $this->table;
}
/**
* @return string the name of the local column
*/
public function getLocal() {
final public function getLocal() {
return $this->local;
}
/**
* @return string the name of the foreignkey column where
* the localkey column is pointing at
*/
public function getForeign() {
final public function getForeign() {
return $this->foreign;
}
/**
* getDeleteOperations
*
......@@ -171,6 +172,8 @@ class Doctrine_Relation {
}
/**
* __toString
*
* @return string
*/
public function __toString() {
$r[] = "<pre>";
......
<?php
class Doctrine_Composite_PrimaryKey_TestCase extends Doctrine_UnitTestCase {
public function prepareData() { }
public function prepareTables() {
$this->tables = array();
$this->tables[] = "CPK_Test";
$this->tables[] = "CPK_Test2";
$this->tables[] = "CPK_Association";
parent::prepareTables();
}
}
?>
<?php
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase {
public function testAsteriskOperator() {
// Selecting with *
$query = new Doctrine_RawSql($this->session);
$query->parseQuery("SELECT {entity.*} FROM entity");
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.*"));
$query->addComponent("entity", "Entity");
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
}
public function testLazyPropertyLoading() {
$query = new Doctrine_RawSql($this->session);
$this->session->clear();
// selecting proxy objects (lazy property loading)
$query->parseQuery("SELECT {entity.name}, {entity.id} FROM entity");
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.name", "entity.id"));
$query->addComponent("entity", "Entity");
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$this->assertEqual($coll[0]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($coll[3]->getState(), Doctrine_Record::STATE_PROXY);
}
public function testSmartMapping() {
$query = new Doctrine_RawSql($this->session);
// smart component mapping (no need for additional addComponent call
$query->parseQuery("SELECT {entity.name}, {entity.id} FROM entity");
$fields = $query->getFields();
$this->assertEqual($fields, array("entity.name", "entity.id"));
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$this->assertEqual($coll[0]->getState(), Doctrine_Record::STATE_PROXY);
$this->assertEqual($coll[3]->getState(), Doctrine_Record::STATE_PROXY);
}
public function testMultipleComponents() {
$query = new Doctrine_RawSql($this->session);
// multi component fetching
$query->parseQuery("SELECT {entity.name}, {entity.id}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id");
$query->addComponent("entity", "Entity");
$query->addComponent("phonenumber", "Entity.Phonenumber");
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$count = $this->dbh->count();
$coll[4]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->dbh->count());
$coll[5]->Phonenumber[0]->phonenumber;
$this->assertEqual($count, $this->dbh->count());
}
public function testPrimaryKeySelectForcing() {
// forcing the select of primary key fields
$query = new Doctrine_RawSql($this->session);
$query->parseQuery("SELECT {entity.name} FROM entity");
$coll = $query->execute();
$this->assertEqual($coll->count(), 11);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
public function testMethodOverloading() {
$query = new Doctrine_RawSql($this->session);
$query->select('{entity.name}')->from('entity');
$query->addComponent("entity", "User");
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
public function testColumnAggregationInheritance() {
// forcing the select of primary key fields
$query = new Doctrine_RawSql($this->session);
$query->parseQuery("SELECT {entity.name} FROM entity");
$query->addComponent("entity", "User");
$coll = $query->execute();
$this->assertEqual($coll->count(), 8);
$this->assertTrue(is_numeric($coll[0]->id));
$this->assertTrue(is_numeric($coll[3]->id));
$this->assertTrue(is_numeric($coll[7]->id));
}
}
?>
......@@ -310,7 +310,7 @@ class ORM_AccessControlsGroups extends Doctrine_Record {
$this->hasColumn("accessControlID", "integer", 11);
$this->hasColumn("accessGroupID", "integer", 11);
$this->setPrimaryKey(array("accessControlID", "accessGroupID"));
$this->setPrimaryKey(array("accessControlID", "accessGroupID"));
}
}
class EnumTest extends Doctrine_Record {
......@@ -328,7 +328,28 @@ class Log_Entry extends Doctrine_Record {
$this->hasOne("Log_Status", "Log_Entry.status_id");
}
}
class CPK_Test extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name", "string", 255);
}
public function setUp() {
$this->hasMany("CPK_Test2 as Test","CPK_Association.test2_id");
}
}
class CPK_Test2 extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name", "string", 255);
}
public function setUp() {
$this->hasMany("CPK_Test as Test","CPK_Association.test1_id");
}
}
class CPK_Association extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("test1_id", "integer", 11, "primary");
$this->hasColumn("test2_id", "integer", 11, "primary");
}
}
class Log_Status extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name", "string", 255);
......
......@@ -19,6 +19,7 @@ require_once("CollectionOffsetTestCase.php");
require_once("QueryTestCase.php");
require_once("CacheQuerySqliteTestCase.php");
require_once("ViewTestCase.php");
require_once("RawSqlTestCase.php");
error_reporting(E_ALL);
......@@ -54,6 +55,7 @@ $test->addTestCase(new Doctrine_Cache_Query_SqliteTestCase());
$test->addTestCase(new Doctrine_QueryTestCase());
$test->addTestCase(new Doctrine_RawSql_TestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
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