Commit 2662b46e authored by zYne's avatar zYne

DQL aggregate value model rewrite

parent d4405bb9
......@@ -74,7 +74,7 @@ class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common
$this->options['server_version'] = '';
*/
parent::__construct($manager, $adapter);
//$this->initFunctions();
$this->initFunctions();
}
/**
* initializes database functions missing in sqlite
......@@ -84,9 +84,16 @@ class Doctrine_Connection_Sqlite extends Doctrine_Connection_Common
*/
public function initFunctions()
{
$this->dbh->sqliteCreateFunction('md5', array('Doctrine_Expression_Sqlite', 'md5Impl'), 1);
$this->dbh->sqliteCreateFunction('mod', array('Doctrine_Expression_Sqlite', 'modImpl'), 2);
$this->dbh->sqliteCreateFunction('concat', array('Doctrine_Expression_Sqlite', 'concatImpl'));
$this->dbh->sqliteCreateFunction('now', 'time', 0);
if ($this->dbh instanceof Doctrine_Db) {
$this->dbh->connect();
$adapter = $this->dbh->getDbh();
} else {
$adapter = $this->dbh;
}
$adapter->sqliteCreateFunction('md5', array('Doctrine_Expression_Sqlite', 'md5Impl'), 1);
$adapter->sqliteCreateFunction('mod', array('Doctrine_Expression_Sqlite', 'modImpl'), 2);
$adapter->sqliteCreateFunction('concat', array('Doctrine_Expression_Sqlite', 'concatImpl'));
$adapter->sqliteCreateFunction('now', 'time', 0);
}
}
......@@ -269,7 +269,7 @@ class Doctrine_Expression extends Doctrine_Connection_Module
*
* @param string|array(string) strings that will be concatinated.
*/
public function concat($args)
public function concat()
{
$args = func_get_args();
......
......@@ -578,9 +578,11 @@ class Doctrine_Hydrate implements Serializable
foreach ($row as $index => $value) {
$agg = false;
if (isset($this->_aliasMap[$alias]['agg'][$index])) {
$agg = $this->_aliasMap[$alias]['agg'][$index];
}
if (is_array($record)) {
$record[$agg] = $value;
} else {
......
This diff is collapsed.
......@@ -160,5 +160,51 @@ class Doctrine_Query_AggregateValue_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($users[1]->Phonenumber[0]->count, 2);
$this->assertEqual($users[2]->Phonenumber[0]->count, 1);
}
public function testAggregateFunctionParser()
{
$q = new Doctrine_Query();
$func = $q->parseAggregateFunction('SUM(i.price)');
$this->assertEqual($func, 'SUM(i.price)');
}
public function testAggregateFunctionParser2()
{
$q = new Doctrine_Query();
$func = $q->parseAggregateFunction('SUM(i.price * i.quantity)');
$this->assertEqual($func, 'SUM(i.price * i.quantity)');
}
public function testAggregateFunctionParser3()
{
$q = new Doctrine_Query();
$func = $q->parseAggregateFunction('MOD(i.price, i.quantity)');
$this->assertEqual($func, 'MOD(i.price, i.quantity)');
}
public function testAggregateFunctionParser4()
{
$q = new Doctrine_Query();
$func = $q->parseAggregateFunction('CONCAT(i.price, i.quantity)');
$this->assertEqual($func, 'CONCAT(i.price, i.quantity)');
}
public function testAggregateFunctionParsingSupportsMultipleComponentReferences()
{
$q = new Doctrine_Query();
$q->select('SUM(i.price * i.quantity)')
->from('QueryTest_Item i');
$this->assertEqual($q->getQuery(), "SELECT SUM(q.price * q.quantity) AS q__0 FROM query_test__item q");
}
}
class QueryTest_Item extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('price', 'decimal');
$this->hasColumn('quantity', 'integer');
}
}
?>
......@@ -32,6 +32,16 @@
*/
class Doctrine_Query_Orderby_TestCase extends Doctrine_UnitTestCase
{
public function testOrderByRandomIsSupported()
{
$q = new Doctrine_Query();
$q->select('u.name, RANDOM() rand')
->from('User u')
->orderby('rand DESC');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, ((RANDOM() + 2147483648) / 4294967296) AS e__0 FROM entity e WHERE (e.type = 0) ORDER BY e__0 DESC');
}
public function testOrderByAggregateValueIsSupported()
{
$q = new Doctrine_Query();
......@@ -43,14 +53,4 @@ class Doctrine_Query_Orderby_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, COUNT(p.phonenumber) AS p__0 FROM entity e LEFT JOIN phonenumber p ON e.id = p.entity_id WHERE (e.type = 0) ORDER BY p__0 DESC');
}
public function testOrderByRandomIsSupported()
{
$q = new Doctrine_Query();
$q->select('u.name, RANDOM() rand')
->from('User u')
->orderby('rand DESC');
$this->assertEqual($q->getQuery(), 'SELECT e.id AS e__id, e.name AS e__name, ((RANDOM() + 2147483648) / 4294967296) AS e__0 FROM entity e WHERE (e.type = 0) ORDER BY e__0 DESC');
}
}
......@@ -32,6 +32,18 @@
*/
class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase
{
public function testAggregateFunctionParsingSupportsMultipleComponentReferences()
{
$q = new Doctrine_Query();
$q->select("CONCAT(u.name, ' ', e.address) value")
->from('User u')->innerJoin('u.Email e');
$this->assertEqual($q->getQuery(), "SELECT CONCAT(e.name, ' ', e2.address) AS e__0 FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0)");
$users = $q->execute();
$this->assertEqual($users[0]->value, 'zYne zYne@example.com');
}
public function testAggregateFunctionWithDistinctKeyword()
{
$q = new Doctrine_Query();
......@@ -40,7 +52,6 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($q->getQuery(), 'SELECT COUNT(DISTINCT e.name) AS e__0 FROM entity e WHERE (e.type = 0)');
}
public function testAggregateFunction()
{
$q = new Doctrine_Query();
......@@ -90,8 +101,7 @@ class Doctrine_Query_Select_TestCase extends Doctrine_UnitTestCase
}
}
public function testAggregateFunctionValueHydration()
public function testAggregateFunctionValueHydration()
{
$q = new Doctrine_Query();
......
......@@ -106,9 +106,14 @@ class UnitTestCase
foreach ($trace as $stack) {
if (substr($stack['function'], 0, 4) === 'test') {
$class = new ReflectionClass($stack['class']);
if ( ! isset($line)) {
$line = $stack['line'];
}
$this->_messages[] = $class->getName() . ' : method ' . $stack['function'] . ' failed on line ' . $line;
break;
}
......
......@@ -9,7 +9,11 @@ function autoload($class) {
$e = explode('_', $class);
$count = count($e);
array_shift($e);
$prefix = array_shift($e);
if ($prefix !== 'Doctrine') {
return false;
}
$dir = array_shift($e);
......@@ -23,7 +27,7 @@ function autoload($class) {
// create a test case file if it doesn't exist
if( ! file_exists($file)) {
if ( ! file_exists($file)) {
$contents = file_get_contents('template.tpl');
$contents = sprintf($contents, $class, $class);
......@@ -47,11 +51,12 @@ spl_autoload_register('autoload');
require_once dirname(__FILE__) . '/../models/location.php';
require_once dirname(__FILE__) . '/classes.php';
/**
require_once dirname(__FILE__) . '/../vendor/simpletest/unit_tester.php';
require_once dirname(__FILE__) . '/../vendor/simpletest/reporter.php';
*/
require_once dirname(__FILE__) . '/Test.php';
require_once dirname(__FILE__) . '/UnitTestCase.php';
require_once dirname(__FILE__) . '/DriverTestCase.php';
error_reporting(E_ALL);
......@@ -138,7 +143,7 @@ $test->addTestCase(new Doctrine_Expression_Oracle_TestCase());
$test->addTestCase(new Doctrine_Expression_Sqlite_TestCase());
// Core
*/
$test->addTestCase(new Doctrine_Access_TestCase());
//$test->addTestCase(new Doctrine_Configurable_TestCase());
......@@ -214,6 +219,7 @@ $test->addTestCase(new Doctrine_Query_ShortAliases_TestCase());
$test->addTestCase(new Doctrine_Query_Expression_TestCase());
$test->addTestCase(new Doctrine_ColumnAggregationInheritance_TestCase());
$test->addTestCase(new Doctrine_ColumnAlias_TestCase());
......@@ -223,6 +229,7 @@ $test->addTestCase(new Doctrine_Cache_Memcache_TestCase());
$test->addTestCase(new Doctrine_Cache_Sqlite_TestCase());
$test->addTestCase(new Doctrine_Query_Check_TestCase());
$test->addTestCase(new Doctrine_Query_Limit_TestCase());
......@@ -246,6 +253,7 @@ $test->addTestCase(new Doctrine_Query_Subquery_TestCase());
$test->addTestCase(new Doctrine_Query_AggregateValue_TestCase());
$test->addTestCase(new Doctrine_Query_Select_TestCase());
$test->addTestCase(new Doctrine_Query_From_TestCase());
$test->addTestCase(new Doctrine_NewCore_TestCase());
......@@ -256,7 +264,7 @@ $test->addTestCase(new Doctrine_Record_State_TestCase());
//$test->addTestCase(new Doctrine_Query_Cache_TestCase());
$test->addTestCase(new Doctrine_Tokenizer_TestCase());
*/
$test->addTestCase(new Doctrine_Collection_Snapshot_TestCase());
......@@ -277,33 +285,27 @@ class MyReporter extends HtmlReporter {
public function paintHeader() {}
public function paintFooter()
{
$colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
print "<pre>";
foreach ($this->_test->getMessages() as $message) {
print $message . "\n";
}
print "</pre>";
$colour = ($this->_test->getFailCount() > 0 ? "red" : "green");
print "<div style=\"";
print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
print "\">";
print $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
print $this->_test->getTestCaseCount() . ' test cases';
print " test cases complete:\n";
print "<strong>" . $this->getPassCount() . "</strong> passes, ";
print "<strong>" . $this->getFailCount() . "</strong> fails and ";
print "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
print "<strong>" . $this->_test->getPassCount() . "</strong> passes, ";
print "<strong>" . $this->_test->getFailCount() . "</strong> fails and ";
print "</div>\n";
}
}
if (TextReporter::inCli()) {
if ($argc == 4)
{
$dsn = $argv[1];
$username = $argv[2];
$password = $argv[3];
}
exit ($test->run(new TextReporter()) ? 0 : 1);
}
?>
<html>
<head>
<title>Doctrine Unit Tests</title>
<style>
.fail { color: red; } pre { background-color: lightgray; }
......@@ -313,60 +315,10 @@ if (TextReporter::inCli()) {
<body>
<h1>Doctrine Unit Tests</h1>
<h3>DSN Settings</h3>
<form method="post">
<table>
<tr>
<th>DSN</th>
<td><input type="text" name="dsn" /></td>
</tr>
<tr>
<th>Username</th>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<th>Password</th>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" /></td>
</tr>
</table>
</form>
<h3>Tests</h3>
<pre>
<?php
ob_start();
if (isset($_POST))
{
$dsn = isset($_POST["dsn"])?$_POST["dsn"]:null;
$username = isset($_POST["username"])?$_POST["username"]:null;
$password = isset($_POST["password"])?$_POST["password"]:null;
}
$test->run(new MyReporter());
$output = ob_get_clean();
/**
$cache = Doctrine_Manager::getInstance()->getCurrentConnection()->getCacheHandler();
if(isset($cache)) {
$a = $cache->getQueries();
print "Executed cache queries: ".count($a)."\n";
foreach($a as $query) {
print $query."\n";
}
}
*/
<?php
$test->run(new MyReporter());
?>
<?php echo $output; ?>
</pre>
<h3>Queries</h3>
<pre>
</pre>
</body>
</html>
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