ClanCats / Hydrahon

🐉 Fast & standalone PHP MySQL Query Builder library.
https://clancats.io/hydrahon/master/
MIT License
278 stars 58 forks source link

Execute raw queries #44

Closed neto737 closed 1 year ago

neto737 commented 3 years ago

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.

neto737 commented 3 years ago

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.

marek-hanzal commented 1 year ago

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

mario-deluna commented 1 year ago

@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]);