Undocumented function.

@param string     $query
@param bool|array $params

@return Database
165834
public function prepareQuery($query, $params = false)
    {
        if (isset($params) and is_array($params)) {
            $this->prepareParms($params);
        }

        if (!isset($this->setQuery)) {
            $this->setQuery = $query . ' ';
        } else {
            $this->setQuery .= $this->getQuery() . ' ' . $query . ' ';
        }

        return $this;
    }
635431
public function queryAndCount(string $query, array $params = []): int
	{
		$prepared = $this->prepare($query, $params);

		$this->execute($prepared);

		return $prepared['statement']->rowCount();
	}
1189720
public function query($query=null, $params = [])
    {
        $result = false;
        $result_key = [];
        
        if ($query && static::isSql($query)) {
            $sql = $query;
        } else {
            $result_key = $this->getQuery()->set($query)->get('result_key');
            
            $queryType = $this->getQuery()->type();
            
            // limit select count
            if ((!$queryType || $queryType === 'select') 
                && !isset($this->getQuery()['count']) 
                && !isset($this->getQuery()['limit'])
                && !isset($this->getQuery()['pagesize'])) 
            {
                $this->getQuery()->set('limit', 1000);
            }
            
            // update must set where
            if (($queryType === 'update' || $queryType === 'delete') && !$this->getQuery()->get('where')) {
                Ioc::logger('db')->error('query no set where: ' . json_encode($this->getQuery()->all()));
                return false;
            }
            
            $sql = $this->parse();
        }
        
        if ($params) {
            $this->bindParams($params);
        }
        
        $this->lastSql = $this->formatSql($sql);
        
        // sql log
        Ioc::logger('db')->debug($sql);
        
        try {
            $this->getAdapter()->prepare($sql);
            if (!empty($this->binds)) {
                $this->getAdapter()->bindParams($this->binds);
            }
            $bool = $this->getAdapter()->execute();
            
            if (!$bool) {
                throw new Exception($this->getAdapter()->getErrorMessage());
            }
        } catch (Exception $e) {
            
            $this->getAdapter()->rollback();
            throw new Exception('Excute sql error: ' . $e->getMessage() . ', sql:' . $sql);
        }
        
        $rawStatement = explode(" ", $sql);
        $statement = strtolower(trim($rawStatement[0]));
        
        if ($statement === 'show') {
            $result = $this->getAdapter()->fetchAll($fetchmode);
        } elseif ($statement === 'select') {
            $rkeys = $result_key ? StringHelper::toArray($result_key, ',\s') : null;
            
            while($rs = $this->getAdapter()->fetch()) {
                // 如果指定返回结果集的key，则按key重组数据
                if ($rkeys) {
                    if (count($rkeys) == 1) {
                        $rk1 = trim($rkeys[0]);
                        if (isset($rs[$rk1])) {
                            $result[$rs[$rk1]] = $rs;
                        } else {
                            $result[] = $rs;
                        }
                    } elseif (count($rkeys) == 2) {
                        $rk1 = trim($rkeys[0]);
                        $rk2 = trim($rkeys[1]);
                        if (!empty($rk2)) {
                            $result[$rs[$rk1]][$rs[$rk2]] = $rs;
                        } else {
                            $result[$rs[$rk1]][] = $rs;
                        }
                    }
                } else {
                    $result[] = $rs;
                }
            }
        } elseif ($statement === 'update' || $statement === 'delete') {
            $result = $this->getAdapter()->numberOfRows();
        } elseif ($statement === 'insert') {
            $result = $this->getAdapter()->lastId();
        } else {
            return null;
        }
        
        return $result;
    }
820226
public function query($query = '', $params = null, $fetchmode = PDO::FETCH_ASSOC)
    {
        $query = trim($query);
        if (empty($query)) {
            $query = $this->build();
            if (!$params) {
                $params = $this->getBindValues();
            }
        }

        $this->resetAll();
        $this->lastSql = $query;

        $this->execute($query, $params);

        $rawStatement = explode(" ", $query);

        $statement = strtolower(trim($rawStatement[0]));
        if ($statement === 'select' || $statement === 'show') {
            return $this->sQuery->fetchAll($fetchmode);
        } elseif ($statement === 'update' || $statement === 'delete') {
            return $this->sQuery->rowCount();
        } elseif ($statement === 'insert') {
            if ($this->sQuery->rowCount() > 0) {
                return $this->lastInsertId();
            }
        } else {
            return null;
        }

        return null;
    }
1360244
public function single($query = '', $params = null)
    {
        $query = trim($query);
        if (empty($query)) {
            $query = $this->build();
            if (!$params) {
                $params = $this->getBindValues();
            }
        }

        $this->resetAll();
        $this->lastSql = $query;

        $this->execute($query, $params);
        return $this->sQuery->fetchColumn();
    }
33266
protected function _save($query, $params)
    {
        $db = $this->db;
        $sth = $db->prepare($query);

        foreach ($params as $key => $val) {
            if (is_int($val)) {
                $sth->bindValue("$key", $val, $db::PARAM_INT);
            } else {
                $sth->bindValue("$key", $val);
            }
        }

        $response = $sth->execute();

        if ($response) {
            return $this;
        }

        return false;
    }
152773
public function query($query,$sqlParams=null)
    {
        $this->query =  (string)$query;
        if ($sqlParams!=null) {
            $this->sqlParams = $sqlParams;
        }
        return $this;
    }
810069
public function query($sql=null, array $params=[]) {
		if($sql === null) {
			$sql = $this->buildSQL();
			$params = $this->getParameters();
		}

		$this->replaceArrayParams($sql, $params);

		return $this->query = $this->db->query($sql, $params);
	}
203015
public function get_column($query, $params = []) {
		$statement = $this->get_statement($query, $params);
		$statement->execute();
		$result = $statement->fetch_assoc();

		$col = [];
		foreach ($result as $row) {
			$col[] = array_shift($row);
		}

		return $col;
	}
497909
public function query($sql, $params = null): Database
    {
        $sth          = null;
        $this->values = [];
        $this->lastid = null;
        if ($sql === '') {
            throw new CoreException(
                __CLASS__
                . '::query(): Query was undefined, please make sure you pass one.'
            );
        }

        if ($params === null) {
            try {
                $sth = $this->connection->query($sql);
            } catch (PDOException $e) {
                throw new CoreException(
                    __CLASS__.'::query()->PDO::query() : ' . $e->getMessage()
                );
            }
        } else {
            try {
                $sth = $this->connection->prepare($sql);
            } catch (PDOException $e) {
                throw new CoreException(
                    __CLASS__.'::query()->PDO::prepare() : ' . $e->getMessage()
                );
            }
            try {
                $sth->execute($params);
            } catch (PDOException $e) {
                throw new CoreException(
                    __CLASS__.'::query()->PDO::execute() : ' . $e->getMessage()
                );
            }
        }

        if ($sth instanceof PDOStatement && 0 !== strpos($sql, 'select')) {
            try {
                $this->values = $sth->fetchAll($this->configs['fetch_mode']);
            } catch (PDOException $e) {
                throw new CoreException(
                    __CLASS__.'::query()->PDOStatement::fetchAll() : '
                    . $e->getMessage()
                );
            }
        }

        if (false !== strpos($sql, 'insert')) {
            try {
                $this->lastid = $this->connection->lastInsertId();
            } catch (PDOException $e) {
                throw new CoreException(
                    __CLASS__.'::query()->PDO::lastInsertId() : ' . $e->getMessage()
                );
            }
        }

        return $this;
    }
1274378
public function query($query, $params = array())
  {
    $results = $this->api->get($query, $params);
    if(!isset($results->errors)) {
      return $results;
    } else {
      return $this->debug_mode ? $results->errors[0]->message : null;
    }
  }
220522
public function buildQuery(array $params = [])
    {
        $params = array_merge($params, [
          'user_login' => $this->login,
          'api_key' => $this->apiKey
        ]);
        return http_build_query($params);
    }
1341465
public function fetchAssoc($query, array $parameters = array())
    {
        $parameters = $this->parameterTransformer->transform($parameters);
        return $this->db->fetchAssoc($query, $parameters);
    }
749504
public static function run( $query, $params = array(), $handler = null ) 
	{
		return Handler::create( $handler )->run( $query, $params );
	}
1080838
final public static function getObjectsByQuery($query, $params=[]) {
		
		$app	= Application::getInstance();
		$db		= Database::getInstance();
		$class	= get_called_class();

		// run query
		$db->setQuery($query);
		$list = $db->loadObjectList($params);
		
		// array that returns and custom binds
		$objects = [];
		$customBinds = [];
		
		if (is_array($list) and isset($list[0])) {
			
			$binds = $class::getBinds();
			
			// get object properties from query
			$fields  = get_object_vars($list[0]);

			// search for custom field names
			foreach ($fields as $field=>$value) {
				if (!array_search($field, $binds)) {
					$customBinds[Utilities::getCamelCase($field)] = $field;
				}
			}

			// build each object
			foreach ($list as $row) {
				
				$object = new $class($row);

				// populate custom properties
				foreach ($customBinds as $customProp=>$customField) {
					$object->$customProp = $row->$customField;
				}
				
				// turn on loaded-from-db flag
				$object->loadedFromDb = TRUE;

				$objects[] = $object;
				
			}
			
		}
		
		$app->logEvent('Loaded ' . count($objects) . ' ' . $class . ' objects with custom fields ' . implode(',', $customBinds));
		
		return $objects;
		
	}
786722
public function sql(string $sql, array $params = []) {
        return $this->loadWiths($this->modelManager->sql($sql, $params));
    }
1339523
public function execute(string $query, array $parameters): CollectionInterface
    {
        $numServers = count($this->availableServers) - 1;

        for ($i = 0; $i <= $numServers; $i++) {
            // always get the first available server
            $server = $this->availableServers[0];

            // Move the selected server to the end of the stack
            $this->availableServers[] = array_shift($this->availableServers);

            $options = array_merge($this->httpOptions, [
                'base_uri' => sprintf('%s://%s', $this->protocol, $server),
                'json'     => [
                    'stmt' => $query,
                    'args' => $parameters,
                ],
            ]);

            try {
                $response     = $this->httpClient->request('POST', '/_sql', $options);
                $responseBody = json_decode((string)$response->getBody(), true);

                return new Collection(
                    $responseBody['rows'],
                    $responseBody['cols'],
                    $responseBody['duration'],
                    $responseBody['rowcount']
                );
            } catch (ConnectException $exception) {
                // Catch it before the BadResponseException but do nothing.
                continue;
            } catch (BadResponseException $exception) {
                $body = (string)$exception->getResponse()->getBody();
                $json = json_decode($body, true);

                if ($json === null && json_last_error() !== JSON_ERROR_NONE) {
                    throw new RuntimeException(sprintf('Server returned non-JSON response: %s', $body), 0, $exception);
                }

                $errorCode    = $json['error']['code'];
                $errorMessage = $json['error']['message'];

                throw new RuntimeException($errorMessage, $errorCode, $exception);
            }
        }

        throw new ConnectException(
            sprintf('No more servers available, exception from last server: %s', $exception->getMessage()),
            $exception->getRequest(),
            $exception
        );
    }