Skip to content

Commit 206898d

Browse files
committed
Separate #new and #connect
1 parent cd7c774 commit 206898d

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
* `#execute`: Replaced `opts` hash with keyword arguments
99
* Removed `symbolize_keys` and `cache_rows` from `#default_query_options`
1010
* `TinyTds::Client.new` now accepts keyword arguments instead of a hash
11-
* Renamed `tds_version` and `tds_version_info` to `server_version` and `server_version_info`.
11+
* Renamed `tds_version` and `tds_version_info` to `server_version` and `server_version_info`
12+
* Separate `#new` and `#connect`
13+
* Instead, before running `#do`, `#execute` or `#insert`, `tiny_tds` will check if the connection is active and re-connect if needed.
1214

1315
## 3.3.0
1416

ext/tiny_tds/client.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
VALUE cTinyTdsClient;
66
extern VALUE mTinyTds, cTinyTdsError;
77
static ID intern_source_eql, intern_severity_eql, intern_db_error_number_eql, intern_os_error_number_eql;
8-
static ID intern_new, intern_dup, intern_transpose_iconv_encoding, intern_local_offset, intern_gsub, intern_call;
8+
static ID intern_new, intern_dup, intern_transpose_iconv_encoding, intern_local_offset, intern_gsub, intern_call, intern_active, intern_connect;
99
VALUE opt_escape_regex, opt_escape_dblquote;
1010

1111
static ID id_ivar_fields, id_ivar_rows, id_ivar_return_code, id_ivar_affected_rows, id_ivar_default_query_options, intern_bigd, intern_divide;
@@ -468,11 +468,15 @@ static VALUE allocate(VALUE klass)
468468

469469

470470
// TinyTds::Client (public)
471-
472471
static VALUE rb_tinytds_server_version(VALUE self)
473472
{
474473
GET_CLIENT_WRAPPER(self);
475-
return INT2FIX(dbtds(cwrap->client));
474+
475+
if (rb_funcall(self, intern_active, 0) == Qtrue) {
476+
return INT2FIX(dbtds(cwrap->client));
477+
} else {
478+
return Qnil;
479+
}
476480
}
477481

478482
static VALUE rb_tinytds_close(VALUE self)
@@ -745,6 +749,11 @@ static VALUE rb_tinytds_execute(int argc, VALUE *argv, VALUE self)
745749
}
746750

747751
GET_CLIENT_WRAPPER(self);
752+
753+
if (rb_funcall(self, intern_active, 0) == Qfalse) {
754+
rb_funcall(self, intern_connect, 0);
755+
}
756+
748757
rb_tinytds_send_sql_to_server(cwrap, sql);
749758

750759
VALUE result = rb_obj_alloc(cTinyTdsResult);
@@ -856,6 +865,11 @@ static VALUE rb_tiny_tds_insert(VALUE self, VALUE sql)
856865
{
857866
VALUE identity = Qnil;
858867
GET_CLIENT_WRAPPER(self);
868+
869+
if (rb_funcall(self, intern_active, 0) == Qfalse) {
870+
rb_funcall(self, intern_connect, 0);
871+
}
872+
859873
rb_tinytds_send_sql_to_server(cwrap, sql);
860874
rb_tinytds_result_exec_helper(cwrap->client);
861875

@@ -885,6 +899,11 @@ static VALUE rb_tiny_tds_insert(VALUE self, VALUE sql)
885899
static VALUE rb_tiny_tds_do(VALUE self, VALUE sql)
886900
{
887901
GET_CLIENT_WRAPPER(self);
902+
903+
if (rb_funcall(self, intern_active, 0) == Qfalse) {
904+
rb_funcall(self, intern_connect, 0);
905+
}
906+
888907
rb_tinytds_send_sql_to_server(cwrap, sql);
889908
rb_tinytds_result_exec_helper(cwrap->client);
890909

@@ -1054,8 +1073,7 @@ void init_tinytds_client()
10541073
rb_define_method(cTinyTdsClient, "escape", rb_tinytds_escape, 1);
10551074
rb_define_method(cTinyTdsClient, "return_code", rb_tinytds_return_code, 0);
10561075
rb_define_method(cTinyTdsClient, "identity_sql", rb_tinytds_identity_sql, 0);
1057-
/* Define TinyTds::Client Protected Methods */
1058-
rb_define_protected_method(cTinyTdsClient, "connect", rb_tinytds_connect, 0);
1076+
rb_define_method(cTinyTdsClient, "connect", rb_tinytds_connect, 0);
10591077
/* Intern TinyTds::Error Accessors */
10601078
intern_source_eql = rb_intern("source=");
10611079
intern_severity_eql = rb_intern("severity=");
@@ -1088,6 +1106,8 @@ void init_tinytds_client()
10881106
intern_timezone = rb_intern("timezone");
10891107
intern_utc = rb_intern("utc");
10901108
intern_local = rb_intern("local");
1109+
intern_active = rb_intern("active?");
1110+
intern_connect = rb_intern("connect");
10911111

10921112
cTinyTdsClient = rb_const_get(mTinyTds, rb_intern("Client"));
10931113
cTinyTdsResult = rb_const_get(mTinyTds, rb_intern("Result"));

lib/tiny_tds/client.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ def initialize(app_name: "TinyTds", azure: false, contained: false, database: ni
5252
@tds_version = tds_versions_setter(tds_version:)
5353
@username = parse_username(azure:, host:, username:)
5454
@use_utf16 = use_utf16.nil? || ["true", "1", "yes"].include?(use_utf16.to_s)
55-
56-
connect
5755
end
5856

5957
def tds_73?
@@ -71,7 +69,7 @@ def active?
7169

7270
private
7371

74-
def parse_username(azure: false, host: nil, username:)
72+
def parse_username(username:, azure: false, host: nil)
7573
return username if username.nil? || !azure
7674
return username if username.include?("@") && !username.include?("database.windows.net")
7775
user, domain = username.split("@")

test/client_test.rb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@ class ClientTest < TinyTds::TestCase
1010
@client = new_connection
1111
end
1212

13-
it "must not be closed" do
14-
assert !@client.closed?
15-
assert @client.active?
13+
it "is considered close without a connection" do
14+
client = TinyTds::Client.new(**connection_options)
15+
16+
assert client.closed?
17+
assert !client.active?
18+
end
19+
20+
it "returns nil values for server version without a connection" do
21+
client = TinyTds::Client.new(**connection_options)
22+
23+
assert_nil client.server_version
24+
assert_nil client.server_version_info
1625
end
1726

1827
it "allows client connection to be closed" do
1928
assert @client.close
2029
assert @client.closed?
2130
assert !@client.active?
2231
assert @client.dead?
23-
action = lambda { @client.execute("SELECT 1 as [one]").each }
24-
assert_raise_tinytds_error(action) do |e|
25-
assert_match %r{closed connection}i, e.message, "ignore if non-english test run"
26-
end
2732
end
2833

2934
it "has getters for the server version information (brittle since conf takes precedence)" do
@@ -299,15 +304,5 @@ class ClientTest < TinyTds::TestCase
299304
assert_client_works(@client)
300305
end
301306
end
302-
303-
it "throws an error if client is closed" do
304-
@client.close
305-
assert @client.closed?
306-
307-
action = lambda { @client.insert("SELECT 1 as [one]") }
308-
assert_raise_tinytds_error(action) do |e|
309-
assert_match %r{closed connection}i, e.message
310-
end
311-
end
312307
end
313308
end

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def close_client(client = @client)
4343

4444
def new_connection(**options)
4545
client = TinyTds::Client.new(**connection_options(options))
46+
4647
if sqlserver_azure?
4748
client.do("SET ANSI_NULLS ON")
4849
client.do("SET CURSOR_CLOSE_ON_COMMIT OFF")

0 commit comments

Comments
 (0)