Baptiste Bibiche Baptiste Bibiche - 1 month ago 7
MySQL Question

Can't initialise \AsyncMysqlQueryResult object

I want simply initialise the

\AsyncMysqlConnectionResult $connec;
object

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

For having this object I use https://docs.hhvm.com/hack/reference/class/AsyncMysqlConnectionPool/connect/ class and connect() method to have this : \AsyncMysqlConnectionResult $connec object.

I can't find a way to initialise this variable type, I've try to
create newtype AsyncMysqlConnectionResult = object but the filechecker return me :
Unbound name: Connection\object (an object type)

Answer

This is the only way a find to rewrite queryf() method.

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

I have no error and everything work fine. If someone have a better and solution I'm here.