ClanCats/Hydrahon

Execute raw queries

neto737 opened this issue · 3 comments

Hi,

Is there any way to run raw queries using Hydrahon?

I want to truncate a table, but I haven't found a function in Hydrahon, and also haven't found a way to run a raw query, is it possible?

Also, is there any way to create tables using Hydrahon? It could be using raw queries as I wanna do to truncate a table.

Thank you in advance.

I've just found that it's possible to truncate using:

$h->table('table_name')->truncate()->execute();

Also found that it's possible to drop a table using:

$h->table('table_name')->drop()->execute();

But I haven't found anything to add/remove an column from an existing table, and also haven't found anything to create a new table.

Hello - I just want to say that this lib is for data manipulation (insert/select/...) AFAIK, but for structure manipulation you could use Phinx (it's migration lib, but it may work for you in a different way too):
https://book.cakephp.org/phinx/0/en/index.html

@neto737 is completely right about the truncate and drop operations.

Hydrahon is query builder not an ORM layer, so running raw queries completely depends on your implementation. If you are using PDO, just use the PDO objects to run your raw queries:

$connection = new PDO('mysql:host=localhost;dbname=my_database;charset=utf8', 'username', 'password');

// create a new mysql query builder
$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
{
    // your implemenation
});

$connection->prepare('delete from something where x = ?')->execute([$myParam]);