Class: DataObjects::SqlServer::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/do_sqlserver.rb

Overview

end

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Connection

Returns a new instance of Connection.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/do_sqlserver.rb', line 33

def initialize uri
  # REVISIT: Allow uri.query to modify this connection's mode?
  #host = uri.host.blank? ? "localhost" : uri.host
  host = uri.host.blank? ? nil : uri.host
  user = uri.user || "sa"
  password = uri.password || ""
  path = uri.path.sub(%r{^/*}, '')
  port = uri.port || "1433"
  if Mode == :ado
    connection_string = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{path};User ID=#{user};Password=#{password};"
  else
    # FIXME: Cannot get a DNS-less configuration without freetds.conf to
    # connect successfully, i.e.:
    # connection_string = "DBI:ODBC:DRIVER=FreeTDS;SERVER=#{host};DATABASE=#{path};TDS_Version=5.0;Port=#{port}"
    #
    # Currently need to setup a dataserver entry in freetds.conf (if
    # using MacPorts, full path is /opt/local/etc/freetds/freetds.conf):
    #
    # [sqlserver]
    #   host = hostname
    #   port = 1433
    #   instance = SQLEXPRESS
    #   tds version = 8.0
    #
    connection_string = "DBI:ODBC:DRIVER=FreeTDS;SERVERNAME=sqlserver;DATABASE=#{path};"
  end

  begin
    @connection = DBI.connect(connection_string, user, password)
  rescue DBI::DatabaseError => e
    # Place to debug connection failures
    raise
  end

  @encoding = uri.query && uri.query["encoding"] || "utf8"

  set_date_format = create_command("SET DATEFORMAT YMD").execute_non_query
  options_reader = create_command("DBCC USEROPTIONS").execute_reader
  while options_reader.next!
    key, value = *options_reader.values
    value = options_reader.values
    case key
    when "textsize"                     # "64512"
    when "language"                     # "us_english", "select * from master..syslanguages" for info
    when "dateformat"                   # "ymd"
    when "datefirst"                    # "7" = Sunday, first day of the week, change with "SET DATEFIRST"
    when "quoted_identifier"            # "SET"
    when "ansi_null_dflt_on"            # "SET"
    when "ansi_defaults"                # "SET"
    when "ansi_warnings"                # "SET"
    when "ansi_padding"                 # "SET"
    when "ansi_nulls"                   # "SET"
    when "concat_null_yields_null"      # "SET"
    else
    end
  end
end

Instance Method Details

#character_setObject



96
97
98
# File 'lib/do_sqlserver.rb', line 96

def character_set
  @encoding
end

#disposeObject



100
101
102
103
104
105
# File 'lib/do_sqlserver.rb', line 100

def dispose
  @connection.disconnect
  true
rescue
  false
end

#rawObject



107
108
109
# File 'lib/do_sqlserver.rb', line 107

def raw
  @connection
end

#using_socket?Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/do_sqlserver.rb', line 91

def using_socket?
  # This might be an unnecessary feature dragged from the mysql driver
  raise "Not yet implemented"
end