Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/sqlserver_adapter.rb

Class Method Summary collapse

Class Method Details

.construct_finder_sql(options) ⇒ Object

In the case of SQL server, the lock value must follow the FROM clause



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 70

def self.construct_finder_sql(options)
  scope = scope(:find)
  sql  = "SELECT #{(scope && scope[:select]) || options[:select] || '*'} "
  sql << "FROM #{(scope && scope[:from]) || options[:from] || table_name} "

  if ActiveRecord::Base.connection.adapter_name == "SQLServer" && !options[:lock].blank? # SQLServer
    add_lock!(sql, options, scope) 
  end

  add_joins!(sql, options, scope)
  add_conditions!(sql, options[:conditions], scope)

  sql << " GROUP BY #{options[:group]} " if options[:group]

  add_order!(sql, options[:order], scope)
  add_limit!(sql, options, scope)
  add_lock!(sql, options, scope)  unless ActiveRecord::Base.connection.adapter_name == "SQLServer" #  SQLServer
  #      $log.debug "database_helper: construct_finder_sql:  sql at end:  #{sql.inspect}"
  sql
end

.sanitize_sql_hash(attrs) ⇒ Object

Overridden to include support for SQL server’s lack of = operator on text/ntext/image columns LIKE operator is used instead



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 57

def self.sanitize_sql_hash(attrs)
  conditions = attrs.map do |attr, value|
    col = self.columns.find {|c| c.name == attr}
    if col && col.respond_to?("is_special") && col.is_special
      "#{table_name}.#{connection.quote_column_name(attr)} LIKE ?"
    else
      "#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
    end
  end.join(' AND ')
  replace_bind_variables(conditions, expand_range_bind_variables(attrs.values))
end

.sqlserver_connection(config) ⇒ Object

:nodoc:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_record/connection_adapters/sqlserver_adapter.rb', line 27

def self.sqlserver_connection(config) #:nodoc:
  require_library_or_gem 'dbi' unless self.class.const_defined?(:DBI)
  
  config = config.symbolize_keys

  mode        = config[:mode] ? config[:mode].to_s.upcase : 'ADO'
  username    = config[:username] ? config[:username].to_s : 'sa'
  password    = config[:password] ? config[:password].to_s : ''
  autocommit  = config.key?(:autocommit) ? config[:autocommit] : true
  if mode == "ODBC"
    raise ArgumentError, "Missing DSN. Argument ':dsn' must be set in order for this adapter to work." unless config.has_key?(:dsn)
    dsn       = config[:dsn]
    driver_url = "DBI:ODBC:#{dsn}"
  else
    raise ArgumentError, "Missing Database. Argument ':database' must be set in order for this adapter to work." unless config.has_key?(:database)
    database  = config[:database]
    host      = config[:host] ? config[:host].to_s : 'localhost'
    unless config[:trusted_connection]
      driver_url = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};"
    else
      driver_url = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};Trusted_Connection=Yes;"
    end
  end
  conn      = DBI.connect(driver_url, username, password)
  conn["AutoCommit"] = autocommit
  ConnectionAdapters::SQLServerAdapter.new(conn, logger, [driver_url, username, password])
end