Created
December 22, 2012 21:08
-
-
Save zph/4361158 to your computer and use it in GitHub Desktop.
Demo of using JDBC with ActiveRecord for accessing legacy SQL Server 2005.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env jruby | |
=begin | |
Implementation notes: | |
Setup SQL Auth User in SQLSERVER | |
Allow Mixed authentication | |
Disable Firewall on SQL port 1433 and 1434 | |
Test connection on localhost | |
Download and extract sqljdbc4.jar | |
http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=21599 | |
http://download.microsoft.com/download/D/6/A/D6A241AC-433E-4CD2-A1CE-50177E8428F0/1033/sqljdbc_3.0.1301.101_enu.tar.gz | |
=end | |
# Script adapted from post on http://techwhizbang.com/2010/03/jruby-activerecord-jdbc-sqlserver/ | |
# Thanks techwhizbang.com | |
require 'active_record' | |
require 'active_record/connection_adapters/jdbc_adapter' | |
require './jars/sqljdbc4.jar' | |
DB_HOST = '10.0.0.1\SQLEXPRESS' # Dummy IP address | |
DB_NAME = "INSERTHERE" # database name | |
USER = 'USERHERE' # sql server username | |
PASSWORD = 'password' | |
config = { | |
:url => "jdbc:sqlserver://#{DB_HOST};databaseName=#{DB_NAME}", | |
:adapter => "jdbc", | |
:driver => "com.microsoft.sqlserver.jdbc.SQLServerDriver", | |
:username => USER, | |
:password => PASSWORD, | |
:timeout => 5000, | |
} | |
ActiveRecord::Base.establish_connection( config ) | |
## The information below will need to be modified to fit each | |
#database. A useful command for checking db tables is | |
# >> ActiveRecord::Base.connection.tables | |
# Which returns an array of table names | |
# => ["accounts", "assets", ...]" | |
# | |
# These table names then become ActiveRecord::Base inherited | |
# classes that can be tweaked for uniformity. | |
class CountyDist < ActiveRecord::Base | |
self.table_name = "countydist" | |
# alias_attribute :jurisdiction, :JUR | |
def jurisdiction | |
read_attribute(:JUR).to_i | |
end | |
end | |
class Permits < ActiveRecord::Base | |
self.table_name = "permits" | |
end | |
# >> ActiveRecord::Base.connection.tables | |
# => ["accounts", "assets", ...]" | |
a = CountyDist.first | |
a.jurisdiction | |
a = Permits.first | |
ActiveRecord::Base.clear_active_connections! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment