执行SQL查询,select a from a where a=%s and %d.

@param string      $sql
@param string|null ...$args

@return null|\PDOStatement
171886
public function fetch($sql, ...$args) {
        if (is_null($this->dialect)) {
            return null;
        }
        $dialect = $this->dialect;
        try {
            // 表前缀处理
            $sql = preg_replace_callback('#\{[a-z][a-z0-9_].*\}#i', function ($r) use ($dialect) {
                return $dialect->getTableName($r[0]);
            }, $sql);
            if ($args) {
                $params = 0;
                $sql    = preg_replace_callback('#%(s|d|f)#', function ($r) use (&$params, $args, $dialect, $sql) {
                    if ($r[1] == 'f') {
                        $v = floatval($args[ $params ]);
                    } else if ($r[1] == 'd') {
                        $v = intval($args[ $params ]);
                    } else if (is_null($args[ $params ])) {
                        $v = null;
                    } else {
                        $v = $dialect->quote($args[ $params ], \PDO::PARAM_STR);
                    }
                    $params++;

                    return $v;
                }, $sql);
            }
            //查询
            $rst = $this->dialect->query($sql);
            if ($rst) {
                return $rst;
            }
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
        }

        return null;
    }
194229
public function getColumn($sql, array $args = [])
    {
        $query = $this->query($sql, $args);
        if (!$query) {
            return  null;
        }
        
        $value = $query->fetchColumn();
        
        return $value;
    }
872180
public function execute($sql, $p = null){
        if ($p === null)
            return $this->conn->exec($sql);

        $stmt = $this->query($sql, $p);
        if (!$stmt)
            return 0;
        
        $code = $stmt->errorCode();
        if ($code !== '00000'){ // Error Code defined by ANSI SQL
            $error = $stmt->errorInfo();

            if (isset($error[2]))
                $msg = $error[2];
            else
                $msg = '';
            
            trigger_error("SQL error [$code]：$msg : arguments : " . var_export($p, true),
                          E_USER_WARNING);
            return 0;
        }

        return $stmt->rowCount();
    }
1358744
protected function query_row ( $sql )
    {
        $sql = $this->sql_printf(func_get_args());
        if (!($res = pg_query($this->conn, $sql)))
        {
            $this->sql_errcheck($sql);
        }
        if ($row = pg_fetch_array($res))
        {
            $rs = $row;
        }
        else
        {
            $rs = false;
        }
        pg_free_result($res);
        return $rs;
    }
607851
public function execute($sql = null, $parameters = array()) {
        if (empty($sql)) {
            return null;
        }
        foreach ($parameters as $key => $item) {
            Str::bindParam($sql, $key + 1, $item, is_numeric($item) ? 'INT' : 'STR');
        }
        $this->result = mysql_query($sql, $this->driver);
        Factory::log()->info(sprintf('MYSQL: %s => %s', $sql,
            $this->getError()));
        return $this->result;
    }
925258
private function _replaceArguments($sql, $arguments)
    {
        if (!is_array($arguments)) {
            $arguments = array($arguments);
        }
        
        // searches string segments (startPos, endPos)
        $stringSegments = [];
        if (preg_match_all('/(["\'`])((?:\\\\\1|.)*?)\1/', $sql, $matches, PREG_OFFSET_CAPTURE)) {
            foreach ($matches[2] as $match) {
                $startPos = $match[1];
                $endPos = $startPos + strlen($match[0]);
                array_push($stringSegments, [$startPos, $endPos]);
            }
        }
        
        // searches arguments position
        $argsPos = [];
        preg_match_all('/\?/', $sql, $matches, PREG_OFFSET_CAPTURE);
        foreach ($matches[0] as $match) {
            array_push($argsPos, $match[1]);
        }

        // replaces arguments
        $matchCount = 0;
        $argCount = 0;
        return preg_replace_callback(
            '/\?/',
            function ($matches) use (&$argCount, &$matchCount, $arguments, $argsPos, $stringSegments) {
                $ret = $matches[0];
                
                if ($argCount < count($arguments)) {
                    // is the current match inside a quoted string?
                    $argPos = $argsPos[$matchCount];
                    $isInsideQuotedString = false;
                    foreach ($stringSegments as $segment) {
                        if ($argPos >= $segment[0] &&  $argPos < $segment[1]) {
                            $isInsideQuotedString = true;
                            break;
                        }
                    }
                    
                    if (!$isInsideQuotedString) {
                        $ret = $this->quote($arguments[$argCount++]);
                    }
                }
                
                $matchCount++;
                return $ret;
            },
            $sql
        );
    }
1121821
public static function dbQuery($sql) {
        
        try {
            return self::query($sql);   
        } catch (PDOException $e) {
            die(self::dbError($sql));
        }
    }
1095061
public function query($sql)
    {
        return $this->getDisconnectRetry()->run(function () use ($sql) {
            $statement = $this->prepare($sql);
            $statement->execute();

            return $statement;
        });
    }
1092345
public static int executeUpdate(final String sql, final Object... args)
    {
       return SqlClosure.sqlExecute(c -> executeUpdate(c, sql, args));
    }
215714
@Deprecated
	public static ResultSet query(String sql, Object... args) {
		ResultSet result = null;
		Connection con = getconnnection();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(sql);
			if (args != null) {
				for (int i = 0; i < args.length; i++) {
					ps.setObject((i + 1), args[i]);
				}
			}
			result = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
252129
public function query(string $sql, array $params = []): Statement
    {
        $this->usedPrepare = false;
        $stmt = $this->prepare($sql, $params);
        $this->usedPrepare = true;
        $stmt->execute();
        return $stmt;
    }
252981
public function createCommand($sql = null, $params = [])
    {
        /** @var Command $command */
        $command = new $this->commandClass([
            'db' => $this,
            'sql' => $sql,
        ]);

        return $command->bindValues($params);
    }
1364737
function sql($sql = null, $params = NULL) {
        $params = $this->autoarr($params);
        $sql = str_replace('table.',$this->alias.'.',$sql);
        return $this->execute($sql, $params);
        return $this;
    }
577075
public function createSqlCommand($sql,$params=array())
	{
		$command=$this->_connection->createCommand($sql);
		$this->bindValues($command,$params);
		return $command;
	}
739340
public function Query($Sql, $Parameters = NULL) {
		$Db = Gdn::Database();

		// Replace db prefixes.
		$Sql = str_replace(array(':_z', ':_'), array($Db->DatabasePrefix.self::TABLE_PREFIX, $Db->DatabasePrefix), $Sql);

      // Figure out the type of the type of the query.
      if (StringBeginsWith($Sql, 'select'))
         $Type = 'select';
      elseif (StringBeginsWith($Sql, 'truncate'))
         $Type = 'truncate';
      elseif (StringBeginsWith($Sql, 'insert'))
         $Type = 'insert';
      elseif (StringBeginsWith($Sql, 'update'))
         $Type = 'update';
      elseif (StringBeginsWith($Sql, 'delete'))
         $Type = 'delete';
      else
         $Type = 'select';

		// Execute the query.
      if (is_array($Parameters))
         $this->SQL->NamedParameters($Parameters);

		$Result = $this->SQL->Query($Sql, $Type);

		//$this->Timer->Split('Sql: '. str_replace("\n", "\n     ", $Sql));

		return $Result;
	}
331917
public function execute($sql = null, $parameters = array()) {
		if (empty($sql)) {
			return null;
		}
		try {
			if (!empty($sql)) {
				$this->prepare($sql);
				$this->bind($parameters);
			}
			$this->result->execute();
		} catch (\PDOException  $ex) {
            if (Config::isDebug()) {
                Factory::log()->info(sprintf('PDO: %s => %s', $sql, $this->error), $parameters);
                throw $ex;
            }
			$this->error = $ex->getMessage();
		}
        return $this->result;
	}
497795
public function getRow($sql, array $args = [])
    {
        $result = $this->query($sql, $args);
        
        if (!$result) {
            return [];
        }
        
        $row = $result->fetch_row();
        
        return $row;
    }