Hi! I'm working on a little project to represent database tables as simple classes. For example, if you had a table with the following definition ( in sql )
create table my_database..foo ( bar int , baz varchar(10) )
Then you would have a simple class my_database::foo with members int and baz. I'd like to solve this by having a Table package which handles the creation of the my_database::foo package for you. IE as a user you'd simply
use Table ( { -dbase => "my_database", -table => "foo" } ); my $f = my_database::foo->new( { bar => 1, baz => "neat" } );
</code> The Table class's import method() will query the local database my_database for the columns of the foo table, and then (I use Class::MethodMaker ) construct the class defintion of my_database_foo ( at _compile_ time!). So, specifically,
#Table's import method sub import { my ( $class, @args ) = @_; my $href = $args[0]; #sanity checking on href... my ( $dbase, $table ) = ( $args->{-dbase}, $args->{table} ); #get_columns returns a reference to an array #containing the columns. my $columns = get_columns( $dbase, $table ); #this is where I get stuck. package $dbase::$table; @ISA = qw(Table); use Class::MethodMaker; make methods get_set => $columns, new_hash_init => 'init'; #broquaint suggested this, but he didn't like it #"this is just so very very wrong" --broquaint #the idea is to export the $dbase::$table package #definition to the caller *{"main::$dbase::$table::"} = *{"$dbase::table"}; }
Perhaps another way to solve this would be to subclass the Table package , passing it the database and table name. Perhaps this is better, because ( this is another question ) would I ever be allowed to do something like this?
use Table ( { -dbase => "mydb", -table => "foo" } ); use Table ( { -dbae => "someotherdb", -table => "bar" } ); #results in the exporting of 2 class definitions: #mydb::foo and someotherdb::bar
as opposed to
package mydb::foo; @ISA = qw(Table); package someotherdb::bar; @ISA = qw(Table);
Anyhow, hope this isn't too rambling, thanks all in advance.

In reply to constructing dynamic class definitions at compile time (from schema definition) by sozinsky

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.