Data Modeling with Alzabo
Once you execute the SQL from within the schema editor, you have two ways to access the data. You may, of course, access it directly using DBI (or a similar interface from another language), creating and executing SQL queries.
For example, let's assume that I have created my addressbook schema with the following table:
CREATE TABLE People (
person_id SERIAL NOT NULL,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
birthday DATE NOT NULL,
PRIMARY KEY(person_id)
);
In order to make things a bit more interesting, let's populate our table with some values:
INSERT INTO People (first_name, last_name, birthday)
VALUES ('Reuven', 'Lerner',
'1970-Jul-14');
INSERT INTO People (first_name, last_name, birthday)
VALUES ('Atara Margalit', 'Lerner-Friedman',
'2000-Dec-16');
Listing 1 contains a simple Perl program that uses DBI to retrieve
the names (and birthdays) of people in our addressbook who match
the SQL pattern entered on the command line. (SQL patterns are much
simpler than UNIX regular expressions—there are only two
characters: % matches zero or more characters and _ matches exactly
one character.)
We retrieve the user's input on the command line and place % signs before and after it to ensure that the string will match, regardless of where it occurs in the first_name or last_name column. Then we connect to the database, turning on AutoCommit (as the DBI documentation encourages us to do) and activating the RaiseError and PrintError diagnostic aids.
Finally, we create our SQL query in the $sql variable, making sure to use placeholders (“?”) instead of directly interpolating variables. Not only does this reduce the risk of someone messing with our SQL, but some database drivers will take advantage of our placeholders in subsequent queries, giving us a speed boost.
Let's rewrite this program using Alzabo instead of straight DBI. We won't write the SQL ourselves or connect to the database ourselves. Rather, we will create a new schema object, naming the schema that we created with Alzabo's interactive tool. This object has a number of methods that let us perform many of the tasks for which we would otherwise use DBI.
As you can see from Listing 2, there are not many differences between the two versions until we connect to the data source. In the DBI version of the program, we connected to the data source itself with DBI->connect. In Alzabo, however, we connect to a schema, which is presumably attached to a database, and assign it to the object $schema.
Listing 2. retrieve-birthday-alzabo.pl, an Alzabo implementation of the program in Listing 1.
Using $schema, we retrieve a table object associated with one of our tables:
my $people = $schema->table("People");
Now that we have an object mapped to our People table, we can retrieve selected rows from the table. The easiest way to retrieve rows is with the rows_where method. This returns a single object of type Alzabo::Runtime::RowCursor:
my $row_cursor =
$people->rows_where
(where => [[$people->column('first_name'),
'LIKE', $look_for_name],
'or',
[$people->column('last_name'),
'LIKE',
$look_for_name]]);
Alzabo's WHERE clauses usually consist of a three-element list: a
column object, a comparison operator and a value or second column
object. We can compare the first_name column for equality with
Zaphod with:
where => [$table->column('first_name'), '=',
'Zaphod']
In Listing 2, we have made this a bit more complicated, linking two
array references with the OR boolean operator:
where => [[$people->column('first_name'),
'LIKE', $look_for_name],
'or',
[$people->column('last_name'),
'LIKE', $look_for_name]]
Alzabo is smart enough to realize that the first and third elements
of its WHERE clause are array references, and it turns the above
code into the appropriate SQL.
Once we have our RowCursor object, we iterate through each row with the next_row method:
while (my $row = $row_cursor->next_row)
{
my $first_name = $row->select('first_name');
my $last_name = $row->select('last_name');
my $birthday = $row->select('birthday');
print "$first_name $last_name
(birthday: $birthday)\n";
$rows_returned++;
}
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Using Salt Stack and Vagrant for Drupal Development
- Validate an E-Mail Address with PHP, the Right Way
- Build a Skype Server for Your Home Phone System
- Why Python?
- Tech Tip: Really Simple HTTP Server with Python
- A Topic for Discussion - Open Source Feature-Richness?
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




55 min 15 sec ago
4 hours 42 min ago
4 hours 50 min ago
7 hours 5 min ago
9 hours 34 min ago
19 hours 37 min ago
1 day 4 min ago
1 day 3 hours ago
1 day 4 hours ago
1 day 6 hours ago