Convert to SQL unit.

@return SQL unit
431851
public SQLUnit toSQL() {
        StringBuilder result = new StringBuilder();
        List<Object> insertParameters = new LinkedList<>();
        for (Object each : segments) {
            if (!(each instanceof ShardingPlaceholder)) {
                result.append(each);
                continue;
            }
            if (each instanceof InsertValuesPlaceholder) {
                appendInsertValuesPlaceholder(null, (InsertValuesPlaceholder) each, insertParameters, result);
            } else if (each instanceof InsertSetPlaceholder) {
                appendInsertSetPlaceholder(null, (InsertSetPlaceholder) each, insertParameters, result);
            } else {
                result.append(each);
            }
        }
        return insertParameters.isEmpty() ? new SQLUnit(result.toString(), parameters) : new SQLUnit(result.toString(), insertParameters);
    }
907934
public function convert()
    {
        $parsed = $this->sqlParser->parse($this->sql);

        if (false === $parsed) {
            throw new \Exception('SQL query is not valid');
        }

        $results = [];
        foreach ($parsed as $section => $data) {
            if ($this->converterFactory->canCreate($section)) {
                $converter = $this->converterFactory->create($section);
                $results = array_merge($results, $converter->convert($data));
            }
        }

        $table = current(array_filter($results, function ($item) {
            return 'table' === $item['name'];
        }));
        unset($results[array_search($table, $results)]);
        array_unshift($results, $table);

        foreach ($results as $function) {
            $args = isset($function['args']) ? $function['args'] : [];
            $this->generator->addFunction($function['name'], $args);
        }

        $this->generator->addFunction('get');

        return $this->generator->generate();
    }
1065533
protected function generateJoinSQL()
        {
            $sql = ' FROM ' . $this->getTable()->getSelectFromSql();
            foreach ($this->query->getJoins() as $join) {
                $sql .= ' ' . $join->getJoinType() . ' ' . $join->getTable()->getSelectFromSql();
                $sql .= ' ON (' . Query::quoteIdentifier($join->getLeftColumn()) . Criterion::EQUALS . Query::quoteIdentifier($join->getRightColumn());

                if ($join->hasAdditionalCriteria()) {
                    $sql_parts = [];

                    foreach ($join->getAdditionalCriteria() as $criteria) {
                        $criteria->setQuery($this->query);
                        $sql_parts[] = $criteria->getSql();
                        foreach ($criteria->getValues() as $value) {
                            $this->query->addValue($value);
                        }
                    }

                    $sql .= ' AND ' . join(' AND ', $sql_parts);
                }

                $sql .= ')';
            }

            return $sql;
        }
511070
public function buildSelectSQL()
	{
		$where = $this->parseCondition(isset($this->operationOption['where']) ? $this->operationOption['where'] : '');
		if ('' !== $where)
		{
			$where = 'where ' . $where . ' ';
		}
		return 'select ' . $this->parseDistinct()
				. $this->parseFieldBefore()
				. $this->parseField(null,'*')
				. 'from '
				. $this->parseTable() . ' '
				. $this->parseJoin()
				. $where
				. $this->parseGroup()
				. $this->parseHaving()
				. $this->parseOrder()
				. $this->parseLimit()
				. $this->parseLock()
				;
	}
697313
public function getQuery()
    {
        $sql = $this->setQuery;
        $sql .= $this->getWhere();
        $sql .= $this->getGroupBy();
        $sql .= $this->getOrderBy();
        $sql .= $this->getHaving();
        $sql .= $this->getLimit();

        $this->setQuery = null;
        $this->setWhere = null;
        $this->setHaving = null;
        $this->setOrderBy = null;
        $this->setGroupBy = null;
        $this->setLimit = null;

        return str_replace('  ', ' ', $sql);
    }
1067772
protected function _convertUnit($unit)
    {
        if ($unit === 'week') {
            return self::ONE_WEEK;
        }

        if ($unit === 'day') {
            return self::ONE_DAY;
        }

        if ($unit === 'hour') {
            return self::ONE_HOUR;
        }

        if ($unit === 'minute') {
            return self::ONE_MINUTE;
        }

        if ($unit === 'second') {
            return self::ONE_SECOND;
        }

        if ($unit === 'millisecond') {
            return self::ONE_MILLISECOND;
        }

        throw new BuildException("Illegal unit '$unit'");
    }
257092
public function getOne()
    {
        $sql = $this->toSQL();
        return $this->table->getOne($sql, $this->getParams());
    }
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']));
    }
936960
private function select() {
        $this->performed  = true;
        $this->resultSet  = [];
        $this->resultSets = [];
        $this->resultIdx  = 0;
        $this->maxIdx     = 0;
        $this->size       = 0;
        try {
            $this->checkDialect();
        } catch (DialectException $e) {
            $this->error = $e->getMessage();

            return;
        }
        //生成SQL
        if (!$this->sql) {
            $this->sql = $this->getSQL();
            if (!$this->sql) {
                $this->error       = 'can not generate the SQL';
                $this->errorSQL    = '';
                $this->errorValues = $this->values->__toString();

                return;
            }
            //重新生成SQL.
            $this->statement = null;
        }
        //生成prepare statement
        if (!$this->statement) {
            $this->prepareStatement();
        }
        //执行
        if ($this->statement) {
            try {
                if ($this->values) {
                    foreach ($this->values as $value) {
                        list ($name, $val, $type, $field, $rkey) = $value;
                        if ($this->whereData) {
                            $val = isset($this->whereData[ $rkey ]) ? $this->whereData[ $rkey ] : (isset($this->whereData[ $name ]) ? $this->whereData[ $name ] : $val);
                        }
                        if (!$this->statement->bindValue($name, $val, $type)) {
                            $this->errorSQL    = $this->sql;
                            $this->errorValues = $this->values->__toString();
                            $this->error       = 'can not bind the value ' . $val . '[' . $type . '] to the argument:' . $name . '(' . $field . ')';

                            return;
                        }
                    }
                }
                $rst = $this->statement->execute();
                QueryBuilder::addSqlCount();
                if ($rst) {
                    $this->resultSets = $this->statement->fetchAll(\PDO::FETCH_ASSOC);
                    $this->size       = count($this->resultSets);
                    if ($this->size > 0) {
                        $this->maxIdx    = $this->size - 1;
                        $this->resultSet = $this->resultSets[0];
                    }
                } else {
                    $this->dumpSQL($this->statement);
                }
            } catch (\PDOException $e) {
                $this->exception   = $e;
                $this->error       = $e->getMessage();
                $this->errorSQL    = $this->sql;
                $this->errorValues = $this->values->__toString();
            }
        }
    }
48014
private function getSQL()
    {
        $sql = $this->getTypeSQL()
                . $this->getWhereSQL()
                . $this->getOrderSQL()
                . $this->getLimitSQL()
                . $this->getOffsetSQL();
        return $sql;
    }
1210077
public function from()
    {
        $from = '';

        $numargs = func_num_args();

        if ($this->oosql_del_multiFlag) {


            if ($numargs < $this->oosql_del_numargs) {
                $msg = 'Columns and passed data do not match! ' . $this->sql();
                throw new \Exception($msg, 9810, null);
            }


        }

        $from .= ' FROM ';

        $from .= $this->oosql_table;

        if ($numargs > 0) {
            $from .= ', ';

            $arg_list = func_get_args();

            for ($i = 0; $i < $numargs; $i++) {
                if ($i !== 0 && $numargs > $i) {
                    $from .= ', ';
                }

                if ($arg_list[$i] instanceof oosql) {

                    $arg_list[$i]->alias();
                    $fields = $arg_list[$i]->getFields($arg_list[$i]->getTableAlias());

                    $this->sql(',' . implode(', ', $fields));
                    $from .= $arg_list[$i]->getSql() . ' AS ' . $arg_list[$i]->getTableAlias();
                } else {
                    $from .= $arg_list[$i];
                }
            }
        }

        $this->sql($from);

        $this->oosql_fromFlag = false;

        return $this;
    }
435684
public function getColumnQuery() {
        // prepare column types
        $type_val = $this->getTypeVal();
        // build query
        $query = $this->db->quotekey($this->name).' '.$type_val.' '.
            $this->getNullable();
        // unify default for booleans
        if (preg_match('/bool/i', $type_val) && $this->default!==null)
            $this->default = (int) $this->default;
        // default value
        if ($this->default !== false) {
            $def_cmds = array(
                'sqlite2?|mysql|pgsql' => 'DEFAULT',
                'mssql|sybase|dblib|odbc|sqlsrv' => 'constraint DF_'.$this->table->name.'_'.$this->name.' DEFAULT',
                'ibm' => 'WITH DEFAULT',
            );
            $def_cmd = $this->findQuery($def_cmds).' '.$this->getDefault();
            $query .= ' '.$def_cmd;
        }
        if (!empty($this->after) && $this->table instanceof TableModifier) {
            // `after` feature only works for mysql
            if (preg_match('/mysql/', $this->db->driver())) {
                $after_cmd = 'AFTER '.$this->db->quotekey($this->after);
                $query .= ' '.$after_cmd;
            }
        }
        return $query;
    }
569364
public function get_sql() : string {
		if ( empty( $this->columns_to_select ) ) {
			// this should be the default behavior
			$this->columns_to_select = [ '*' ];
		}

		$from = ' FROM ' . implode( ', ', $this->from ) . ' ';

		if ( ! empty( $this->joins ) ) {
			$from .= $this->joins;
		}

		$from  .= 'WHERE ' . $this->where_clause;
		$select = 'SELECT ' . implode( ', ', $this->columns_to_select ) . $from;

		if ( ! empty( $this->group_by ) ) {
			$select .= 'GROUP BY ' . implode( ', ', $this->group_by ) . ' ';
		}

		if ( $this->order_by !== '' ) {
			$select .= 'ORDER BY ' . $this->order_by . ' ';
		}

		$select .= 'LIMIT ' . $this->limit;

		if ( $this->offset !== '' ) {
			$select .= ' OFFSET ' . $this->offset;
		}

		return $select;
	}
1021516
public function convert()
    {
        if (count($this->arrayBlocks) <= 0) {
            return null;
        }
        try {
            $this->blockRepository->startTransaction();
            $result = $this->deleteBlocks();
            if (false !== $result) {
                $languages = $this->languageRepository->activeLanguages();
                foreach ($this->arrayBlocks as $block) {
                    foreach ($languages as $language) {
                        $result = $this->updateBlock($block, $language->getId(), 1);
                    }
                }

                if ($result) {
                    $this->blockRepository->commit();
                } else {
                    $this->blockRepository->rollBack();
                }
            }

            return $result;
        } catch (\Exception $e) {
            if (isset($this->blockRepository) && $this->blockRepository !== null) {
                $this->blockRepository->rollBack();
            }

            throw $e;
        }
    }
365925
public function prepare($sql)
    {
        $this->_connect();
        $stmtClass = $this->_defaultStmtClass;
        $stmt = new $stmtClass($this, $sql);
        $stmt->setFetchMode($this->_fetchMode);
        return $stmt;
    }
514518
public function setToSql(): array
    {
        if (!$this->set) {
            throw new SqlException('Undefined UPDATE SET columns.');
        }

        $sql = $params = [];

        foreach ($this->set as $item) {
            $sqlPart = $item['sql'];

            $item['params'] && $params = array_merge($params, $item['params']);

            $sql[] = $sqlPart;
        }

        $sql = 'SET ' . implode(', ', $sql);

        return [$sql, $params];
    }
8858
public function genUnionQuery  ()
	{

		$sql	= $this->genUnionSQL (func_get_args (), 'UNION ALL');

		// Prepare database query

		$query	= $this->prepare ($sql);
		
		// Bind values
		
		$this->genUnionBindValues ($query, func_get_args ());

		// Return query

		return $query;

	}