Class: TinyTds::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_tds/client.rb,
ext/tiny_tds/client.c

Constant Summary collapse

TDS_VERSIONS_SETTERS =
{
  'unknown' => 0,
  '46'      => 1,
  '100'     => 2,
  '42'      => 3,
  '70'      => 4,
  '80'      => 5,
  '90'      => 6  # TODO: untested
}.freeze
TDS_VERSIONS_GETTERS =
{
  0  => {:name => 'DBTDS_UNKNOWN', :description => 'Unknown'},
  1  => {:name => 'DBTDS_2_0',     :description => 'Pre 4.0 SQL Server'},
  2  => {:name => 'DBTDS_3_4',     :description => 'Microsoft SQL Server (3.0)'},
  3  => {:name => 'DBTDS_4_0',     :description => '4.0 SQL Server'},
  4  => {:name => 'DBTDS_4_2',     :description => '4.2 SQL Server'},
  5  => {:name => 'DBTDS_4_6',     :description => '2.0 OpenServer and 4.6 SQL Server.'},
  6  => {:name => 'DBTDS_4_9_5',   :description => '4.9.5 (NCR) SQL Server'},
  7  => {:name => 'DBTDS_5_0',     :description => '5.0 SQL Server'},
  8  => {:name => 'DBTDS_7_0',     :description => 'Microsoft SQL Server 7.0'},
  9  => {:name => 'DBTDS_8_0',     :description => 'Microsoft SQL Server 2000'},
  10 => {:name => 'DBTDS_9_0',     :description => 'Microsoft SQL Server 2005'}
}.freeze
@@default_query_options =
{
  :as => :hash,
  :symbolize_keys => false,
  :cache_rows => true,
  :timezone => :local
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client

Raises:

  • (ArgumentError)

49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tiny_tds/client.rb', line 49

def initialize(opts={})
  @query_options = @@default_query_options.dup
  user       = opts[:username]
  pass       = opts[:password]
  dataserver = opts[:dataserver]
  database   = opts[:database]
  appname    = opts[:appname] || 'TinyTds'
  version    = TDS_VERSIONS_SETTERS[opts[:tds_version].to_s] || TDS_VERSIONS_SETTERS['80']
  ltimeout   = opts[:login_timeout] || 60
  timeout    = opts[:timeout] || 5
  encoding   = (opts[:encoding].nil? || opts[:encoding].downcase == 'utf8') ? 'UTF-8' : opts[:encoding].upcase
  raise ArgumentError, 'missing :username option' if user.nil? || user.empty?
  connect(user, pass, dataserver, database, appname, version, ltimeout, timeout, encoding)
end

Instance Attribute Details

#query_optionsObject (readonly)

Returns the value of attribute query_options


35
36
37
# File 'lib/tiny_tds/client.rb', line 35

def query_options
  @query_options
end

Class Method Details

.default_query_optionsObject


37
38
39
# File 'lib/tiny_tds/client.rb', line 37

def self.default_query_options
  @@default_query_options
end

.transpose_iconv_encoding(encoding) ⇒ Object

Most, if not all, iconv encoding names can be found by ruby. Just in case, you can overide this method to return a string name that Encoding.find would work with. Default is to return the passed encoding.


44
45
46
# File 'lib/tiny_tds/client.rb', line 44

def self.transpose_iconv_encoding(encoding)
  encoding
end

Instance Method Details

#charsetObject


127
128
129
130
# File 'ext/tiny_tds/client.c', line 127

static VALUE rb_tinytds_charset(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return cwrap->charset;
}

#closeObject


94
95
96
97
98
99
100
101
# File 'ext/tiny_tds/client.c', line 94

static VALUE rb_tinytds_close(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  if (cwrap->client && !cwrap->closed) {
    dbclose(cwrap->client);
    cwrap->closed = 1;
  }
  return Qtrue;
}

#closed?Boolean


103
104
105
106
# File 'ext/tiny_tds/client.c', line 103

static VALUE rb_tinytds_closed(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return cwrap->closed ? Qtrue : Qfalse;
}

#encodingObject


132
133
134
135
136
137
138
139
# File 'ext/tiny_tds/client.c', line 132

static VALUE rb_tinytds_encoding(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  #ifdef HAVE_RUBY_ENCODING_H
    return rb_enc_from_encoding(cwrap->encoding);
  #else
    return Qnil;
  #endif
}

#escape(string) ⇒ Object


141
142
143
144
145
146
147
148
149
# File 'ext/tiny_tds/client.c', line 141

static VALUE rb_tinytds_escape(VALUE self, VALUE string) {
  Check_Type(string, T_STRING);
  GET_CLIENT_WRAPPER(self);
  VALUE new_string = rb_funcall(string, intern_gsub, 2, opt_escape_regex, opt_escape_dblquote);
  #ifdef HAVE_RUBY_ENCODING_H
    rb_enc_associate(new_string, cwrap->encoding);
  #endif
  return new_string;
}

#execute(sql) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'ext/tiny_tds/client.c', line 108

static VALUE rb_tinytds_execute(VALUE self, VALUE sql) {
  GET_CLIENT_WRAPPER(self);
  REQUIRE_OPEN_CLIENT(cwrap);
  dbcmd(cwrap->client, StringValuePtr(sql));
  if (dbsqlexec(cwrap->client) == FAIL) {
    // TODO:  for dbsqlexec() returned FAIL.
    rb_warn("TinyTds: dbsqlexec() returned FAIL.\n");
    return Qfalse;
  }
  VALUE result = rb_tinytds_new_result_obj(cwrap->client);
  rb_iv_set(result, "@query_options", rb_funcall(rb_iv_get(self, "@query_options"), intern_dup, 0));
  GET_RESULT_WRAPPER(result);
  rwrap->local_offset = rb_funcall(cTinyTdsClient, intern_local_offset, 0);
  #ifdef HAVE_RUBY_ENCODING_H
    rwrap->encoding = cwrap->encoding;
  #endif
  return result;  
}

#tds_versionObject

TinyTds::Client (public)


89
90
91
92
# File 'ext/tiny_tds/client.c', line 89

static VALUE rb_tinytds_tds_version(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return INT2FIX(dbtds(cwrap->client));
}

#tds_version_infoObject


64
65
66
67
# File 'lib/tiny_tds/client.rb', line 64

def tds_version_info
  info = TDS_VERSIONS_GETTERS[tds_version]
  "#{info[:name]} - #{info[:description]}" if info
end