Skip to content

Commit 321c66a

Browse files
committed
Rename encoding keyword argument to charset
`encoding` denotes the actual `Encoding` then used to associate strings.
1 parent 206898d commit 321c66a

File tree

3 files changed

+12
-40
lines changed

3 files changed

+12
-40
lines changed

ext/tiny_tds/client.c

Lines changed: 4 additions & 24 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, intern_active, intern_connect;
8+
static ID intern_new, intern_dup, 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;
@@ -15,15 +15,6 @@ static VALUE cTinyTdsResult, cKernel, cDate;
1515
rb_encoding *binaryEncoding;
1616
VALUE opt_onek, opt_onebil, opt_float_zero, opt_four, opt_tenk;
1717

18-
static void rb_tinytds_client_mark(void *ptr)
19-
{
20-
tinytds_client_wrapper *cwrap = (tinytds_client_wrapper *)ptr;
21-
22-
if (cwrap) {
23-
rb_gc_mark(cwrap->charset);
24-
}
25-
}
26-
2718
static void rb_tinytds_client_free(void *ptr)
2819
{
2920
tinytds_client_wrapper *cwrap = (tinytds_client_wrapper *)ptr;
@@ -51,7 +42,7 @@ static size_t tinytds_client_wrapper_size(const void* data)
5142
static const rb_data_type_t tinytds_client_wrapper_type = {
5243
.wrap_struct_name = "tinytds_client_wrapper",
5344
.function = {
54-
.dmark = rb_tinytds_client_mark,
45+
.dmark = NULL,
5546
.dfree = rb_tinytds_client_free,
5647
.dsize = tinytds_client_wrapper_size,
5748
},
@@ -459,7 +450,6 @@ static VALUE allocate(VALUE klass)
459450
tinytds_client_wrapper *cwrap;
460451
obj = TypedData_Make_Struct(klass, tinytds_client_wrapper, &tinytds_client_wrapper_type, cwrap);
461452
cwrap->closed = 1;
462-
cwrap->charset = Qnil;
463453
cwrap->userdata = malloc(sizeof(tinytds_client_userdata));
464454
cwrap->userdata->closed = 1;
465455
rb_tinytds_client_reset_userdata(cwrap->userdata);
@@ -910,12 +900,6 @@ static VALUE rb_tiny_tds_do(VALUE self, VALUE sql)
910900
return rb_tinytds_affected_rows(cwrap->client);
911901
}
912902

913-
static VALUE rb_tinytds_charset(VALUE self)
914-
{
915-
GET_CLIENT_WRAPPER(self);
916-
return cwrap->charset;
917-
}
918-
919903
static VALUE rb_tinytds_encoding(VALUE self)
920904
{
921905
GET_CLIENT_WRAPPER(self);
@@ -954,7 +938,7 @@ static VALUE rb_tinytds_connect(VALUE self)
954938
contained = rb_iv_get(self, "@contained");
955939
database = rb_iv_get(self, "@database");
956940
dataserver = rb_iv_get(self, "@dataserver");
957-
charset = rb_iv_get(self, "@encoding");
941+
charset = rb_iv_get(self, "@charset");
958942
login_timeout = rb_iv_get(self, "@login_timeout");
959943
password = rb_iv_get(self, "@password");
960944
tds_version = rb_iv_get(self, "@tds_version");
@@ -1021,7 +1005,6 @@ static VALUE rb_tinytds_connect(VALUE self)
10211005
VALUE transposed_encoding, timeout_string;
10221006

10231007
cwrap->closed = 0;
1024-
cwrap->charset = charset;
10251008

10261009
if (!NIL_P(tds_version)) {
10271010
dbsetversion(NUM2INT(tds_version));
@@ -1043,8 +1026,7 @@ static VALUE rb_tinytds_connect(VALUE self)
10431026
dbuse(cwrap->client, StringValueCStr(database));
10441027
}
10451028

1046-
transposed_encoding = rb_funcall(cTinyTdsClient, intern_transpose_iconv_encoding, 1, charset);
1047-
cwrap->encoding = rb_enc_find(StringValueCStr(transposed_encoding));
1029+
cwrap->encoding = rb_enc_find(StringValueCStr(charset));
10481030
cwrap->identity_insert_sql = "SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident";
10491031
}
10501032

@@ -1068,7 +1050,6 @@ void init_tinytds_client()
10681050
rb_define_method(cTinyTdsClient, "execute", rb_tinytds_execute, -1);
10691051
rb_define_method(cTinyTdsClient, "insert", rb_tiny_tds_insert, 1);
10701052
rb_define_method(cTinyTdsClient, "do", rb_tiny_tds_do, 1);
1071-
rb_define_method(cTinyTdsClient, "charset", rb_tinytds_charset, 0);
10721053
rb_define_method(cTinyTdsClient, "encoding", rb_tinytds_encoding, 0);
10731054
rb_define_method(cTinyTdsClient, "escape", rb_tinytds_escape, 1);
10741055
rb_define_method(cTinyTdsClient, "return_code", rb_tinytds_return_code, 0);
@@ -1082,7 +1063,6 @@ void init_tinytds_client()
10821063
/* Intern Misc */
10831064
intern_new = rb_intern("new");
10841065
intern_dup = rb_intern("dup");
1085-
intern_transpose_iconv_encoding = rb_intern("transpose_iconv_encoding");
10861066
intern_local_offset = rb_intern("local_offset");
10871067
intern_gsub = rb_intern("gsub");
10881068
intern_call = rb_intern("call");

lib/tiny_tds/client.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module TinyTds
22
class Client
3-
attr_reader :app_name, :contained, :database, :dataserver, :encoding, :message_handler, :login_timeout, :password, :port, :tds_version, :timeout, :username, :use_utf16
3+
attr_reader :app_name, :charset, :contained, :database, :dataserver, :message_handler, :login_timeout, :password, :port, :tds_version, :timeout, :username, :use_utf16
44

55
@default_query_options = {
66
as: :hash,
@@ -13,14 +13,6 @@ class Client
1313
class << self
1414
attr_reader :default_query_options
1515

16-
# Most, if not all, iconv encoding names can be found by ruby. Just in case, you can
17-
# overide this method to return a string name that Encoding.find would work with. Default
18-
# is to return the passed encoding.
19-
#
20-
def transpose_iconv_encoding(encoding)
21-
encoding
22-
end
23-
2416
def local_offset
2517
::Time.local(2010).utc_offset.to_r / 86_400
2618
end
@@ -30,7 +22,7 @@ def local_offset
3022
# rubocop:disable Metrics/MethodLength
3123
# rubocop:disable Metrics/CyclomaticComplexity
3224
# rubocop:disable Metrics/PerceivedComplexity
33-
def initialize(app_name: "TinyTds", azure: false, contained: false, database: nil, dataserver: nil, encoding: "UTF-8", message_handler: nil, host: nil, login_timeout: 60, password: nil, port: 1433, tds_version: nil, timeout: 5, username: nil, use_utf16: true)
25+
def initialize(app_name: "TinyTds", azure: false, charset: "UTF-8", contained: false, database: nil, dataserver: nil, message_handler: nil, host: nil, login_timeout: 60, password: nil, port: 1433, tds_version: nil, timeout: 5, username: nil, use_utf16: true)
3426
if dataserver.to_s.empty? && host.to_s.empty?
3527
raise ArgumentError, "missing :host option if no :dataserver given"
3628
end
@@ -42,9 +34,9 @@ def initialize(app_name: "TinyTds", azure: false, contained: false, database: ni
4234
end
4335

4436
@app_name = app_name
37+
@charset = (charset.nil? || charset.casecmp("utf8").zero?) ? "UTF-8" : charset.upcase
4538
@database = database
4639
@dataserver = dataserver || "#{host}:#{port}"
47-
@encoding = (encoding.nil? || encoding.casecmp("utf8").zero?) ? "UTF-8" : encoding.upcase
4840
@login_timeout = login_timeout.to_i
4941
@password = password if password && password.to_s.strip != ""
5042
@port = port.to_i

test/client_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class ClientTest < TinyTds::TestCase
4545
assert_equal "''hello''", @client.escape("'hello'")
4646
end
4747

48-
["CP850", "CP1252", "ISO-8859-1"].each do |encoding|
49-
it "allows valid iconv character set - #{encoding}" do
50-
client = new_connection(encoding: encoding)
51-
assert_equal encoding, client.charset
52-
assert_equal Encoding.find(encoding), client.encoding
48+
["CP850", "CP1252", "ISO-8859-1"].each do |charset|
49+
it "allows valid iconv character set - #{charset}" do
50+
client = new_connection(charset:)
51+
assert_equal charset, client.charset
52+
assert_equal Encoding.find(charset), client.encoding
5353
ensure
5454
client&.close
5555
end

0 commit comments

Comments
 (0)