2016-08-18 3 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 ($의 CONN)를 초기화 원하는 초기화 할 수 없습니다;

이 객체를 사용하려면 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 함수의 다른 형식으로 다른 문제에 붙어있어,이 함수에 문자열 형식을 보낼 수 없습니다. 왜 이런 complexe 형식이 ... 모르겠어요. – bbichero

+0

queryf 형식 문자열 자리 표시자를 정적으로 typechecked 수 있도록 리터럴 문자열이 필요합니다. 여기에 정보 : https://docs.hhvm.com/hack/reference/class/AsyncMysqlConnection/queryf/ –

답변

0

이것은 findf() 메소드를 재 작성하는 유일한 방법입니다.

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); 
} 

결과가 클래스에 저장되도록하려면 nullable로 설정하십시오. 이렇게하면 생성자에서 설정하지 않고 쿼리가 설정된 후에 만 ​​설정할 수 있습니다.

private ?\AsyncMysqlQueryResult $result; 
+0

클래스에서이 변수를 저장하도록 강요받지는 않지만, queryf에 문자열을 보내면 그의 프로토 타입은 다음과 같다 :'public function queryf (HH \ FormatString $ query, ... $ args) : Awaitable {}' – bbichero

+0

queryf는 문자열 리터럴 – alexriedl