1 # Common methods for handling TSQL databases.
4 def modify_types(tp) #:nodoc:
5 tp[:primary_key] = "int NOT NULL IDENTITY(1, 1) PRIMARY KEY"
6 tp[:integer][:limit] = nil
7 tp[:boolean] = {:name => "bit"}
8 tp[:binary] = { :name => "image"}
12 def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
13 return super unless type.to_s == 'integer'
15 if limit.nil? || limit == 4
26 def add_limit_offset!(sql, options)
27 if options[:limit] and options[:offset]
28 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
29 if (options[:limit] + options[:offset]) >= total_rows
30 options[:limit] = (total_rows - options[:offset] >= 0) ? (total_rows - options[:offset]) : 0
32 sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i, "SELECT * FROM (SELECT TOP #{options[:limit]} * FROM (SELECT\\1 TOP #{options[:limit] + options[:offset]} ")
35 options[:order] = options[:order].split(',').map do |field|
36 parts = field.split(" ")
38 if sql =~ /\.\[/ and tc =~ /\./ # if column quoting used in query
39 tc.gsub!(/\./, '\\.\\[')
42 if sql =~ /#{tc} AS (t\d_r\d\d?)/
44 elsif parts[0] =~ /\w+\.(\w+)/
49 sql << " ORDER BY #{change_order_direction(options[:order])}) AS tmp2 ORDER BY #{options[:order]}"
53 elsif sql !~ /^\s*SELECT (@@|COUNT\()/i
54 sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i) do
55 "SELECT#{$1} TOP #{options[:limit]}"
56 end unless options[:limit].nil?