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

Add $dbh.execute, rework $sth.execute #192

Merged
merged 1 commit into from Jun 25, 2020
Merged

Conversation

rbt
Copy link
Collaborator

@rbt rbt commented Jun 24, 2020

dbh.execute, unlike dbh.do, returns a statement handle instead of a (sometimes incorrect) row count. This allows for a simple query with parameters to be executed without being prepared first.

I didn't want to change do(), and preferred a bit more naming consistency with sth.execute(). do is removed from documentation but not (yet) deprecated. Feedback is appreciated!

OLD:
$dbh.do( 'CREATE TABLE tab (foo text, id int5)' );

my $sth = $dbh.prepare('SELECT foo FROM tab WHERE id = ?');
$sth.execute($id);
for $sth.allrows(:array-of-hash) -> $row {}

NEW:
$dbh.execute( 'CREATE TABLE tab (foo text, id int5)' );

for $dbh.execute('SELECT foo FROM tab WHERE id = ?', $id).allrows(:array-of-hash) -> $row {}

$dbh.do still exists but it is now undocumented to discourage use and should become deprecated in the near future. do() did not allow statements which has results which made it rather unhelpful. It's main purpose was to bypass prepare() for statements which broke under prepare() such as LOCK TABLE on MySQL.

$sth.execute previously returned a count of the effected rows. Now, for consistency with $dbh.execute, it returns a statement handle to allow chaining of rows, allrows, etc. directly onto the execute result. Fail/exceptions are far better for error handling than the return code was.

NOTE, MySQL for type conversion (such as the JSON example) still requires going through prepare.execute as the result set is handled differently.

Solves #185.

@rbt rbt force-pushed the rbt.execute-instead-of-do branch 2 times, most recently from 78ab8f3 to bfcc0c3 Compare June 24, 2020 17:54
@rbt
Copy link
Collaborator Author

rbt commented Jun 24, 2020

JJ suggested I bump the API, now set to 1.

Also took a look through the first 50 pages of results for Raku code with sth.execute and found an unmaintained package that looked at the return code BUT it's already been replaced by a different package which does it differently and won't be impacted.

@abraxxa
Copy link
Contributor

abraxxa commented Jun 24, 2020

The naming is fine for me.

@rbt rbt force-pushed the rbt.execute-instead-of-do branch from bfcc0c3 to a0e39af Compare June 24, 2020 19:19
@JJ JJ requested a review from salortiz June 25, 2020 06:50
Copy link
Contributor

@JJ JJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks perfectly fine. You probably need to bump up the API number anyway.

dbh.execute, unlike dbh.do, returns a statement handle instead of a (sometimes incorrect) row count. This allows for a simple query with parameters to be executed without being prepared first.

OLD:

 my $sth = $dbh.prepare('SELECT foo FROM tab WHERE id = ?');
 $sth.execute($id);
 for $sth.allrows(:array-of-hash) -> $row {}

NEW:

 for $sth.execute('SELECT foo FROM tab WHERE id = ?', $id).allrows(:array-of-hash) -> $row {}

$dbh.do still exists but it is now undocumented to discourage use and should become deprecated in the near future. do() did not allow statements which has results which made it rather unhelpful. It's main purpose was to bypass prepare() for statements which broke under prepare() such as LOCK TABLE on MySQL.

$sth.execute previously returned a count of the effected rows. Now, for consistency with $dbh.execute, it returns a statement handle to allow chaining of rows, allrows, etc. directly onto the execute result. Fail/exceptions are far better for error handling than the return code was.
@rbt rbt force-pushed the rbt.execute-instead-of-do branch from a0e39af to 96a0e4d Compare June 25, 2020 12:58
@rbt rbt merged commit 96a0e4d into master Jun 25, 2020
@rbt rbt deleted the rbt.execute-instead-of-do branch June 25, 2020 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants