forked from jruby/activerecord-jdbc-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored cache and mssql adapters to share common code.
* Added test cases for mssql * Added tsql helper as a place to share common tsql adapter code git-svn-id: svn+ssh://rubyforge.org/var/svn/jruby-extras/trunk/activerecord-jdbc@971 8ba958d5-0c1a-0410-94a6-a65dfc1b28a6
- Loading branch information
kofno
committed
Apr 23, 2008
1 parent
417551e
commit de6c0bf
Showing
6 changed files
with
88 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Common methods for handling TSQL databases. | ||
module TSqlMethods | ||
|
||
def modify_types(tp) #:nodoc: | ||
tp[:primary_key] = "int NOT NULL IDENTITY(1, 1) PRIMARY KEY" | ||
tp[:integer][:limit] = nil | ||
tp[:boolean] = {:name => "bit"} | ||
tp[:binary] = { :name => "image"} | ||
tp | ||
end | ||
|
||
def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc: | ||
return super unless type.to_s == 'integer' | ||
|
||
if limit.nil? || limit == 4 | ||
'int' | ||
elsif limit == 2 | ||
'smallint' | ||
elsif limit == 1 | ||
'tinyint' | ||
else | ||
'bigint' | ||
end | ||
end | ||
|
||
def add_limit_offset!(sql, options) | ||
if options[:limit] and options[:offset] | ||
total_rows = select_all("SELECT count(*) as TotalRows from (#{sql.gsub(/\bSELECT(\s+DISTINCT)?\b/i, "SELECT\\1 TOP 1000000000")}) tally")[0]["TotalRows"].to_i | ||
if (options[:limit] + options[:offset]) >= total_rows | ||
options[:limit] = (total_rows - options[:offset] >= 0) ? (total_rows - options[:offset]) : 0 | ||
end | ||
sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i, "SELECT * FROM (SELECT TOP #{options[:limit]} * FROM (SELECT\\1 TOP #{options[:limit] + options[:offset]} ") | ||
sql << ") AS tmp1" | ||
if options[:order] | ||
options[:order] = options[:order].split(',').map do |field| | ||
parts = field.split(" ") | ||
tc = parts[0] | ||
if sql =~ /\.\[/ and tc =~ /\./ # if column quoting used in query | ||
tc.gsub!(/\./, '\\.\\[') | ||
tc << '\\]' | ||
end | ||
if sql =~ /#{tc} AS (t\d_r\d\d?)/ | ||
parts[0] = $1 | ||
elsif parts[0] =~ /\w+\.(\w+)/ | ||
parts[0] = $1 | ||
end | ||
parts.join(' ') | ||
end.join(', ') | ||
sql << " ORDER BY #{change_order_direction(options[:order])}) AS tmp2 ORDER BY #{options[:order]}" | ||
else | ||
sql << " ) AS tmp2" | ||
end | ||
elsif sql !~ /^\s*SELECT (@@|COUNT\()/i | ||
sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i) do | ||
"SELECT#{$1} TOP #{options[:limit]}" | ||
end unless options[:limit].nil? | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
config = { | ||
:username => 'blog', | ||
:password => '', | ||
:adapter => 'jdbc', | ||
:url => "jdbc:jtds:sqlserver://localhost:1433/weblog_development", | ||
:driver => 'net.sourceforge.jtds.jdbc.Driver' | ||
} | ||
|
||
ActiveRecord::Base.establish_connection( config ) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'jdbc_common' | ||
require 'db/mssql' | ||
|
||
class MsSQLSimpleTest < Test::Unit::TestCase | ||
include SimpleTestMethods | ||
end |