Produce a sql statement which resolves to a string but is a valid json
@return string Sql which evaluates to JSON
337355
public function donovan( QuoteInterface $quoting, $prefix, $columns )
    {
        $sql = array();
        foreach( $columns as $column => $type ) {
            $sql[] = sprintf(
                'donovan(%s%s)',
                $prefix,
                $quoting->quoteIdent( $column )
            );
        }
        return sprintf(
            "'['||%s||']'",
            implode("||','||", $sql)
        );
    }
1080503
public function getSelectSql()
    {
        $sql = "SELECT ";

        $sql .= $this->implodeSqlClauses($this->columns, ", ").
            " FROM `".$this->schemaName."` AS `".$this->getAlias()."`";

        foreach($this->joins as $join){
            $joinsSql = $join->getSql($this);
            if (strpos($joinsSql," ") !== false){
                $joinsSql = "(".$joinsSql.")";
            }

            $sql .= " ".$join->joinType." ".$joinsSql." AS `".$join->statement->getAlias()."` ON `".$this->getAlias()."`.`".
                $join->parentColumn."` = `".$join->statement->getAlias()."`.`".$join->childColumn."`";
        }

        if ($this->whereExpression) {
            $havingSql = $this->whereExpression->getWhereSql($this);
            if ($havingSql != "" ){
                $sql .= " WHERE ".$havingSql;
            }
        }
        
        if (count($this->groups)){
            $sql .= " GROUP BY ".$this->implodeSqlClauses($this->groups, ", ");
        }

        if ($this->whereExpression) {
            $havingSql = $this->whereExpression->getHavingSql($this);
            if ($havingSql != "" ){
                $sql .= " HAVING ".$havingSql;
            }
        }

        if (count($this->sorts)){
            $sql .= " ORDER BY ".$this->implodeSqlClauses($this->sorts, ", ");
        }

        if ($this->hasLimit()){
            $sql .= " LIMIT ".$this->limitStart.", ".$this->limitCount;
        }

        return $sql;
    }
944783
protected function jsonable()
    {
        return DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql'
               && version_compare(DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.7.8', 'ge')
            ? 'json' : 'text';
    }
97457
public function generateValidRef(): string
    {
        Factory::helper('string');

        $oDb  = Factory::service('Database');
        $oNow = Factory::factory('DateTime');

        do {

            $sRef = $oNow->format('Ym') . '-' . strtoupper(random_string('alnum'));
            $oDb->where('ref', $sRef);
            $bRefExists = (bool) $oDb->count_all_results($this->table);

        } while ($bRefExists);

        return $sRef;
    }
1267619
public function jsonVal()
    {
        $json = $this->propertyVal();
        if (!is_string($json)) {
            $json = json_encode($json);
        }
        if (!$json || $json == 'null') {
            $json = '';
        }
        return $json;
    }
1109595
public function asJavascript()
    {
        return implode("\n    ", array_map(function ($push) {
            return '_paq.push([' . implode(', ', array_map(function ($item) {
                    if ($item instanceof RawExpression) {
                        return $item->value;
                    }

                    // JSON encoding is used to escape data injected into javascript
                    return json_encode($item);
                }, $push)) . ']);';
        }, $this->pushs));
    }
62379
private function createJson() {
    $content = $this->getContent()->get();
    $json = json_encode(
      $content,
      JSON_PRESERVE_ZERO_FRACTION |
      JSON_BIGINT_AS_STRING |
      JSON_NUMERIC_CHECK |
      JSON_PRETTY_PRINT |
      JSON_UNESCAPED_SLASHES
    );

    if (!$this->isValidJson($json)) {
      throw new \Nuki\Exceptions\Base('Encoded json string is not valid, check input');
    }
    
    return $json;
  }
383526
public static function build()
    {
        $args = func_get_args();

        $sql = [];

        foreach ($args as $arg) {
            if ($arg === '' || $arg === null || $arg === false) {
                continue;
            }

            $sql[] = $arg;
        }

        return implode(' ', $sql);
    }
325802
public function toSQL()
    {
        if (! $this->has()) return '';

        $sql = [];
        $sql[] = 'ORDER BY';

        $sub = array();
        foreach ($this->conditions as $condition) {
            $sub[] = $condition;
        }
        $sql[] = join(', ', $sub);

        return join(' ', $sql);
    }
1113743
public function toString()
    {
        $driver = $this->getDriver();
        $prefix = $driver->getTablePrefix();
        $query  = 'DELETE';

        if($this->table && $this->join) {
            $query .= ' '.$driver->quoteIdentifier(!is_numeric(key($this->table)) ? key($this->table) : current($this->table));
        }

        if($this->table) {
            $query .= ' FROM '.$driver->quoteIdentifier($prefix.current($this->table).(!is_numeric(key($this->table)) ? ' AS '.key($this->table) : ''));
        }

        if($this->join)
        {
            $joins = array();
            foreach($this->join as $alias => $join)
            {
                $tmp = '';

                if($join['type']) {
                    $tmp .= ' '.$join['type'];
                }

                if($join['table'] instanceof DatabaseQuerySelect) {
                    $tmp .= ' JOIN ('.$join['table'].')'.(is_string($alias) ? ' AS '.$driver->quoteIdentifier($alias) : '');
                } else {
                    $tmp .= ' JOIN '.$driver->quoteIdentifier($prefix.$join['table'].(is_string($alias) ? ' AS '.$alias : ''));
                }

                if($join['condition']) {
                    $tmp .= ' ON ('.$driver->quoteIdentifier($join['condition']).')';
                }

                $joins[] = $tmp;
            }

            $query .= implode('', $joins);
        }

        if($this->where)
        {
            $query .= ' WHERE';

            foreach($this->where as $where)
            {
                if(!empty($where['combination'])) {
                    $query .= ' '.$where['combination'];
                }

                $query .= ' '.$driver->quoteIdentifier($where['condition']);
            }
        }

        if($this->order)
        {
            $query .= ' ORDER BY ';

            $list = array();
            foreach($this->order as $order) {
                $list[] = $driver->quoteIdentifier($order['column']).' '.$order['direction'];
            }

            $query .= implode(' , ', $list);
        }

        if($this->limit) {
            $query .= ' LIMIT '.$this->offset.' , '.$this->limit;
        }

        if($this->_parameters) {
            $query = $this->_replaceParams($query);
        }

        return $query;
    }
1342782
public function toSQL()
    {
        $sql = [];
        $this->params = [];

        $sql[] = 'SELECT';
        $sql[] = $this->columns->toSQL();
        $this->bind($this->columns->getParams());
        $sql[] = 'FROM ' . $this->table->getTableName();
        $sql[] = $this->where->toSQL();
        $this->bind($this->where->getParams());
        if ($this->group->has()) {
            $sql[] = $this->group->toSQL();
            $this->bind($this->order->getParams());
        }
        if ($this->having->has()) {
            $sql[] = $this->having->toSQL();
            $this->bind($this->having->getParams());
        }
        if ($this->order->has()) {
            $sql[] = $this->order->toSQL();
            $this->bind($this->order->getParams());
        }

        if ($this->limit !== null) {
            $sql[] = 'LIMIT ?';
            $this->params[] = $this->limit;
        }
        if ($this->offset !== null) {
            $sql[] = 'OFFSET ?';
            $this->params[] = $this->offset;
        }
        if ($this->lock_mode === Onikiri::LOCK_FOR_UPDATE) {
            $sql[] = 'FOR UPDATE';
        } elseif($this->lock_mode === Onikiri::LOCK_IN_SHARED) {
            $sql[] = 'LOCK IN SHARE MODE';
        }

        return join(' ', $sql);
    }
1152213
public function toString()
    {
        $adapter = $this->getAdapter();
        $prefix  = $adapter->getTablePrefix();
        $query   = 'SELECT';

        if($this->columns)
        {
            if($this->distinct) {
                $query .= ' DISTINCT';
            }

            $columns = array();
            foreach($this->columns as $alias => $column)
            {
                if($column instanceof KDatabaseQuerySelect) {
                    $columns[] = '('.$column.')'.(is_string($alias) ? ' AS '.$adapter->quoteIdentifier($alias) : '');
                } else {
                    $columns[] = $adapter->quoteIdentifier($column.(is_string($alias) ? ' AS '.$alias : ''));
                }
            }

            $query .= ' '.implode(', ', $columns);
        }
        else $query .= ' *';

        if($this->table)
        {
            if(current($this->table) instanceof KDatabaseQuerySelect) {
                $table= '('.current($this->table).')'.(!is_numeric(key($this->table)) ? ' AS '.$adapter->quoteIdentifier(key($this->table)) : '');
            } else {
                $table = $adapter->quoteIdentifier($prefix.current($this->table).(!is_numeric(key($this->table)) ? ' AS '.key($this->table) : ''));
            }

            $query .= ' FROM '.$table;
        }

        if($this->join)
        {
            $joins = array();
            foreach($this->join as $alias => $join)
            {
                $tmp = '';

                if($join['type']) {
                    $tmp .= ' '.$join['type'];
                }

                if($join['table'] instanceof KDatabaseQuerySelect) {
                    $tmp .= ' JOIN ('.$join['table'].')'.(is_string($alias) ? ' AS '.$adapter->quoteIdentifier($alias) : '');
                } else {
                    $tmp .= ' JOIN '.$adapter->quoteIdentifier($prefix.$join['table'].(is_string($alias) ? ' AS '.$alias : ''));
                }

                if($join['condition']) {
                    $tmp .= ' ON ('.$adapter->quoteIdentifier($join['condition']).')';
                }

                $joins[] = $tmp;
            }

            $query .= implode('', $joins);
        }

        if($this->where)
        {
            $query .= ' WHERE';

            foreach($this->where as $where)
            {
                if($where['combination']) {
                    $query .= ' '.$where['combination'];
                }

                $query .= ' '. $adapter->quoteIdentifier($where['condition']);
            }
        }

        if($this->group)
        {
            $columns = array();
            foreach($this->group as $column) {
                $columns[] = $adapter->quoteIdentifier($column);
            }

            $query .= ' GROUP BY '.implode(' , ', $columns);
        }

        if($this->having)
        {
            $columns = array();
            foreach($this->having as $column) {
                $columns[] = $adapter->quoteIdentifier($column);
            }

            $query .= ' HAVING '.implode(' , ', $columns);
        }

        if($this->order)
        {
            $query .= ' ORDER BY ';

            $list = array();
            foreach($this->order as $order) {
                $list[] = $adapter->quoteIdentifier($order['column']).' '.$order['direction'];
            }

            $query .= implode(' , ', $list);
        }

        if($this->limit) {
            $query .= ' LIMIT '.$this->offset.' , '.$this->limit;
        }

        if($this->_parameters) {
            $query = $this->_replaceParams($query);
        }

        return $query;
    }
1309646
private function getSQLForInsert()
    {
        return 'INSERT INTO ' . $this->sqlParts['from']['table'] .
        ' (' . implode(', ', array_keys($this->sqlParts['values'])) . ')' .
        ' VALUES(' . implode(', ', $this->sqlParts['values']) . ')';
    }
505651
protected function whereSql() {
        $this->params = [];
        $sql = $this->parseWhere($this->wheres, $this->params);
        if ($sql) {
            $sql = " WHERE " . $sql;
        }
        return $sql;
    }
676435
public function toSql()
    {
        if ($this->isEmpty()) {
            return '';
        }

        $params = $this->getParams();

        $sql = array('SET');
        $set = array();

        foreach ($params as $column => $value) {
            $set[] = implode(' = ', array(
                $column, $this->getBuilder()->getHelper()->toDbValue($value)
            ));
        }

        $sql[] = implode(', ', $set);

        return implode(' ', $sql);
    }
166368
public function getSql() {
        $sql = "CREATE TABLE IF NOT EXISTS {$this->getTable()} (";
        $column = $this->_data;
        if (!empty($this->primaryKey)) {
            $column[] = "PRIMARY KEY (`{$this->primaryKey}`)";
        }
        foreach ($this->checks as $key => $item) {
            $column[] = (!is_integer($key) ? "CONSTRAINT `{$key}` " : null)." CHECK ({$item})";
        }
        foreach ($this->index as $key => $item) {
            $column[] = (count($item) > 2 ? 'UNIQUE ': null). "INDEX `{$key}` (`{$item[0]}` {$item['1']})";
        }
        foreach ($this->foreignKey as $key => $item) {
            $column[] = "CONSTRAINT `{$key}` FOREIGN KEY (`{$item[0]}`) REFERENCES `{$item[1]}` (`{$item[2]}`) ON DELETE {$item[2]} ON UPDATE {$item[3]}";
        }
        $sql .= implode(',', $column).") ENGINE={$this->engine}";
        if ($this->aiBegin > 1) {
            $sql .= ' AUTO_INCREMENT='.$this->aiBegin;
        }
        return $sql." DEFAULT CHARSET={$this->charset} COMMENT='{$this->comment}';";
    }
1042262
public function toString()
    {
        if (!$this->_parts['table']) {
            throw new SqlException("Invalid `UPDATE` statement, missing `TABLE` clause.");
        }

        if (!$this->_parts['values']) {
            throw new SqlException("Invalid `UPDATE` statement, missing `VALUES` clause.");
        }

        return 'UPDATE' .
            $this->_buildFlags($this->_parts['flags']) .
            $this->_buildChunk($this->dialect()->names($this->_parts['table'])) .
            $this->_buildSet() .
            $this->_buildClause('WHERE', $this->dialect()->conditions($this->_parts['where'], [
                'schemas' => ['' => $this->_schema]
            ])) .
            $this->_buildOrder() .
            $this->_buildClause('LIMIT', $this->_parts['limit']) .
            $this->_buildClause('RETURNING', $this->dialect()->names($this->_parts['returning']));
    }