Have you even tried it yet? Look ... like i said before, whether you pass the SQL around or you pass the
resulting data structure around ... it's all going to be spaghetti code unless you take care for it not
to be. Here were i work, we use something like this to create a select box:
my $fields = [ qw(id name) ];
my $table = $dbh->selectall_arrayref('
select id, name from some_table
');
my $select = $self->select_prep($fields, $table, $selected_id);
# now "send" $select to HTML::Template
select_prep takes a 2 dimensional array and returns a datastructure suitable for HTML::Template.
However, i have had much maintainability success by using Class::Phrasebook::SQL in some of my
past personal projects. I store the SQL queries in an XML file that is parsed into a hash. The user
passes in the key as a query parameter:
# param might contain go_director=42 (id for the director)
# or maybe go_genre=72 or go_writer=150
if (my ($choice) = grep /^go_/, param()) {
my $query = $BOOK->get($choice);
print make_table($dbh, $query, param($choice));
}
Where make_table is a thin wrapper for my DBIx::XHTML_Table module, which takes
a SQL query string and returns the results already wrapped in an XHTML table. Here is an entry from the
XML Phrasebook:
<phrase name="go_director">
select movie.id,movie.title,movie.year,movie.id as img
from movie inner join director_xref on movie.id=director_xref.movi
+e_id
inner join director on director_xref.director_id = director.id
where director.id= ?
order by movie.year
</phrase>
The SQL is tucked away in an XML file (i can pretend they are stored procedures ;)) and my code is free
of SQL. What's not maintainable about that? Now, my design of what i do with the SQL could be improved,
but that's my doing ... not the technique itself.
Why are we having this conversation ... go ... experiment! Try new things. Even if your gut tells you
it won't work, it's all good experience. :)
|