Permalink
Browse files

blobs work much better

  • Loading branch information...
1 parent c72f38e commit 6e7e7c330fc5e6eb92e829d65ec9a69699da21c3 @tenderlove committed Feb 10, 2010
Showing with 45 additions and 14 deletions.
  1. +7 −1 ext/sqlite3/sqlite3.c
  2. +1 −0 ext/sqlite3/sqlite3_ruby.h
  3. +18 −8 ext/sqlite3/statement.c
  4. +0 −5 lib/sqlite3/statement.rb
  5. +19 −0 test/test_statement.rb
View
@@ -1,6 +1,7 @@
#include <sqlite3_ruby.h>
VALUE mSqlite3;
+VALUE cSqlite3Blob;
static VALUE libversion(VALUE klass)
{
@@ -9,7 +10,12 @@ static VALUE libversion(VALUE klass)
void Init_sqlite3_native()
{
- mSqlite3 = rb_define_module("SQLite3");
+ mSqlite3 = rb_define_module("SQLite3");
+
+ /* A class for differentiating between strings and blobs, when binding them
+ * into statements.
+ */
+ cSqlite3Blob = rb_define_class_under(mSqlite3, "Blob", rb_cString);
// Initialize the sqlite3 library
sqlite3_initialize();
@@ -10,6 +10,7 @@
#include <sqlite3.h>
extern VALUE mSqlite3;
+extern VALUE cSqlite3Blob;
#include <database.h>
#include <statement.h>
View
@@ -149,8 +149,8 @@ static VALUE step(VALUE self)
);
#ifdef HAVE_RUBY_ENCODING_H
rb_enc_associate_index(str, enc_index);
- rb_ary_push(list, str);
#endif
+ rb_ary_push(list, str);
}
break;
case SQLITE_NULL:
@@ -217,13 +217,23 @@ static VALUE bind_param(VALUE self, VALUE key, VALUE value)
value = rb_str_export_to_enc(value, enc);
#endif
- status = sqlite3_bind_text(
- ctx->st,
- index,
- (const char *)StringValuePtr(value),
- (int)RSTRING_LEN(value),
- SQLITE_TRANSIENT
- );
+ if(CLASS_OF(value) == cSqlite3Blob) {
+ status = sqlite3_bind_blob(
+ ctx->st,
+ index,
+ (const char *)StringValuePtr(value),
+ (int)RSTRING_LEN(value),
+ SQLITE_TRANSIENT
+ );
+ } else {
+ status = sqlite3_bind_text(
+ ctx->st,
+ index,
+ (const char *)StringValuePtr(value),
+ (int)RSTRING_LEN(value),
+ SQLITE_TRANSIENT
+ );
+ }
break;
case T_FLOAT:
status = sqlite3_bind_double(ctx->st, index, NUM2DBL(value));
View
@@ -8,11 +8,6 @@ def to_blob
end
module SQLite3
-
- # A class for differentiating between strings and blobs, when binding them
- # into statements.
- class Blob < String; end
-
# A statement represents a prepared-but-unexecuted SQL query. It will rarely
# (if ever) be instantiated directly by a client, and is most often obtained
# via the Database#prepare method.
View
@@ -7,6 +7,22 @@ def setup
@stmt = SQLite3::Statement.new(@db, "select 'foo'")
end
+ def test_prepare_blob
+ @db = SQLite3::Database.new(':memory:')
+ @db.execute('create table foo(text BLOB)')
+ stmt = @db.prepare('insert into foo(text) values (?)')
+ stmt.bind_param(1, SQLite3::Blob.new('hello'))
+ stmt.step
+ stmt.close
+ end
+
+ def test_select_blob
+ @db = SQLite3::Database.new(':memory:')
+ @db.execute('create table foo(text BLOB)')
+ @db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
+ assert_equal 'hello', @db.execute('select * from foo').first.first
+ end
+
def test_new
assert @stmt
end
@@ -64,6 +80,9 @@ def test_bind_nil
assert_equal [nil], result
end
+ def test_bind_blobs
+ end
+
def test_bind_64
stmt = SQLite3::Statement.new(@db, "select ?")
stmt.bind_param(1, 2 ** 31)

0 comments on commit 6e7e7c3

Please sign in to comment.