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,
  '71'      => 5,
  '80'      => 5,
  '72'      => 6,
  '90'      => 6
}.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_7_1',     :description => 'Microsoft SQL Server 2000'},
  10 => {:name => 'DBTDS_7_2',     :description => 'Microsoft SQL Server 2005'}
}.freeze
@@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)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tiny_tds/client.rb', line 56

def initialize(opts={})
  raise ArgumentError, 'missing :username option' if opts[:username].to_s.empty?
  raise ArgumentError, 'missing :host option if no :dataserver given' if opts[:dataserver].to_s.empty? && opts[:host].to_s.empty?
  @query_options = @@default_query_options.dup
  opts[:appname] ||= 'TinyTds'
  opts[:tds_version] = TDS_VERSIONS_SETTERS[opts[:tds_version].to_s] || TDS_VERSIONS_SETTERS['71']
  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.



38
39
40
# File 'lib/tiny_tds/client.rb', line 38

def query_options
  @query_options
end

Class Method Details

.default_query_optionsObject



42
43
44
# File 'lib/tiny_tds/client.rb', line 42

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.



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

def transpose_iconv_encoding(encoding)
  encoding
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/tiny_tds/client.rb', line 75

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

#canceled?Boolean

Returns:

  • (Boolean)


173
174
175
176
# File 'ext/tiny_tds/client.c', line 173

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

#charsetObject



203
204
205
206
# File 'ext/tiny_tds/client.c', line 203

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

#closeObject



153
154
155
156
157
158
159
160
161
# File 'ext/tiny_tds/client.c', line 153

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)


168
169
170
171
# File 'ext/tiny_tds/client.c', line 168

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

#dead?Boolean

Returns:

  • (Boolean)


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

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

#encodingObject



208
209
210
211
212
213
214
215
# File 'ext/tiny_tds/client.c', line 208

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



217
218
219
220
221
222
223
224
225
# File 'ext/tiny_tds/client.c', line 217

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/tiny_tds/client.c', line 183

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;  
}

#freetds_091_or_higer?Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
243
# File 'ext/tiny_tds/client.c', line 237

static VALUE rb_tinytds_freetds_nine_one_or_higher(VALUE self) {
  #ifdef DBSETLDBNAME
    return Qtrue;
  #else
    return Qfalse;
  #endif
}

#return_codeObject

Duplicated in result.c



228
229
230
231
232
233
234
235
# File 'ext/tiny_tds/client.c', line 228

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)


178
179
180
181
# File 'ext/tiny_tds/client.c', line 178

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

#tds_versionObject

TinyTds::Client (public)



148
149
150
151
# File 'ext/tiny_tds/client.c', line 148

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

#tds_version_infoObject



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

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