Commit cb79b1d9 authored by doctrine's avatar doctrine

--no commit message

--no commit message
parent b6e93657
...@@ -33,12 +33,21 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -33,12 +33,21 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* @var boolean $expanded whether or not this collection has been expanded * @var boolean $expanded whether or not this collection has been expanded
*/ */
protected $expanded = false; protected $expanded = false;
/**
* @var mixed $generator
*/
protected $generator;
/** /**
* constructor * constructor
*/ */
public function __construct(Doctrine_Table $table) { public function __construct(Doctrine_Table $table) {
$this->table = $table; $this->table = $table;
$name = $table->getAttribute(Doctrine::ATTR_COLL_KEY);
if($name !== null) {
$this->generator = new Doctrine_IndexGenerator($name);
}
} }
/** /**
* @return object Doctrine_Table * @return object Doctrine_Table
...@@ -46,6 +55,25 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -46,6 +55,25 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
public function getTable() { public function getTable() {
return $this->table; return $this->table;
} }
/**
* @param Doctrine_IndexGenerator $generator
* @return void
*/
public function setGenerator($generator) {
$this->generator = $generator;
}
/**
* @return Doctrine_IndexGenerator
*/
public function getGenerator() {
return $this->generator;
}
/**
* @return array
*/
public function getData() {
return $this->data;
}
/** /**
* @param array $data * @param array $data
*/ */
...@@ -214,7 +242,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -214,7 +242,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
/** /**
* count * count
* this class implements interface countable * this class implements interface countable
* @return integer number of data access objects in this collection * @return integer number of records in this collection
*/ */
public function count() { public function count() {
return count($this->data); return count($this->data);
...@@ -231,11 +259,11 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -231,11 +259,11 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$this->data[$key] = $record; $this->data[$key] = $record;
} }
/** /**
* add * adds a record to collection
* adds a dao instance to this collection * @param Doctrine_Record $record record to be added
* @param Doctrine_Record $record data access object to be added * @param string $key optional key for the record
* @param string $key optional key for the DAO
* @return boolean * @return boolean
*/ */
public function add(Doctrine_Record $record,$key = null) { public function add(Doctrine_Record $record,$key = null) {
...@@ -243,27 +271,29 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -243,27 +271,29 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$record->rawSet($this->reference_field,$this->reference); $record->rawSet($this->reference_field,$this->reference);
if(isset($key)) { if(isset($key)) {
if(isset($this->data[$key])) if(isset($this->data[$key]))
return false; return false;
$this->data[$key] = $record; $this->data[$key] = $record;
} }
$this->data[] = $record; if(isset($this->generator)) {
$key = $this->generator->getIndex($record);
$this->data[$key] = $record;
} else
$this->data[] = $record;
return true; return true;
} }
/** /**
* save * save
* saves all data access objects * saves all records
*/ */
public function save() { public function save() {
$this->table->getSession()->saveCollection($this); $this->table->getSession()->saveCollection($this);
} }
/** /**
* single shot delete * single shot delete
* deletes all dao instances from this collection * deletes all records from this collection
* uses only one database query to perform this operation * uses only one database query to perform this operation
* @return boolean * @return boolean
*/ */
......
...@@ -23,10 +23,19 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { ...@@ -23,10 +23,19 @@ class Doctrine_Collection_Batch extends Doctrine_Collection {
public function __construct(Doctrine_DQL_Parser $graph,$key) { public function __construct(Doctrine_DQL_Parser $graph,$key) {
parent::__construct($graph->getTable($key)); parent::__construct($graph->getTable($key));
$this->data = $graph->getData($key); $this->data = $graph->getData($key);
if( ! is_array($this->data)) if( ! is_array($this->data))
$this->data = array(); $this->data = array();
if(isset($this->generator)) {
foreach($this->data as $k => $v) {
$record = $this->get($k);
$i = $this->generator->getIndex($record);
$this->data[$i] = $record;
unset($this->data[$k]);
}
}
$this->batchSize = $this->getTable()->getAttribute(Doctrine::ATTR_BATCH_SIZE); $this->batchSize = $this->getTable()->getAttribute(Doctrine::ATTR_BATCH_SIZE);
} }
...@@ -138,6 +147,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { ...@@ -138,6 +147,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection {
if( ! isset($this->data[$key])) if( ! isset($this->data[$key]))
$this->data[$key] = $this->table->create(); $this->data[$key] = $this->table->create();
} }
......
...@@ -13,16 +13,18 @@ class Doctrine_Collection_Immediate extends Doctrine_Collection { ...@@ -13,16 +13,18 @@ class Doctrine_Collection_Immediate extends Doctrine_Collection {
* @param integer $key * @param integer $key
*/ */
public function __construct(Doctrine_DQL_Parser $graph,$key) { public function __construct(Doctrine_DQL_Parser $graph,$key) {
parent::__construct($graph->getTable($key)); $table = $graph->getTable($key);
parent::__construct($table);
$name = $this->table->getComponentName();
$name = $table->getComponentName();
$data = $graph->getData($name); $data = $graph->getData($name);
if(is_array($data)) { if(is_array($data)) {
foreach($data as $k=>$v): foreach($data as $k=>$v):
$this->table->setData($v); $table->setData($v);
$this->add($this->table->getRecord()); $this->add($this->table->getRecord());
endforeach; endforeach;
} }
} }
} }
?> ?>
...@@ -86,16 +86,25 @@ abstract class Doctrine_Configurable { ...@@ -86,16 +86,25 @@ abstract class Doctrine_Configurable {
break; break;
case Doctrine::ATTR_CREATE_TABLES: case Doctrine::ATTR_CREATE_TABLES:
$value = (bool) $value; $value = (bool) $value;
break;
case Doctrine::ATTR_COLL_KEY:
if( ! ($this instanceof Doctrine_Table))
throw new Doctrine_Exception("This attribute can only be set at table level.");
if( ! $this->hasColumn($value))
throw new Doctrine_Exception("Couldn't set collection key attribute. No such column '$value'");
break; break;
case Doctrine::ATTR_VLD: case Doctrine::ATTR_VLD:
break; break;
case Doctrine::ATTR_CACHE: case Doctrine::ATTR_CACHE:
if($value != Doctrine::CACHE_SQLITE && $value != Doctrine::CACHE_NONE) if($value != Doctrine::CACHE_SQLITE && $value != Doctrine::CACHE_NONE)
throw new Doctrine_Exception("Unknown cache container. See Doctrine::CACHE_* constants for availible containers."); throw new Doctrine_Exception("Unknown cache container. See Doctrine::CACHE_* constants for availible containers.");
break; break;
default: default:
throw new Exception("Unknown attribute."); throw new Doctrine_Exception("Unknown attribute.");
endswitch; endswitch;
$this->attributes[$attribute] = $value; $this->attributes[$attribute] = $value;
...@@ -115,7 +124,7 @@ abstract class Doctrine_Configurable { ...@@ -115,7 +124,7 @@ abstract class Doctrine_Configurable {
final public function getAttribute($attribute) { final public function getAttribute($attribute) {
$attribute = (int) $attribute; $attribute = (int) $attribute;
if($attribute < 1 || $attribute > 14) if($attribute < 1 || $attribute > 15)
throw new InvalidKeyException(); throw new InvalidKeyException();
if( ! isset($this->attributes[$attribute])) { if( ! isset($this->attributes[$attribute])) {
......
...@@ -128,6 +128,18 @@ class Doctrine_DQL_Parser { ...@@ -128,6 +128,18 @@ class Doctrine_DQL_Parser {
} }
} }
} }
/**
* @param integer $limit
*/
final public function setLimit($limit) {
$this->limit = $limit;
}
/**
* @param integer $offset
*/
final public function setOffset($offset) {
$this->offset = $offset;
}
/** /**
* @return integer * @return integer
*/ */
...@@ -422,7 +434,7 @@ class Doctrine_DQL_Parser { ...@@ -422,7 +434,7 @@ class Doctrine_DQL_Parser {
if($this->aggregate) { if($this->aggregate) {
$keys = array_keys($this->tnames); $keys = array_keys($this->tnames);
$query = $this->getQuery(); $query = $this->getQuery();
$stmt = $this->tnames[$keys[0]]->getSession()->select($query); $stmt = $this->tnames[$keys[0]]->getSession()->select($query,$this->limit,$this->offset);
$data = $stmt->fetch(PDO::FETCH_ASSOC); $data = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($data) == 1) { if(count($data) == 1) {
return current($data); return current($data);
...@@ -454,7 +466,7 @@ class Doctrine_DQL_Parser { ...@@ -454,7 +466,7 @@ class Doctrine_DQL_Parser {
case "order": case "order":
$p = $part; $p = $part;
$i = $k+1; $i = $k+1;
if(isset($e[$i]) AND strtolower($e[$i]) == "by") { if(isset($e[$i]) && strtolower($e[$i]) == "by") {
$parts[$part] = array(); $parts[$part] = array();
} }
break; break;
...@@ -633,7 +645,7 @@ class Doctrine_DQL_Parser { ...@@ -633,7 +645,7 @@ class Doctrine_DQL_Parser {
* @param string $e2 the second bracket, usually ')' * @param string $e2 the second bracket, usually ')'
*/ */
public static function bracketTrim($str,$e1,$e2) { public static function bracketTrim($str,$e1,$e2) {
if(substr($str,0,1) == $e1 AND substr($str,-1) == $e2) if(substr($str,0,1) == $e1 && substr($str,-1) == $e2)
return substr($str,1,-1); return substr($str,1,-1);
else else
return $str; return $str;
...@@ -781,10 +793,8 @@ class Doctrine_DQL_Parser { ...@@ -781,10 +793,8 @@ class Doctrine_DQL_Parser {
$this->tnames[$name] = $objTable; $this->tnames[$name] = $objTable;
} }
} catch(Doctrine_Exception $e) { } catch(Exception $e) {
throw new DQLException(); throw new DQLException($e->getMessage(),$e->getCode());
} catch(InvalidKeyException $e) {
throw new DQLException();
} }
} }
return $objTable; return $objTable;
......
...@@ -116,6 +116,10 @@ final class Doctrine { ...@@ -116,6 +116,10 @@ final class Doctrine {
* create tables attribute * create tables attribute
*/ */
const ATTR_CREATE_TABLES = 14; const ATTR_CREATE_TABLES = 14;
/**
* collection key attribute
*/
const ATTR_COLL_KEY = 15;
...@@ -189,7 +193,7 @@ final class Doctrine { ...@@ -189,7 +193,7 @@ final class Doctrine {
public static function getPath() { public static function getPath() {
if(! self::$path) if(! self::$path)
self::$path = dirname(__FILE__); self::$path = dirname(__FILE__);
return self::$path; return self::$path;
} }
/** /**
......
<?php
class Doctrine_IndexGenerator {
/**
* @var string $name
*/
private $name;
/**
* @param string $name
*/
public function __construct($name) {
$this->name = $name;
}
/**
* @param Doctrine_Record $record
* @return mixed
*/
public function getIndex(Doctrine_Record $record) {
$value = $record->get($this->name);
if($value === null)
throw new Doctrine_Exception("Couldn't create collection index. Record field '".$this->name."' was null.");
return $value;
}
}
?>
...@@ -957,7 +957,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -957,7 +957,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return void * @return void
*/ */
final public function hasColumn($name, $type, $length = 20, $options = "") { final public function hasColumn($name, $type, $length = 20, $options = "") {
$this->table->hasColumn($name, $type, $length, $options); $this->table->setColumn($name, $type, $length, $options);
} }
/** /**
* returns a string representation of this object * returns a string representation of this object
......
...@@ -12,15 +12,15 @@ class Sensei_Entity extends Doctrine_Record { ...@@ -12,15 +12,15 @@ class Sensei_Entity extends Doctrine_Record {
} }
} }
class Sensei_Variable extends Doctrine_Record { class Sensei_Variable extends Doctrine_Record {
public function setUp() {
//$this->setAttribute(Doctrine::ATTR_COLL_KEY, "name");
}
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",50); $this->hasColumn("name","string",50,"unique");
$this->hasColumn("value","string",10000); $this->hasColumn("value","string",10000);
$this->hasColumn("session_id","integer"); $this->hasColumn("session_id","integer");
} }
} }
class Sensei_Entity_Var extends Sensei_Variable { }
class Sensei_Session_Var extends Doctrine_Record { }
class Sensei_Session extends Doctrine_Record { class Sensei_Session extends Doctrine_Record {
public function setUp() { public function setUp() {
$this->ownsMany("Sensei_variable","Sensei_variable.session_id"); $this->ownsMany("Sensei_variable","Sensei_variable.session_id");
...@@ -159,17 +159,17 @@ class Sensei extends Doctrine_Access { ...@@ -159,17 +159,17 @@ class Sensei extends Doctrine_Access {
return true; return true;
} }
private function read($id) { private function read($id) {
$coll = $this->session->query("FROM Sensei_Session WHERE Sensei_Session.session_id = ?",array($id)); $coll = $this->session->query("FROM Sensei_Session, Sensei_Session.Sensei_Variable WHERE Sensei_Session.session_id = ?",array($id));
$this->record = $coll[0]; $this->record = $coll[0];
$this->record->user_agent = $_SERVER['HTTP_USER_AGENT']; $this->record->user_agent = $_SERVER['HTTP_USER_AGENT'];
$this->record->updated = time(); $this->record->updated = time();
$this->record->session_id = $id; $this->record->session_id = $id;
$this->vars = $this->record->Sensei_variable;
if($this->record->getState() == Doctrine_Record::STATE_TDIRTY) { if($this->record->getState() == Doctrine_Record::STATE_TDIRTY) {
$this->record->created = time(); $this->record->created = time();
$this->record->save(); $this->record->save();
} }
$this->vars = $this->record->Sensei_variable;
return ""; return "";
} }
public function write($id,$sess_data) { public function write($id,$sess_data) {
......
...@@ -201,14 +201,14 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -201,14 +201,14 @@ class Doctrine_Table extends Doctrine_Configurable {
endswitch; endswitch;
} }
/** /**
* hasColumn * setColumn
* @param string $name * @param string $name
* @param string $type * @param string $type
* @param integer $length * @param integer $length
* @param mixed $options * @param mixed $options
* @return void * @return void
*/ */
final public function hasColumn($name, $type, $length, $options = "") { final public function setColumn($name, $type, $length, $options = "") {
$this->columns[$name] = array($type,$length,$options); $this->columns[$name] = array($type,$length,$options);
$e = explode("|",$options); $e = explode("|",$options);
...@@ -216,6 +216,13 @@ class Doctrine_Table extends Doctrine_Configurable { ...@@ -216,6 +216,13 @@ class Doctrine_Table extends Doctrine_Configurable {
$this->primaryKeys[] = $name; $this->primaryKeys[] = $name;
} }
} }
/**
* hasColumn
* @return boolean
*/
final public function hasColumn($name) {
return isset($this->columns[$name]);
}
/** /**
* returns all primary keys * returns all primary keys
* @return array * @return array
......
<?php <?php
class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase {
public function testAdd() { public function testAdd() {
$coll = new Doctrine_Collection($this->objTable);
} $coll->add(new User());
$this->assertEqual($coll->count(),1);
$coll->add(new User());
$this->assertTrue($coll->count(),2);
$this->assertEqual($coll->getKeys(), array(0,1));
$coll[2] = new User();
$this->assertTrue($coll->count(),3);
$this->assertEqual($coll->getKeys(), array(0,1,2));
}
public function testExpand() {
$users = $this->session->query("FROM User, User.Phonenumber-l WHERE User.Phonenumber.phonenumber LIKE '%123%'");
$this->assertTrue($users instanceof Doctrine_Collection);
$this->assertTrue($users[1] instanceof User);
$data = $users[1]->Phonenumber->getData();
$coll = $users[1]->Phonenumber;
$this->assertEqual(count($data), 1);
$coll[1];
$this->assertEqual(count($coll), 3);
$this->assertTrue($coll[2]->getState() == Doctrine_Record::STATE_PROXY);
$generator = new Doctrine_IndexGenerator("id");
$coll->setGenerator($generator);
$generator = $coll->getGenerator();
$this->assertEqual($generator->getIndex($this->old), 4);
}
public function testGenerator() {
$generator = new Doctrine_IndexGenerator("name");
$coll = new Doctrine_Collection($this->objTable);
$coll->setGenerator($generator);
$user = new User();
$user->name = "name";
$coll->add($user);
$this->assertEqual($coll["name"], $user);
$this->session->getTable("email")->setAttribute(Doctrine::ATTR_COLL_KEY,"address");
$emails = $this->session->getTable("email")->findAll();
foreach($emails as $k => $v) {
$this->assertTrue(gettype($k), "string");
}
}
} }
?> ?>
...@@ -54,6 +54,29 @@ class Doctrine_ConfigurableTestCase extends Doctrine_UnitTestCase { ...@@ -54,6 +54,29 @@ class Doctrine_ConfigurableTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($e instanceof Exception); $this->assertTrue($e instanceof Exception);
$this->session->commit(); $this->session->commit();
} }
$e = false;
try {
$this->manager->setAttribute(Doctrine::ATTR_COLL_KEY, "name");
} catch(Exception $e) {
}
$this->assertTrue($e instanceof Exception);
$e = false;
try {
$this->objTable->setAttribute(Doctrine::ATTR_COLL_KEY, "unknown");
} catch(Exception $e) {
}
$this->assertTrue($e instanceof Exception);
$e = true;
try {
$this->objTable->setAttribute(Doctrine::ATTR_COLL_KEY, "name");
} catch(Exception $e) {
}
$this->assertTrue($e);
$e = false;
try { try {
$this->session->beginTransaction(); $this->session->beginTransaction();
$this->session->setAttribute(Doctrine::ATTR_LOCKMODE, Doctrine::LOCK_PESSIMISTIC); $this->session->setAttribute(Doctrine::ATTR_LOCKMODE, Doctrine::LOCK_PESSIMISTIC);
......
...@@ -52,8 +52,7 @@ class Sensei_UnitTestCase extends UnitTestCase { ...@@ -52,8 +52,7 @@ class Sensei_UnitTestCase extends UnitTestCase {
$this->init(); $this->init();
} }
public function testConstructor() { public function testConstructor() {
$this->assertTrue($this->record instanceof Sensei_Session); $this->assertTrue($this->record instanceof Sensei_Session);
if(isset($_COOKIE["PHPSESSID"])) { if(isset($_COOKIE["PHPSESSID"])) {
......
...@@ -7,7 +7,7 @@ class Entity extends Doctrine_Record { ...@@ -7,7 +7,7 @@ class Entity extends Doctrine_Record {
} }
public function setTableDefinition() { public function setTableDefinition() {
$this->hasColumn("name","string",50); $this->hasColumn("name","string",50);
$this->hasColumn("loginname","string",20); $this->hasColumn("loginname","string",20,"unique");
$this->hasColumn("password","string",16); $this->hasColumn("password","string",16);
$this->hasColumn("type","integer",1); $this->hasColumn("type","integer",1);
$this->hasColumn("created","integer",11); $this->hasColumn("created","integer",11);
......
...@@ -11,6 +11,7 @@ require_once("RecordTestCase.class.php"); ...@@ -11,6 +11,7 @@ require_once("RecordTestCase.class.php");
require_once("DQLParserTestCase.class.php"); require_once("DQLParserTestCase.class.php");
require_once("AccessTestCase.class.php"); require_once("AccessTestCase.class.php");
require_once("ValidatorTestCase.class.php"); require_once("ValidatorTestCase.class.php");
require_once("CollectionTestCase.class.php");
require_once("CacheSqliteTestCase.class.php"); require_once("CacheSqliteTestCase.class.php");
require_once("SenseiTestCase.class.php"); require_once("SenseiTestCase.class.php");
...@@ -24,7 +25,7 @@ $test = new GroupTest("Doctrine Framework Unit Tests"); ...@@ -24,7 +25,7 @@ $test = new GroupTest("Doctrine Framework Unit Tests");
/**
$test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase()); $test->addTestCase(new Doctrine_SessionTestCase());
...@@ -34,16 +35,19 @@ $test->addTestCase(new Doctrine_ManagerTestCase()); ...@@ -34,16 +35,19 @@ $test->addTestCase(new Doctrine_ManagerTestCase());
$test->addTestCase(new Doctrine_TableTestCase()); $test->addTestCase(new Doctrine_TableTestCase());
$test->addTestCase(new Doctrine_AccessTestCase()); $test->addTestCase(new Doctrine_AccessTestCase());
$test->addTestCase(new Doctrine_ConfigurableTestCase());
$test->addTestCase(new Doctrine_EventListenerTestCase()); $test->addTestCase(new Doctrine_EventListenerTestCase());
$test->addTestCase(new Doctrine_DQL_ParserTestCase()); $test->addTestCase(new Doctrine_DQL_ParserTestCase());
$test->addTestCase(new Doctrine_BatchIteratorTestCase()); $test->addTestCase(new Doctrine_BatchIteratorTestCase());
*/
$test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_ConfigurableTestCase());
$test->addTestCase(new Sensei_UnitTestCase()); $test->addTestCase(new Sensei_UnitTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$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