Still little Moose

More code than none day here t the Moose-Pen.

Not much got done in yesterday's post-ette and today just a little more me thinks. Well the first thing I had to get done to-day was start work on the Driver::DBI code a little and I started by importing the constants from Database::Accessor so here is what the Driver::DBI looks like;


package Database::Accessor::Driver::DBI;
# ABSTRACT: The DBI (SQL) driver for Database Accessor
# Dist::Zilla: +PkgVersion

# DADNote: the DAD writer should use this set of constants will save time and because DA has to me installed this will be as well

use Database::Accessor::Constants;
use Moose;
with(qw( Database::Accessor::Roles::Driver));

sub execute {
my $self = shift;
my ( $type, $dbh, $container, $opts ) = @_;
return 1;

}
sub DB_Class {
my $self = shift;
return 'DBI::db';
}


You might notice a few little things in the above, I have added in some Dist::Zilla comments for Abstract and Package version as I want to use Dist::Zilla from the start. Next you will see a #DADNote which is a note I adding in as I go along so at one point I can gather them all together and then use them to write up a DAD writers guide. I will be using the Pod::Weaver [CommentString] section plug in for this at a much later date. The plan being once I get this all up and testing nicely I collect all these notes then write up a POD and remove them from my code.

As the note explains I can import the DA constants without worry as I need DA installed to use the driver. Now I will also need to create a constants file for my SQL code as well. I do not really have to but it is good coding style and will stop or at least limit the number of typos that could be introiduced.

Fortunately for my old pure perl version of Data::Accessor has an SQL constant file so I just need to add it into my project folder in the name-space 'Database::Accessor::Driver::DBI::SQL' and make the appropriate changes to that file and then add it to the Driver::DBI;


use Database::Accessor::Constants;
++use Database::Accessor::Driver::DBI::SQL;
use Moose;

I next had to decide how to handle the incoming commands from Accessor into the 'execute' sub and I went for now just a simple if fall though like this

sub execute {
my $self = shift;
my ( $type, $dbh, $container, $opts ) = @_;
my $sql;

if ( $type eq Database::Accessor::Constants::CREATE ) {
$sql = $self->_insert($container);
}
elsif ( $type eq Database::Accessor::Constants::UPDATE ) {
$sql = $self->_update($container);
}
elsif ( $type eq Database::Accessor::Constants::DELETE ) {
$sql = $self->_delete();
}
else {
$sql = $self->_select_clause();
}
my $sth;
eval {
$sth = $dbh->prepare($sql);
$sth->execute();
};
if ($@) {
#warn( "error=" . $@ );
}
}


Simple enough really and when I get the '_insert' sub written I will most likely pass my first test, in '10_crud_basic.t' to pass, however with the above code I am painting myself into a corner as I will not be able to do any more extensive testing as I mentioned in my last post.

Before I get to far along I am going to make a change to Database::Accessor first by adding in some sort of functionality where I can call a CRUD operation, skip the execute and return just the raw query in this case the SQL. If I add that in I will really be able to test the more advanced SQL calls and features.

Anyway I have tomorrow topic and a little code written today.
maxresdefault33.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations