Moose on the edge

Now in the Moose-pen we are coming to the home stretch of my Database::Accessor Classes, starting with 'Gather'. This one represents a fields in an in SQL 'Group by' clause, and in Mongo 'Group' stage.

Looking at it it really is just an array of predicate clauses so I think I can just drop this class altogether, which is always a good thing. So out it goes and the test as well. So that was easy.

Next it 'Filter' which is a 'Having' clause in SQL and I not 100% sure what it is in Mongo most likely something to do with group and project or maybe match? I guess it is up to the Mongo DAD writer to come up with that one.

Like 'Gather' this is just an array of predicates or the same as my 'Condition' class so I can just drop that one and its tests as well. So that is two classes I do not really need.

Now we come to the last one 'Sort' or 'Order by' in SQL and I think 'Sort' in Mongo depending on which version you are using. Now this one is again just an array of element but there is a little extra you can sort results like this in SQL


SELECT last_name,age FROM users ORDER BY last_name DESC, age ASC

and like wise in Mongo

db.collection.find( { $query: {}, $orderby: { last_name: -1 ,age : 1 } } )

so it cannot be just a array of elements as my elements do not have an attribute that expresses ascending or descending, so I have to add that in but how??
Well I think it is time that I bight the bullet and 'extent' a Moose class rather than go though the role approach I have been using.
To accomplish this is really quite simple and here is all I need to do is use the apply named sugar function 'extents' like this;

package Database::Accessor::Sort;
use Moose;
extends 'Database::Accessor::Element'

Nice neat an clean unlike mucking with the ISA@ and without the unwanted problems of 'use Base' and perhaps having all sorts of common shared data, depending on your Perl version. Now that I have all the Attributes and Methods of the Elements class all I need to do is add in one new attribute like so

has order => (
is => 'rw',
isa => 'Order',
default => Database::Accessor::Constants::ASC
);

Now I added in yet another new type called 'Order', that should be old hat by now so no need for the details on that one. You will also see that I set the default to a constant. That will cause a error at this stage as I have yet to import that field into my package. Fortunately I can do this in the Accessor class and no way down here so in that package I just add


use Moose;
with qw(Database::Accessor::Types);
++ use Database::Accessor::Constants;
use Moose::Util qw(does_role);
...

and I am off to the races.

Now onto other things.

moose-on-the-edge.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