예제 #1
0
 /**
  * Generates the LIMIT part of a SQL query
  *
  * @param int $limit the limit clause
  * @param Query $query The query that is being compiled
  * @return string
  */
 protected function _buildLimitPart(int $limit, Query $query) : string
 {
     if ($limit === null || $query->clause('offset') === null) {
         return '';
     }
     return sprintf(' FETCH FIRST %d ROWS ONLY', $limit);
 }
예제 #2
0
 /**
  * Helper function used to build the string representation of a SELECT clause,
  * it constructs the field list taking care of aliasing and
  * converting expression objects to string. This function also constructs the
  * DISTINCT clause for the query.
  *
  * @param array $parts list of fields to be transformed to string
  * @param Query $query The query that is being compiled
  * @param ValueBinder $generator the placeholder generator to be used in expressions
  * @return string
  */
 protected function _buildSelectPart(array $parts, Query $query, ValueBinder $generator) : string
 {
     $select = 'SELECT %s%s%s';
     if ($this->_orderedUnion && $query->clause('union')) {
         $select = '(SELECT %s%s%s';
     }
     $distinct = $query->clause('distinct');
     $modifiers = $query->clause('modifier') ?: null;
     $normalized = [];
     $parts = $this->_stringifyExpressions($parts, $generator);
     foreach ($parts as $k => $p) {
         if (!is_numeric($k)) {
             $p = $p . ' AS ' . "`{$k}`";
         }
         $normalized[] = $p;
     }
     if ($distinct === true) {
         $distinct = 'DISTINCT ';
     }
     if (is_array($distinct)) {
         $distinct = $this->_stringifyExpressions($distinct, $generator);
         $distinct = sprintf('DISTINCT ON (%s) ', implode(', ', $distinct));
     }
     if ($modifiers !== null) {
         $modifiers = $this->_stringifyExpressions($modifiers, $generator);
         $modifiers = implode(' ', $modifiers) . ' ';
     }
     return sprintf($select, $distinct, $modifiers, implode(', ', $normalized));
 }
예제 #3
0
 /**
  * Prepares a sql statement to be executed
  *
  * @param \CoreTyson\Database\Query $query The query to convert into a statement.
  * @return \CoreTyson\Database\Statement\AbstractStatement
  */
 public function prepare($query) : AbstractStatement
 {
     if (!$this->isConnected()) {
         $this->connect();
     }
     $isObject = $query instanceof Query;
     $statement = $this->_PdoConnection->prepare($isObject ? $query->sql() : $query);
     return new PdoStatement($statement, $this);
 }