Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add abstraction for different IDENTITY functions

  • Loading branch information...
commit 0e44bbae6aed8ceb3b62a69658912a7244c09ac4 1 parent 7c7d87b
@vjt vjt authored metaskills committed
View
13 ext/tiny_tds/client.c
@@ -193,7 +193,7 @@ static VALUE rb_tinytds_execute(VALUE self, VALUE sql) {
return Qfalse;
}
cwrap->userdata->dbsql_sent = 1;
- VALUE result = rb_tinytds_new_result_obj(cwrap->client);
+ 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);
@@ -237,6 +237,11 @@ static VALUE rb_tinytds_return_code(VALUE self) {
}
}
+static VALUE rb_tinytds_identity_sql(VALUE self) {
+ GET_CLIENT_WRAPPER(self);
+ return rb_str_new2(cwrap->identity_insert_sql);
+}
+
static VALUE rb_tinytds_freetds_nine_one_or_higher(VALUE self) {
#ifdef DBSETLDBNAME
return Qtrue;
@@ -304,6 +309,11 @@ static VALUE rb_tinytds_connect(VALUE self, VALUE opts) {
VALUE transposed_encoding = rb_funcall(cTinyTdsClient, intern_transpose_iconv_encoding, 1, charset);
cwrap->encoding = rb_enc_find(StringValuePtr(transposed_encoding));
#endif
+
+ if (dbtds(cwrap->client) <= 7)
+ cwrap->identity_insert_sql = "SELECT CAST(@@IDENTITY AS bigint) AS Ident";
+ else
+ cwrap->identity_insert_sql = "SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident";
}
return self;
}
@@ -326,6 +336,7 @@ void init_tinytds_client() {
rb_define_method(cTinyTdsClient, "encoding", rb_tinytds_encoding, 0);
rb_define_method(cTinyTdsClient, "escape", rb_tinytds_escape, 1);
rb_define_method(cTinyTdsClient, "return_code", rb_tinytds_return_code, 0);
+ rb_define_method(cTinyTdsClient, "identity_sql", rb_tinytds_identity_sql, 0);
rb_define_method(cTinyTdsClient, "freetds_091_or_higer?", rb_tinytds_freetds_nine_one_or_higher, 0);
/* Define TinyTds::Client Protected Methods */
rb_define_protected_method(cTinyTdsClient, "connect", rb_tinytds_connect, 1);
View
1  ext/tiny_tds/client.h
@@ -20,6 +20,7 @@ typedef struct {
short int closed;
VALUE charset;
tinytds_client_userdata *userdata;
+ const char *identity_insert_sql;
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *encoding;
#endif
View
7 ext/tiny_tds/result.c
@@ -91,11 +91,12 @@ static void rb_tinytds_result_free(void *ptr) {
xfree(ptr);
}
-VALUE rb_tinytds_new_result_obj(DBPROCESS *c) {
+VALUE rb_tinytds_new_result_obj(tinytds_client_wrapper *cwrap) {
VALUE obj;
tinytds_result_wrapper *rwrap;
obj = Data_Make_Struct(cTinyTdsResult, tinytds_result_wrapper, rb_tinytds_result_mark, rb_tinytds_result_free, rwrap);
- rwrap->client = c;
+ rwrap->cwrap = cwrap;
+ rwrap->client = cwrap->client;
rwrap->local_offset = Qnil;
rwrap->fields = rb_ary_new();
rwrap->fields_processed = rb_ary_new();
@@ -467,7 +468,7 @@ static VALUE rb_tinytds_result_insert(VALUE self) {
if (rwrap->client) {
rb_tinytds_result_cancel_helper(rwrap->client);
VALUE identity = Qnil;
- dbcmd(rwrap->client, "SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident");
+ dbcmd(rwrap->client, rwrap->cwrap->identity_insert_sql);
if (dbsqlexec(rwrap->client) != FAIL && dbresults(rwrap->client) != FAIL && DBROWS(rwrap->client) != FAIL) {
while (dbnextrow(rwrap->client) != NO_MORE_ROWS) {
int col = 1;
View
3  ext/tiny_tds/result.h
@@ -7,9 +7,10 @@
#endif
void init_tinytds_result();
-VALUE rb_tinytds_new_result_obj(DBPROCESS *c);
+VALUE rb_tinytds_new_result_obj(tinytds_client_wrapper *cwrap);
typedef struct {
+ tinytds_client_wrapper *cwrap;
DBPROCESS *client;
VALUE local_offset;
VALUE fields;
View
11 test/result_test.rb
@@ -128,11 +128,8 @@ class ResultTest < TinyTds::TestCase
afrows = @client.execute("SELECT @@ROWCOUNT AS AffectedRows").each.first['AffectedRows']
assert_instance_of Fixnum, afrows
@client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
- pk1 = @client.execute("SELECT SCOPE_IDENTITY() AS Ident").each.first['Ident']
- assert_instance_of BigDecimal, pk1, 'native is numeric(38,0) for SCOPE_IDENTITY() function'
- pk2 = @client.execute("SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident").each.first['Ident']
- assert_instance_of Fixnum, pk2, 'we it be able to CAST to bigint'
- assert_equal pk2, pk1.to_i, 'just making sure the 2 line up'
+ pk1 = @client.execute(@client.identity_sql).each.first['Ident']
+ assert_instance_of Fixnum, pk1, 'we it be able to CAST to bigint'
@client.execute("UPDATE [datatypes] SET [varchar_50] = NULL WHERE [varchar_50] = '#{text}'").do
afrows = @client.execute("SELECT @@ROWCOUNT AS AffectedRows").each.first['AffectedRows']
assert_equal 1, afrows
@@ -164,12 +161,12 @@ class ResultTest < TinyTds::TestCase
end
end
- it 'has an #insert method that cancels result rows and returns the SCOPE_IDENTITY() natively' do
+ it 'has an #insert method that cancels result rows and returns IDENTITY natively' do
rollback_transaction(@client) do
text = 'test scope identity rows native'
@client.execute("DELETE FROM [datatypes] WHERE [varchar_50] = '#{text}'").do
@client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
- sql_identity = @client.execute("SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident").each.first['Ident']
+ sql_identity = @client.execute(@client.identity_sql).each.first['Ident']
native_identity = @client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").insert
assert_equal sql_identity+1, native_identity
end
Please sign in to comment.
Something went wrong with that request. Please try again.