|There's more than one way to do things|
Module Design Review -- DB mini languageby elusion (Curate)
|on Feb 10, 2003 at 20:30 UTC||Need Help??|
In an attempt to rid my code of SQL, which I find generally distasteful, I've designed a mini language (but have not implemented it yet) for accessing databases. I've submitted it here for review. Also, I'm not sure about the name, so I'd like some thoughts on that as well.
Update: I apologize for the huge font. A runaway h1 tag was to blame. My browser (safari) did not render it, so I didn't know.
DBIx::DAL -- Database Abstraction Language
Let's face it: SQL is ugly, especially when a query uses data from multiple tables. DBIx::DAL is an attempt to solve this problem. This module implements a mini language that is converted to SQL. All SQL is tailored to MySQL, so it may or may not work with other databases. There is no support for database creation, only for accessing, storing, and changing data.
To demonstrate how DAL works, we will use a table of colors that have an id and a rating (1-10). All queries will be done from this table.
First off you want to want to select the rating of the color green. That would be done using this command:
As you can see, the table name comes first, followed by the where clause and, finally, what it is that you want to select. We used a binding for the value of the color (the ?) that allows us to pass the variable we want to use in order to make sure it's quoted right. To select all the columns from the table, don't specify any: table().
All results from the query are returned, as an array of anonymous arrays, which correspond to the different rows returned.
If you want to alter the data in the table, the command is very similar. Someone has decided that they don't like green so much, so we'll change its rating to 8.
That has the same order: table name, where clause, and column selection. In fact it is identical to the above command, except that here we do an assignment, using =. We even use a binding for the value, as we did before.
Statements in the where clause are seperated by commas, which mean 'and'. If you want an 'or', use the keyword or. Junctions, like those coming in perl 6, are also supported -- 'or' as | and 'and' as &.
It's important to note here that we can nest calls. In other words, say we have a table of items which each have a color.
Now we decide we want to know the rating of the color of the sky. Instead of making the query much more complex by using identifiers for each table name, we do this:
Now someone wants to add a leaf to the items table.
To insert items, leave out a where clause and do an assignment, similar to an alter. DAL will automatically seperate the values into the different rows (if you specify a column, you must pass a value for it, even if that value is ''). Optionally, you can surround the different rows with square brackets.
Matt Diephouse <email@example.com>