Skip to content

Commit

Permalink
[Pg] small refactor
Browse files Browse the repository at this point in the history
move statement preparation out of submethod BUILD to get better error handling
use grammar-based tokenizer for rewriting placeholders
  • Loading branch information
moritz committed May 4, 2012
1 parent 66f1aca commit c4f91e1
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions lib/DBDish/Pg.pm6
Expand Up @@ -199,37 +199,28 @@ class DBDish::Pg::StatementHandle does DBDish::StatementHandle {
has @!column_names;
has Int $!row_count;
has $!field_count;
has $!current_row;
has $!current_row = 0;

method !handle-errors {
my $status = PQresultStatus($!result);
if $status != PGRES_EMPTY_QUERY | PGRES_COMMAND_OK | PGRES_TUPLES_OK | PGRES_COPY_OUT | PGRES_COPY_IN {
self!set_errstr(PQresultErrorMessage($!result));
die self.errstr if $.RaiseError;
return Nil;
}
else {
self!reset_errstr;
return True;
}
self!set_errstr(Any);
}

method !munge_statement {
my $count = 0;
$!statement.subst(:g, '?', { '$' ~ ++$count});
}

submethod BUILD(:$!statement, :$!pg_conn) {
state $statement_postfix = 0;
$!statement_name = join '_', 'pg', $*PID, $statement_postfix++;
my $munged = self!munge_statement;

$!result = PQprepare(
$!pg_conn,
$!statement_name,
$munged,
0,
OpaquePointer
);
self!handle-errors;
my $info = PQdescribePrepared($!pg_conn, $!statement_name);
$!param_count = PQnparams($info);
True;
submethod BUILD(:$!statement, :$!pg_conn, :$!statement_name, :$!param_count,
:$!dbh) {
}
method execute(*@params is copy) {
$!current_row = 0;
Expand Down Expand Up @@ -349,12 +340,34 @@ class DBDish::Pg::Connection does DBDish::Connection {
method BUILD(:$!pg_conn) { }

method prepare(Str $statement, $attr?) {
state $statement_postfix = 0;
my $statement_name = join '_', 'pg', $*PID, $statement_postfix++;
my $munged = DBDish::Pg::pg-replace-placeholder($statement);
my $result = PQprepare(
$!pg_conn,
$statement_name,
$munged,
0,
OpaquePointer
);
my $status = PQresultStatus($result);
if $status != PGRES_EMPTY_QUERY | PGRES_COMMAND_OK | PGRES_TUPLES_OK | PGRES_COPY_OUT | PGRES_COPY_IN {
self!set_errstr(PQresultErrorMessage($result));
die self!errstr if $.RaiseError;
return Nil;
}
my $info = PQdescribePrepared($!pg_conn, $statement_name);
my $param_count = PQnparams($info);

my $statement_handle = DBDish::Pg::StatementHandle.bless(
DBDish::Pg::StatementHandle.CREATE(),
*,
:$!pg_conn,
:$statement,
:$.RaiseError,
:dbh(self),
:$statement_name,
:$result,
:$param_count,
);
return $statement_handle;
}
Expand Down Expand Up @@ -433,7 +446,7 @@ class DBDish::Pg::Connection does DBDish::Connection {

class DBDish::Pg:auth<mberends>:ver<0.0.1> {

sub pg-replace-placeholder(Str $query) is export {
our sub pg-replace-placeholder(Str $query) is export {
PgTokenizer.parse($query, :actions(PgTokenizer::Actions.new))
and $/.ast;
}
Expand Down

0 comments on commit c4f91e1

Please sign in to comment.