convert to SQL

@return  string
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);
    }
776730
private function _constructSql() {
        if($this->sql) {
            return [$this->sql,$this->params];
        }
        $distinct = $this->distinct ? 'DISTINCT' : '';
        $groupby = '';
        if (!empty($this->group)) {
            $groupby = 'GROUP BY ' . $this->group;
        }
        if (!empty($this->having)) {
            $groupby .= ' ' . $this->having;
        }
        $order = !empty($this->order) ? "ORDER BY $this->order" : '';

        $this->fields = $this->fields?$this->fields:'*';
        $sql = "SELECT $distinct $this->fields FROM `$this->table` $this->alias $this->join $this->where $groupby $order $this->limit";
        $sql = str_replace('table.',$this->alias.'.',$sql);
        return [$sql,$this->params];
    }
305519
public function getSql()
    {
        $where = (count($this->conditions) === 0)
            ? ''
            : 'WHERE ' . implode(' and ', $this->conditions);
        $sql = "SELECT $this->select FROM $this->table $where $this->order $this->limit $this->offset";
        return $sql;
    }
693719
public function toSQL()
    {
        $this->res['sql'] = self::sortSqlArray($this->res['sql']);

        $res = $this->res['sql'][0] . ' ' . $this->getFlagsSQL() . ' ' . $this->res['select_expr'][0];
        $join_references_i = $join_conditions_i = 0;

        if (isset($this->res['where_conditions']) && $this->res['where_conditions'] && !in_array('WHERE', $this->res['sql'])) {
            trigger_error('Where conditions were set manually, but WHERE construction was not found in initial query. If you need to add conditions, WHERE construction should be in your query added manually.');
        }

        if (isset($this->res['order_conditions']) && $this->res['order_conditions'] && !in_array('ORDER BY', $this->res['sql'])) {
            trigger_error('Order conditions were set manually, but ORDER BY construction was not found in initial query. If you need to add conditions, ORDER BY construction should be in your query added manually.');
        }

        if (isset($this->res['group_conditions']) && $this->res['group_conditions'] && !in_array('GROUP BY', $this->res['sql'])) {
            trigger_error('Group conditions were set manually, but GROUP BY construction was not found in initial query. If you need to add conditions, GROUP BY construction should be in your query added manually.');
        }

        if (isset($this->res['having_conditions']) && $this->res['having_conditions'] && !in_array('HAVING', $this->res['sql'])) {
            trigger_error('Having conditions were set manually, but HAVING construction was not found in initial query. If you need to add conditions, HAVING construction should be in your query added manually.');
        }

        if (isset($this->res['limit_conditions']) && $this->res['limit_conditions'] && !in_array('LIMIT', $this->res['sql'])) {
            trigger_error('Limit conditions were set manually, but LIMIT construction was not found in initial query. If you need to add conditions, LIMIT construction should be in your query added manually.');
        }

        for ($i = 1, $so = count($this->res['sql']); $i < $so; $i++) {
            switch ($this->res['sql'][$i]) {
                default:
                    $res .= $this->res['sql'][$i] . ' ';
                    break;
                case 'FROM':
                    if (!isset($this->res['table_references'][0])) {
                        break;
                    }
                    $res .= ' FROM ' . $this->res['table_references'][0];
                    break;
                case 'JOIN':
                case 'LEFT JOIN':
                case 'RIGHT JOIN':
                case 'INNER JOIN':
                    $res .= ' ' . $this->res['sql'][$i] . ' ' . $this->res['join_references'][$join_references_i++] . ' ON ' . $this->res['join_conditions'][$join_conditions_i++]->toSQL();
                    break;
                case 'WHERE':
                    if (!isset($this->res['where_conditions'][0])) {
                        break;
                    }
                    $res .= ' WHERE ' . $this->res['where_conditions'][0]->toSQL();
                    break;
                case 'HAVING':
                    if (!isset($this->res['having_conditions'][0])) {
                        break;
                    }
                    $res .= ' HAVING ' . $this->res['having_conditions'][0];
                    break;
                case 'ORDER BY':
                    if (!isset($this->res['order_conditions'][0])) {
                        break;
                    }
                    $res .= ' ORDER BY ' . $this->res['order_conditions'][0];
                    break;
                case 'GROUP BY':
                    if (!isset($this->res['group_conditions'][0])) {
                        break;
                    }
                    $res .= ' GROUP BY ' . $this->res['group_conditions'][0];
                    break;
                case 'LIMIT':
                    if ($this->res['limit_conditions'][0]->isEmpty()) {
                        break;
                    }
                    $res .= ' LIMIT ' . $this->res['limit_conditions'][0];
                    break;
            }
        }
        return $res;
    }
1309646
private function getSQLForInsert()
    {
        return 'INSERT INTO ' . $this->sqlParts['from']['table'] .
        ' (' . implode(', ', array_keys($this->sqlParts['values'])) . ')' .
        ' VALUES(' . implode(', ', $this->sqlParts['values']) . ')';
    }
386538
public function getSql()
    {
        if ($this->sql !== null && $this->state === self::STATE_CLEAN) {
            return $this->sql;
        }

        switch ($this->type) {
            case self::DELETE:
                $this->sql = $this->getSqlForDelete();
                break;

            case self::UPDATE:
                $this->sql = $this->getSqlForUpdate();
                break;

            case self::SELECT:
            default:
                $this->sql = $this->getSqlForSelect();
                break;
        }

        $this->state = self::STATE_CLEAN;

        return $this->sql;
    }
12658
public function sqlPagination()
    {
        $pager = $this->pagination();
        if (!$pager instanceof DatabasePagination) {
            $pager = $this->createPagination($pager->data());
        }

        $sql = $pager->sql();
        if (strlen($sql) > 0) {
            $sql = ' '.$sql;
        }

        return $sql;
    }
975364
public function dump()
	{
		$pdo = $this->pdo();

		$sql = '';

		foreach ($this->list_tables() as $table)
		{
			$query = $pdo->query("SELECT * FROM `{$table}`");

			while ($row = $query->fetch(\PDO::FETCH_NUM)) 
			{
				$values = array();

				foreach ($row as $column) 
				{
					$values[] = is_null($column) ? 'NULL' : $pdo->quote($column);
				}

				$sql .= "INSERT INTO `{$table}` VALUES (".join(',', $values). ");\n";
			}
		}

		return $sql;
	}
921070
public function refSQL()
    {
        /** @type SQL_Model $q */
        $q = $this->ref('model');
        $q->addCondition($q->id_field, $this);

        return $q;
    }
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();
            }
        }
    }
320580
protected function sql()
    {
        if ( null === $this->sql )
        {
            $this->sql = new Sql(
                $this->getDbAdapter(),
                $this->getTableInSchema( 'settings' )
            );
        }

        return $this->sql;
    }
1271206
private function getSQL() {
        try {
            $this->checkDialect();
        } catch (DialectException $e) {
            return null;
        }
        if (!$this->values) {
            $this->values = new BindValues ();
        } else if (!$this->valueFixed) {
            $this->values->reset();
        }
        $fields = $this->prepareFields($this->fields, $this->values);
        $from   = $this->prepareFrom($this->sanitize($this->from));
        $joins  = $this->prepareJoins($this->sanitize($this->joins));
        $having = $this->sanitize($this->having);
        $group  = $this->sanitize($this->group);
        $order  = $this->sanitize($this->order);

        return $this->dialect->getSelectSQL($fields, $from, $joins, $this->where, $having, $group, $order, $this->limit, $this->values, $this->forupdate);
    }
446785
public function getQuery()
    {
        $sql = $this->prepareSelectString();
        $sql .= $this->prepareJoinString();
        $sql .= $this->prepareWhereString();
        $sql .= $this->prepareGroupByString();
        $sql .= $this->prepareHavingString();
        $sql .= $this->prepareOrderByString();
        $sql .= $this->prepareLimitString();

        return $sql;
    }
255257
public function toArray()
    {
        $arrayResult = array(
            'id' => $this->id,
            'string' => $this->sql,
            'command' => $this->getCommand(),
            'executed' => $this->executed,
            'isDDL' => $this->isDDL(),
            'isDML' => $this->isDML(),
            'isDCL' => $this->isDCL(),
            'isTCL' => $this->isTCL(),
            'isAnon' => $this->isAnon(),
            'isRead' => $this->isRead(),
            'isWrite' => $this->isWrite(),
            'result' => $this->result->toarray(),
        );

        return $arrayResult;
    }
643833
public function SQL() {
      if(is_null($this->_SQL)) {
         $Name = $this->Engine . 'Driver';
         $this->_SQL = Gdn::Factory($Name);
         $this->_SQL->Database = $this;
      }
      
      return $this->_SQL;
   }
783677
public function sqlOrders()
    {
        if (!$this->hasOrders()) {
            return '';
        }

        $parts = [];
        foreach ($this->orders() as $order) {
            if (!$order instanceof DatabaseOrder) {
                $order = $this->createOrder($order->data());
            }

            $sql = $order->sql();
            if (strlen($sql) > 0) {
                $parts[] = $sql;
            }
        }

        if (empty($parts)) {
            return '';
        }

        return ' ORDER BY '.implode(', ', $parts);
    }
878259
public function generateCountSQL()
        {
            if ($this->sql === null) {
                $this->detectDistinct();
                $this->action = QueryInterface::ACTION_COUNT;
                $sql_generator = new SqlGenerator($this);

                $this->sql = $sql_generator->getCountSQL();
            }

            return $this->sql;
        }