JRuby, ActiveRecord, JDBC to SQL Server
I recently had to figure out a good way to export data in XML form
from a SQL Server database. I searched high and low for a good
ActiveRecord adapter written in pure Ruby to talk to SQL Server.
Unfortunately, the setup and overhead to get the right ODBC driver, DSN
configs, and all those funky libraries to work properly on any
*Nix-based machine were adding up way too quickly and complicating
matters. So back to the drawing board.
RESTful service from .NET – no
Way too much work for such a simple task that only runs but once a
week. Beside anything Microsoft is my nemesis and I am not even going to
put my toe in that water.
SQL Server bcp export – no.
The bottom line – writing code in your database is always bad news. Sure the bcp export is the quick and dirty, but it might be too
quick and dirty. It can make upgrades and migrations almost impossible.
Just think how difficult it would be to migrate from SQL Server to say
MySQL if the use of the bcp utility compounds over time. More
importantly, how do you test it?
JDBC – yes!
Really? I was kind of shocked to learn that Microsoft jumped on the
Java bandwagon and wrote a JDBC adapter. Woo-hoo! Wait…I don’t want to
write a bunch of junky DAO’s in Java.
Enter – my good friends JRuby and ActiveRecord.
I can write sweet sugary Ruby code while utilizing the power of any
or all of the existing Java libraries – which so happens to include
JDBC!
So in order to glue this together it is quite simple.
- First install JRuby, it’s simple and there are 2 million blog posts
about how to do this, all you need is Java pre-installed on your
machine. Download it here from here.
- jruby> gem install activerecord
- jruby> gem install activerecord-jdbc-adapter
- Download the SQL Server JDBC driver JAR, I recommend getting version 2 or higher
- activerecord-jdbc-adapter is a nifty JRuby specific gem that acts as
an adapter for ActiveRecord to speak JDBC (yet another thanks to Nick
Sieger).
Once your have these things installed you can write a simple Ruby
class to establish the connection and define any number of ActiveRecord
classes to map to the tables you’re after. Here is a small snippet on
how easy it is to connect to SQL Server using this technique.
#assuming this file is inline with your sqljdbc.jar or just put it into your $JRUBY_HOME/lib directory
require 'rubygems'
require 'active_record'
require 'active_record/connection_adapters/jdbc_adapter'
require 'sqljdbc4.jar'
ActiveRecord::Base.establish_connection(
:adapter => "jdbc",
:username => "XXXX",
:password => "XXXX",
:driver => "com.microsoft.sqlserver.jdbc.SQLServerDriver",
:url=> "jdbc:sqlserver://Server;databaseName=XXXX"
)
class Tclient < ActiveRecord::Base
set_table_name "CLIENT"
end
puts Tclient.column_names()
Approximately 10 lines of actual code and you’re done. It can’t be
done any more elegant or simple than that. Here is the really beauty
part, you can actually test your “script”. Use RSpec or TestUnit, but it
is tested! Let cron or whatever scheduling mechanism of your choice run
your script with confidence
分享到:
相关推荐
ActiveRecord-JDBC-Adapter提供对以下各项的完全或几乎完全支持: MySQL , PostgreSQL , SQLite3和MSSQL *(SQLServer)。 除非我们获得更多的贡献,否则我们将不会支持更多的适配器。 请注意,获得另一个适配器...
它可以在ActiveRecord 4.2+上运行,并完全支持PostgresSQL,MySQL和SQLite适配器,包括本机适配器和JDBC(JRuby)。尝试为和 Active Record适配器(Sequel中的oracle和tinytds )提供支持。其他适配器也可以工作,但...
您可以通过裸机连接到数据库,例如Mysql2::Client (来自MRI上的mysql2 gem)或Java::OrgPostgresqlJdbc4::Jdbc4Connection (来自Jruby上的jdbc-postgres )。 随着数据库开始原生支持SQL MERGE(基本上是upsert...
6. **ActiveRecord 配置**:Rails的ActiveRecord是ORM(对象关系映射)库,需要配置适配器为`jdbc_oracle`。此外,还需要设置`pool`大小,确保并发访问时的性能。 7. **迁移和模型**:创建Rails模型并指定对应的...
阿拉丁项目可能配置了`jdbc`或`jruby-odbc` gem,这两个gem允许Rails在JRuby环境下与ODBC进行交互。Rails的数据库配置文件(通常是`config/database.yml`)会包含有关如何连接到4D服务器的详细信息,包括DSN(数据源...