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



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

def initialize(opts = {})
  if opts[:dataserver].to_s.empty? && opts[:host].to_s.empty?
    raise ArgumentError, 'missing :host option if no :dataserver given'
  end

  @message_handler = opts[:message_handler]
  if @message_handler && !@message_handler.respond_to?(:call)
    raise ArgumentError, ':message_handler must implement `call` (eg, a Proc or a Method)'
  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.



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

def default_query_options
  @default_query_options
end

Instance Attribute Details

#message_handlerObject (readonly)

Returns the value of attribute message_handler.



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

def message_handler
  @message_handler
end

#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



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

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.



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

def transpose_iconv_encoding(encoding)
  encoding
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/tiny_tds/client.rb', line 72

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

#canceled?Boolean

Returns:

  • (Boolean)


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

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

#charsetObject



315
316
317
318
# File 'ext/tiny_tds/client.c', line 315

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

#closeObject



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

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

#closed?Boolean

Returns:

  • (Boolean)


279
280
281
282
# File 'ext/tiny_tds/client.c', line 279

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

#dead?Boolean

Returns:

  • (Boolean)


274
275
276
277
# File 'ext/tiny_tds/client.c', line 274

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

#encodingObject



320
321
322
323
# File 'ext/tiny_tds/client.c', line 320

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

#escape(string) ⇒ Object



325
326
327
328
329
330
331
332
333
# File 'ext/tiny_tds/client.c', line 325

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'ext/tiny_tds/client.c', line 294

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_raise(cTinyTdsError, "failed dbsqlsend() function");
  }
  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



345
346
347
348
# File 'ext/tiny_tds/client.c', line 345

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



336
337
338
339
340
341
342
343
# File 'ext/tiny_tds/client.c', line 336

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)


289
290
291
292
# File 'ext/tiny_tds/client.c', line 289

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

#tds_73?Boolean

Returns:

  • (Boolean)


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

def tds_73?
  tds_version >= 11
end

#tds_versionObject

TinyTds::Client (public)



258
259
260
261
# File 'ext/tiny_tds/client.c', line 258

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

#tds_version_infoObject



67
68
69
70
# File 'lib/tiny_tds/client.rb', line 67

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