query function.

@access public
@todo implement cache

@param string $query
@param array  $data (default: array())
@param string|callable $entity (default: null)

@return DatabaseResult
355055
public function query ($query, $data = array(), $entity = null)
		{
			if ($this->cache->isCached($query)) {
				return $this->cache->getCachedResult($query);
			} else {
				try {
					$statement = $this->database->prepare((string) $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
					$result = $statement->execute($data);
					$this->lastQuery = $statement->queryString;
					$this->lastStatement = $statement;

					if (! $result) {
						throw new DatabaseException(implode(': ', $statement->errorInfo()) . PHP_EOL . PHP_EOL . $query);
					}

				} catch (PDOException $e) {
					throw new DatabaseException($e->getMessage() . PHP_EOL . PHP_EOL . $query);
				}

				$databaseResult = $this->createResult($statement, $entity);

				if ($query instanceof DatabaseQuery && $databaseResult instanceof DatabaseResult) {
					$this->cache->cacheQuery($query, $databaseResult);
				}

				if ($query instanceof DatabaseQuery) {
					$this->cache->refreshCache($query);
				}

				if ($databaseResult instanceof DatabaseResult) {
					return $databaseResult;

				} else if ($query instanceof DatabaseQuery && $query->getType() == DatabaseQuery::QUERY_TYPE_INSERT) {
					return $this->insertId($query->getTable());

				} else {
					return $databaseResult;
				}
			}
		}
16965
public function findOne($query, array $data = null) {       
        $stmt = $this->executeQuery($query, $data);   
        
        return $stmt->fetch(\PDO::FETCH_ASSOC);
    }
940666
protected function execute($query)
    {
        $query->execute($this->bindings);
        
        $this->bindings = [];
        $this->wheres   = [];

        return $query;
    }
980042
public function merge($query, array $data)
    {
        $expression = "/(\\?)(?=(?:[^']|'[^']*')*$)/";
        $callback   = function () use (&$data) {
            if (!$data) {
                return '?';
            }

            return $this->quote(array_shift($data));
        };

        return preg_replace_callback($expression, $callback, $query);
    }
1038784
public function query( $query, array $data = [ ] )
    {
        $statement = $this->database->prepare( $query );

        if ( FALSE === $statement )
        {
            throw new DatabaseException( 'Failed to create prepared statement.' );
        }

        $result = $statement->execute( $data );

        $this->query = $statement->queryString;

        if ( FALSE === $result )
        {
            return [ ];
        }

        return $statement->fetchAll();
    }
646334
protected function _buildQuery(Query $query, $field, $value, array $data = [])
    {
        $method = $this->config('method');
        if (is_callable($method)) {
            return call_user_func_array($method, [$query, $field, $value, $data, $this->config()]);
        }

        return $query;
    }
1137507
protected function _buildQuery(Query $query, $field, $value, array $data = [])
    {
        if (is_array($value)) {
            $field .= ' IN';
        }

        return $query->where([$field => $value]);
    }
463125
public function query($entity, $query)
	{
		$retValue = array();

		try{
			$m = new MongoDB\Driver\Manager($this->url);
			$bulk = new MongoDB\Driver\BulkWrite;

			$options = [];

			$mQuery = new \MongoDB\Driver\Query($query, $options);
			$mCol = $this->db . "."  . $entity;
			$jsonData = $m->executeQuery($mCol, $mQuery);

			$retValue = $jsonData->toArray();
		}
		catch(Exception $e) {
			$this->_log->addError($e->getMessage());
		}

		return $retValue;
	}
201888
protected static function loadByQuery($query, $fields = null)
    {
        //
        $table = static::getTable();
        $writer = static::getDatabase()->getWriter();
        $alias = static::getClassName();

        //
        $join = null;
        $allFields = $fields ? $fields : static::getDefaultFields();

        // parse SQL select fields
        $selectFields = $writer->selectFields($allFields, $alias, $join);

        //
        $values = [];
        $whereConditions = [];

        //
        if (isset($query['where'])) {
            $whereConditions[] = '('.$query['where'].')';
            unset($query['where']);
        }

        //
        foreach ($query as $field => $value) {
            $token = ':'.$field;
            $whereConditions[] = "{$field} = {$token}";
            $values[$field] = $value;
        }

        //
        $where = implode(' AND ', $whereConditions);

        // prepare SQL query
        $sql = "SELECT {$selectFields} "
             ."FROM {$table} AS {$alias} {$join} "
             ."WHERE {$where} "
             .'LIMIT 1';

        // fetch data on database and return it
        $result = static::fetch(
            $sql,
            $values,
            true,
            is_string($fields),
            is_null($fields)
        );

        return $result;
    }
698129
public function doQueryOne($query, $data = null)
	{
		// Prepare and execute the entered query
		$prepQuery = parent::prepare($query);
		$prepQuery->execute($data);
		
		// Since this function is only to return one row, only return the first row
		return $prepQuery->fetch();
	}
783433
public function update(KDatabaseQueryUpdate $query)
    {
        $context = $this->getContext();
        $context->query = $query;

        //Execute the update operation
        if ($this->invokeCommand('before.update', $context) !== false)
        {
            if (!empty($context->query->values))
            {
                //Execute the query
                $context->result   = $this->execute($context->query);
                $context->affected = $this->_affected_rows;

                $this->invokeCommand('after.update', $context);
            }
            else $context->affected = false;
        }

        return $context->affected;
    }
130275
protected function _buildQuery(Query $query, $field, $value, array $data = [])
    {
        $type = $this->config('type');
        $value = '%' . $value . '%';
        $fields = $this->config('fields');
        $typesMap = $this->config('fieldTypes');
        $types = [];
        foreach ($fields as $field) {
            if (is_array($typesMap) && array_key_exists($field, $typesMap)) {
                $types[$field] = $typesMap[$field];
            } else {
                $types[$field] = null;
            }
        }
        if (empty($type)) {
            $type = 'and';
        }

        return $query->where(function ($exp) use (&$query, $value, $type, $fields, $types) {
            return $exp->{$type . '_'}(function ($ex) use ($value, $fields, $types) {
                collection($fields)->each(function ($field) use ($value, &$ex, $types) {
                    return $ex->like($field, $value, $types[$field]);
                });

                return $ex;
            });
        });
    }
858889
public function queryFromFilter(Query $query, array $data = [])
    {
        $query = parent::queryFromFilter($query, $data);

        //"Name" field
        if (!empty($data['name']) && strlen($data['name']) > 2) {
            $query->where([sprintf('%s.tag LIKE', $this->getAlias()) => sprintf('%%%s%%', $data['name'])]);
        }

        return $query;
    }
402278
protected function getCacheData(Query $query, $cache, $data, &$key = null)
    {
        // 判断查询缓存
        $key = is_string($cache['key']) ? $cache['key'] : $this->getCacheKey($query, $data);

        return $this->cache->get($key);
    }
851167
protected function triggerBeforeSave(
        $query, EntityInterface $entity, array $data
    ) {
        $save = $query instanceof Insert
            ? (new Save($entity, $data))
                ->setAction(Save::ACTION_BEFORE_INSERT)
            : (new Save($entity, $data))
                ->setAction(Save::ACTION_BEFORE_UPDATE);

        /** @var Save $save */
        return Orm::getEmitter($this->getEntityClassName())->emit($save);
    }
270392
public function insert($query, array $data){        
        $this->conn->prepare($query)->execute($data);
        
        return $this->conn->lastInsertId();
    }
1261295
public function queryResult($query, $params, $row, $col)
    {
        trigger_error('queryResult() is deprecated in favour of method chaining, eg $sql->query($query, $params)->result(0, 0)', E_USER_DEPRECATED);
        return $this->query($query, $params)->result($row, $col);
    }