Fey - Better SQL Generation Through Perl
use Fey::Literal::Function; use Fey::Placeholder; use Fey::Schema; use Fey::SQL; my $schema = hand_waving(); my $user = $schema->table('User'); my $group = $schema->table('Group') my $select = Fey::SQL->new_select(); my $func = Fey::Literal::Function->new( 'LCASE', $user->column('username') ); $select->select( $user->columns( 'user_id', 'username' ) ) ->from( $user, $group ) ->where( $group->group_id, 'IN', 1, 2, 3 ) ->and ( $func, 'LIKE', 'smith%' ); print $select->sql($dbh);
Fey distribution contains a set of modules for representing the components of a DBMS schema, and for dynamically generating SQL queries based on that schema.
Loading this module does nothing. It's just here to provide docs and a version number for the distro.
The goal of the core
Fey distro is to provide a simple, flexible way of dynamically generating complex SQL queries in Perl. Other packages build on top of this functionality to create a complete ORM (
If you're interested in an ORM, take a look at the
To generate SQL with Fey, you first need to create a set of objects representing the tables and foreign keys in your schema. The simplest way to do this is to use the
Fey-Loader distro, which will connect to an existing schema and generate a set of objects for you.
Alternatively, you can create these objects via Fey's API. You would first create a Fey::Schema object. This object will hold all of your tables and foreign keys. If you want to create your schema this way, you should start with the Fey::Schema, Fey::Table, and Fey::FK APIs. You'll also want to use the Fey::Column API.
The emphasis in the core Fey distro is on dynamic queries, particularly on the tables/columns/etc involved in the query, not just the bound parameters.
This is not what I mean by a dynamic query ...
SELECT user_id FROM User where username = ?
While this is dynamic in the sense that the username is parameterized and may change on each invocation, it is still easily handled by a phrasebook class. If that is all you need, I suggest checking out any of
SQL::Library on CPAN.
Imagine that we have a database with a User table and a Message table, where each message has a user who is that message's creator. We might want to grab all the users in the database, in which case we would do a simple
SELECT against the User table ...
SELECT * FROM User
But maybe we want to get all the users who have created a message in the last week:
SELECT User.* FROM User JOIN Message USING (user_id) WHERE Message.creation_date >= ?
The resultset for our query is still the same (0+ users) but the constraints of the query are more complex. Now imagine another dozen or so permutations on how we search for users. This is what I mean by "dynamically" generating queries.
You probably don't need to read this if you just wanted to know how to use Fey.
Let's assume we have a simple User table with the following columns:
username state first_name last_name access_level
Limiting ourselves to queries of equality ("username = ?", "state = ?"), we would still need 32 (1 + 5 + 10 + 10 + 5 + 1) entries to handle all the possible combinations of columns. Now imagine adding in variants like allowing for wildcard searches using LIKE or regexes, or more complex variants involving an "OR" in a subclause.
This gets even more complicated if you start adding in joins, outer joins, and so on. It's plain to see that a phrasebook gets too large to be usable at this point. You'd probably have to write a program just to generate the phrasebook and keep it up to date!
The next idea that might come to mind is to dump the phrasebook in favor of string manipulation. This is simple enough at first, but quickly gets ugly. Handling all of the possible options correctly requires lots of fiddly code that has to concatenate bits of SQL in the correct order, taking into account where to put in commas,
AND, and so on and so forth. I've been there, and trust me, it's madness.
The core Fey modules provide a solution to the dynamic SQL problem. Using Fey, you can specify queries in the form of Perl methods and objects. Fey provides a set of objects to represent the parts of a schema, specifically tables, columns, and foreign keys. Using these objects along with Fey::SQL, you can easily generate very complex queries.
This core distro is also intended to be the foundation for building higher-level tools like an ORM. See
Fey::ORM for just such a thing.
This module comes from my experience writing and using Alzabo. Alzabo does everything this module does, and a lot more. The fact that Alzabo does so many things has become a fairly problematic in its maintenance, and Alzabo was over 6 years old at the time this project was begun (August of 2006).
Rather than coming up with a very smart solution that allows us to use 80% of a DBMS's functionality, I'd rather come up with a solution that's dumber but supports all (or at least 99%) of the DBMS's features. It's easy to add smarts on top of a dumb layer, but it can be terribly hard to add that last 20% once you've got something really smart.
The goals for Fey, based on my experience with Alzabo, are the following:
Rather than complex data structures, with Fey you call methods on a
Fey::SQL object to build up a query. This turns out to be simpler to work with.
Fey, unlike Alzabo, can be used to generate multi-row updates and deletes, and it supports sub-selects, unions, etc. and all that other good stuff.
For example, in using Alzabo I often came across cases where I wanted to add a table to a query's join if it hasn't already been added. With Alzabo, there's no nice clean way to do this. Simply adding the table to the join parameter twice will cause an error. It would be nice to simply be able to do this
$select->join( $foo_table => $bar_table );
and have it do the right thing if that join already exists (where the right thing is just do nothing).
Fey::SQL does exactly that.
At the same time, query generation and the ORM are decoupled. You can use Fey::SQL to generate queries without having to every use the
Fey::ORMORM is as declarative as possible, and aims to emulate Moose's declarative sugar style where possible.
Here are some of the problems I've had with Alzabo over the years which inspired me to create Fey ...
In retrospect, being a lot dumber and allowing for foot-shooting makes supporting a new DBMS much easier. People generally know how their DBMS works, and if they generate an invalid query or table name, it will throw an error.
For example, while Fey can accommodate per-DBMS query (sub)classes, it does not include any by default, and is capable of supporting many DBMS-specific features without per-DBMS classes.
Alzabo::SQLMaker, which limits you to what's already defined, or forces you to go through a cumbersome API to define a new SQL function for use in your Perl code.
By contrast, Fey has simple generic support for arbitrary functions via the
Fey::Literal::Function class. If you need more flexibility you can use the
Fey::Literal::Term subclass to generate an arbitrary snippet to insert into your SQL.
A related problem is that Alzabo doesn't support multiple versions of a DBMS very well. Either it doesn't work with an older version at all, or it doesn't support some enhanced capability of a newer version. It mostly supports whatever version I happened to be using when I wrote a specific piece of functionality.
Nowadays, I prefer to have my business logic classes simple use the Alzabo classes. In other words, I now prefer "has-a" and "uses-a" versus "is-a" object design for this case.
Method auto-generation based on a specific schema can be quite handy, but it should be done in the domain-specific application classes, not as a subclass of the core functionality.
When I first started working on Fey, it was named "Q". This was a nice short name to type, but obviously unsuitable for releasing on CPAN. I wanted a nice short name that could be used in multiple distributions, like John Siracusa's "Rose" modules.
I was standing in the shower one day and had the following series of thoughts leading to Fey. Reading this will may give you an unpleasant insight into my mind. You have been warned.
This module is "SQL-y", as in "related to SQL". However, this name is bad for a number of reasons. First, it's not clear how to pronounce it. It may make you think of a YACC grammar ("SQL.y"). It's a weird combo of upper- and lower-case letters.
"SQLy" and "Squall" share a number of letters, obviously.
Squall is a single short word, which is good. However, it's a bit awkward to type and has a somewhat negative meaning to me, because a storm can mean trouble.
Squall Lionheart is a character in Final Fantasy VIII, which IMO is the best Final Fantasy game before the PS2.
The inimitable Faye Wong sang the theme song for FF VIII. I love Faye Wong.
And thus we arrive at "Fey". It's nice and short, easy to type, and easy to say.
Some of its meanings are "otherworldly" or "magical". Attempting to combine SQL and OO in any way is certainly unnatural, and if done right, perhaps magical. Fey can also mean "appearing slightly crazy". This project is certainly that.
Yes, I'm a nerd, I know.
Please report any bugs or feature requests to
firstname.lastname@example.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
Dave Rolsky <email@example.com>
This software is Copyright (c) 2011 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)