2016-08-18 65 views
0

我想簡單地初始化\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)

+0

不是一個真正的回答你的問題,但也有文檔在這裏的一些例子:https://docs.hhvm.com/hack/async/examples#accessing-mysql –

+0

感謝鏈接,我已經讀了很多一個hhvm文檔,但不是這個頁面。我被卡住了HH \ FormatString 類型的queryf函數的另一個問題,我不能發送字符串類型到這個函數,我不知道爲什麼有這些複雜類型.... – bbichero

+0

queryf需要一個文字字符串,以便可以對格式字符串佔位符進行靜態類型檢查。信息在這裏:https://docs.hhvm.com/hack/reference/class/AsyncMysqlConnection/queryf/ –

回答

0

這是找到重寫queryf()方法的唯一方法。

public async function queryf($query, array<int, string> $params): Awaitable<\AsyncMysqlQueryResult> 
{ 
    $result = ""; 

    /* Try to execute the query, if fail display error */ 
    try // execute query 
    { 
     $result = await $this->connec->queryf($query, implode(', ', $params)); 

     // If log_request === TRUE log request with ini 
     if ($this->log_request === TRUE) 
      await $this->logRequest($query, 0); 
    } 
    catch (Exception $e) // If the query can't be execute display error 
    { 
     echo "\nCouldn't execute the request, error with message :<br>\n"; 
     var_dump($e->getMessage()); 
     //log request with fail 

     await $this->logRequest((string)$query, -1); 
    } 
    return (object)$result; 
} 

我沒有錯誤,一切工作正常。 如果有人有更好的解決方案,我在這裏。

0

而不是存儲在類,爲什麼結果你的查詢方法不是簡單地返回結果。例如:

public async function query(string $query): Awaitable<\AsyncMysqlQueryResult> 
{ 
    $query = $this->connec->escapeString($query); 
    return await $this->connec->query($query); 
} 

或者如果您確實希望將結果存儲在類中,請將其設置爲空。這將允許您不在構造函數中設置它,並且只在查詢完成後才設置它。

private ?\AsyncMysqlQueryResult $result; 
+0

我不是被迫在類中存儲這個變量,但我可以' t發送給queryf一個字符串,他的原型是:'public function queryf(HH \ FormatString $ query,... $ args):Awaitable {}' – bbichero

+0

queryf需要一個字符串文字 – alexriedl