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
# File 'lib/tiny_tds/client.rb', line 49

def initialize(opts={})
  raise ArgumentError, 'missing :username option' if opts[:username].nil? || opts[:username].empty?
  @query_options = @@default_query_options.dup
  opts[:appname] ||= 'TinyTds'
  opts[:tds_version] = TDS_VERSIONS_SETTERS[opts[:tds_version].to_s] || TDS_VERSIONS_SETTERS['80']
  opts[:login_timeout] ||= 60
  opts[:timeout] ||= 5
  opts[:encoding] = (opts[:encoding].nil? || opts[:encoding].downcase == 'utf8') ? 'UTF-8' : opts[:encoding].upcase
  connect(opts)
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

#canceled?Boolean

Returns:

  • (Boolean)


158
159
160
161
# File 'ext/tiny_tds/client.c', line 158

static VALUE rb_tinytds_canceled(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return cwrap->userdata->dbcancel_sent ? Qtrue : Qfalse;
}

#charsetObject



188
189
190
191
# File 'ext/tiny_tds/client.c', line 188

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

#closeObject



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

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

#closed?Boolean

Returns:

  • (Boolean)


153
154
155
156
# File 'ext/tiny_tds/client.c', line 153

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

#encodingObject



193
194
195
196
197
198
199
200
# File 'ext/tiny_tds/client.c', line 193

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



202
203
204
205
206
207
208
209
210
# File 'ext/tiny_tds/client.c', line 202

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'ext/tiny_tds/client.c', line 168

static VALUE rb_tinytds_execute(VALUE self, VALUE sql) {
  GET_CLIENT_WRAPPER(self);
  rb_tinytds_client_reset_userdata(cwrap->userdata);
  REQUIRE_OPEN_CLIENT(cwrap);
  dbcmd(cwrap->client, StringValuePtr(sql));
  if (dbsqlsend(cwrap->client) == FAIL) {
    rb_warn("TinyTds: dbsqlsend() returned FAIL.\n");
    return Qfalse;
  }
  cwrap->userdata->dbsql_sent = 1;
  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;  
}

#return_codeObject

Duplicated in result.c



213
214
215
216
217
218
219
220
# File 'ext/tiny_tds/client.c', line 213

static VALUE rb_tinytds_return_code(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  if (cwrap->client && dbhasretstat(cwrap->client)) {
    return LONG2NUM((long)dbretstatus(cwrap->client));
  } else {
    return Qnil;
  }
}

#sqlsent?Boolean

Returns:

  • (Boolean)


163
164
165
166
# File 'ext/tiny_tds/client.c', line 163

static VALUE rb_tinytds_sqlsent(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return cwrap->userdata->dbsql_sent ? Qtrue : Qfalse;
}

#tds_versionObject

TinyTds::Client (public)



138
139
140
141
# File 'ext/tiny_tds/client.c', line 138

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

#tds_version_infoObject



60
61
62
63
# File 'lib/tiny_tds/client.rb', line 60

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