Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run SQL commands on connection #145

Closed
langabi opened this issue Oct 27, 2021 · 6 comments
Closed

Run SQL commands on connection #145

langabi opened this issue Oct 27, 2021 · 6 comments

Comments

@langabi
Copy link

langabi commented Oct 27, 2021

I'm trying to run specific SQL queries at connection time, eg. SET time_zone = '+03:00';.

Is there a way to do so that will re-run this query every time the connection is re-established? As far as I can see, no, because if one chains a ->then() onto the original LazyConnection or Connection, this will NOT be rerun if the connection closes and is reopened.

Alternatively, one could use an 'open' or 'connect' event, but it doesn't look like there is one.

Any good ideas? Thanks!

@clue
Copy link
Contributor

clue commented Nov 25, 2021

@langabi Thanks for reporting, makes a lot of sense and is indeed something we've already toyed around with!

Similar requests have come up (perhaps indirectly) in #133 and others.

Here's my concept for a future MysqlClient API as discussed in #147:

$db = new React\Mysql\MysqlClient('alice:secret@localhost/bookstore');

$db->on('open', fn () => $db->exec("SET time_zone = '+03:00'"));

$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 clue added this to the v0.6.0 milestone Dec 15, 2021
@clue
Copy link
Contributor

clue commented Dec 20, 2021

The same API can also be used to set SQL modes:

$db = new React\Mysql\MysqlClient('alice:secret@localhost/bookstore');

$db->on('open', fn () => $db->exec("SET SQL_MODE='NO_BACKSLASH_ESCAPES'"));

$db->query('SELECT "foo\nbar"')->then(fn (QueryResult $result) => var_dump($result));

See also #139

@clue
Copy link
Contributor

clue commented Dec 20, 2021

I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can always reopen this 👍

@clue clue closed this as completed Dec 20, 2021
@clue clue added the question label Dec 20, 2021
@langabi
Copy link
Author

langabi commented Dec 20, 2021

Thanks! I don’t know how I missed your earlier comment, sorry!

This looks perfect, and solves my use case. Thanks!

@pfk84
Copy link

pfk84 commented Sep 18, 2022

Any news/progress on the 'open' event? I'd also need to SET the time_zone (to UTC) for any MySQL connection...

@clue
Copy link
Contributor

clue commented Sep 23, 2022

Any news/progress on the 'open' event? I'd also need to SET the time_zone (to UTC) for any MySQL connection...

@pfk84 The open event will be implemented as part of the API discussed in #147.

@clue clue removed this from the v0.6.0 milestone Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants