in reply to EZDBI is an easier interface to SQL databases

Here's the equivalent code using Class::Tangram:
#-------------------------------------------------------- # define "Person" objects package Person; use base qw(Class::Tangram); $schema = { table => "names", fields => { string => [ qw(first last) ] } }; #-------------------------------------------------------- # define which objects we have in our database package Project; use Tangram::Relational; $dbschema = Tangram::Relational->schema ({ classes => [ 'Person' => $Person::schema ]}); sub schema { $dbschema }; #-------------------------------------------------------- # the main program package main; # connect to the database. my $storage = Tangram::Relational->connect(Project->schema, $dsn, $u, $p); # insert a new Person $storage->insert(Person->new( first => "Harry", last => "Potter" )); # $remote_person refers to a person in the database, # for select queries. my $remote_person = $storage->remote("Person"); if ($storage->select ( $remote_person, $remote_person->{first} eq "Harry" )) { print "Potter is IN THE HOUSE.\n"; } # iterate through all rows for my $person ($storage->select("Person")) { print $person->last.": ".$person->first."\n"; } # delete every row matching "Potter" $storage->erase ($storage->select ( $remote_person, $remote_person->{last} eq "Potter" )); if ($storage->select ( $remote_person, $remote_person->{last} eq "Potter" )) { die "Can't get rid of that damn Harry Potter!"; }

Tangram is very cool. Check out the guided tour. Once you divorce yourself from the concept of tables and rows and start thinking in terms of objects, life becomes a lot easier...

Replies are listed 'Best First'.
Re: EZDBI is an easier interface to SQL databases
by Dominus (Parson) on Oct 11, 2001 at 19:55 UTC
    mugwumpjism says:
    Once you divorce yourself from the concept of tables and rows and start thinking in terms of objects, life becomes a lot easier...
    Maybe you could elaborate on why you think so. From here, looking at the 30-line program that you wrote that is "equivalent code" for my 11-line program, the reasons are far from obvious.

    What's the benefit that justifies a 2-3x increase in code size?

    --
    Mark Dominus
    Perl Paraphernalia

      Maybe you could elaborate on why you think so. From here, looking at the 30-line program that you wrote that is "equivalent code" for my 11-line program, the reasons are far from obvious.

      What's the benefit that justifies a 2-3x increase in code size?

      I agree that it doesn't necessarily make life any easier, and the code increase (line count-wise) is alot less extreme if you ignore the Tangram schema setup and the comments and the line breaks for readability. But the benefit is that its a tool for 'object persistence'. Which puts Tangram and EZDBI in entirely different categories anyway. But I do appreciate the examples of both :)

      Update:Re: Dominus's reply - I also said 'ignoring the Tangram ... setup', which is everything up to the 'package main'. Which you can count or not. I figure there's an argument for not counting it since its a constant that, once set up, you can do as much inserting/deleting/updating/selecting as you like. So IF you don't count it, then the amount of code is not much greater.

      Since the original program didn't have any objects, I don't see how that could be a benefit in this case.

      That was my point, the two modules are for entirely different things :) Making DBI EZ or storing objects in a database. And how can you say its not a benefit in this case just because your script didn't have any objects. I kind of think of Harry Potter as an object :)
      Besides, if you're going to make that sort of argument, then I submit that this script is far superior:

      use HarryPotter; InsertHarryPotter(); IsPotterInTheHouse(); PrintHarry() for SelectPotter(); DeletePotter(); SelectPotter() and DamnPotter();
        Says runrig:
        the code increase (line count-wise) is alot less extreme if you ignore the Tangram schema setup and the comments and the line breaks for readability.
        I did ignore the comments and the line breaks. Whether you count them or not, it is still three times as long.

        the benefit is a tool for 'object persistence'
        Since the original program didn't have any objects, I don't see how that could be a benefit in this case.

        --
        Mark Dominus
        Perl Paraphernalia