Add to My Yahoo!
RSS Feeds
Deutsch
English
Catalyst Web Framework: Building Your Own Model
Category: Computer & Technology
Article added by: Shriharsha Bhat


E-Mail Article
Print Article

Catalyst Web Framework: Building Your Own Model

As we know, DBIx::Class can provide a powerful interface to your data. Sometimes, however, DBIx::Class is not the right tool for the job. Situations often arise in which your application won't be able to access database tables directly and instead you'll need to access data through predefined stored procedures. In this case, DBIx::Class would be useless as you aren't able to read and modify objects with the usual SELECT, INSERT, UPDATE, and DELETE command set—everything must be done by calling a procedure and reading back the result. In other cases, your data won't be in a database at all. You might instead choose to store and retrieve information from files in a directory.

In this article, author Jonathan Rockway who has written the book Catalyst published by Packt Publishing, covers three common cases—mixing a procedural interface with a relational DBIx::Class interface, writing a database interface without DBIx:: Class, and building a custom Model that doesn't use a database at all.

This article has been provided by Jonathan Rockway who is the author of the book 'Catalyst - Accelerating Perl Web Application Development', published by Packt Publishing. For more information please visit http://www.packtpub.com/catalyst-perl-web-application/book.

Extending a DBIx::Class Model

A common occurrence is a situation in which your application has free reign over most of the database, but needs to use a few stored procedure calls to get at certain pieces of data. In that case, you'll want to create a normal DBIC schema and then add methods for accessing the unusual data.

As an example, let's look back to the AddressBook application and imagine that for some reason we couldn't use DBIx::Class to access the user table, and instead need to write the raw SQL to return an array containing everyone's username. In AddressBook::Model::AddressDB, we just need to write a subroutine to do our work as follows:

Here's how the code works. On the first line, we get our DBIC::Schema object and then obtain the schema's storage object. The storage object is what DBIC uses to execute its generated SQL on the database, and is usually an instance of DBIx:: Class::Storage::DBI. This class contains a method called dbh_do which will execute a piece of code, passed to dbh_do as a coderef (or "anonymous subroutine"), and provide the code with a standard DBI database handle (usually called $dbh). dbh_do will make sure that the database handle is valid before it calls your code, so you don't need to worry about things like the database connection timing out. DBIC will reconnect if necessary and then call your code. dbh_do will also handle exceptions raised within your code in a standard way, so that errors can be caught normally.

The rest of the code deals with actually executing our query. When the database handle is ready, it's passed as the second argument to our coderef (the first is the storage object itself, in case you happen to need that). Once we have the database handle, the rest of the code is exactly the same as if we were using plain DBI instead of DBIx::Class. We first prepare our query (which need not be a SELECT; it could be EXEC or anything else), execute it and, finally, process the result. The map statement converts the returned data to the form we expect it in, a list of names (instead of a list of rows each containing a single name). Note that the return statement in the coderef returns to dbh_do, not to the caller of get_users. This means that you can execute dbh_do as many times as required and then further process the results before returning from the get_users subroutine.

Once you've written this subroutine, you can easily call it from elsewhere in your application:


This article has been provided by Jonathan Rockway who is the author of the book 'Catalyst - Accelerating Perl Web Application Development', published by Packt Publishing. For more information please visit http://www.packtpub.com/catalyst-perl-web-application/book.


Posted By: Shriharsha Bhat
Web: http://www.packtpub.com
Contact: e-mail


About the Author:
Jonathan Rockway, a member of the Catalyst Core Team, has been programming Perl since his middle school years. He became professionally involved with Perl when we was a desktop support minion at the University of Chicago and inherited a mod_perl application. He now works as a software developer at Infi nity Interactive. In his spare time, he maintains a collection of modules on the CPAN and tries to speak at as many Perl conferences as possible.


Another articles posted by Shriharsha Bhat: