Syntactic Confectionery Delight | |
PerlMonks |
Re: Abstracting sqlby chunlou (Curate) |
on Jul 31, 2003 at 07:39 UTC ( [id://279506]=note: print w/replies, xml ) | Need Help?? |
When you need User_data_mysql, User_data_postgre, User_data_flatfiles, etc. or something like User_Data_Mysql::get_data, polymorphism will be a good option to consider. One reason you don't want to do $sql="select foo,baz,bar from qux where foo=1 and baz=2 order by bar limit 0,20" is that the DB becomes responsible for most of the error handling, whereas something like getdata(\%data) helps trap errors before it even reaches the DB You probaby would want to start with the design of a generic data access module all your app specific modules will use. That is, something like:
App01.pm --> MyDB.pm --> DBI.pm --> MySQL MyDB will most likely have to be OO, since at least you have the persistent data object "DB Handler" hanging around. OO way is more convenient to handle it. MyDB won't have any table and app specific methods. Only a few generic database methods, such as "insert_row," "delete_row," "show_table_asXML," "show_table_asArrays," etc. It would work if you confine your methods to do the SQL stuff that are more or less supported by every DB. If not, you might want to split out some DB specific methods into some DB specific module. At application level, you might want to create two types of modules, one is tables specific but not business logic specific (say, MyAppTable.pm); the others are business logic specific (such MyAppThumbnail.pm, MyAppMessageBoard.pm, etc.). Something like:
MyAppMessageBoard --> MyAppTable --> MyDB --> DBI --> MySQL If you want to create methods with hardcoded parameters such as "get_user_data_by_id," "get_user_data_by_name," etc. (sometimes there're good reasons for that, such as being backward compatible with legacy code), you could try to see if you can design in such a way that you can generate the methods as automatically as possible, such as (not a direct example but it shows the idea):
In Section
Meditations
|
|