|
DBRPresentation
DBR Presentation for San Diego perl mongers
DrJays.com Inc. says:
DBR: A sordid tale( or: How I learned to stop worrying and love the ORM ) DBR is a Database wrapper library; a shameless reinvention of the wheel.
"Any customer can have a car painted any color that he wants, so long as it is black." Intended audience: People with medium to large size projects with a high number of tables / records, and an aversion to pain. Get the code: Definitions
What is an ORM anyways?
Why Not an OODBMS?
Why not do it the old fashioned way, with SQL?
Value quoting has to be perfect Some of your codebase can use SQL, while the rest can use DBR. They can play nice together. So... Why DBR?
See CodeComparison1 See CodeComparison1 less code is easier-to-understand code status 'active' is legit, status 'fubar' is bogus Uses blessed arrayrefs internally, yielding a much smaller memory footprint Stop worrying about executing thousands of tiny queries inside loops, It's taken care of transparently. Small is beautiful Who cares what fields you will use... just use them already. Currently supports Mysql and SQLite. PostgreSQL and Oracle support coming soon. How's it better than those other libraries?DBR is not better... But it just might be better for you. Here's why: RoseDB::Object and DBIx::Class are great libraries, but they are too generic for some jobs, especially if those jobs are big. Furthermore, they:
Lots of tables? have fun hand coding hundreds of modules. Supports nested transactions (emulated for Mysql), and auto rollback. Album::Manager->get_albums ( query => price => { lt => 1.00 } ); # Drugs $schema->resultset('Album')-> search( { price =>{ '<' => 1.00 } ); $dbh->album->where( price => LT $value ); #Pfm DBR does. But hey, they don't stop you from writing it your dammed self
How does it work?The concepts:
Very fast. Some DBR Features
You can use DBR in two different levels: Basic and Advanced (Hostname, username, password etc) Stop relying on the database to check your numeric ranges and string lengths (grr mysql) $dbrh->tablenameA->where (myfield => 'foo', 'relationshipB.fieldname' => 'bar'); A different approach to value enumeration that you might actually use Certain values returned are actually overload objects... With handy consequences.
No, really... it's as easy as falling off the sofa. All your old date mangling/ beautifying code will vanish. Returns false (overload dummy object) when it experiences an error, but won't crash your program with a null ref error.
Retrieving 1 Miiiillion records? no problem. DBR will retrieve records as you consume them, not all at once. Stop worrying about query performance for nested loops, start focusing on your code.
Edit relationships, fields, DB replicas (instances) etc. DBR AdminA curses application - Get started in a hurry ┌ DBR Admin Main Menu ───────────────────────────────────────────────────┐ │< Close > │ │ │ │Enums │ │Schemas │ │ │ │ │ │ │ │ ____ ____ ____ _ _ _ │ │| _ \| __ )| _ \ / \ __| |_ __ ___ (_)_ __ │ │| | | | _ \| |_) | / _ \ / _` | '_ ` _ \| | '_ \ │ │| |_| | |_) | _ < / ___ \ (_| | | | | | | | | | | │ │|____/|____/|_| \_\ /_/ \_\__,_|_| |_| |_|_|_| |_| │ │ │ │Global keys: │ │Control-Q: Quit │ │Tab: next input widget │ │Enter or right-arrow: select │ │Up/Down arrows: previous/next item in current input widget │ │Mouse supported depending on OS (Linux - probably, Mac - probably not) │ └────────────────────────────────────────────────────────────────────────┘ Value TranslationValue translation is very important. Most pre and post processing code is dedicated to translating values going into or coming out of the database. Some very handy value translators
What is an Enum?Enumerated values are very handy, because:
Smartvalue examplesALL of these translators return "smart" overload objects that morph into the best value for the job at hand. Some examples:
prints: $1,000.00 TRUE prints 09/16/08 08:51:31 PST TRUE prints: 09/23/08 08:51:31 PST prints 09/16/08 00:00:00 PST prints 09/16/08 23:59:59 PST prints: Ready to ship TRUE TRUE And so on... Query OptimizationField prefetchDBR automatically profiles the fields you use. It saves this information and uses it in subsequent queries. the below code results in exactly one DB query # get all the cars salesman #3 sold
my $cars = $dbrh->cars->where(salesman_id => 3);
while( my $car = $cars->next ){
print $car->VIN . "\n";
}
Relationship PrefetchWhen calling a relationship accessor, DBR reads ahead and fetches the relationship records it expects you'll use. The below code results in exactly Two DB queries. # get all the cars salesman #3 sold
my $cars = $dbrh->cars->where(salesman_id => 3);
while( my $car = $cars->next ){
print $car->VIN . "\n";
while(my $option = $car->options->next){ #Just works the way it should
print "\t" . $option->name . "\n";
}
}
More ExamplesSee examples directory Future Enhancements
Dynamic fields attached to an object, stored in a records-based table structure. Q&A TimeReady... GO |