Add $dbh.execute, rework $sth.execute #192
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.