diff --git a/lib/sqlite3/database.rb b/lib/sqlite3/database.rb index 53f29ac..aff2606 100644 --- a/lib/sqlite3/database.rb +++ b/lib/sqlite3/database.rb @@ -65,7 +65,11 @@ def quote(string) # By default, the new database will return result rows as arrays # (#results_as_hash) and has type translation disabled (#type_translation=). def initialize(file_name, options = {}) - @encoding = Encoding.find(options.fetch(:encoding, "utf-8")) + if RUBY_VERSION >= '1.9.1' + @encoding = Encoding.find(options.fetch(:encoding, "utf-8")) + else + @encoding = nil + end @driver = Driver.new diff --git a/lib/sqlite3/driver.rb b/lib/sqlite3/driver.rb index 14f09b3..3f704b3 100644 --- a/lib/sqlite3/driver.rb +++ b/lib/sqlite3/driver.rb @@ -10,7 +10,7 @@ def open(filename, utf_16 = false) filename = filename.encode(Encoding.utf_16native) result = API.sqlite3_open16(c_string(filename), handle) else - filename = filename.encode(Encoding.utf_8) + filename = filename.encode(Encoding.utf_8) if RUBY_VERSION >= '1.9.1' result = API.sqlite3_open(filename, handle) end [result, handle.get_pointer(0)] @@ -21,7 +21,11 @@ def errmsg(db, utf_16 = false) ptr = API.sqlite3_errmsg16(db) get_string_utf_16(ptr).force_encoding(Encoding.utf_16native) else - API.sqlite3_errmsg(db).force_encoding(Encoding.utf_8) + if RUBY_VERSION >= '1.9.1' + API.sqlite3_errmsg(db).force_encoding(Encoding.utf_8) + else + API.sqlite3_errmsg(db) + end end end @@ -42,7 +46,8 @@ def prepare(db, sql) end def bind_string(stmt, index, value) - case value.encoding + if RUBY_VERSION >= '1.9.1' + case value.encoding when Encoding.utf_8, Encoding.us_ascii API.sqlite3_bind_text(stmt, index, value, value.bytesize, TRANSIENT) when Encoding.utf_16le, Encoding.utf_16be @@ -50,6 +55,9 @@ def bind_string(stmt, index, value) API.sqlite3_bind_text16(stmt, index, value, value.bytesize, TRANSIENT) else API.sqlite3_bind_blob(stmt, index, value, value.bytesize, TRANSIENT) + end + else + API.sqlite3_bind_blob(stmt, index, value, value.bytesize, TRANSIENT) end end @@ -65,7 +73,11 @@ def column_text(stmt, column, utf_16 = false) length = API.sqlite3_column_bytes16(stmt, column) ptr.get_bytes(0, length).force_encoding(Encoding.utf_16native) # free? else - API.sqlite3_column_text(stmt, column).force_encoding(Encoding.utf_8) + if RUBY_VERSION >= '1.9.1' + API.sqlite3_column_text(stmt, column).force_encoding(Encoding.utf_8) + else + API.sqlite3_column_text(stmt, column) + end end end @@ -120,11 +132,16 @@ def c_string(string) end def add_byte_order_mask(string) - "\uFEFF".encode(string.encoding) + string + if RUBY_VERSION >= '1.9.1' + "\uFEFF".encode(string.encoding) + string + else + "\uFEFF" + string + end end def terminate_string!(string) - string << "\0\0".force_encoding(string.encoding) + string << "\0\0" + RUBY_VERSION >= '1.9.1' ? string.force_encoding(string.encoding) : string end def get_string_utf_16(ptr) diff --git a/lib/sqlite3/encoding.rb b/lib/sqlite3/encoding.rb index 9af4aa0..36e65a1 100644 --- a/lib/sqlite3/encoding.rb +++ b/lib/sqlite3/encoding.rb @@ -17,8 +17,12 @@ def find(encoding) end def utf_16?(str_or_enc) - enc = str_or_enc.kind_of?(::Encoding) ? str_or_enc : str_or_enc.encoding - [utf_16le, utf_16be].include?(enc) + if RUBY_VERSION >= '1.9.1' + enc = str_or_enc.kind_of?(::Encoding) ? str_or_enc : str_or_enc.encoding + [utf_16le, utf_16be].include?(enc) + else + false + end end def utf_16native diff --git a/test/test_database_initialization.rb b/test/test_database_initialization.rb index 5f32423..e64cdeb 100644 --- a/test/test_database_initialization.rb +++ b/test/test_database_initialization.rb @@ -30,22 +30,22 @@ def test_encoding_conversion_from_utf_16_to_utf_8 File.delete(db_filename) if File.exists?(db_filename) db = SQLite3::Database.new(db_filename, :encoding => "utf-16le") db.execute("CREATE TABLE t1(t TEXT)") - db.execute("INSERT INTO t1 VALUES (?)", expected_string.encode(Encoding::UTF_8)) - db.execute("INSERT INTO t1 VALUES (?)", expected_string.encode(Encoding::UTF_16LE)) + db.execute("INSERT INTO t1 VALUES (?)", expected_string) + db.execute("INSERT INTO t1 VALUES (?)", expected_string) rows = db.execute("SELECT * FROM t1") assert_equal 2, rows.size - assert_equal expected_string.encode(Encoding::UTF_16LE), rows[0][0] - assert_equal Encoding::UTF_16LE, rows[0][0].encoding - assert_equal expected_string.encode(Encoding::UTF_16LE), rows[1][0] - assert_equal Encoding::UTF_16LE, rows[1][0].encoding + assert_equal expected_string, rows[0][0] + assert_equal Encoding::UTF_16LE, rows[0][0].encoding if RUBY_VERSION >= '1.9.1' + assert_equal expected_string, rows[1][0] + assert_equal Encoding::UTF_16LE, rows[1][0].encoding if RUBY_VERSION >= '1.9.1' db.close db = SQLite3::Database.new(db_filename) rows = db.execute("SELECT * FROM t1") assert_equal 2, rows.size assert_equal expected_string, rows[0][0] - assert_equal Encoding::UTF_8, rows[0][0].encoding + assert_equal Encoding::UTF_8, rows[0][0].encoding if RUBY_VERSION >= '1.9.1' assert_equal expected_string, rows[1][0] - assert_equal Encoding::UTF_8, rows[1][0].encoding + assert_equal Encoding::UTF_8, rows[1][0].encoding if RUBY_VERSION >= '1.9.1' File.delete(db_filename) if File.exists?(db_filename) end end diff --git a/test/test_database_queries_utf_16.rb b/test/test_database_queries_utf_16.rb index 22b00c2..7cdfbb9 100644 --- a/test/test_database_queries_utf_16.rb +++ b/test/test_database_queries_utf_16.rb @@ -1,80 +1,82 @@ -require "helper" +if RUBY_VERSION >= '1.9.1' + require "helper" -class TestDatabaseQueriesUtf16 < Test::Unit::TestCase - def setup - @db = SQLite3::Database.new(":memory:", :encoding => "utf-16") - @db.execute("CREATE TABLE t1(id INTEGER PRIMARY KEY ASC, t TEXT, nu1 NUMERIC, i1 INTEGER, i2 INTEGER, no BLOB)") - end + class TestDatabaseQueriesUtf16 < Test::Unit::TestCase + def setup + @db = SQLite3::Database.new(":memory:", :encoding => "utf-16") + @db.execute("CREATE TABLE t1(id INTEGER PRIMARY KEY ASC, t TEXT, nu1 NUMERIC, i1 INTEGER, i2 INTEGER, no BLOB)") + end - def teardown - @db.close - end + def teardown + @db.close + end - def test_tables_empty - assert_equal [], @db.execute("SELECT * FROM t1") - end + def test_tables_empty + assert_equal [], @db.execute("SELECT * FROM t1") + end - def test_execute - @db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, 4294967296, NULL)") - rows = @db.execute("SELECT * FROM t1") - assert_equal 1, rows.size - row = rows[0] - assert_equal "text1".encode(Encoding::UTF_16LE), row[1] - assert_equal Encoding::UTF_16LE, row[1].encoding - assert_equal 1.22, row[2] - assert_equal 42, row[3] - assert_equal 4294967296, row[4] - assert_nil row[5] - end + def test_execute + @db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, 4294967296, NULL)") + rows = @db.execute("SELECT * FROM t1") + assert_equal 1, rows.size + row = rows[0] + assert_equal "text1".encode(Encoding::UTF_16LE), row[1] + assert_equal Encoding::UTF_16LE, row[1].encoding + assert_equal 1.22, row[2] + assert_equal 42, row[3] + assert_equal 4294967296, row[4] + assert_nil row[5] + end - def test_execute_with_bindings - blob = open("test/fixtures/SQLite.gif", "rb").read - @db.execute("INSERT INTO t1 VALUES(?, ?, ?, ?, ?, ?)", nil, "text1", 1.22, 42, 4294967296, blob) - rows = @db.execute("SELECT * FROM t1") - assert_equal 1, rows.size - row = rows[0] - assert_equal "text1".encode(Encoding::UTF_16LE), row[1] - assert_equal Encoding::UTF_16LE, row[1].encoding - assert_equal 1.22, row[2] - assert_equal 42, row[3] - assert_equal 4294967296, row[4] - assert_equal blob, row[5] - assert_equal Encoding::ASCII_8BIT, row[5].encoding - end + def test_execute_with_bindings + blob = open("test/fixtures/SQLite.gif", "rb").read + @db.execute("INSERT INTO t1 VALUES(?, ?, ?, ?, ?, ?)", nil, "text1", 1.22, 42, 4294967296, blob) + rows = @db.execute("SELECT * FROM t1") + assert_equal 1, rows.size + row = rows[0] + assert_equal "text1".encode(Encoding::UTF_16LE), row[1] + assert_equal Encoding::UTF_16LE, row[1].encoding + assert_equal 1.22, row[2] + assert_equal 42, row[3] + assert_equal 4294967296, row[4] + assert_equal blob, row[5] + assert_equal Encoding::ASCII_8BIT, row[5].encoding + end - def test_execute_with_different_encodings - expected_string = "text1" - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::ASCII_8BIT)) - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_8)) - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16LE)) - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16BE)) - rows = @db.execute("SELECT * FROM t1") - assert_equal 4, rows.size - assert_equal expected_string, rows[0][1] - assert_equal expected_string.encode(Encoding::UTF_16LE), rows[1][1] - assert_equal expected_string.encode(Encoding::UTF_16LE), rows[2][1] - assert_equal expected_string.encode(Encoding::UTF_16LE), rows[3][1] - assert_equal Encoding::ASCII_8BIT, rows[0][1].encoding - assert_equal Encoding::UTF_16LE, rows[1][1].encoding - assert_equal Encoding::UTF_16LE, rows[2][1].encoding - assert_equal Encoding::UTF_16LE, rows[3][1].encoding - end + def test_execute_with_different_encodings + expected_string = "text1" + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::ASCII_8BIT)) + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_8)) + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16LE)) + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16BE)) + rows = @db.execute("SELECT * FROM t1") + assert_equal 4, rows.size + assert_equal expected_string, rows[0][1] + assert_equal expected_string.encode(Encoding::UTF_16LE), rows[1][1] + assert_equal expected_string.encode(Encoding::UTF_16LE), rows[2][1] + assert_equal expected_string.encode(Encoding::UTF_16LE), rows[3][1] + assert_equal Encoding::ASCII_8BIT, rows[0][1].encoding + assert_equal Encoding::UTF_16LE, rows[1][1].encoding + assert_equal Encoding::UTF_16LE, rows[2][1].encoding + assert_equal Encoding::UTF_16LE, rows[3][1].encoding + end - def test_execute_with_bad_query - assert_raise(SQLite3::SQLException) { @db.execute("bad query") } - assert_equal %Q{near "bad": syntax error}, @db.errmsg - assert_equal 1, @db.errcode - end + def test_execute_with_bad_query + assert_raise(SQLite3::SQLException) { @db.execute("bad query") } + assert_equal %Q{near "bad": syntax error}, @db.errmsg + assert_equal 1, @db.errcode + end - def test_last_insert_row_id - @db.execute("INSERT INTO t1 VALUES(NULL, NULL, NULL, NULL, NULL, NULL)") - id = @db.last_insert_row_id - rows = @db.execute("SELECT * FROM t1 WHERE id = #{id}") - assert_equal 1, rows.size - end + def test_last_insert_row_id + @db.execute("INSERT INTO t1 VALUES(NULL, NULL, NULL, NULL, NULL, NULL)") + id = @db.last_insert_row_id + rows = @db.execute("SELECT * FROM t1 WHERE id = #{id}") + assert_equal 1, rows.size + end - # def test_execute_with_closed_database - # @db.close - # @db.execute("SELECT * FROM t1") - # end -end + # def test_execute_with_closed_database + # @db.close + # @db.execute("SELECT * FROM t1") + # end + end +end \ No newline at end of file diff --git a/test/test_database_queries_utf_8.rb b/test/test_database_queries_utf_8.rb index b907295..e464380 100644 --- a/test/test_database_queries_utf_8.rb +++ b/test/test_database_queries_utf_8.rb @@ -20,7 +20,7 @@ def test_execute assert_equal 1, rows.size row = rows[0] assert_equal "text1", row[1] - assert_equal Encoding::UTF_8, row[1].encoding + assert_equal Encoding::UTF_8, row[1].encoding if RUBY_VERSION >= '1.9.1' assert_equal 1.22, row[2] assert_equal 42, row[3] assert_equal 4294967296, row[4] @@ -34,30 +34,32 @@ def test_execute_with_bindings assert_equal 1, rows.size row = rows[0] assert_equal "text1", row[1] - assert_equal Encoding::UTF_8, row[1].encoding + assert_equal Encoding::UTF_8, row[1].encoding if RUBY_VERSION >= '1.9.1' assert_equal 1.22, row[2] assert_equal 42, row[3] assert_equal 4294967296, row[4] assert_equal blob, row[5] - assert_equal Encoding::ASCII_8BIT, row[5].encoding + assert_equal Encoding::ASCII_8BIT, row[5].encoding if RUBY_VERSION >= '1.9.1' end - - def test_execute_with_different_encodings - expected_string = "text1" - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::ASCII_8BIT)) - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_8)) - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16LE)) - @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16BE)) - rows = @db.execute("SELECT * FROM t1") - assert_equal 4, rows.size - assert_equal expected_string, rows[0][1] - assert_equal expected_string, rows[1][1] - assert_equal expected_string, rows[2][1] - assert_equal expected_string, rows[3][1] - assert_equal Encoding::ASCII_8BIT, rows[0][1].encoding - assert_equal Encoding::UTF_8, rows[1][1].encoding - assert_equal Encoding::UTF_8, rows[2][1].encoding - assert_equal Encoding::UTF_8, rows[3][1].encoding + + if RUBY_VERSION >= '1.9.1' + def test_execute_with_different_encodings + expected_string = "text1" + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::ASCII_8BIT)) + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_8)) + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16LE)) + @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16BE)) + rows = @db.execute("SELECT * FROM t1") + assert_equal 4, rows.size + assert_equal expected_string, rows[0][1] + assert_equal expected_string, rows[1][1] + assert_equal expected_string, rows[2][1] + assert_equal expected_string, rows[3][1] + assert_equal Encoding::ASCII_8BIT, rows[0][1].encoding + assert_equal Encoding::UTF_8, rows[1][1].encoding + assert_equal Encoding::UTF_8, rows[2][1].encoding + assert_equal Encoding::UTF_8, rows[3][1].encoding + end end def test_execute_with_bad_query