Exemplo n.º 1
0
 /**
  * Protected for now.  Likely to be public in the future.
  * @return QueryStatement
  */
 protected function getGroupByClause($conn = null)
 {
     $statement = new QueryStatement($conn);
     if ($this->_groups) {
         $groups = $this->_groups;
         foreach ($groups as &$group) {
             $statement->addIdentifier($group);
             $group = QueryStatement::IDENTIFIER;
         }
         $statement->string = "\nGROUP BY " . implode(', ', $groups);
     }
     return $statement;
 }
Exemplo n.º 2
0
 /**
  * Creates and executes INSERT query string for this object
  * @return int
  */
 protected function insert()
 {
     $conn = static::getConnection();
     $pk = static::$_primaryKey;
     $fields = array();
     $values = array();
     $placeholders = array();
     foreach (static::$_columnNames as &$column) {
         $value = $this->{$column};
         if ($value === null && !$this->isColumnModified($column)) {
             continue;
         }
         $fields[] = $conn->quoteIdentifier($column, true);
         $values[] = $value;
         $placeholders[] = '?';
     }
     $quoted_table = $conn->quoteIdentifier(static::getTableName(), true);
     $query_s = 'INSERT INTO ' . $quoted_table . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $placeholders) . ') ';
     if ($pk && $this->isAutoIncrement() && $conn instanceof DBPostgres) {
         $query_s .= ' RETURNING ' . $conn->quoteIdentifier($pk, true);
     }
     $statement = new QueryStatement($conn);
     $statement->setString($query_s);
     $statement->setParams($values);
     $result = $statement->bindAndExecute();
     $count = $result->rowCount();
     if ($pk && $this->isAutoIncrement()) {
         $id = null;
         if ($conn instanceof DBPostgres) {
             $id = $result->fetchColumn(0);
         } elseif ($conn->isGetIdAfterInsert()) {
             $id = $conn->lastInsertId();
             if (empty($id) && $conn instanceof DBRedshift) {
                 $id = $conn->query('SELECT MAX(' . $conn->quoteIdentifier($pk) . ") FROM {$quoted_table}")->fetchColumn();
             }
         }
         if (null !== $id) {
             $this->{"set{$pk}"}($id);
         }
     }
     $this->resetModified();
     $this->setNew(false);
     $this->insertIntoPool($this);
     return $count;
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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;
 }