Ingres Corporation

Ingres Ruby Active Record Adapter/Driver Version 1.3.0


  1. Welcome
  2. Overview
    1. New in This Release
  3. Operating System Support
  4. Installation Considerations
  5. General Considerations
    1. Features Not Included
    2. Syntax for the Ingres Ruby Driver
    3. Syntax for the Ingres AR Adapter
  6. Building and Installing the Ingres Ruby AR Adapter/Driver
    1. Ingres Ruby Source Code
    2. Ingres Ruby Driver
    3. Ingres AR Adapter
  7. Example Code
    1. IRB (Interactive Ruby) Using Ingres Ruby Driver Only (Without AR Adapter)
    2. Ruby Program Using Ingres Ruby Driver Only (Without AR Adapter)
    3. Rails Cookbook Tutorial (Web Application) Using Ingres AR Adapter
    4. Ingres Flight Planner Demo Using Ingres Ruby Driver Only (Without AR Adapter)
  8. Known Issues
  9. More Information

Welcome

This readme contains all of the documentation on the Ingres Ruby Active Record (AR) adapter and driver. AR is the standard database access interface used by Ruby on Rails (ROR or Rails) applications.

Please review this readme before building or installing this software. We encourage users to test the software and provide feedback.


Overview

There are two components to the Ingres support for Ruby:

Both components are required for Rails applications using Active Record (AR), which is the standard Rails Object-to-Relations Mapping (ORM) interface. The adapter implements the AR classes for Ingres; internally, it invokes the classes and methods in the driver to actually communicate with Ingres.

The driver, while primarily intended to service the adapter, can also be invoked directly from any Ruby program to communicate with Ingres. The driver interface does not follow any industry standard and may change in the future, if required.

To summarize, the AR interface provides an object interface while the driver interface is a more direct SQL-level interface.


New in This Release

This is a major release that addresses the following:


Operating System Support

This Ingres Ruby Active Record adapter/driver supports all of the platforms supported by Ingres, including:


Installation Considerations

To build and install the Ingres Ruby interface, the following components are required:


General Considerations


Features Not Included

The following features are currently not included in the Ingres Open Source Ruby interface (also see the Known Issues section):


Syntax for the Ingres Ruby Driver

Note: We do not recommend that production applications use this API since it is intended for the Ingres AR adapter. The API may change in the future. It is, however, useful for testing Ingres connectivity and functionality.

Ruby program must have a "require 'Ingres'" statement to load in driver file.

Create Ingres class object:

object_instance_name=Ingres.new

Methods (by category):

Connect to database:

connect(database_name)

connect_with_credentials(database_name, user, password)

Disconnect from database:

disconnect()

Execute SQL queries and transactional statements:

execute(sql)

(Alias for "execute" is "exec".)

For SQL queries or database procedure calls that contain parameters, use the pexecute() method in one of the following formats.

For selects, inserts, deletes, and updates:

pexecute(sql [ [, param_type, param_value ] … ] )

For database procedure calls:

pexecute("{ <call> | <execute procedure> procedure_name [ (column_name = ? [ [ , column_name = ? ] … ] ) ] }" [ [ , column_name, param_type, param_value ] … ] )

where parameters in the sql text are represented by question marks (?) and there is one set of param_type/param_value entries for each one.

param_type must be one of the following single characters:

param_type Description
b byte
B long byte
c char
d date,
D decimal
f float
i integer
n nchar
N nvarchar
t text
T long text
v varchar
V long varchar

param_value should correspond to the type.

Both methods return a result set consisting of an array of rows, each of which is in turn an array of the columns within the row. For example:

ing.pexecute("select * from t1 where f1 = ?", 'i', 2)

Result Set Metadata:

Method Description
rows_affected() Returns the number of rows affected by last execute()
column_list_of_names() Returns the names of the columns in the result set
data_types() Returns the data types of the columns in the result set
data_sizes() Returns the data lengths of the columns in the result set

Database Metadata:

Method Description
current_database() Returns name of the current database connected to
tables() Returns list of all tables in the current database

Miscellaneous:

To turn trace debugging on or off:

set_debug_flag(flag, flag_value)

where

flag is one of following:

flag_value is TRUE or FALSE.


Syntax for the Ingres AR Adapter

See Ruby Active Record documentation: http://ar.rubyonrails.com/.

ActiveRecord Type Mappings

The following table shows the type mappings between Ingres, Ruby and ActiveRecord.

ActiveRecord TypeRuby ClassIngres TypeNotes
:primary_key INTEGER NOT NULL PRIMARY KEY 
:stringStringCHAR, NCHAR, VARCHAR, NVARCHAR,INTERVAL 
:textStringVARCHAR(32000) 
:booleanBooleanINTEGER1 
:integerFixnumINTEGER2|4|8 
 Bignumif INTEGER4|8 won't fit in Fixnum 
:floatFloatFLOAT4, FLOAT8, MONEY 
:decimalStringDECIMAL 
:dateDateANSIDATEIngres 9.1.0 or later
:datetimeTimeDATE, INGRESDATE 
:timeTimeTIMEIngres 9.1.0 or later
:timestampTimeTIMESTAMPIngres 9.1.0 or later

Note that the :binary ActiveRecord type is missing due to the lack of support for Ingres LONG BYTE, LONG VARCHAR support.


Building and Installing the Ingres Ruby AR Adapter/Driver


Ingres Ruby Source Code

The source code for the Ingres Ruby driver is kept in a Subversion repository at http://code.ingres.com. In order to download the source you will need an SVN client. Below is a list of some of the clients available for working with Subversion To download the source you need to specify the following URL when performing a checkout: For example if using the command line Subversion client 'svn' the following will check out all the code for the Ingres Ruby driver:
svn co http://code.ingres.com/ingres/drivers/ruby/
This will download the complete tree for the Ruby driver into the directory 'ruby'. If you are only interested in the 'main' code line use the following command:
svn co http://code.ingres.com/ingres/drivers/ruby/main/ ruby-main
Note that an additional parameter of 'ruby-main' has been specified. The code for http://code.ingres.com/ingres/drivers/ruby/main/ will be extracted in to this directory. Otherwise the checkout will download the code into a directory named 'main'. For more information on working with Ingres source code control system see - http://community.ingres.com/wiki/Connectivity_Drivers_Source_Code

Ingres Ruby Driver

A binary version of the driver is provided for Windows (Ingres.so). If using the provided binary, skip to the install step below.

The driver can also be built from the source provided (Ingres.c). To build and install the Ingres Ruby driver, the following components are needed AND MUST BE IN YOUR PATH:

To build the driver

  1. Copy source Ingres.c and extconf.rb to your build directory (anywhere).
  2. Create the makefile using the Ruby mkmf utility. Run the following command after customizing it to your installation of Ingres:
    ruby -r mkmf extconf.rb
        --with-ingres-include='/opt/Ingres/IngresII/ingres/files/'
        --with-ingres-lib='/opt/Ingres/IngresII/ingres/lib/'
    

    A sample createMake.bat has been included for Windows. The utility mkmf pulls configuration information from the extconf.rb file, which may need slight library name changes for different versions of Ingres or operating systems; see comments in extconf.rb file.

    The output should look similar to the following:

    bash-2.05$ ruby -r mkmf extconf.rb --with-ingres-include='/usr/ingres/files/' --with-ingres-lib='/usr/ingres/lib'
    checking for iiapi.h... yes
    checking for main() in -lingres... yes
    checking for main() in -lingres... yes
    creating Makefile
    
  3. Compile and link the driver. Type make on Linux and UNIX or nmake on Windows. This should compile and link cleanly, creating output shared library Ingres.so.

To install the driver

Type make install. (On Windows, type nmake).

Or, with the pre-built Windows binary, copy Ingres.so to Ruby i386-msvcrt directory, such as: c:\ruby\lib\ruby\site_ruby\1.8\i386-msvcrt.


Ingres AR Adapter

At a minimum, Ruby's Active Record must be installed. If running Rails, this will already exist as part of the Rails installation. For non-Rails users, AR may come with Ruby or may be installed separately as a Ruby "gem" into the Ruby installation.

There are several steps required to install the Ingres AR adapter into the Ruby AR installation.

Note: If only using the Ingres Ruby driver interface (not AR), installation of the adapter can be skipped and you can go directly to installation of the driver below.

Typically, the base path of the AR installation will be similar to the following and will be referred to hereafter in this documentation as AR_BASE (where the last digits in the path refer to the AR version):

UNIX/Linux: /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.2

Windows: C:\ruby\lib\ruby\gems\1.8\gems\activerecord-1.15.3

To install the Ingres AR adapter into the Ruby AR installation

  1. Copy the adapter file, ingres_adapter.rb, to the AR_BASE/lib/active-record/connection_adapters directory.
  2. NOTE: Not required for Rails 2.1 or later Add Ingres as a valid database to AR. Edit active_record.rb file in AR_BASE/lib by adding "ingres" to the end of a line that looks like:
    RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase ingres )
    
  3. Set up database connection information. Create a new directory called native_ingres under the AR_BASE/test/connections directory. Copy and edit the sample connection.rb file to define database name, user ID, password information.
  4. Set II_DATE_FORMAT to SWEDEN. Either set this as an OS environment variable in your RAILS application environment or as an Ingres variable (with the ingsetenv command). This restriction will be removed in the future. Also, code in the adapter file, if uncommented (scan for SWEDEN), may allow successful processing without setting II_DATE_FORMAT.
  5. For each Rails application accessing Ingres, edit the database.yml file in the application's config folder to specify the Ingres adapter plus database and user information. The following is a sample entry for a local Ingres database connection named my_rails_app_development:
    development:
        adapter: ingres
        database: my_rails_app_development
        username: ingres
        password: ingres
        host: localhost
    

    For remote connections, change database and host to Ingres remote connection format in either "vnode::databasename" where vnode is a name configured in the Ingres Network Utility or a dynamic vnode in @host,protocol,port[user,pwd]" format. For details, see the Ingres Connectivity Guide.


Example Code


IRB (Interactive Ruby) Using Ingres Ruby Driver Only (Without AR Adapter)

>>>$ irb

irb(main):001:0> require 'Ingres'

=> true

irb(main):002:0> i = Ingres.new

=> #<Ingres:0xb7fba520>

irb(main):003:0> i.connect("activerecord_unittest")

=> #<Ingres:0xb7fba520>

irb(main):006:0> sql="select * from developers"

=> "select * from developers"

irb(main):007:0> result_set = i.execute(sql)

=> [[5, "fixture_5", 100000, "NULL", "NULL"], [6, "fixture_6", 100000, "NULL", "NULL"], [7,"fixture_7", 100000, "NULL", "NULL"], [11, "Jamis", 9000, "NULL", "NULL"], [8, "fixture_8", 100000, "NULL", "NULL"], [9, "fixture_9", 100000, "NULL", "NULL"], [2, "Jamis", 150000, "NULL", "NULL"], [10, "fixture_10", 100000, "NULL", "NULL"], [3, "fixture_3", 100000, "NULL", "NULL"], [1, "David", 80000, "NULL", "NULL"], [4, "fixture_4", 100000, "NULL", "NULL"]]

Ruby Program Using Ingres Ruby Driver Only (Without AR Adapter)

require "Ingres"
ing = Ingres.new
# You must connect to a database.
# Be sure you've created the database using "createdb".
ing.connect(some_database)
# you can also use:
# ing.connect_with_credentials( some_database, user_name, password)
sql = "select * from some_table"
result_set = ing.execute(sql)
# or
# result_set = ing.exec(sql)
# the result set is an array of arrays.
# you get one array for each row
# {{a,b,c}, {d,e,f}, etc}
# metadata is stored in these routines
# how many rows were touched by the last query
# usually an update
puts ing.rows_affected
# what's the current database (from the connect call)
puts ing.current_database
# the names of each column in the result set
puts ing.column_list_of_names
# the size of the data in each column
# this could be the length of a varchar or an int
puts ing.data_sizes
# what type is in each column
puts ing.data_types
# all the tables in the current database
puts ing.tables
# properly terminate your database connection
ing.disconnect

Rails Cookbook Tutorial (Web Application) Using Ingres AR Adapter

A Cookbook Tutorial can be accessed from the Rails website (http://rubyonrails.org/ docs) under the Tutorials section. Selection Rolling with Ruby on Rails: Part I takes you to http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html. The demo referred to in the tutorial is installed as part of some of the prepackaged Ruby on Rails versions, such as InstantRails on Windows or Rails LiveCD on Linux.

To run this demo in InstantRails, which is preconfigured to run against another database, do the following:

  1. Install the Ingres Ruby AR adapter and driver into InstantRails per the instructions above (InstantRails comes with its own version of Ruby and Active Record).
  2. Create the database in Ingres with schemas.
    1. Create the database:
      createdb cookbook_development
      
    2. Create and populate the tables:
      sql cookbook_development < cookbook_ingres.sql
      
  3. Configure the application to use the Ingres ActiveRecord adapter.

    Edit database.yml in ...\rails_apps\cookbook:

    1. Change adapter from "mysql" to "ingres" in all (three) occurrences.
    2. Update username and password as needed; for example, username: ingres.
    3. Edit database to be the full name of the database. Unlike MySql, the _development, _test, _production suffixes are not automatically added.
  4. Start up systems (if not already started):
    1. Start up Ingres (ingstart)
    2. Start up Rails. For Instant Rails:
      cd d:\InstantRails
      use_ruby
      cd ..
      InstantRails
      
    3. Start up Mongrel (web server)

      If Mongrel is already up and app or even database.yml was changed, you must restart Mongrel:

      Shutdown with Control-C in Mongrel startup command window.

      cd d:\InstantRails\rails_apps\cookbook
      mongrel_rails start
      
  5. Run application:

    From web browser, set url=http://localhost:3000/category. You should get a screen showing "Online Cookbook" at the top.


Ingres Flight Planner Demo Using Ingres Ruby Driver Only (Without AR Adapter)

If you would like to get a copy of this, please post a request on the Ingres Community Forum in the Database Drivers and APIs area.


Known Issues

Known issues are as follows:


More Information

As this product is still in alpha status, issues specific to the Ingres Ruby AR adapter and driver should be posted on the Ingres forum http://community.ingres.com/forums/index.php?c=3 in the Database Driver and APIs forum rather than contacting Ingres Technical Support.

You can find more information on using the Ingres Ruby driver and ActiveRecord bindings at http://community.ingres.com/wiki/Ingres_Ruby_Development_Center.

For online technical assistance for other Ingres products or components, and a complete list of locations, primary service hours, and telephone numbers, contact technical support at http://ingres.com/support.


Revision $Id: README.html 48 2008-05-02 15:45:23Z crogr01 $

Available from: $Url$

© 2008 Ingres Corporation. All rights reserved.