Skip to content

Instantly share code, notes, and snippets.

@macks
Created November 12, 2009 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macks/233113 to your computer and use it in GitHub Desktop.
Save macks/233113 to your computer and use it in GitHub Desktop.
Patch to support encoded string object for data_objects and do_sqlite3. (incomplete)
diff --git a/data_objects/lib/data_objects/spec/encoding_spec.rb b/data_objects/lib/data_objects/spec/encoding_spec.rb
index 4b14e17..de3a974 100644
--- a/data_objects/lib/data_objects/spec/encoding_spec.rb
+++ b/data_objects/lib/data_objects/spec/encoding_spec.rb
@@ -40,4 +40,43 @@ share_examples_for 'a driver supporting encodings' do
end
+
+ if defined?(::Encoding)
+
+ describe 'with encoded string support' do
+
+ include DataObjectsSpecHelpers
+
+ before :all do
+ setup_test_environment
+ end
+
+ describe 'reading a String' do
+ before do
+ @reader = @connection.create_command("SELECT name FROM widgets WHERE ad_description = ?").execute_reader('Buy this product now!')
+ @reader.next!
+ @values = @reader.values
+ end
+
+ it 'should return UTF-8 encoded String' do
+ @values.first.should be_kind_of(String)
+ @values.first.encoding.name.should == 'UTF-8'
+ end
+ end
+
+ describe 'reading a ByteArray' do
+ before do
+ @reader = @connection.create_command("SELECT ad_image FROM widgets WHERE ad_description = ?").execute_reader('Buy this product now!')
+ @reader.next!
+ @values = @reader.values
+ end
+
+ it 'should return ASCII-8BIT encoded ByteArray' do
+ @values.first.should be_kind_of(::Extlib::ByteArray)
+ @values.first.encoding.name.should == 'ASCII-8BIT'
+ end
+ end
+ end
+ end
+
end
diff --git a/do_sqlite3/ext/do_sqlite3_ext/do_sqlite3_ext.c b/do_sqlite3/ext/do_sqlite3_ext/do_sqlite3_ext.c
index 87923d5..8993244 100755
--- a/do_sqlite3/ext/do_sqlite3_ext/do_sqlite3_ext.c
+++ b/do_sqlite3/ext/do_sqlite3_ext/do_sqlite3_ext.c
@@ -534,6 +534,18 @@ static VALUE cConnection_quote_string(VALUE self, VALUE string) {
return result;
}
+static VALUE cConnection_quote_byte_array(VALUE self, VALUE string) {
+ VALUE source = StringValue(string);
+ VALUE array = rb_funcall(source, rb_intern("unpack"), 1, rb_str_new2("H*"));
+ rb_ary_unshift(array, rb_str_new2("X'"));
+ rb_ary_push(array, rb_str_new2("'"));
+ return rb_ary_join(array, Qnil);
+}
+
+static VALUE cConnection_character_set(VALUE self) {
+ return rb_iv_get(self, "@encoding");
+}
+
static VALUE build_query_from_args(VALUE klass, int count, VALUE *args) {
VALUE query = rb_iv_get(klass, "@text");
int i;
@@ -604,6 +616,9 @@ static VALUE cCommand_execute_reader(int argc, VALUE *argv, VALUE self) {
rb_iv_set(reader, "@reader", Data_Wrap_Struct(rb_cObject, 0, 0, sqlite3_reader));
rb_iv_set(reader, "@field_count", INT2NUM(field_count));
+#ifdef HAVE_RUBY_ENCODING_H
+ rb_iv_set(reader, "@connection", conn_obj);
+#endif
field_names = rb_ary_new();
field_types = rb_iv_get(self, "@field_types");
@@ -752,6 +767,8 @@ void Init_do_sqlite3_ext() {
rb_define_method(cConnection, "dispose", cConnection_dispose, 0);
rb_define_method(cConnection, "quote_boolean", cConnection_quote_boolean, 1);
rb_define_method(cConnection, "quote_string", cConnection_quote_string, 1);
+ rb_define_method(cConnection, "quote_byte_array", cConnection_quote_byte_array, 1);
+ rb_define_method(cConnection, "character_set", cConnection_character_set, 0);
cCommand = SQLITE3_CLASS("Command", cDO_Command);
rb_define_method(cCommand, "set_types", cCommand_set_types, -1);
diff --git a/do_sqlite3/spec/connection_spec.rb b/do_sqlite3/spec/connection_spec.rb
index beee8c7..482f961 100644
--- a/do_sqlite3/spec/connection_spec.rb
+++ b/do_sqlite3/spec/connection_spec.rb
@@ -16,4 +16,11 @@ describe DataObjects::Sqlite3::Connection do
it_should_behave_like 'a Connection'
it_should_behave_like 'a Connection via JDNI' if JRUBY
+
+ describe "byte array quoting" do
+ it "should properly escape unprintable characters" do
+ @connection.quote_byte_array("\000\001").should == "X'0001'"
+ end
+ end
+
end
diff --git a/do_sqlite3/spec/encoding_spec.rb b/do_sqlite3/spec/encoding_spec.rb
new file mode 100644
index 0000000..54859fe
--- /dev/null
+++ b/do_sqlite3/spec/encoding_spec.rb
@@ -0,0 +1,8 @@
+# encoding: utf-8
+
+require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
+require 'data_objects/spec/encoding_spec'
+
+describe DataObjects::Sqlite3::Connection do
+ it_should_behave_like 'a driver supporting encodings'
+end
diff --git a/do_sqlite3/spec/spec_helper.rb b/do_sqlite3/spec/spec_helper.rb
index afbd480..f7c8002 100644
--- a/do_sqlite3/spec/spec_helper.rb
+++ b/do_sqlite3/spec/spec_helper.rb
@@ -112,7 +112,7 @@ module DataObjectsSpecHelpers
1.upto(16) do |n|
conn.create_command(<<-EOF).execute_non_query
- insert into widgets(code, name, shelf_location, description, image_data, ad_description, ad_image, whitepaper_text, cad_drawing, super_number, weight) VALUES ('W#{n.to_s.rjust(7,"0")}', 'Widget #{n}', 'A14', 'This is a description', 'IMAGE DATA', 'Buy this product now!', 'AD IMAGE DATA', 'String', 'CAD DRAWING', 1234, 13.4);
+ insert into widgets(code, name, shelf_location, description, image_data, ad_description, ad_image, whitepaper_text, cad_drawing, super_number, weight) VALUES ('W#{n.to_s.rjust(7,"0")}', 'Widget #{n}', 'A14', 'This is a description', 'IMAGE DATA', 'Buy this product now!', 'AD IMAGE DATA', 'String', X'434144200120002044524157494e47', 1234, 13.4);
EOF
end
diff --git a/do_sqlite3/spec/typecast/byte_array_spec.rb b/do_sqlite3/spec/typecast/byte_array_spec.rb
index 08f4b0f..819f2de 100644
--- a/do_sqlite3/spec/typecast/byte_array_spec.rb
+++ b/do_sqlite3/spec/typecast/byte_array_spec.rb
@@ -4,6 +4,5 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
require 'data_objects/spec/typecast/byte_array_spec'
describe 'DataObjects::Sqlite3 with ByteArray' do
- # We need to switch to using parameter binding for this to work with Sqlite3
- # it_should_behave_like 'supporting ByteArray'
+ it_should_behave_like 'supporting ByteArray'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment