Ticket #1792: fix-parse-between.diff

File fix-parse-between.diff, 3.5 KB (added by ajbrown, 14 months ago)

Bugfix for Doctrine_Query_Where including Test Case, and supporting Test Model

  • tests/QueryTestCase.php

     
    3232 */ 
    3333class Doctrine_Query_TestCase extends Doctrine_UnitTestCase  
    3434{ 
    35  
     35     
    3636    public function testWhereInSupportInDql() 
    3737    { 
    3838        $q = Doctrine_Query::create() 
     
    300300        $q1->free(); 
    301301        $q2->free(); 
    302302    } 
     303     
     304    public function testParseTableAliasesWithBetweenInWhereClause() 
     305    { 
     306         
     307        $q1 = Doctrine_Query::create() 
     308            ->select('u.id') 
     309            ->from('QueryTest_User u') 
     310            ->where("CURRENT_DATE() BETWEEN u.QueryTest_Subscription.begin AND u.QueryTest_Subscription.begin") 
     311            ->addWhere( 'u.id != 5' ) 
     312            ; 
     313             
     314        $expected = 'SELECT q.id AS q__id FROM query_test__user q LEFT JOIN query_test__subscription q2 ON q.subscriptionid = q2.id WHERE CURRENT_DATE() BETWEEN q2.begin AND q2.begin AND q.id != 5'; 
     315         
     316        $this->assertEqual( $q1->getSql(), $expected ); 
     317         
     318    }  
    303319} 
    304320 
    305321 
  • tests/models/QueryTest_Subscription.php

     
     1<?php 
     2class QueryTest_Subscription extends Doctrine_Record 
     3{ 
     4    public function setTableDefinition() 
     5    {   $this->hasColumn('id', 'integer', 4, array('primary', 'autoincrement', 'notnull'));      
     6        $this->hasColumn('begin', 'date'); 
     7        $this->hasColumn('end', 'date'); 
     8    } 
     9} 
  • tests/models/QueryTest_User.php

     
    77        $this->hasColumn('username as username', 'string', 50, 
    88                array('notnull')); 
    99        $this->hasColumn('visibleRankId', 'integer', 4); 
     10        $this->hasColumn('subscriptionId', 'integer', 4); 
    1011    } 
    1112 
    1213    /** 
     
    1718        $this->hasOne('QueryTest_Rank as visibleRank', array( 
    1819            'local' => 'visibleRankId', 'foreign' => 'id' 
    1920        )); 
     21         
     22        $this->hasOne('QueryTest_Subscription', array( 
     23            'local' => 'subscriptionId', 'foreign' => 'id' 
     24        )); 
    2025 
    2126        $this->hasMany('QueryTest_Rank as ranks', array( 
    2227            'local' => 'userId', 'foreign' => 'rankId', 'refClass' => 'QueryTest_UserRank' 
  • lib/Doctrine/Query/Where.php

     
    7979            } 
    8080 
    8181            $sql = $this->_buildSql($leftExpr, $operator, $rightExpr); 
     82             
    8283 
    8384            //echo '<pre>Built SQL: '.$sql.'</pre>'; 
    8485 
     
    9192 
    9293    protected function _buildSql($leftExpr, $operator, $rightExpr) 
    9394    { 
    94         // Left Expression 
     95         
    9596        $leftExpr = $this->query->parseClause($leftExpr); 
    96  
     97         
     98        // BETWEEN operation 
     99        if ( 'BETWEEN' == strtoupper( substr( $operator, 0, 7 ) ) ) { 
     100            $midExpr = trim( substr( $operator, 7, -3 ) ); 
     101            $operator = 'BETWEEN ' . $this->query->parseClause( $midExpr ) . ' AND';          
     102        } 
     103      
    97104        $op = strtolower($operator); 
    98105 
    99106        // Right Expression