Changeset 1317

Show
Ignore:
Timestamp:
11/27/11 12:00:43 (19 months ago)
Author:
vakopian
Message:

reviewed by alex. added unit tests. Fixes #79

Location:
framework/branches/2.0
Files:
1 added
5 modified

Legend:

Unmodified
Added
Removed
  • framework/branches/2.0/assets/_core/php/examples/includes/examples.inc.php

    r1296 r1317  
    5151                        self::AddCoreExampleFile($intIndex, '/qcubed_query/expandasarray.php * ExpandAsArray: Multiple related tables in one swift query'); 
    5252                        self::AddCoreExampleFile($intIndex, '/qcubed_query/alias.php * SQL Aliases for QQuery'); 
     53                        self::AddCoreExampleFile($intIndex, '/qcubed_query/qqselect.php * Picking database columns for QQuery'); 
    5354                        self::AddCoreExampleFile($intIndex, '/qcubed_query/subsql.php * Custom SQL Subqueries for QQuery'); 
    5455                        self::AddCoreExampleFile($intIndex, '/qcubed_query/intro.php * Performing Custom SQL Queries'); 
  • framework/branches/2.0/includes/qcubed/_core/codegen/templates/db_orm/class_gen/qcubed_query_methods.tpl

    r1315 r1317  
    3737                        } 
    3838                        if ($blnAddAllFieldsToSelect) { 
    39                                 <%= $objTable->ClassName %>::GetSelectFields($objQueryBuilder); 
     39                                <%= $objTable->ClassName %>::GetSelectFields($objQueryBuilder, null, QQuery::extractSelectClause($objOptionalClauses)); 
    4040                        } 
    4141                        $objQueryBuilder->AddFromItem('<%= $objTable->Name %>'); 
     
    215215                 * @param string $strPrefix optional prefix to add to the SELECT fields 
    216216                 */ 
    217                 public static function GetSelectFields(QQueryBuilder $objBuilder, $strPrefix = null) { 
     217                public static function GetSelectFields(QQueryBuilder $objBuilder, $strPrefix = null, QQSelect $objSelect = null) { 
    218218                        if ($strPrefix) { 
    219219                                $strTableName = $strPrefix; 
     
    224224                        } 
    225225 
     226            if ($objSelect) { 
     227<% foreach ($objTable->PrimaryKeyColumnArray as $objColumn) { %> 
     228                            $objBuilder->AddSelectItem($strTableName, '<%= $objColumn->Name %>', $strAliasPrefix . '<%= $objColumn->Name %>'); 
     229<% } %> 
     230                $objSelect->AddSelectItems($objBuilder, $strTableName, $strAliasPrefix); 
     231            } else { 
    226232<% foreach ($objTable->ColumnArray as $objColumn) { %> 
    227                         $objBuilder->AddSelectItem($strTableName, '<%= $objColumn->Name %>', $strAliasPrefix . '<%= $objColumn->Name %>'); 
     233                            $objBuilder->AddSelectItem($strTableName, '<%= $objColumn->Name %>', $strAliasPrefix . '<%= $objColumn->Name %>'); 
    228234<% } %> 
    229                 } 
     235            } 
     236                } 
  • framework/branches/2.0/includes/qcubed/_core/framework/QQuery.class.php

    r1310 r1317  
    8282                } 
    8383 
    84                 abstract public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection); 
    85                 abstract public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null); 
     84                abstract public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection, QQSelect $objSelect = null); 
     85                abstract public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null); 
    8686        } 
    8787 
     
    166166                } 
    167167 
    168                 public function GetTable(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null) { 
     168                public function GetTable(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
    169169                        // Make sure our Root Tables Match 
    170170                        if ($this->_RootTableName != $objBuilder->RootTableName) 
     
    177177                                // Use the Helper to Iterate Through the Parent Chain and get the Parent Alias 
    178178                                try { 
    179                                         $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection); 
     179                                        $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection, QQ::Select()); 
    180180 
    181181                                        if ($this->strTableName) { 
     
    185185 
    186186                                                if ($blnExpandSelection) 
    187                                                         call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias); 
     187                                                        call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias, $objSelect); 
    188188                                        } 
    189189                                } catch (QCallerException $objExc) { 
     
    196196                } 
    197197 
    198                 public function GetTableAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null) { 
    199                         $strTable = $this->GetTable($objBuilder, $blnExpandSelection, $objJoinCondition); 
     198                public function GetTableAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
     199                        $strTable = $this->GetTable($objBuilder, $blnExpandSelection, $objJoinCondition, $objSelect); 
    200200                        return $objBuilder->GetTableAlias($strTable); 
    201201                } 
     
    210210                } 
    211211 
    212                 public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null) { 
    213                         $strTableAlias = $this->GetTableAlias($objBuilder, $blnExpandSelection, $objJoinCondition); 
     212                public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
     213                        $strTableAlias = $this->GetTableAlias($objBuilder, $blnExpandSelection, $objJoinCondition, $objSelect); 
    214214                        // Pull the Begin and End Escape Identifiers from the Database Adapter 
    215215                        return $this->MakeColumnAlias($objBuilder, $strTableAlias); 
     
    221221                } 
    222222 
    223                 public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection) { 
     223                public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection, QQSelect $objSelect = null) { 
    224224                        // Are we at the Parent Node? 
    225225                        if (is_null($this->objParentNode)) 
     
    229229                                try { 
    230230                                        // No -- First get the Parent Alias 
    231                                         $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection); 
     231                                        $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection, QQ::Select()); 
    232232 
    233233                                        $strJoinTableAlias = $strParentAlias . '__' . $this->strAlias; 
     
    241241                                // Next, Expand the Selection Fields for this Table (if applicable) 
    242242                                if ($blnExpandSelection) { 
    243                                         call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias); 
     243                                        call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias, $objSelect); 
    244244                                } 
    245245 
     
    372372                } 
    373373 
    374                 public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null) { 
    375                         $this->GetTableAlias($objBuilder, $blnExpandSelection, $objJoinCondition); 
     374                public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
     375                        $this->GetTableAlias($objBuilder, $blnExpandSelection, $objJoinCondition, $objSelect); 
    376376                        return null; 
    377377                } 
     
    408408                } 
    409409 
    410                 public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null) { 
     410                public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
    411411                        // Make sure our Root Tables Match 
    412412                        if ($this->_RootTableName != $objBuilder->RootTableName) 
     
    424424                        else { 
    425425                                // Use the Helper to Iterate Through the Parent Chain and get the Parent Alias 
    426                                 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection); 
     426                                $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection, QQ::Select()); 
    427427 
    428428                                if ($this->strTableName) { 
     
    432432 
    433433                                        if ($blnExpandSelection) 
    434                                                 call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strParentAlias . '__' . $this->strName); 
     434                                                call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strParentAlias . '__' . $this->strName, $objSelect); 
    435435                                } 
    436436 
     
    442442                } 
    443443                 
    444                 public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection) { 
     444                public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection, QQSelect $objSelect = null) { 
    445445                        // Are we at the Parent Node? 
    446446                        if (is_null($this->objParentNode)) 
     
    449449                        else { 
    450450                                // No -- First get the Parent Alias 
    451                                 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection); 
     451                                $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection, QQ::Select()); 
    452452 
    453453                                // Next, Join the Appropriate Table 
     
    10701070                } 
    10711071 
    1072                 static public function Expand($objNode, QQCondition $objJoinCondition = null) { 
     1072                static public function Expand($objNode, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
    10731073//                      if (gettype($objNode) == 'string') 
    10741074//                              return new QQExpandVirtualNode(new QQVirtualNode($objNode)); 
     
    10771077                                return new QQExpandVirtualNode($objNode); 
    10781078                        else 
    1079                                 return new QQExpand($objNode, $objJoinCondition); 
    1080                 } 
    1081  
    1082                 static public function ExpandAsArray($objNode) { 
    1083                         return new QQExpandAsArray($objNode); 
     1079                                return new QQExpand($objNode, $objJoinCondition, $objSelect); 
     1080                } 
     1081 
     1082                static public function ExpandAsArray($objNode, QQSelect $objSelect = null) { 
     1083                        return new QQExpandAsArray($objNode, $objSelect); 
     1084                } 
     1085 
     1086                static public function Select(/* array and/or parameterized list of QQNode objects*/) { 
     1087                        return new QQSelect(func_get_args()); 
    10841088                } 
    10851089 
     
    10901094                static public function Distinct() { 
    10911095                        return new QQDistinct(); 
     1096                } 
     1097 
     1098                /** 
     1099                 * @param QQClause[]|QQClause|null $objClauses QQClause object or array of QQClause objects 
     1100                 * @return QQSelect QQSelect clause containing all the nodes from all the QQSelect clauses from $objClauses, 
     1101                 * or null if $objClauses contains no QQSelect clauses 
     1102                 */ 
     1103                public static function extractSelectClause($objClauses) { 
     1104                        if ($objClauses instanceof QQSelect) 
     1105                                return $objClauses; 
     1106 
     1107                        if (is_array($objClauses)) { 
     1108                                $hasSelects = false; 
     1109                                $objSelect = QQuery::Select(); 
     1110                                foreach ($objClauses as $objClause) { 
     1111                                        if ($objClause instanceof QQSelect) { 
     1112                                                $hasSelects = true; 
     1113                                                $objSelect->Merge($objClause); 
     1114                                        } 
     1115                                } 
     1116                                if (!$hasSelects) 
     1117                                        return null; 
     1118                                return $objSelect; 
     1119                        } 
     1120                        return null; 
    10921121                } 
    10931122 
     
    11671196                        $this->strSql = $strSql; 
    11681197                } 
    1169                 public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null) { 
     1198                public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
    11701199                        $strSql = $this->strSql; 
    11711200                        for ($intIndex = 1; $intIndex < count($this->objParentQueryNodes); $intIndex++) { 
     
    11851214                        $this->objSubQueryDefinition = $objSubQueryDefinition; 
    11861215                } 
    1187                 public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null) { 
     1216                public function GetColumnAlias(QQueryBuilder $objBuilder, $blnExpandSelection = false, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 
    11881217                        if ($this->objSubQueryDefinition) { 
    11891218                                $objBuilder->SetVirtualNode($this->strName, $this->objSubQueryDefinition); 
     
    13771406                protected $objNode; 
    13781407                protected $objJoinCondition; 
    1379  
    1380                 public function __construct($objNode, QQCondition $objJoinCondition = null) { 
     1408                protected $objSelect; 
     1409 
     1410                public function __construct($objNode, QQCondition $objJoinCondition = null, QQSelect $objSelect  = null) { 
    13811411                        // Check against root and table QQNodes 
    13821412                        if ($objNode instanceof QQAssociationNode) 
     
    13891419                        $this->objNode = $objNode; 
    13901420                        $this->objJoinCondition = $objJoinCondition; 
    1391                 } 
    1392                 public function UpdateQueryBuilder(QQueryBuilder $objBuilder) { 
    1393                         $this->objNode->GetColumnAlias($objBuilder, true, $this->objJoinCondition); 
     1421                        $this->objSelect = $objSelect; 
     1422                } 
     1423                public function UpdateQueryBuilder(QQueryBuilder $objBuilder) { 
     1424                        $this->objNode->GetColumnAlias($objBuilder, true, $this->objJoinCondition, $this->objSelect); 
    13941425                } 
    13951426                public function __toString() { 
     
    14531484                /** @var QQNode|QQAssociationNode */ 
    14541485                protected $objNode; 
    1455                 public function __construct($objNode) { 
     1486                protected $objSelect; 
     1487                public function __construct($objNode, QQSelect $objSelect = null) { 
    14561488                        // Ensure that this is an QQAssociationNode 
    14571489                        if ((!($objNode instanceof QQAssociationNode)) && (!($objNode instanceof QQReverseReferenceNode))) 
     
    14591491                                 
    14601492                        $this->objNode = $objNode; 
     1493                        $this->objSelect = $objSelect; 
    14611494                } 
    14621495                public function UpdateQueryBuilder(QQueryBuilder $objBuilder) { 
    14631496                        if ($this->objNode instanceof QQAssociationNode) 
    1464                                 $this->objNode->_ChildTableNode->GetColumnAlias($objBuilder, true); 
     1497                                $this->objNode->_ChildTableNode->GetColumnAlias($objBuilder, true, null, $this->objSelect); 
    14651498                        else 
    1466                                 $this->objNode->GetColumnAlias($objBuilder, true); 
     1499                                $this->objNode->GetColumnAlias($objBuilder, true, null, $this->objSelect); 
    14671500                        $objBuilder->AddExpandAsArrayNode($this->objNode); 
    14681501                } 
     
    15141547                public function __toString() { 
    15151548                        return 'QQGroupBy Clause'; 
     1549                } 
     1550        } 
     1551 
     1552        class QQSelect extends QQClause { 
     1553                protected $arrNodeObj = array(); 
     1554 
     1555                public function __construct($arrNodeObj) { 
     1556                        $this->arrNodeObj = $arrNodeObj; 
     1557                } 
     1558 
     1559                public function UpdateQueryBuilder(QQueryBuilder $objBuilder) { 
     1560                } 
     1561 
     1562                public function AddSelectItems(QQueryBuilder $objBuilder, $strTableName, $strAliasPrefix) { 
     1563                        foreach ($this->arrNodeObj as $objNode) { 
     1564                                $objBuilder->AddSelectItem($strTableName, $objNode->_Name, $strAliasPrefix . $objNode->_Name); 
     1565                        } 
     1566                } 
     1567 
     1568                public function Merge(QQSelect $objSelect = null) { 
     1569                        if ($objSelect) foreach ($objSelect->arrNodeObj as $objNode) 
     1570                                array_push($this->arrNodeObj, $objNode); 
     1571                } 
     1572 
     1573                public function __toString() { 
     1574                        return 'QQSelectColumn Clause'; 
    15161575                } 
    15171576        } 
  • framework/branches/2.0/includes/qcubed/_core/tests/qcubed-unit/BasicOrmTests.php

    r1007 r1317  
    129129                $this->assertEqual($targetPerson, null, "QuerySingle should return null for a not-found record");                
    130130        } 
     131 
     132        public function testQuerySelectSubset() { 
     133                $objPersonArray = Person::LoadAll(QQ::Select(QQN::Person()->FirstName)); 
     134                foreach ($objPersonArray as $objPerson) { 
     135                        $this->assertNull($objPerson->LastName, "LastName should be null, since it was not selected"); 
     136                        $this->assertNotNull($objPerson->Id, "Id should not be null since it's always added to the select list"); 
     137                } 
     138        } 
    131139} 
    132140?> 
  • framework/branches/2.0/includes/qcubed/_core/tests/qcubed-unit/ExpandAsArrayTests.php

    r989 r1317  
    6060                $this->verifyObjectPropertyHelper($targetProject->_MilestoneArray, 'Name', 'Milestone H'); 
    6161        } 
     62 
     63        public function testSelectSubsetInExpand() { 
     64                $objPersonArray = Person::QueryArray( 
     65                        QQ::OrCondition( 
     66                                QQ::Like(QQN::Person()->ProjectAsManager->Name, '%ACME%'), 
     67                                QQ::Like(QQN::Person()->ProjectAsManager->Name, '%HR%') 
     68                        ), 
     69                        // Let's expand on the Project, itself 
     70                        QQ::Clause( 
     71                                QQ::Select(QQN::Person()->LastName), 
     72                                QQ::Expand(QQN::Person()->ProjectAsManager, null, QQ::Select(QQN::Person()->ProjectAsManager->Spent)), 
     73                                QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName) 
     74                        ) 
     75                ); 
     76 
     77                foreach ($objPersonArray as $objPerson) { 
     78                        $this->assertNull($objPerson->FirstName, "FirstName should be null, since it was not selected"); 
     79                        $this->assertNotNull($objPerson->Id, "Id should not be null since it's always added to the select list"); 
     80                        $this->assertNotNull($objPerson->_ProjectAsManager->Id, "ProjectAsManager->Id should not be null since id's are always added to the select list"); 
     81                        $this->assertNull($objPerson->_ProjectAsManager->Name, "ProjectAsManager->Name should be null since it was not selected"); 
     82                } 
     83        } 
     84 
     85        public function testSelectSubsetInExpandAsArray() { 
     86                $objPersonArray = Person::LoadAll( 
     87                        QQ::Clause( 
     88                                QQ::Select(QQN::Person()->FirstName), 
     89                                QQ::ExpandAsArray(QQN::Person()->Address, QQ::Select(QQN::Person()->Address->Street, QQN::Person()->Address->City)), 
     90                                QQ::ExpandAsArray(QQN::Person()->ProjectAsManager, QQ::Select(QQN::Person()->ProjectAsManager->StartDate)), 
     91                                QQ::ExpandAsArray(QQN::Person()->ProjectAsManager->Milestone, QQ::Select(QQN::Person()->ProjectAsManager->Milestone->Name)) 
     92                        ) 
     93                ); 
     94 
     95                foreach ($objPersonArray as $objPerson) { 
     96                        $this->assertNull($objPerson->LastName, "LastName should be null, since it was not selected"); 
     97                        $this->assertNotNull($objPerson->Id, "Id should not be null since it's always added to the select list"); 
     98                        if (sizeof($objPerson->_AddressArray) > 0) { 
     99                                foreach ($objPerson->_AddressArray as $objAddress) { 
     100                                        $this->assertNotNull($objAddress->Id, "Address->Id should not be null since it's always added to the select list"); 
     101                                        $this->assertNull($objAddress->PersonId, "Address->PersonId should be null, since it was not selected"); 
     102                                } 
     103                        } 
     104                        if (sizeof($objPerson->_ProjectAsManagerArray) > 0) { 
     105                                foreach($objPerson->_ProjectAsManagerArray as $objProject) { 
     106                                        $this->assertNotNull($objProject->Id, "Project->Id should not be null since it's always added to the select list"); 
     107                                        $this->assertNull($objProject->Name, "Project->Name should be null, since it was not selected"); 
     108                                        if (sizeof($objProject->_MilestoneArray) > 0) { 
     109                                                foreach ($objProject->_MilestoneArray as $objMilestone) { 
     110                                                        $this->assertNotNull($objMilestone->Id, "Milestone->Id should not be null since it's always added to the select list"); 
     111                                                        $this->assertNull($objMilestone->ProjectId, "Milestone->ProjectId should be null, since it was not selected"); 
     112                                                } 
     113                                        } 
     114                                } 
     115                        } 
     116                } 
     117        } 
    62118} 
    63119?>