reactphp-sqlite icon indicating copy to clipboard operation
reactphp-sqlite copied to clipboard

Enable foreign keys with LazyDatabase.

Open bartvanhoutte opened this issue 6 years ago • 6 comments

Small support request/possible feature request. I would like to use foreign keys in my SQLite database and according to the SQLite documentation I have to enable this per connection via PRAGMA foreign_keys = ON. Is there a way to execute this command everytime LazyDatabase creates a connection to the database?

bartvanhoutte avatar Nov 22 '19 15:11 bartvanhoutte

I've currently worked around the issue by enabling FKs every time when selecting/updating/deleting, but this obviously isn't ideal ...

bartvanhoutte avatar Nov 22 '19 16:11 bartvanhoutte

@bartvanhoutte Thanks for reporting!

This is indeed not currently supported and your feature request makes perfect sense.

It's my understanding we should probably provide some API to pass arbitrary commands that are executed once the underlying connection is ready or do you think it makes more sense to provide some predefined ones?

clue avatar Nov 29 '19 18:11 clue

Looking at the amount of options available to be configured this way I think it makes more sense to somehow provide an array of queries to be executed when a connection is made.

Something like this would suffice I guess:

$options['onConnect'][] = 'PRAGMA foreign_keys = on';
$options['onConnect'][] = 'PRAGMA threads = 4';
$db = (new DatabaseFactory($loop))->openLazy($path, $flags, $options);

What do you think?

bartvanhoutte avatar Dec 02 '19 09:12 bartvanhoutte

Here's my concept for a future SqliteClient API as discussed in https://github.com/clue/reactphp-sqlite/issues/47:

$db = new Clue\React\Sqlite\SqliteClient('users.db');

$db->on('open', fn () => $db->exec('PRAGMA foreign_keys = on'));

$db->query('SELECT 1')->then(fn (QueryResult $result) => var_dump($result));

The idea is to fire the open event immediately after the database is ready to accept commands, right before the query passed to query() would actually be executed.

What do you think about this?

clue avatar Nov 10 '21 16:11 clue

Sounds good to me!

Stevemoretz avatar Nov 23 '21 15:11 Stevemoretz

@clue excellent.

bartvanhoutte avatar Dec 02 '21 09:12 bartvanhoutte