Item Description: Easy interface to SQL database

Review Synopsis:

This review was requested by one of the module's authors, belg4mit. A while ago, we discussed some easier ways of using DBI, and of course his EZDBI and my DBIx::Simple were mentioned. I will not discuss DBIx::Simple in this review. If anyone wants to review it, please do so.

What is EZDBI?
EZDBI (Easy DBI) is a module that provides functions for database connections, using DBI as its backend. Many people find DBI either too hard or too much work (why fetchrow_arrayref if you can have something shorter?), and several modules try to end that problem. EZDBI uses no object orientation, so anyone without too much programming experience can install and use the module immediately. I review version 0.1.

Its name
Normally, modules in CPAN have some sort of hierarchy. It isn't always consistent or well-chosen, but most modules are grouped in top-level namespaces. DBI extensions should be in the DBIx:: namespace (that's right, not even DBI::), but this one uses a top-level namespace of its own. It is hard to find when searching CPAN: when using keywords like 'Easy' and 'Simple' in a module-name search, EZDBI is not one of the results. Even when you look for modules with 'DBI' in the name, EZDBI is unclear until you pronounce it letter by letter in English (which may not be a natural thing to do for those who don't natively speak English). There is a module called EasyDB, one called DBIx::Easy and one called EZDBI. That isn't very handy.

Connecting to a database
The connecting function is simply called Connect. It's a straightforward function call that takes either a DBI DSN or named arguments.

Of course, the most important thing you will want to do with a database module is sending queries to the database and getting results. EZDBI provides a function for each of the most used SQL commands: Select, Insert, Update, Delete. If you have a normal database select like SELECT foo FROM bar, you would put the Select part outside of the quotes and have Select 'foo From bar';. I changed FROM to From to match the ucfirsted Select.

The Select function is rather intelligent. By default, it fetches everything and returns it, but it can also return an object that can be used to fetch one row at a time. Insert has a nice ??L shortcut that is turned into (?, ?, ?, ...) with as many question marks as the number of remaining arguments.

There is Sql for when the given functions cannot execute the SQL you want it to. Unfortunately, this uses DBI's do, so don't expect to be able to get information out of it (For example with mysql's SHOW TABLES command).

Every module needs documentation. Without the documentation, I wouldn't be able to write this review, as I didn't actually test the module thoroughly. I did not have to, as the manual provided almost everything I wanted to know, and the source gave me the rest of the clues. Although the programming style itself is not the one I like, the manual is very clear and easy to read. It is written with beginning programmers in mind, so the EZ is worth its bits. I especially like the vivid examples.

However, I found something in the documentation that bothers me. EZDBI's manual states that EZDBI takes care of ? placeholders, but this module only expands its special ??L placeholder (which is in turn only for Insert). Placeholder substitution is performed by DBI's execute(), but up to three times, EZDBI takes credit for what DBI does.

Multiple databases
This is quite a hassle with EZDBI. You can have multiple databases, but you'll have to use Use, which can be compared to Perl's own select that selects a filehandle. This way, copying data from one database to another (which is not uncommon: a lot of people migrate from MySQL to Postgres, for example) has to be done using temporary variables and a lot of Use calls. I don't think this is EZ, object orientation would be so much better for this.

When the Perl interpreter ends, it will destroy all variables, including the DBI object that is stored inside of EZDBI. That way, database connections are properly terminated. If the end of the interpreter never happens (i.e. when using PPerl or mod_perl), you're stuck with the database connection even after your script ends. You will have to explicitly call Disconnect. EZDBI is not object oriented, so there is no object to destroy automatically when it goes out of scope. This is potentially very dangerous.

I wouldn't use EZDBI myself, but not because the module is bad. It's a very good module, but I happen to like object orientation, and I prefer raw SQL to semi-abstracted SQL. Maybe I'll steal the ??L idea one day, though. If object orientation is too hard for you, or if you want to do things the easiest way, EZDBI is perfect for you. Don't forget to Disconnect explicitly when using EZDBI in mod_perl, because otherwise someone else might be able to access to your database!

This is the very first module review I've ever written. Please tell me if I did anything wrong (reply).