Changeset 1317
- Timestamp:
- 11/27/11 12:00:43 (19 months ago)
- Location:
- framework/branches/2.0
- Files:
-
- 1 added
- 5 modified
-
assets/_core/php/examples/includes/examples.inc.php (modified) (1 diff)
-
assets/_core/php/examples/qcubed_query/qqselect.php (added)
-
includes/qcubed/_core/codegen/templates/db_orm/class_gen/qcubed_query_methods.tpl (modified) (3 diffs)
-
includes/qcubed/_core/framework/QQuery.class.php (modified) (25 diffs)
-
includes/qcubed/_core/tests/qcubed-unit/BasicOrmTests.php (modified) (1 diff)
-
includes/qcubed/_core/tests/qcubed-unit/ExpandAsArrayTests.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
framework/branches/2.0/assets/_core/php/examples/includes/examples.inc.php
r1296 r1317 51 51 self::AddCoreExampleFile($intIndex, '/qcubed_query/expandasarray.php * ExpandAsArray: Multiple related tables in one swift query'); 52 52 self::AddCoreExampleFile($intIndex, '/qcubed_query/alias.php * SQL Aliases for QQuery'); 53 self::AddCoreExampleFile($intIndex, '/qcubed_query/qqselect.php * Picking database columns for QQuery'); 53 54 self::AddCoreExampleFile($intIndex, '/qcubed_query/subsql.php * Custom SQL Subqueries for QQuery'); 54 55 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 37 37 } 38 38 if ($blnAddAllFieldsToSelect) { 39 <%= $objTable->ClassName %>::GetSelectFields($objQueryBuilder );39 <%= $objTable->ClassName %>::GetSelectFields($objQueryBuilder, null, QQuery::extractSelectClause($objOptionalClauses)); 40 40 } 41 41 $objQueryBuilder->AddFromItem('<%= $objTable->Name %>'); … … 215 215 * @param string $strPrefix optional prefix to add to the SELECT fields 216 216 */ 217 public static function GetSelectFields(QQueryBuilder $objBuilder, $strPrefix = null ) {217 public static function GetSelectFields(QQueryBuilder $objBuilder, $strPrefix = null, QQSelect $objSelect = null) { 218 218 if ($strPrefix) { 219 219 $strTableName = $strPrefix; … … 224 224 } 225 225 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 { 226 232 <% foreach ($objTable->ColumnArray as $objColumn) { %> 227 $objBuilder->AddSelectItem($strTableName, '<%= $objColumn->Name %>', $strAliasPrefix . '<%= $objColumn->Name %>');233 $objBuilder->AddSelectItem($strTableName, '<%= $objColumn->Name %>', $strAliasPrefix . '<%= $objColumn->Name %>'); 228 234 <% } %> 229 } 235 } 236 } -
framework/branches/2.0/includes/qcubed/_core/framework/QQuery.class.php
r1310 r1317 82 82 } 83 83 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); 86 86 } 87 87 … … 166 166 } 167 167 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) { 169 169 // Make sure our Root Tables Match 170 170 if ($this->_RootTableName != $objBuilder->RootTableName) … … 177 177 // Use the Helper to Iterate Through the Parent Chain and get the Parent Alias 178 178 try { 179 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection );179 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection, QQ::Select()); 180 180 181 181 if ($this->strTableName) { … … 185 185 186 186 if ($blnExpandSelection) 187 call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias );187 call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias, $objSelect); 188 188 } 189 189 } catch (QCallerException $objExc) { … … 196 196 } 197 197 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); 200 200 return $objBuilder->GetTableAlias($strTable); 201 201 } … … 210 210 } 211 211 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); 214 214 // Pull the Begin and End Escape Identifiers from the Database Adapter 215 215 return $this->MakeColumnAlias($objBuilder, $strTableAlias); … … 221 221 } 222 222 223 public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection ) {223 public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection, QQSelect $objSelect = null) { 224 224 // Are we at the Parent Node? 225 225 if (is_null($this->objParentNode)) … … 229 229 try { 230 230 // No -- First get the Parent Alias 231 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection );231 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection, QQ::Select()); 232 232 233 233 $strJoinTableAlias = $strParentAlias . '__' . $this->strAlias; … … 241 241 // Next, Expand the Selection Fields for this Table (if applicable) 242 242 if ($blnExpandSelection) { 243 call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias );243 call_user_func(array($this->strClassName, 'GetSelectFields'), $objBuilder, $strJoinTableAlias, $objSelect); 244 244 } 245 245 … … 372 372 } 373 373 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); 376 376 return null; 377 377 } … … 408 408 } 409 409 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) { 411 411 // Make sure our Root Tables Match 412 412 if ($this->_RootTableName != $objBuilder->RootTableName) … … 424 424 else { 425 425 // 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()); 427 427 428 428 if ($this->strTableName) { … … 432 432 433 433 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); 435 435 } 436 436 … … 442 442 } 443 443 444 public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection ) {444 public function GetColumnAliasHelper(QQueryBuilder $objBuilder, $blnExpandSelection, QQSelect $objSelect = null) { 445 445 // Are we at the Parent Node? 446 446 if (is_null($this->objParentNode)) … … 449 449 else { 450 450 // No -- First get the Parent Alias 451 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection );451 $strParentAlias = $this->objParentNode->GetColumnAliasHelper($objBuilder, $blnExpandSelection, QQ::Select()); 452 452 453 453 // Next, Join the Appropriate Table … … 1070 1070 } 1071 1071 1072 static public function Expand($objNode, QQCondition $objJoinCondition = null ) {1072 static public function Expand($objNode, QQCondition $objJoinCondition = null, QQSelect $objSelect = null) { 1073 1073 // if (gettype($objNode) == 'string') 1074 1074 // return new QQExpandVirtualNode(new QQVirtualNode($objNode)); … … 1077 1077 return new QQExpandVirtualNode($objNode); 1078 1078 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()); 1084 1088 } 1085 1089 … … 1090 1094 static public function Distinct() { 1091 1095 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; 1092 1121 } 1093 1122 … … 1167 1196 $this->strSql = $strSql; 1168 1197 } 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) { 1170 1199 $strSql = $this->strSql; 1171 1200 for ($intIndex = 1; $intIndex < count($this->objParentQueryNodes); $intIndex++) { … … 1185 1214 $this->objSubQueryDefinition = $objSubQueryDefinition; 1186 1215 } 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) { 1188 1217 if ($this->objSubQueryDefinition) { 1189 1218 $objBuilder->SetVirtualNode($this->strName, $this->objSubQueryDefinition); … … 1377 1406 protected $objNode; 1378 1407 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) { 1381 1411 // Check against root and table QQNodes 1382 1412 if ($objNode instanceof QQAssociationNode) … … 1389 1419 $this->objNode = $objNode; 1390 1420 $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); 1394 1425 } 1395 1426 public function __toString() { … … 1453 1484 /** @var QQNode|QQAssociationNode */ 1454 1485 protected $objNode; 1455 public function __construct($objNode) { 1486 protected $objSelect; 1487 public function __construct($objNode, QQSelect $objSelect = null) { 1456 1488 // Ensure that this is an QQAssociationNode 1457 1489 if ((!($objNode instanceof QQAssociationNode)) && (!($objNode instanceof QQReverseReferenceNode))) … … 1459 1491 1460 1492 $this->objNode = $objNode; 1493 $this->objSelect = $objSelect; 1461 1494 } 1462 1495 public function UpdateQueryBuilder(QQueryBuilder $objBuilder) { 1463 1496 if ($this->objNode instanceof QQAssociationNode) 1464 $this->objNode->_ChildTableNode->GetColumnAlias($objBuilder, true );1497 $this->objNode->_ChildTableNode->GetColumnAlias($objBuilder, true, null, $this->objSelect); 1465 1498 else 1466 $this->objNode->GetColumnAlias($objBuilder, true );1499 $this->objNode->GetColumnAlias($objBuilder, true, null, $this->objSelect); 1467 1500 $objBuilder->AddExpandAsArrayNode($this->objNode); 1468 1501 } … … 1514 1547 public function __toString() { 1515 1548 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'; 1516 1575 } 1517 1576 } -
framework/branches/2.0/includes/qcubed/_core/tests/qcubed-unit/BasicOrmTests.php
r1007 r1317 129 129 $this->assertEqual($targetPerson, null, "QuerySingle should return null for a not-found record"); 130 130 } 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 } 131 139 } 132 140 ?> -
framework/branches/2.0/includes/qcubed/_core/tests/qcubed-unit/ExpandAsArrayTests.php
r989 r1317 60 60 $this->verifyObjectPropertyHelper($targetProject->_MilestoneArray, 'Name', 'Milestone H'); 61 61 } 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 } 62 118 } 63 119 ?>
