Class: TinyTds::Client

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tiny_tds/client.rb', line 36

def initialize(opts = {})
  if opts[:dataserver].to_s.empty? && opts[:host].to_s.empty?
    raise ArgumentError, 'missing :host option if no :dataserver given'
  end
  opts[:username] = parse_username(opts)
  @query_options = self.class.default_query_options.dup
  opts[:password] = opts[:password].to_s if opts[:password] && opts[:password].to_s.strip != ''
  opts[:appname] ||= 'TinyTds'
  opts[:tds_version] = tds_versions_setter(opts)
  opts[:use_utf16] = opts[:use_utf16].nil? || ["true", "1", "yes"].include?(opts[:use_utf16].to_s)
  opts[:login_timeout] ||= 60
  opts[:timeout] ||= 5
  opts[:encoding] = opts[:encoding].nil? || opts[:encoding].casecmp('utf8').zero? ? 'UTF-8' : opts[:encoding].upcase
  opts[:port] ||= 1433
  opts[:dataserver] = "#{opts[:host]}:#{opts[:port]}" if opts[:dataserver].to_s.empty?
  forced_integer_keys = [:login_timeout, :port, :timeout]
  forced_integer_keys.each { |k| opts[k] = opts[k].to_i if opts[k] }
  connect(opts)
end

Class Attribute Details

.default_query_optionsObject (readonly)

Returns the value of attribute default_query_options


16
17
18
# File 'lib/tiny_tds/client.rb', line 16

def default_query_options
  @default_query_options
end

Instance Attribute Details

#query_optionsObject (readonly)

Returns the value of attribute query_options


12
13
14
# File 'lib/tiny_tds/client.rb', line 12

def query_options
  @query_options
end

Class Method Details

.local_offsetObject


26
27
28
# File 'lib/tiny_tds/client.rb', line 26

def local_offset
  ::Time.local(2010).utc_offset.to_r / 86_400
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.


22
23
24
# File 'lib/tiny_tds/client.rb', line 22

def transpose_iconv_encoding(encoding)
  encoding
end

Instance Method Details

#active?Boolean


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

def active?
  !closed? && !dead?
end

#canceled?Boolean


223
224
225
226
# File 'ext/tiny_tds/client.c', line 223

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

#charsetObject


255
256
257
258
# File 'ext/tiny_tds/client.c', line 255

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

#closeObject


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

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


218
219
220
221
# File 'ext/tiny_tds/client.c', line 218

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

#dead?Boolean


213
214
215
216
# File 'ext/tiny_tds/client.c', line 213

static VALUE rb_tinytds_dead(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return dbdead(cwrap->client) ? Qtrue : Qfalse;
}

#encodingObject


260
261
262
263
# File 'ext/tiny_tds/client.c', line 260

static VALUE rb_tinytds_encoding(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return rb_enc_from_encoding(cwrap->encoding);
}

#escape(string) ⇒ Object


265
266
267
268
269
270
271
272
273
# File 'ext/tiny_tds/client.c', line 265

static VALUE rb_tinytds_escape(VALUE self, VALUE string) {
  VALUE new_string;
  GET_CLIENT_WRAPPER(self);

  Check_Type(string, T_STRING);
  new_string = rb_funcall(string, intern_gsub, 2, opt_escape_regex, opt_escape_dblquote);
  rb_enc_associate(new_string, cwrap->encoding);
  return new_string;
}

#execute(sql) ⇒ Object


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/tiny_tds/client.c', line 233

static VALUE rb_tinytds_execute(VALUE self, VALUE sql) {
  VALUE result;

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

#identity_sqlObject


285
286
287
288
# File 'ext/tiny_tds/client.c', line 285

static VALUE rb_tinytds_identity_sql(VALUE self) {
  GET_CLIENT_WRAPPER(self);
  return rb_str_new2(cwrap->identity_insert_sql);
}

#return_codeObject

Duplicated in result.c


276
277
278
279
280
281
282
283
# File 'ext/tiny_tds/client.c', line 276

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


228
229
230
231
# File 'ext/tiny_tds/client.c', line 228

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

#tds_73?Boolean


56
57
58
# File 'lib/tiny_tds/client.rb', line 56

def tds_73?
  tds_version >= 11
end

#tds_versionObject

TinyTds::Client (public)


198
199
200
201
# File 'ext/tiny_tds/client.c', line 198

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