コード例 #1
0
	public function __construct(Connection $connection)
	{
		$this->connection = $connection;
		$this->driver = $connection->getSupplementalDriver();
		$this->arrayModes = array(
			'INSERT' => $this->driver->isSupported(ISupplementalDriver::SUPPORT_MULTI_INSERT_AS_SELECT) ? 'select' : 'values',
			'REPLACE' => 'values',
			'UPDATE' => 'assoc',
			'WHERE' => 'and',
			'HAVING' => 'and',
			'ORDER BY' => 'order',
			'GROUP BY' => 'order',
		);
	}
コード例 #2
0
ファイル: SqlBuilder.php プロジェクト: ricco24/database
 protected function addWhereComposition(array $columns, array $parameters)
 {
     if ($this->driver->isSupported(ISupplementalDriver::SUPPORT_MULTI_COLUMN_AS_OR_COND)) {
         $conditionFragment = '(' . implode(' = ? AND ', $columns) . ' = ?) OR ';
         $condition = substr(str_repeat($conditionFragment, count($parameters)), 0, -4);
         return $this->addWhere($condition, Nette\Utils\Arrays::flatten($parameters));
     } else {
         return $this->addWhere('(' . implode(', ', $columns) . ') IN', $parameters);
     }
 }
コード例 #3
0
ファイル: SqlBuilder.php プロジェクト: ppwalks33/cleansure
 /**
  * Returns SQL query.
  * @param  list of columns
  * @return string
  */
 public function buildSelectQuery($columns = NULL)
 {
     $join = $this->buildJoins(implode(',', $this->conditions), TRUE);
     $join += $this->buildJoins(implode(',', $this->select) . ",{$this->group},{$this->having}," . implode(',', $this->order));
     $prefix = $join ? "{$this->delimitedTable}." : '';
     if ($this->select) {
         $cols = $this->tryDelimite($this->removeExtraTables(implode(', ', $this->select)));
     } elseif ($columns) {
         $cols = array_map(array($this->driver, 'delimite'), $columns);
         $cols = $prefix . implode(', ' . $prefix, $cols);
     } elseif ($this->group && !$this->driver->isSupported(ISupplementalDriver::SUPPORT_SELECT_UNGROUPED_COLUMNS)) {
         $cols = $this->tryDelimite($this->removeExtraTables($this->group));
     } else {
         $cols = $prefix . '*';
     }
     return "SELECT{$this->buildTopClause()} {$cols} FROM {$this->delimitedTable}" . implode($join) . $this->buildConditions();
 }
コード例 #4
0
 private function formatValue($value, $mode = NULL)
 {
     if (!$mode || $mode === 'auto') {
         if (is_string($value)) {
             if (strlen($value) > 20) {
                 $this->remaining[] = $value;
                 return '?';
             } else {
                 return $this->connection->quote($value);
             }
         } elseif (is_int($value)) {
             return (string) $value;
         } elseif (is_float($value)) {
             return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
         } elseif (is_bool($value)) {
             return $this->driver->formatBool($value);
         } elseif ($value === NULL) {
             return 'NULL';
         } elseif ($value instanceof Table\IRow) {
             return $value->getPrimary();
         } elseif ($value instanceof SqlLiteral) {
             $prep = clone $this;
             list($res, $params) = $prep->process(array_merge([$value->__toString()], $value->getParameters()));
             $this->remaining = array_merge($this->remaining, $params);
             return $res;
         } elseif ($value instanceof \DateTimeInterface) {
             return $this->driver->formatDateTime($value);
         } elseif ($value instanceof \DateInterval) {
             return $this->driver->formatDateInterval($value);
         } elseif (is_object($value) && method_exists($value, '__toString')) {
             return $this->formatValue((string) $value);
         } elseif (is_resource($value)) {
             $this->remaining[] = $value;
             return '?';
         }
     } elseif ($mode === 'name') {
         if (!is_string($value)) {
             $type = gettype($value);
             throw new Nette\InvalidArgumentException("Placeholder ?{$mode} expects string, {$type} given.");
         }
         return $this->delimite($value);
     }
     if ($value instanceof \Traversable && !$value instanceof Table\IRow) {
         $value = iterator_to_array($value);
     }
     if (is_array($value)) {
         $vx = $kx = [];
         if ($mode === 'auto') {
             $mode = $this->arrayMode;
         }
         if ($mode === 'values') {
             // (key, key, ...) VALUES (value, value, ...)
             if (array_key_exists(0, $value)) {
                 // multi-insert
                 foreach ($value[0] as $k => $v) {
                     $kx[] = $this->delimite($k);
                 }
                 foreach ($value as $val) {
                     $vx2 = [];
                     foreach ($val as $v) {
                         $vx2[] = $this->formatValue($v);
                     }
                     $vx[] = implode(', ', $vx2);
                 }
                 $select = $this->driver->isSupported(ISupplementalDriver::SUPPORT_MULTI_INSERT_AS_SELECT);
                 return '(' . implode(', ', $kx) . ($select ? ') SELECT ' : ') VALUES (') . implode($select ? ' UNION ALL SELECT ' : '), (', $vx) . ($select ? '' : ')');
             }
             foreach ($value as $k => $v) {
                 $kx[] = $this->delimite($k);
                 $vx[] = $this->formatValue($v);
             }
             return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
         } elseif (!$mode || $mode === 'set') {
             foreach ($value as $k => $v) {
                 if (is_int($k)) {
                     // value, value, ... OR (1, 2), (3, 4)
                     $vx[] = is_array($v) ? '(' . $this->formatValue($v) . ')' : $this->formatValue($v);
                 } elseif (substr($k, -1) === '=') {
                     // key+=value, key-=value, ...
                     $k2 = $this->delimite(substr($k, 0, -2));
                     $vx[] = $k2 . '=' . $k2 . ' ' . substr($k, -2, 1) . ' ' . $this->formatValue($v);
                 } else {
                     // key=value, key=value, ...
                     $vx[] = $this->delimite($k) . '=' . $this->formatValue($v);
                 }
             }
             return implode(', ', $vx);
         } elseif ($mode === 'and' || $mode === 'or') {
             // (key [operator] value) AND ...
             foreach ($value as $k => $v) {
                 if (is_int($k)) {
                     $vx[] = $this->formatValue($v);
                     continue;
                 }
                 list($k, $operator) = explode(' ', $k . ' ');
                 $k = $this->delimite($k);
                 if (is_array($v)) {
                     if ($v) {
                         $vx[] = $k . ' ' . ($operator ? $operator . ' ' : '') . 'IN (' . $this->formatValue(array_values($v)) . ')';
                     } elseif ($operator === 'NOT') {
                     } else {
                         $vx[] = '1=0';
                     }
                 } else {
                     $v = $this->formatValue($v);
                     $vx[] = $k . ' ' . ($operator ?: ($v === 'NULL' ? 'IS' : '=')) . ' ' . $v;
                 }
             }
             return $value ? '(' . implode(') ' . strtoupper($mode) . ' (', $vx) . ')' : '1=1';
         } elseif ($mode === 'order') {
             // key, key DESC, ...
             foreach ($value as $k => $v) {
                 $vx[] = $this->delimite($k) . ($v > 0 ? '' : ' DESC');
             }
             return implode(', ', $vx);
         } else {
             throw new Nette\InvalidArgumentException("Unknown placeholder ?{$mode}.");
         }
     } elseif (in_array($mode, ['and', 'or', 'set', 'values', 'order'], TRUE)) {
         $type = gettype($value);
         throw new Nette\InvalidArgumentException("Placeholder ?{$mode} expects array or Traversable object, {$type} given.");
     } elseif ($mode && $mode !== 'auto') {
         throw new Nette\InvalidArgumentException("Unknown placeholder ?{$mode}.");
     } else {
         throw new Nette\InvalidArgumentException('Unexpected type of parameter: ' . (is_object($value) ? get_class($value) : gettype($value)));
     }
 }