Page 265 - Asterisk™: The Future of Telephony
P. 265

Database integration
               Though immensely successful in the web development space for serving dynamic con-
               tent,  database  integration  has  always  been  an  underutilized  possibility  for  driving
               dynamic voice applications with Asterisk. Most Asterisk applications that do accom-
               plish database integration outsource the complexity to a PHP or Perl AGI script because
               the extensions.conf or AEL grammars are simply impractical for the level of sophisti-
               cation required.

               Adhearsion uses a database integration library, called ActiveRecord, developed by the
               makers of the Ruby on Rails framework. With ActiveRecord, the end user seldom, if
               ever, writes SQL statements. Instead, the developer accesses the database just like any
               Ruby object. Because Ruby allows such flexible dynamism, access to the database looks
               and feels quite natural. Additionally, ActiveRecord abstracts the differences between
               database management systems, making your database access implementation agnostic.
               Without going too much into the internals of ActiveRecord and more sophisticated
               uses of it, let us consider the following simple MySQL schema:
                   CREATE TABLE groups (
                    `id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
                    `description` varchar(255) DEFAULT NULL,
                    `hourly_rate` decimal DEFAULT NULL
                   );
                   CREATE TABLE customers (
                    `id` int(11) DEFAULT NULL auto_increment PRIMARY KEY,
                    `name` varchar(255) DEFAULT NULL,
                    `phone_number` varchar(10) DEFAULT NULL,
                    `usage_this_month` int(11) DEFAULT 0,
                    `group_id` int(11) DEFAULT NULL
                   );
               In practice we would obviously store much more information about the customer and
               keep the service usage information in a database-driven call detail record, but this de-
               gree of simplicity helps demonstrate ActiveRecord fundamentals more effectively.
               To connect Adhearsion to this database, one simply specifies the database access in-
               formation in a YAML configuration file like so:

                   adapter: mysql
                   host: localhost
                   database: adhearsion
                   username: root
                   password: pass
               This tells Adhearsion how to connect to the database, but how we access information
               in the tables depends on how we model our ActiveRecord objects. Since an object is
               an instance of a class, we write a class definition to wrap around each table. We define
               simple properties and relationships in the class with the superclass’s methods.
               Here are two classes we may use with the aforementioned tables:



                                                           Asterisk Development with Adhearsion | 237
   260   261   262   263   264   265   266   267   268   269   270