Skip to content

Instantly share code, notes, and snippets.

@rogersguedes
Created May 30, 2016 15:14
Show Gist options
  • Select an option

  • Save rogersguedes/d0d492bf32dfebda77f1a3b4d441a2bd to your computer and use it in GitHub Desktop.

Select an option

Save rogersguedes/d0d492bf32dfebda77f1a3b4d441a2bd to your computer and use it in GitHub Desktop.
<?php
function buildSelect($tableStruct){
$queryString = "SELECT ";
$queryBinds = array();
$sqlQueryFields = array(
"states.id",
"states.code",
"states.name"
);
$sqlQueryTable = array(
"tables" => array(
"states" => array(
"tableName" => "State"
/*,
"operation" => "INNER JOIN",
"operand" => "countries"
*/
)
),
"conditions" => array(
)
);
$sqlQueryConditions = array();
if( $address && $address->getCountry() != null ){
$sqlQueryTable["tables"]["states"]["operation"] = "INNER JOIN";
$sqlQueryTable["tables"]["states"]["operand"] = "countries";
$sqlQueryTable["tables"]["countries"] = array(
"tableName" =>"Country",
"operation" => null,
"operand" => null
);
$sqlQueryTable["conditions"][] = "states.fkCountry = countries.id";
$sqlQueryConditions[] = "states.fkCountry = ?";
$queryBinds[] = $address->getCountry();
}
//Building the QueryString
foreach($sqlQueryFields as $field){
$queryString .= $field.",";
}
$queryString[strlen($queryString)-1]=" ";
$queryString .= "FROM ";
if( count( $sqlQueryTable["tables"] ) == 1 ){
foreach($sqlQueryTable["tables"] as $key => $value){
$queryString .= $value["tableName"]." as ".$key;
}
}
else{
foreach($sqlQueryTable["tables"] as $key => $value){
$queryString .= $value["tableName"]." as ".$key;
if( isset($value["operation"]) && isset($value["operand"])){
$queryString .= " ".$value["operation"]." ";
}
}
if( count( $sqlQueryTable["conditions"] ) > 0 ){
$queryString .= " ON ";
foreach($sqlQueryTable["conditions"] as $value){
$queryString .= $value;
}
}
}
if( count( $sqlQueryConditions ) > 0 ){
$queryString .= " WHERE ";
foreach( $sqlQueryConditions as $value){
$queryString .= $value;
}
}
$queryString .= ";";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment