Find by query
@param $sqlQuery
@param array $params
@param int $fetchMode
@return mixed
975738
static public function findByQuery($sqlQuery, array $params, $fetchMode = \PDO::FETCH_OBJ)
    {
        $model = new static;
        if($model->table) {
            $sqlQuery = str_replace(':table', $model->table, $sqlQuery);
        }

        $result = $model->execute($sqlQuery);
        return $result;
    }
1268786
public function fetch($fetchMode = NULL, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
        $fetchMode = empty($fetchMode) ? $this->fetchMode : $fetchMode;
        Assertions::assertSupportedFetchMode($fetchMode);

        return count($this->result) === 0 ? false : array_pop($this->result);
    }
595166
public function fetch($hydrationMode = self::HYDRATE_OBJECT)
	{
		if (null === $this->resultSet) {
			$items = [];
			$objectRepository = $this->objectManager->getRepository($this->className);
			
			if ($this->countItemsCallable instanceof \Closure) {
				$fn = $this->countItemsCallable;
				$this->fullyItems = $fn($objectRepository, $this->_queryParts['where']);
					
				if (!is_integer($this->fullyItems)) {
					throw new \UnexpectedValueException('The closure "countItemsCallable" returned an unexcepted value.');
				}
			} elseif ($this->countQuery instanceof \Doctrine\ORM\Query/* || $this->countQuery instanceof \Doctrine\ODM\MongoDB\Query\Query*/) {
				$this->fullyItems = $this->countQuery->getSingleScalarResult();
			} else {
				$this->internalCountQuery = $this->createCountQuery($this->_queryParts['select'], $this->_queryParts['where'], $this->_queryParts['distinct']);
				$this->fullyItems = (int) ($this->internalCountQuery instanceof \Doctrine\ORM\Query ?
						$this->internalCountQuery->getSingleScalarResult() : $this->internalCountQuery->execute());
			}
			
			if ($this->fullyItems > 0) {
				if ($this->selectItemsCallable instanceof \Closure) {
					$fn = $this->selectItemsCallable;
					$items = $fn($objectRepository, $this->_queryParts['where'], $this->_queryParts['orderBy'], $this->itemPerPage, $this->getItemOffset());
			
					if (!is_array($items)) {
						throw new \UnexpectedValueException('The closure "selectItemsCallable" returned an unexcepted value.');
					}
				} elseif ($this->selectQuery instanceof \Doctrine\ORM\Query) {
					$items = $this->selectQuery->setFirstResult($this->getItemOffset())
												->setMaxResults($this->itemPerPage)
												->getResult();
				} else {
					$this->internalSelectQuery = $this->createSelectQuery($this->_queryParts['select'], $this->_queryParts['where'], $this->_queryParts['orderBy'], $this->_queryParts['distinct']);
					$items = $this->internalSelectQuery instanceof \Doctrine\ORM\Query ?
					$this->internalSelectQuery->getResult() : $this->internalSelectQuery->execute()->toArray(false);
				}
			}
			
			// Set pagination result set
			$this->setResultSet($items);
			
			if (isset($this->options['twig_extension_enabled']) && true === $this->options['twig_extension_enabled']) {
				$globals = $this->twig->getGlobals();
					
				if (isset($globals[OkaPaginationExtension::TWIG_GLOBAL_VAR_NAME])) {
					$globals[OkaPaginationExtension::TWIG_GLOBAL_VAR_NAME][$this->managerName] = $this->resultSet->toArray(['items']);
					$this->twig->addGlobal(OkaPaginationExtension::TWIG_GLOBAL_VAR_NAME, $globals[OkaPaginationExtension::TWIG_GLOBAL_VAR_NAME]);
				}
			}
		}
		
		return $hydrationMode == self::HYDRATE_ARRAY ? $this->resultSet->toArray() : $this->resultSet;
	}
451123
protected function _fetchRow($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);
            }
        }
        $sth->execute();

        return $sth->fetch($db::FETCH_ASSOC);
    }
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);
    }
900640
public function getOne($sql = null,$params = array())
	{
		if(!isset($this->operationOption['params']))
		{
			$this->operationOption['params'] = array();
		}
		$this->prepareQuery($sql ? $sql : $this->buildSelectSQL(),$params);
		if(false === $this->lastStmt)
		{
			return false;
		}
		else
		{
			return $this->lastStmt->fetch(PDO::FETCH_ASSOC);
		}
	}
400179
public function paginate($fetchMode = PDO::FETCH_ASSOC) {
        $this->fields = $this->fields?$this->fields:'*';
        $this->fields = 'SQL_CALC_FOUND_ROWS '.$this->fields;
        $result = $this->query()->fetchAll($fetchMode);
        $result = $this->_data($result,true);
        $num = $this->sql('SELECT FOUND_ROWS()')->fetchColumn();
        return [$num,$result];
    }
190999
private function getFetchMode($className, $fetchMode)
    {
        $fetchMode = strtoupper($fetchMode);
        if (!defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) {
            throw ORM\MappingException::invalidFetchMode($className, $fetchMode);
        }

        return constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode);
    }
1320156
public function find( $table, $fields = null, $where = null, $order = null, $limit = null, $offset = null, $array = array (), $fetchMode = \PDO::FETCH_ASSOC )
     {
         // TODO: Implement find() method.
     }
45935
protected function statement($query, $fetchMode=null)
    {

        return $this->attempt(function () use ($query, $fetchMode) {

            $statement = $this->pdo()->prepare("$query");

            if ($fetchMode !== null) {
                $statement->setFetchMode($fetchMode);
            }
            return $statement;
        }, $query);

    }
76473
public function fetchAll(string $sql, array $params = []) : array
    {
        $result = $this->query($sql, $params, true);

        $return = [];

        foreach ($result as $item) {
            $return[] = $item;
        }

        return $return;
    }
1237230
public final function get(string $query, array $queryParams = null, string $fetchClass = null)
    {
        return $this->query($query, $queryParams, 1, $fetchClass)->getData(0);
    }
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;
    }
27070
public function query ($sql, array $bound_parameters = array(), $fetch = true) {

		$statement = $this -> entity_manager -> getConnection() -> prepare($sql);

		foreach ($bound_parameters as $parameter => $value) {
			$statement -> bindValue($parameter, $value);
		}

		$statement -> execute();

		if ($fetch) {
			return $statement -> fetchAll();
		} else {
			return null;
		}
	}
46820
function fetchAll($fetchMode = PDO::FETCH_ASSOC) {
        return $this->_data($this->query()->fetchAll($fetchMode),true);
    }
1312102
public function sqlFetchColumn(string $sql, array $params = [], string $column = '0')
    {
        $stmt = $this->prepare($sql, $params);

        if (ctype_digit((string) $column)) {
            return $stmt->fetchColumn($column);
        }

        if ($record = $stmt->fetch()) {
            return array_key_exists($column, $record) ? $record[$column] : null;
        }

        return null;
    }
1244409
public function query($query, $params = null, $fetchResult = false)
        {
            $this->checkConnection(true);
            $this->log('Query: ' . $query);

            if(!empty($params)) {
                $this->log('Params: ' . print_r($params, true));
            }
            $start =  microtime(true);

            if(empty($params)) {
                $res = $this->executeQuery($query);
            } else {
                $res = $this->executePreparedStatement($query, $params);
            }

            $end = microtime(true);
            $this->log("Execution time: " . ($end - $start) . " seconds");


            if($fetchResult) {
    	        $this->log('Num Rows: '.odbc_num_rows($res));
                $resutlSet = $this->getRows($res);
                odbc_free_result($res);
                $res = $resutlSet;
                $resultSet = null;
                $fetch = microtime(true);
                $this->log("Fetch time: " . ($fetch - $end) . " seconds");
            }
            return $res;
        }