我想简单地初始化\AsyncMysqlConnectionResult $connec;
对象无法初始化 AsyncMysqlQueryResult对象
<?hh
namespace Connection;
require_once("ini.php");
/**
* Class for execute and fetch query
*/
class MyQuery
{
/**
* if connection isn't valid recreate pool
*/
private ?\AsyncMysqlConnectionPool $pool;
/**
* \AsyncMysqlConnection object, store $conn
*/
private \AsyncMysqlConnection $connec;
/**
* \AsyncMysqlQueryReturn object, store return query
*/
private \AsyncMysqlQueryResult $result;
/**
* check if $conn object isValid(), if not release
* connection
*/
public function __construct(\AsyncMysqlConnection $conn)
{
if ($conn->isValid() == false)
{
$this->pool = new MyPool();
$this->connec = $this->pool->connect();
}
else
$this->connec = $conn;
$this->result = object;
}
/**
* escape query and execute it
*/
public async function query(string $query): Awaitable<\AsyncMysqlQueryResult>
{
$query = $this->connec->escapeString($query);
echo "Query escape\n";
/* Try to execute the query, if fail display error */
try
{
$this->result = await $this->connec->query($query);
//log request with ini
}
catch (Exception $e)
{
echo "Couldn't execute the request, error with message :<br>";
var_dump($e->getMessage());
//log request with fail
}
echo "Query done succefully\n";
return $this->result;
}
/**
* escape Map array and execute the request
*/
public async function queryf(HH\FormatString<HH\SQLFormatter> $query, array<string> $params): Awaitable<\AsyncMysqlQueryResult>
{
$i = 0;
while ($params[$i++])
$params[$i] = $this->connec->escapeString($params[$i]);
/* Try to execute the query, if fail display error */
try
{
$result = await $this->connec->queryf($query, implode(', ', $params));
//log request with ini
}
catch (Exception $e)
{
echo "Couldn't execute the request, error with message :<br>";
var_dump($e->getMessage());
//log request with fail
}
echo "Query done succefully\n";
return $this->result;
}
}
newtype AsyncMysqlConnectionResult = object;
newtype FormatString<T> = string;
async function simple_query(\AsyncMysqlConnection $conn): Awaitable<Vector>
{
$connec = new MyQuery($conn);
$ret = await $connec->query('SELECT * FROM users');
return $ret->vectorRows();
}
function run_query(\AsyncMysqlConnection $conn): void
{
$r = \HH\Asio\join(simple_query($conn));
var_dump($r);
}
run_query($康恩);
对于此对象,我使用https://docs.hhvm.com/hack/reference/class/AsyncMysqlConnectionPool/connect/类和connect()方法来获得此:\ AsyncMysqlConnectionResult $ connec对象。
我无法找到一个方法来初始化这个变量的类型,我已经尝试 创建NEWTYPE AsyncMysqlConnectionResult =对象,但filechecker回我: Unbound name: Connection\object (an object type)
不是一个真正的回答你的问题,但也有文档在这里的一些例子:https://docs.hhvm.com/hack/async/examples#accessing-mysql –
感谢链接,我已经读了很多一个hhvm文档,但不是这个页面。我被卡住了HH \ FormatString类型的queryf函数的另一个问题,我不能发送字符串类型到这个函数,我不知道为什么有这些复杂类型.... –
bbichero
queryf需要一个文字字符串,以便可以对格式字符串占位符进行静态类型检查。信息在这里:https://docs.hhvm.com/hack/reference/class/AsyncMysqlConnection/queryf/ –