Relax it is Just Moose

Well it clean-up and re-factor day in the Moose-pen. So no new Moose Majick today just cleaning up and adding in stuff that I have left out (forgotten actually) and moving things about.

Well the first thing I did was finish off the last three of my remaining Accessor attibutes, 'gathers', 'filters' and 'sort' or if you are SQL inclined 'group by', 'having'. and 'sorts'.

The 'gathers' and 'sorts' attributes are just a pair of 'ArrayRefofElements' which I covered off in this post and types and the 'filters' is a 'ArrayRefofConditions' which I covered in this post. I did of course spend a little time making sure that my DAD role attributes matched up with the accessosr ones and as this is me programming I created two new test cases, 37_gathers.t that handles filters as well, and 39_sorts.t

No for what I have left out, the 'Database::Accessor::Function' and 'Database::Accessor::Expression' classes which handle operations that are too complex for a simple 'Element' (Field) or 'Param' class. For example this SQL


SELECT concat(first_name,' ',last_name) from users;

'concat' is the function, and this

SELECT price * local_tax as total_price from sale;

'*' is the expression,

I also need a new type for 'Expression' my new Expression class is another where I use a new constant 'EXPRESSION' to do simple string validation like so;


subtype 'Expression',
as 'Str',
where { exists( Database::Accessor::Constants::EXPRESSION->{ uc($_) } ) },
message { "The Expression '$_', is not a valid Accessor
Expression!"._try_one_of(Database::Accessor::Constants::EXPRESSION()) };

Now for some re-factoring. Both of the new classes have a 'left' and 'right' element with an operator like my 'Predicate' class but without the 'condition' and 'operator' attributes. So if I strip out the common parts into this role;

{
package Database::Accessor::Roles::Comparators;
BEGIN {
$Database::Accessor::Roles::Comparators = "0.01";
}
use Moose::Role;
use MooseX::Aliases;

has left => (
is => 'rw',
isa => 'Element',
required => 1,
coerce => 1,
);
has right => (
is => 'rw',
isa => 'Param|Element',
required => 1,
coerce => 1,
);
has open_parenthes => (
is => 'rw',
isa => 'Bool',
default => 0,
alias => [qw(open open_paren)]
);

has close_parenthes => (
is => 'rw',
isa => 'Bool',
default => 0,
alias => [qw(close close_paren)]
);
1;
}


Then re-write 'Predicate', 'Function' and 'Expression' classes to this

{
package Database::Accessor::Predicate;
use Moose;
with qw(Database::Accessor::Roles::Base
Database::Accessor::Roles::Comparators);
use MooseX::Aliases;

has operator => (
is => 'rw',
isa => 'Operator',
default => '='
);
has condition => (
is => 'rw',
isa => 'Operator',
default=>'='

);
1;
}
{
package Database::Accessor::Function;
use Moose;
use MooseX::Aliases;
with qw(Database::Accessor::Roles::Base
Database::Accessor::Roles::Comparators);

has 'function' => (
isa => 'Str',
is => 'rw',
required=>1,
);

1;
}
{
package Database::Accessor::Expression;
use Moose;
use MooseX::Aliases;
with qw(Database::Accessor::Roles::Base
Database::Accessor::Roles::Comparators);

has 'expression' => (
isa => 'Expression',
is => 'rw',
required=>1,
);
1;
}


all I need to do is another test case 19_function_expression.t for function and expression, re-run all my cases and I done for today, time to relax and see how many credits my Tau station bot mined today.

83d427f711cf9c123217df423e8bf5b0.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