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 =

From sybdb.h comments: DBVERSION_xxx are used with dbsetversion()

{
  'unknown' => 0,
  '46'      => 1,
  '100'     => 2,
  '42'      => 3,
  '70'      => 4,
  '71'      => 5,
  '80'      => 5,
  '72'      => 6,
  '90'      => 6
}.freeze
TDS_VERSIONS_GETTERS =

From sybdb.h comments: DBTDS_xxx are returned by DBTDS() The integer values of the constants are poorly chosen.

{
  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/DBTDS_8_0',  :description => 'Microsoft SQL Server 2000'},
  10 => {:name => 'DBTDS_7_2/DBTDS_9_0',  :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)

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tiny_tds/client.rb', line 63

def initialize(opts={})
  if opts[:password] && opts[:password].to_s.strip != ''
    opts[:password] = opts[:password].to_s
    warn 'FreeTDS may have issues with passwords longer than 30 characters!' if opts[:password].length > 30
  end
  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


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

def query_options
  @query_options
end

Class Method Details

.default_query_optionsObject


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

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.


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

def transpose_iconv_encoding(encoding)
  encoding
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

85
86
87
# File 'lib/tiny_tds/client.rb', line 85

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


201
202
203
204
# File 'ext/tiny_tds/client.c', line 201

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


206
207
208
209
# File 'ext/tiny_tds/client.c', line 206

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

#escape(string) ⇒ Object


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

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);
  rb_enc_associate(new_string, cwrap->encoding);
  return new_string;
}

#execute(sql) ⇒ Object


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# 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);
  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;  
}

#freetds_091_or_higer?Boolean

Returns:

  • (Boolean)

234
235
236
237
238
239
240
# File 'ext/tiny_tds/client.c', line 234

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

#identity_sqlObject


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

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


220
221
222
223
224
225
226
227
# File 'ext/tiny_tds/client.c', line 220

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


80
81
82
83
# File 'lib/tiny_tds/client.rb', line 80

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