Title: [971] trunk/activerecord-jdbc: Refactored cache and mssql adapters to share common code.
Revision
971
Author
kofno
Date
2008-04-23 09:47:30 -0400 (Wed, 23 Apr 2008)

Log Message

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

Modified Paths

Added Paths

Diff

Modified: trunk/activerecord-jdbc/Rakefile (970 => 971)


--- trunk/activerecord-jdbc/Rakefile	2008-04-23 13:47:12 UTC (rev 970)
+++ trunk/activerecord-jdbc/Rakefile	2008-04-23 13:47:30 UTC (rev 971)
@@ -91,6 +91,12 @@
   t.libs << 'test'
 end
 
+# Ensure that the jTDS driver in on your classpath before launching rake
+Rake::TestTask.new(:test_mssql) do | t |
+  t.test_files = FileList[ 'test/mssql_simple_test.rb' ]
+  t.libs << 'test'
+end
+
 MANIFEST = FileList["History.txt", "Manifest.txt", "README.txt", 
   "Rakefile", "LICENSE.txt", "lib/**/*.rb", "lib/jdbc_adapter/jdbc_adapter_internal.jar", "test/**/*.rb",
    "lib/**/*.rake", "src/**/*.java"]

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_cachedb.rb (970 => 971)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_cachedb.rb	2008-04-23 13:47:12 UTC (rev 970)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_cachedb.rb	2008-04-23 13:47:30 UTC (rev 971)
@@ -1,3 +1,5 @@
+require 'jdbc_adapter/tsql_helper'
+
 module ::JdbcSpec
   module ActiveRecordExtensions
     def cachedb_connection( config )
@@ -9,6 +11,7 @@
   end
 
   module CacheDB
+    include TSqlMethods
 
     def self.column_selector
       [ /cache/i, lambda {  | cfg, col | col.extend( ::JdbcSpec::CacheDB::Column ) } ]
@@ -20,26 +23,7 @@
 
     module Column
     end
-
-    def modify_types(tp)
-      tp[:primary_key] = "int NOT NULL IDENTITY(1, 1) PRIMARY KEY"
-      tp
-    end
-
-    def type_to_sql(type, limit = nil, precision = nil, scale = nil)
-      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 create_table(name, options = { })
       super(name, options)
       primary_key = options[:primary_key] || "id"

Modified: trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mssql.rb (970 => 971)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mssql.rb	2008-04-23 13:47:12 UTC (rev 970)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/jdbc_mssql.rb	2008-04-23 13:47:30 UTC (rev 971)
@@ -1,3 +1,5 @@
+require 'jdbc_adapter/tsql_helper'
+
 module ::ActiveRecord
   class Base
     # After setting large objects to empty, write data back with a helper method
@@ -19,6 +21,8 @@
 
 module JdbcSpec
   module MsSQL
+    include TSqlMethods
+    
     def self.column_selector
       [/sqlserver|tds/i, lambda {|cfg,col| col.extend(::JdbcSpec::MsSQL::Column)}]
     end
@@ -94,14 +98,6 @@
       end
     end
     
-    def modify_types(tp)
-      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 quote(value, column = nil)
       return value.quoted_id if value.respond_to?(:quoted_id)
 
@@ -132,41 +128,6 @@
         "[#{name}]"
       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
-    
-      
       def change_order_direction(order)
         order.split(",").collect {|fragment|
           case fragment
@@ -208,20 +169,6 @@
         execute "EXEC sp_rename '#{table}.#{column}', '#{new_column_name}'"
       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 change_column(table_name, column_name, type, options = {}) #:nodoc:
         sql_commands = ["ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"]
         if options_include_default?(options)

Added: trunk/activerecord-jdbc/lib/jdbc_adapter/tsql_helper.rb (0 => 971)


--- trunk/activerecord-jdbc/lib/jdbc_adapter/tsql_helper.rb	                        (rev 0)
+++ trunk/activerecord-jdbc/lib/jdbc_adapter/tsql_helper.rb	2008-04-23 13:47:30 UTC (rev 971)
@@ -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

Added: trunk/activerecord-jdbc/test/db/mssql.rb (0 => 971)


--- trunk/activerecord-jdbc/test/db/mssql.rb	                        (rev 0)
+++ trunk/activerecord-jdbc/test/db/mssql.rb	2008-04-23 13:47:30 UTC (rev 971)
@@ -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 )

Added: trunk/activerecord-jdbc/test/mssql_simple_test.rb (0 => 971)


--- trunk/activerecord-jdbc/test/mssql_simple_test.rb	                        (rev 0)
+++ trunk/activerecord-jdbc/test/mssql_simple_test.rb	2008-04-23 13:47:30 UTC (rev 971)
@@ -0,0 +1,6 @@
+require 'jdbc_common'
+require 'db/mssql'
+
+class MsSQLSimpleTest < Test::Unit::TestCase
+  include SimpleTestMethods
+end
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to