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



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

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.



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

def default_query_options
  @default_query_options
end

Instance Attribute Details

#message_handlerObject (readonly)

Returns the value of attribute message_handler.



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

def message_handler
  @message_handler
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



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

def query_options
  @query_options
end

Class Method Details

.local_offsetObject



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

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.



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

def transpose_iconv_encoding(encoding)
  encoding
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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

#canceled?Boolean

Returns:

  • (Boolean)


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

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

#charsetObject



373
374
375
376
377
# File 'ext/tiny_tds/client.c', line 373

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

#closeObject



311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/tiny_tds/client.c', line 311

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)


331
332
333
334
335
# File 'ext/tiny_tds/client.c', line 331

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

#dead?Boolean

Returns:

  • (Boolean)


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

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

#encodingObject



379
380
381
382
383
# File 'ext/tiny_tds/client.c', line 379

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

#escape(string) ⇒ Object



385
386
387
388
389
390
391
392
393
394
# File 'ext/tiny_tds/client.c', line 385

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



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'ext/tiny_tds/client.c', line 349

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



408
409
410
411
412
# File 'ext/tiny_tds/client.c', line 408

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



397
398
399
400
401
402
403
404
405
406
# File 'ext/tiny_tds/client.c', line 397

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)


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

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

#tds_73?Boolean

Returns:

  • (Boolean)


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

def tds_73?
  tds_version >= 11
end

#tds_versionObject

TinyTds::Client (public)



305
306
307
308
309
# File 'ext/tiny_tds/client.c', line 305

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