Well how about some coding today?

Lets look at the first little bits from my last post the 'View' and 'Element' and I am going to focus in on using it with SQL in this post. So we have this;





{

view => 'person',

elements => [

{ name => 'name' },

{

name => 'street',

view => 'address'

},

{

name => 'country'

view => 'address'

}

{

name => 'city',

view => 'address'

}

],

}





has view => (

is => 'rw',

isa => 'Str',

); has elements => (

is => 'rw',

isa => 'ArrayRef',

);





{

view => 'person',

alias => 'Me',

...



Now the question, 'How am I going to express the above in the code of my Data Accessor?' Do I just have one attribute that stores the above in a hash-ref? Do I break them down into two attributes a string and an array-ref of hashes Like this;Well the 'View' in SQL terms just the name of the Table (or View) that we are interested in, simple enough. Looking a little deeper most if not all implementations of SQL add in the concept 'Alias' for any table. So I can't have a simple scalar attribute I need a hash-ref at least. So my model expandsNow looking at an 'Element' which corresponds to a 'Field' in a 'Table' in SQL and I have already expanded my model by making it an array ref of 'Elements'. In SQL a Field can also have an 'Alias' and one can use expressions, functions, constants or even other SQL queries as a 'Field'. Now add on top of that the syntax for a field my change when they used in the differing 'SQL Clauses' one gets the picture that the Element is not simple at all.

What I did in the Data Accessor was to separate out the out these components into something like this





has view => (

is => 'rw',

isa => 'Object',

); has elements => (

isa => 'ArrayRef',

is => 'rw',

);





{ package

DA::View;

use Moose;

has 'name' => (

required => 1,

isa => 'Str'

is => 'rw',

);

has 'alias' => (

required => 1,

isa => 'Str'

is => 'rw',

);

}

{ package

DA::Element;

use Moose;

has 'name' => (

required => 1,

isa => 'Str',

is => 'rw',

);

has 'alias' => (

required => 1,

isa => 'Str',

is => 'rw',

);

}





sub sql {

my $self = shift;

if ( $self->alias() ) {

return $self->name() . " AS " . $self->alias();

}

else {

return $self->name();

}

}





sub _execute {

my $self = shift;

my $delimiter = " ";

my $sql = "SELECT "; foreach my $element ( @{ $self->elements() } ) {

$sql .= $delimiter . $element->sql();

$delimiter = ", ";

}

return $sql ." FROM ". $self->view()->sql();

}



and two packages like thisNow I need some way to express the above in SQLish so lets on a sub like this to eachand I think we are in business. Just need to put them together somehow and I have defined that before with the '_execute' clause and in there I just do thisAnd we are off to the races. Now to type this up and write up a test or two and see what come up.



