示例#1
0
 /**
  * Builds and returns a string representation of $this Condition
  * @return QueryStatement
  */
 function getQueryStatement(DABLPDO $conn = null)
 {
     if (0 === count($this->conds)) {
         return null;
     }
     $stmnt = new QueryStatement($conn);
     $is_first = true;
     $is_second = false;
     foreach ($this->conds as &$cond) {
         $cond_stmnt = null;
         // avoid call_user_func_array for better stack traces
         switch (count($cond[1])) {
             case 1:
                 $cond_stmnt = self::processCondition($cond[1][0]);
                 break;
             case 2:
                 $cond_stmnt = self::processCondition($cond[1][0], $cond[1][1]);
                 break;
             case 3:
                 $cond_stmnt = self::processCondition($cond[1][0], $cond[1][1], $cond[1][2]);
                 break;
             case 4:
                 $cond_stmnt = self::processCondition($cond[1][0], $cond[1][1], $cond[1][2], $cond[1][3]);
                 break;
         }
         if (null === $cond_stmnt) {
             continue;
         }
         if ($is_first) {
             $sep = '';
             $is_first = false;
             $is_second = true;
         } else {
             $sep = ($is_second && 'OR' === $this->conds[0][0] ? 'OR' : $cond[0]) . ' ';
             $is_second = false;
         }
         $stmnt->string .= "\n\t{$sep}" . $cond_stmnt->string;
         $stmnt->addParams($cond_stmnt->params);
         $stmnt->addIdentifiers($cond_stmnt->identifiers);
     }
     return $stmnt;
 }
示例#2
0
文件: Query.php 项目: abcarroll/DABL
 /**
  * Protected for now.  Likely to be public in the future.
  * @return QueryStatement
  */
 protected function getTablesClause($conn)
 {
     $table = $this->getTable();
     if (!$table) {
         throw new Exception('No table specified.');
     }
     $statement = new QueryStatement($conn);
     $alias = $this->getAlias();
     // if $table is a Query, get its QueryStatement
     if ($table instanceof Query) {
         $table_statement = $table->getQuery($conn);
         $table_string = '(' . $table_statement->string . ')';
     } else {
         $table_statement = null;
     }
     switch ($this->_action) {
         case self::ACTION_UPDATE:
         case self::ACTION_COUNT:
         case self::ACTION_SELECT:
             // setup identifiers for $table_string
             if (null !== $table_statement) {
                 $statement->addIdentifiers($table_statement->identifiers);
                 $statement->addParams($table_statement->params);
             } else {
                 // if $table has no spaces, assume it is an identifier
                 if (strpos($table, ' ') === false) {
                     $statement->addIdentifier($table);
                     $table_string = QueryStatement::IDENTIFIER;
                 } else {
                     $table_string = $table;
                 }
             }
             // append $alias, if it's not empty
             if ($alias) {
                 $table_string .= " AS {$alias}";
             }
             // setup identifiers for any additional tables
             if ($this->_extraTables) {
                 $table_string = '(' . $table_string;
                 foreach ($this->_extraTables as $t_alias => $extra_table) {
                     if ($extra_table instanceof Query) {
                         $extra_table_statement = $extra_table->getQuery($conn);
                         $extra_table_string = '(' . $extra_table_statement->string . ') AS ' . $t_alias;
                         $statement->addParams($extra_table_statement->params);
                         $statement->addIdentifiers($extra_table_statement->identifiers);
                     } else {
                         $extra_table_string = $extra_table;
                         if (strpos($extra_table_string, ' ') === false) {
                             $extra_table_string = QueryStatement::IDENTIFIER;
                             $statement->addIdentifier($extra_table);
                         }
                         if ($t_alias != $extra_table) {
                             $extra_table_string .= " AS {$t_alias}";
                         }
                     }
                     $table_string .= ", {$extra_table_string}";
                 }
                 $table_string .= ')';
             }
             $statement->string = $table_string;
             break;
         case self::ACTION_DELETE:
             if (null !== $table_statement) {
                 $statement->addIdentifiers($table_statement->identifiers);
                 $statement->addParams($table_statement->params);
             } else {
                 // if $table has no spaces, assume it is an identifier
                 if (strpos($table, ' ') === false) {
                     $statement->addIdentifier($table);
                     $table_string = QueryStatement::IDENTIFIER;
                 } else {
                     $table_string = $table;
                 }
             }
             // append $alias, if it's not empty
             if ($alias) {
                 $table_string .= " AS {$alias}";
             }
             $statement->string = $table_string;
             break;
         default:
             throw new RuntimeException('Uknown action "' . $this->_action . '", cannot build table list');
             break;
     }
     return $statement;
 }
示例#3
0
 /**
  * @param DABLPDO $conn
  * @return QueryStatement
  */
 function getQueryStatement(DABLPDO $conn = null)
 {
     $statement = new QueryStatement($conn);
     $table = $this->_table;
     $on_clause = $this->_onClause;
     $join_type = $this->_joinType;
     $alias = $this->_alias;
     if ($table instanceof Query) {
         $table_statement = $table->getQuery($conn);
         $table = '(' . $table_statement->getString() . ')';
         $statement->addParams($table_statement->getParams());
         $statement->addIdentifiers($table_statement->getIdentifiers());
     } else {
         $statement->addIdentifier($table);
         $table = QueryStatement::IDENTIFIER;
     }
     if ($alias) {
         $table .= " AS {$alias}";
     }
     if ($this->_isLikePropel) {
         $statement->addIdentifiers(array($this->_leftColumn, $this->_rightColumn));
         $on_clause = QueryStatement::IDENTIFIER . ' = ' . QueryStatement::IDENTIFIER;
     } elseif (null === $on_clause) {
         $on_clause = '1 = 1';
     } elseif ($on_clause instanceof Condition) {
         $on_clause_statement = $on_clause->getQueryStatement();
         $on_clause = $on_clause_statement->getString();
         $statement->addParams($on_clause_statement->getParams());
         $statement->addIdentifiers($on_clause_statement->getIdentifiers());
     }
     if ('' !== $on_clause) {
         $on_clause = "ON ({$on_clause})";
     }
     $statement->setString("{$join_type} {$table} {$on_clause}");
     return $statement;
 }