Class: TinyTds::Client

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

Constant Summary collapse

@@default_query_options =
{
  :as => :hash,
  :symbolize_keys => false,
  :cache_rows => true,
  :timezone => :local,
  :empty_sets => true
}

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)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tiny_tds/client.rb', line 31

def initialize(opts={})
  raise ArgumentError, 'missing :host option if no :dataserver given' if opts[:dataserver].to_s.empty? && opts[:host].to_s.empty?
  opts[:username] = parse_username(opts)
  @query_options = @@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[:login_timeout] ||= 60
  opts[:timeout] ||= 5
  opts[:encoding] = (opts[:encoding].nil? || opts[:encoding].downcase == 'utf8') ? 'UTF-8' : opts[:encoding].upcase
  opts[:port] ||= 1433
  opts[:dataserver] = "#{opts[:host]}:#{opts[:port]}" if opts[:dataserver].to_s.empty?
  connect(opts)
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

.default_query_optionsObject



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

def 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.



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

def transpose_iconv_encoding(encoding)
  encoding
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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

#canceled?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


46
47
48
# File 'lib/tiny_tds/client.rb', line 46

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



50
51
52
53
# File 'lib/tiny_tds/client.rb', line 50

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