Jan 06 2009

Rails and Sybase Adapative Server Anywhere (SQLAnywhere) - Part 3

etienne @ 8:28 am

For anyone struggling to get SQL Anywhere working with Rails there is some good news, I’ve received and email from Eric Farrar from Sybase to let me know that he has been doing a lot of work on developing a new adaptor for SQL Anywhere. I’ve included the content of his email below for anyone interested.

There is now a ActiveRecord adapter specifically for SQL Anywhere. It is available on RubyForge at sqlanywhere.rubyforge.org/. The source is hosted on GitHub and is available at github.com/sqlanywhere. This driver does not go through ODBC, but instead uses the new C API library to directly interface with SQL Anywhere databases. Please note that this driver can only be used with SQL Anywhere 10 and greater.

 


May 20 2008

Rails and Sybase Adapative Server Anywhere (SQLAnywhere) - Part 2

etienne @ 10:26 am

As discussed in the Rails and Sybase Adapative Server Anywhere (SQLAnywhere) article I had to modify the odbc_adapter.rb code to allow the adapter to be used with my SybaseASA DB. Making the change to the adapter code directly is not an acceptable solution and until the developers of the adapter apply the change I had to come up with an alternate solution.

The solution I came up with was to monkey-patch the method responsible for checking the DB type to allow SybaseASA to be seen as a valid DB. I added the following code to my environment.rb:

# Monkeypatch the ODBC Adapter so that it will work with SybaseASA
# see
http://odbc-rails.rubyforge.org/index.html
module ActiveRecord
  module ConnectionAdapters
    class ODBCAdapter < AbstractAdapter 
      alias :orig_dbmsNameToSym :dbmsNameToSym

      def dbmsNameToSym(dbmsName, dbmsVer)   
        begin
          # Call original method
          symbl = orig_dbmsNameToSym(dbmsName, dbmsVer)
        rescue ActiveRecord::ActiveRecordError
            # Catch exception if db not found in existing list, check
            # if SybaseASA, if not raise exception
            if dbmsName =~ /adaptiveserveranywhere/i
              symbl = :sqlanywhere
            else
              raise ActiveRecord::ActiveRecordError, "ODBCAdapter: Unsupported database (#{dbmsName})"
            end
        end
        return symbl
      end

    end
  end
end


May 19 2008

Rails and Sybase Adapative Server Anywhere (SQLAnywhere)

etienne @ 1:13 pm

In a previous article (Sybase DB Adapter) I wrote how I tried to install the Sybase adapter in an attempt to get access to an existing SybaseASA 9.0.2 DB. After doing some research I found that this adapter only works for the Enterprise edition of Sybase (i.e. SybaseASE).

So I needed to find an alternative that would work with SybaseASA or SQLAnywhere. After a lot of searching I came across a group dedicated to this task, at this stage there is not a lot of activity. One of the postings from Bryan Lahartinger proved very helpful, in the article he mentions "odbc-rails". I did a google search and found there is a Ruby project ODBC Adapter for Ruby on Rails / Active Record that has been developing an ODBC adapter for use with Rails.

The latest release has added support for SQLAnywhere so I dowloaded and installed it. There is a home page for this project which provides detailed installation instructions. I do my Rails development on Windows using InstantRails, a quick check confirmed that Christian Werner’s ODBC module comes preloaded with InstantRails so I did not have to install it separately. I downloaded the gem file for V2.0 from rubyforge and installed it using:

gem install -l activerecord-odbc-adapter-2.0.gem –include-dependencies

I setup my database.yml file to use the DSN I had previously defined to access the DB, i.e.

development:
  adapter: odbc
  dsn: mydatabase
  username: uid
  password: pwd

I started my server which loaded without any problem but when I tried to run my application I got the following error:

ODBCAdapter: Unsupported database (adaptiveserveranywhere)

As SybaseASA is the same as SQLAnywhere I changed the following line in odbc_adapter.rb (line #:1585) to get me going from:

elsif dbmsName =~ /SQLAnywhere/i

to:

elsif dbmsName =~ /SQLAnywhere/i or dbmsName =~ /adaptiveserveranywhere/i

So far everything seems to working without any problems, I’ve asked if it will be possible to get this change added to the gem, if not I will need to monkeypatch the source. Next I will attempt to do an install under Suse Linux as this is where the application will ultimately reside.

Update: I’ve now added the monkey-patch to my application see Rails ODBC Adapter with Sybase Adaptive Server Anywhere (SybaseASA)


May 15 2008

Sybase DB Adapter

etienne @ 12:26 pm

 Update: This adapter only works with the enterprise edition of Sybase (i.e. Sybase ASE) not with SQLAnywhere (Sybase ASA)

One of my Rails projects require me to connect to a Sybase ASA database, I quickly figured out that I needed to install the sybase adapter as in Rails 2.0.2 these and some of the other databases have been pulled out of Rails core. According to the information in this article you can achieve this by using this command:

gem install activerecord-sybase-adapter

When I tried this I got the following error:

ERROR:  could not find activerecord-database-adapter locally or in a repository

The only way I got it to work was to use the -s option ("Use URL as the remote source for gems"), i.e.

gem install activerecord-sybase-adapter -s http://gems.rubyonrails.org