Write a constant array clause as SQL query syntax
@param Parameters $params The query parameters: tables and placeholder values
@return string The generated SQL
512036
public function toSQL(Parameters $params, bool $inner_clause)
    {
        if ($key = $this->getKey())
        {
            try
            {
                $params->get($key);
            }
            catch (\OutOfRangeException $e)
            {
                // Not a valid key, replace
                $key = null;
            }
        }

        if (!$key)
            $key = $params->assign(null);

        // Rebind, to be sure
        $this->bind($params, $key, array($params->getDriver(), 'formatArray'));
        return ':' . $key;
    }
220522
public function buildQuery(array $params = [])
    {
        $params = array_merge($params, [
          'user_login' => $this->login,
          'api_key' => $this->apiKey
        ]);
        return http_build_query($params);
    }
47690
public function toSQL(Parameters $params, bool $inner_clause)
    {
        $drv = $params->getDriver();
        // First get the source tables: FROM and JOIN clauses
        $source = array();

        if ($table = $this->getTable())
            $source[] = "FROM " . $drv->toSQL($params, $table);

        foreach ($this->getJoins() as $join)
            $source[] = $drv->toSQL($params, $join);

        // Now build the start of the query, all tables should be known
        $parts = array();

        $parts[] = "SELECT";
        $fields = $this->getFields();
        if (!empty($fields))
        {
            $field_parts = array();
            foreach ($fields as $field)
                $field_parts[] = $drv->toSQL($params, $field);
            $parts[] = implode(", ", $field_parts);
        }
        else
            $parts[] = "*";

        // Add the source tables and joins to the query
        foreach ($source as $part)
            $parts[] = $part;

        if ($where = $this->getWhere())
            $parts[] = $drv->toSQL($params, $where);

        if ($union = $this->getUnion())
            $parts[] = $drv->toSQL($params, $union);
        
        if ($order = $this->getOrder())
            $parts[] = $drv->toSQL($params, $order);

        if ($limit = $this->getLimit())
            $parts[] = $drv->toSQL($params, $limit);
        
        if ($offset = $this->getOffset())
            $parts[] = $drv->toSQL($params, $offset);
        
        return implode(" ", $parts);
    }
598349
protected function formatQueryString($params = array())
    {
        $qString = array();
        foreach ($params as $name => $value) {
            if (is_array($value)) {
                foreach ($value as $val) {
                    $val = $this->isUtf8($val) ? $val : utf8_encode($val);
                    $qString[] = $name . '=' . urlencode($val);
                }
            } else {
                $value = $this->isUtf8($value) ? $value : utf8_encode($value);
                $qString[] = $name . '=' . urlencode($value);
            }
        }
        return implode('&', $qString);
    }
1042675
public function multiquery($query, $params = []) {

		if(true === is_array($query)) {
			$query = implode(";\n", $query);
		}

		if(false === is_string($query)) {
			return trigger_error(sprintf('Argument 1 passed to %s() must be of the type string or array, "%s" given', __METHOD__, gettype($query)), E_USER_ERROR);
		}

		$results = new ResultSet([], 'array');

		if(null === $this -> connection) {
			$this -> connect();
		}

		//Escape paramaters
		$query = $this -> escapeSql($query, $params);

		if($this -> connection -> multi_query($query)) {

			$array = [];

			do {

				if($result = $this -> connection -> use_result()) {

					while($row = $result -> fetch_array(MYSQLI_ASSOC)) {
						$array[] = $row;
					}

					$result -> close();
					$results -> append($array);
					$array = [];
				}
			}

			while($this -> connection -> more_results() && $this -> connection -> next_result());
		}

		return $results;
	}
974874
public static function dump(string $query, array $params = [])
	{
		[$query, $params] = Doctrine\DBAL\SQLParserUtils::expandListParameters($query, $params, []);

		$parts = explode('?', $query);

		$output = [];
		while ($parts && $params) {
			$output[] = array_shift($parts);
			$output[] = self::formatParameter(array_shift($params));
		}

		return self::highlightQuery(implode('', array_merge($output, $parts ? : [], $params ? : [])));
	}
110412
public function insert($params)
    {
        if (is_array($params)) {
            $count_rows = count($params['columns']);
            $increment = 1;
            foreach ($params['columns'] as $keys => $value) {
                for ($i = 1; $i <= $count_rows; $i++) {
                    $data[$keys] = $value;
                }
            }
            foreach ($data as $keys => $values) {
                if ($increment == $count_rows) {
                    $columns[] = "{$keys} = '{$values}'";
                } else {
                    $columns[] = "{$keys} = '{$values}'";
                }
                $increment++;
            }
            $columns = implode(' , ', $columns);
            $query = "INSERT INTO `{$params['table']}`SET {$columns}";
            if (isset($params['debug']) and strtolower($params['debug']) === 'on') {
                var_dump($query);
            }

            return $query;
        } else {
            return false;
        }
    }
418784
public function queryRow($params = array())
    {
        $this->mergeParams($params);
        $this->beginQuery();
        $rst = $this->statement->fetch(\PDO::FETCH_ASSOC);
        $this->statement->closeCursor();
        return $rst;
    }
587654
public function toSQL(Parameters $params, bool $inner_clause)
    {
        $drv = $params->getDriver();
        $fieldname = $drv->toSQL($params, $this->getField());
        $value = $drv->toSQL($params, $this->getValue());

        return $fieldname . ' = ' . $value;
    }
175612
public function query($query, array $params = null)
    {
        # If the next query should be cached then run the cache function instead
        if ($this->cacheNext) {
            $this->cacheNext = false;
            return $this->cache($query, $params);
        }

        # Ensure we have a connection to run this query on
        $this->connect();

        $this->query = $query;
        $this->params = null;
        $this->preparedQuery = false;

        if (is_array($params)) {
            $this->params = $params;
        }

        $this->quoteChars($query);
        $this->functions($query);
        $this->limit($query);
        $this->tableNames($query);
        $this->namedParams($query, $params);
        $this->paramArrays($query, $params);
        $this->convertNulls($params);

        $preparedQuery = $this->prepareQuery($query, $params);
        $this->preparedQuery = $preparedQuery;

        if ($this->output) {
            if ($this->htmlMode) {
                echo "<pre>";
            }

            echo $preparedQuery;

            if ($this->htmlMode) {
                echo "<hr>";
            } else {
                echo "\n";
            }
        }

        switch ($this->mode) {

            case "mysql":
                if (!$result = $this->server->query($preparedQuery)) {
                    $this->error();
                }
                break;

            case "postgres":
            case "redshift":
                $tmpQuery = $query;
                $query = "";

                $noParams = false;
                if ($this->mode == "redshift" && count($params) > 32767) {
                    $noParams = true;
                }

                $i = 1;
                reset($params);
                while ($pos = strpos($tmpQuery, "?")) {
                    if ($noParams) {
                        $query .= substr($tmpQuery, 0, $pos) . "'" . pg_escape_string(current($params)) . "'";
                        next($params);
                    } else {
                        $query .= substr($tmpQuery, 0, $pos) . "\$" . $i++;
                    }
                    $tmpQuery = substr($tmpQuery, $pos + 1);
                }
                $query .= $tmpQuery;

                $params = Helper::toArray($params);
                if (!$result = pg_query_params($this->server, $query, $params)) {
                    $this->error();
                }
                break;

            case "odbc":
                if (!$result = odbc_prepare($this->server, $query)) {
                    $this->error();
                }
                $params = Helper::toArray($params);
                if (!odbc_execute($result, $params)) {
                    $this->error();
                }
                break;

            case "sqlite":

                if (!is_array($params)) {
                    if (!$result = $this->server->query($preparedQuery)) {
                        $this->error();
                    }

                # If we have some parameters then we must convert them to the sqlite format
                } else {
                    $newQuery = "";
                    foreach ($params as $key => $val) {
                        $pos = strpos($query, "?");
                        $newQuery .= substr($query, 0, $pos);
                        $query = substr($query, $pos + 1);

                        $newQuery .= ":var" . $key;
                    }
                    $newQuery .= $query;

                    if (!$result = $this->server->prepare($newQuery)) {
                        $this->error();
                    }

                    foreach ($params as $key => $val) {
                        switch (gettype($val)) {

                            case "boolean":
                            case "integer":
                                $type = SQLITE3_INTEGER;
                                break;

                            case "double":
                                $type = SQLITE3_FLOAT;
                                break;

                            case "NULL":
                                if ($this->allowNulls) {
                                    $type = SQLITE3_NULL;
                                } else {
                                    $type = SQLITE3_TEXT;
                                    $val = "";
                                }
                                break;

                            default:
                                $type = SQLITE3_TEXT;
                        }

                        $result->bindValue(":var" . $key, $val, $type);
                    }

                    if (!$result = $result->execute()) {
                        $this->error();
                    }
                }
                break;

            case "mssql":
                if (!$result = mssql_query($preparedQuery, $this->server)) {
                    $this->error();
                }
                break;

            case "mssqlsrv":
                if (!$result = sqlsrv_query($this->server, $preparedQuery)) {
                    $this->error();
                }
        }

        if (!$result) {
            $this->error();
        }

        return new Result($result, $this->mode);
    }
1024001
public function where($where, &$params)
    {
        if (!is_array($where)) {
            throw new \Exception("Invalid where clause specified, must be an array");
        }

        $params = Helper::toArray($params);

        $query = "";

        $andFlag = false;

        foreach ($where as $field => $value) {

            # Add the and flag if this isn't the first field
            if ($andFlag) {
                $query .= "AND ";
            } else {
                $andFlag = true;
            }

            # Add the field name to the query
            $query .= $this->quoteField($field);

            # Convert arrays to use the in helper
            if (is_array($value)) {
                $helperMap = [
                    "<"     =>  "lessThan",
                    "<="    =>  "lessThanOrEqualTo",
                    ">"     =>  "greaterThan",
                    ">="    =>  "greaterThanOrEqualTo",
                    "="     =>  "equals",
                    "<>"    =>  "notEqualTo",
                ];
                $clause = (string) reset($value);
                if (count($value) === 2 && isset($helperMap[$clause])) {
                    $method = $helperMap[$clause];
                    $val = next($value);
                    trigger_error("Using arrays for complex where clauses is deprecated in favour of the helper methods, eg Sql::" . $method . "(" . $val . ")", E_USER_DEPRECATED);
                    $value = static::$method($val);
                } else {
                    $value = static::in($value);
                }
            }

            # Any parameters not using a helper should use the standard equals helper
            if (!is_object($value)) {
                $value = static::equals($value);
            }

            $query .= " " . $value->getClause() . " ";
            foreach ($value->getValues() as $val) {
                $params[] = $val;
            }
        }

        return $query;
    }
798803
public function sqlSearch($params)
    {
        $this->load($params);

        $where =[
           'id' => sprintf('id = "%s"',$this->id),
           'status' => sprintf('status = "%s"',$this->status),
           'created_at' => sprintf('created_at = "%s"',$this->created_at),
           'updated_at' => sprintf('updated_at = "%s"',$this->updated_at),
           'username' => sprintf('username like "%s%%"',$this->username),
           'auth_key' => sprintf('auth_key like "%s%%"',$this->auth_key),
           'password_hash' => sprintf('password_hash like "%s%%"',$this->password_hash),
           'password_reset_token' => sprintf('password_reset_token like "%s%%"',$this->password_reset_token),
           'email' => sprintf('email like "%s%%"',$this->email),
        ];

        $filtedParams = array_filter($this->attributes,
            function($val){return $val!='';});

        $FiltedWhere = ['1=1'];
        foreach ($filtedParams as $key => $value) {
            $FiltedWhere[] = $where[$key];
        }

        $querySql = sprintf('
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ', '*', $this->tableName(), implode(' AND ', $FiltedWhere));

        $countSql = sprintf('
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ', 'count(1)', $this->tableName(), implode(' AND ', $FiltedWhere));

        $sqlDataProvider = new SqlDataProvider([
            'sql' => $querySql,
            // 'params' => [':sex' => 1],
            'totalCount' => Yii::$app->db->createCommand($countSql)->queryScalar(),
            //'sort' =>false,//如果为假则删除排序
            'key' => 'id',
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ],
                'attributes' => array_keys($this->attributes),
            ],
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

        return $sqlDataProvider;
    }
1324215
public function toSQL(Parameters $params, bool $inner_clause)
    {
        if ($key = $this->getKey())
        {
            try
            {
                $params->get($key);
            }
            catch (\OutOfRangeException $e)
            {
                $key = null;
            }
        }

        if ($key === null)
        {
            $val = $this->getValue();
            if (is_bool($val))
                $val = $val ? 1 : 0;
            $key = $params->assign($val, $this->getParameterType());
        }
        $this->bind($params, $key, null);

        return ':' . $key;
    }
83473
private function prepareQuery($params = []) {
		if(!is_array($params)) {
			return $params;
		}

		$query = [];

		foreach($params as $key => $value) {
			if(is_array($value)) {
				foreach($value as $element) {
					$query[] = sprintf(
						'%s[]=%s',
						$key,
						urlencode($element)
					);
				}
			} else {
				$query[] = sprintf(
					'%s=%s',
					$key,
					urlencode($value)
				);
			}
		}

		return implode("&",$query);
	}
343621
public static function getSqlWithParams(string $sql, array $params = []): string
    {
        if (! empty($params)) {
            $indexed = ($params === array_values($params));
            foreach ($params as $k => $v) {
                if (is_string($v)) {
                    $v = "'${v}'";
                }
                if (is_array($v)) {
                    $v = "'" . implode("','", $v) . "'";
                }
                if ($indexed) {
                    $sql = preg_replace('/\?/', $v, $sql, 1);
                } else {
                    $sql = str_replace(":${k}", $v, $sql);
                }
            }
        }

        return str_replace(PHP_EOL, '', $sql);
    }
628556
public function deleteByParams($params = [])
    {
        extract($params);

        $query = $this->newDeleteQuery();

        if (isset($where)) {
            if (is_array($where)) {
                foreach ($where as $condition) {
                    $condition = (array)$condition;
                    $query->where($condition[0], $condition[1]);
                }
            } else {
                call_user_func_array([$query, 'where'], $where);
            }
        }

        if (isset($order)) {
            call_user_func_array([$query, 'order'], $order);
        }

        if (isset($limit)) {
            call_user_func_array([$query, 'limit'], $limit);
        }

        $this->getDB()->execute($query);

        return $this;
    }
269298
public function buildQuery($params)
	{
		if (!is_array($params) || count($params) == 0)
		{
			return false;
		}

		return urldecode(http_build_query($params, '', '&'));
	}