Skip to content

Latest commit

 

History

History
executable file
·
54 lines (41 loc) · 1.46 KB

Database.md

File metadata and controls

executable file
·
54 lines (41 loc) · 1.46 KB

Database

Connect to a database and send queries with DatabaseInterface.

Note

Currently only the Mysql driver is supported.

You will need to define a connection string in your environment variables (env.ini).

interface = 127.0.0.1:5757
staticsLocation = statics
apiLocation = src/api
mysql="host=localhost user=root password=root db=test"

Raw queries

You can send raw queries

// src/api/{email}/get.php
use CatPaw\Database\Interfaces\DatabaseInterface;

return static fn (DatabaseInterface $db, string $email) => $db->send(
    <<<SQL
        select email, name from users where email = :email
        SQL,
    ['email' => $email]
);

Query builder

You can also use SqlBuilderInterface to build and send queries

// src/api/{email}/get.php
use CatPaw\Database\Interfaces\SqlBuilderInterface;

return static fn (SqlBuilderInterface $sql, string $email) => $sql
    ->select('email','name')
    ->from('users')
    ->where()
    ->name('email')
    ->equals()
    ->parameter('email', $email)
    ->one();

Note

In case you're wondering - No, there is no ORM.
This project highly discourages the use of ORMs.
Raw queries and query builders are encouraged because they are more performant in some cases and they are more explicit, which goes hand in hand with the explicit error management guidelines.