Defining a data structure - is there a standard/best practice way?

J

Justin C

I am writing a module which interacts with a database. It is only
able to perform certain types of query, and return data in certain
formats (I'm not opening up the whole of DBIx for users of the
module - if they want to do more then my module isn't for them).

So the user creates a new query using my module:
my $q = Module::DB::Query->new({
type => 'select',
table => 'products',
cols_wanted => [qw/foo bar baz/],
criteria => [
{ column => 'id', value => 7, operator => '>' },
{ column => 'description', value => 'lorem', operator => '=' },
],
return => 'aoa' # array of arrays
});

I'm looking for a way to describe all the permutations for the
different queries my module can handle, in a way that can be used
to test that I have all the data I need to run the query.

I started with this:

my %supported = {
select => {
table => 'SCALAR',
cols_wanted => 'ARRAY',
criteria => [{
column => 'SCALAR',
value => 'SCALAR',
operator => 'SCALAR'
}],
}
update => {

}
insert => {

}
}

But as you can see it very soon breaks down. There's no way in my
scheme for 'criteria' to be specified as an array of hashes.

I'm sure this problem must have come up before and a solution
exist. Maybe I'm just thinking about this the wrong way, and
someone could steer my thinking in the right way?

Thank you for your suggestions.


Justin.
 
C

ccc31807

I am writing a module which interacts with a database. It is only
able to perform certain types of query, and return data in certain
formats (I'm not opening up the whole of DBIx for users of the
module - if they want to do more then my module isn't for them).

I frequently have great big globs of data that I need to print/export in particular ways. Aside from the fact that the requirements of your printing/exporting activity is EXTREMELY important, I customarily start with stuffingall the data in a single hash \ref. I then print the hashref in a simple ASCII file, which allows me to look at the data and determine how I want to print it out.

For example, a very simple example, suppose my data file looks like this:
ID,NAME,COLLEGE,PROGRAM,LEVEL,TERM,COURSE,GPA,CREDITS,RESTRICTIONS

use Text::parseWords;
my %bighash;
#first, grab all the data and stuff it into memory
while (<IN>)
{
my($ID,$NAM,$COL,$PRO,$LEV,$TER,$COU,$GPA,$CRE,$RES)
$bighash{$COL}{$LEV}{$PRO}{$TER} = {
ID => $ID,
NAM => $NAM,
COL => $COL,
PRO => $PRO,
LEV => $LEV,
TER => $TER,
COU => $COU,
GPA => $GPA,
CRE => $CRE,
RES =>$RES, };
}
# now, print all the data out
while (<OUT>)
{
foreach my $COL (sort keys %bighash) {
foreach my $LEV (sort keys %{$bighash{$COL}}) {
foreach my $PRO (sort keys %{$bighash{$COL}{$LEV}}) {
foreach my $TER (sort keys %{$bighash{$COL}{$LEV}{$PRO}) {
foreach my $key (sort keys %{$bighash{$COL}{$LEV}{$PRO}{$TER}) {
print OUT " $key =>$bighash$COL}{$LEV}{$PRO}{$TER}{$KEY}\n";
}}}}}}

Yeah, I know this is mindless, but that's the point. Once you do the mindless part, you can study your output requirements and construct one or more hashes to conveniently output what you need.

Anyway, this technique works for me, I use it almost every day.

CC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top