No Baby Moose Steps
Almost some code day here in the Moose-Pen.
Not much code today unlike yesterday's post where I changed eighteen files and checked in one new one.. Today I had a look at how I am going to proceed with Driver::DBI, With all the changes I made to Accessor.pm I have some rethinking to do with Driver::DBI
I will now have to
- work with a results class
- capture any errors into the appropriate attribute of that class
- set the appropriate class attributes on success
- set the appropriate class meta attributes and
- check for any flags that may be set for special processing
Not a big deal really I thing this will really work out well. As for testing I think I will keep 10_crud_basic.t for now and just get that one all passing before I move onto more complicated queries and proccessing.
The good thing is I do not have to do any validation on the parameter being passed into the 'execute' sub, as I do all that validation on the Accessor.pm side of things first.
I was just thinking I may want to move more validation into Accessor.pm as I go along right now I can think of test that I can move into the Accessor.pm part of the code. In my older Accessor code I just ignored container keys that did not match up to any defined element in the DA class. I could do this even before I get to the Driver. For now though I will just make a note this for future reference.
So the only coding today in Driver::DBI was I got rid of the '_sql' sub and modified the 'execute' sub as follows;
sub execute {
my $self = shift;
my ( $result, $action, $dbh, $container, $opt ) = @_;
my $sql;
if ( $action eq Database::Accessor::Constants::CREATE ) {
$sql = $self->_insert($container);
}
elsif ( $action eq Database::Accessor::Constants::UPDATE ) {
$sql = $self->_update($container);
}
elsif ( $action eq Database::Accessor::Constants::DELETE ) {
$sql = $self->_delete();
}
else {
$sql = $action->_select_clause();
}
$result->query($sql);
$self->_warn("SQL=$sql")
if $self->da_warning();
return 1
if $self->da_compose_only();
my $sth;
eval {
$sth = $dbh->prepare($sql);
};
if ($@) {
$result->is_error(1);
$result->error($@);
return 0;
}
…
In the above you can see I am still calling out to a sub to create the SQL for the four translated SQL actions, this is more to make my code much easier to read rather than for re-use. The first thing I do after I have the SQL is set the attribute on the 'result' class with the value.
You will see next a check of the 'da_warning' flag which will call up this sub
sub _warn {
my $self = shift;
my ($message) = @_;
warn("Database::Accessor::Driver::DBI: $message ");
}
I am still debating at this point wheather on not to put some sort of value on this flag to give more or less debugging info as one does with the 'trace' attribute found in DBI.
Next I simply return true if the da_compose_only flag is set as all I am asking for is the query that would have been run, this will come in handy in advanced testing.
I do an eval around the 'prepare', to check for an error and if one is found I set the attributes on the results class and return false.
So that is it for now as I want to check the pre-validation idea out before I get too far along. Back to Accessor.pm for the next post maybe?
Leave a comment