Skip to content
This repository has been archived by the owner on Apr 18, 2021. It is now read-only.

Commit

Permalink
fixes for ruby 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
tgildea committed Feb 12, 2010
1 parent 2305ae4 commit 5ca2c4a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 108 deletions.
6 changes: 5 additions & 1 deletion lib/sqlite3/database.rb
Expand Up @@ -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

Expand Down
29 changes: 23 additions & 6 deletions lib/sqlite3/driver.rb
Expand Up @@ -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)]
Expand All @@ -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

Expand All @@ -42,14 +46,18 @@ 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
value = add_byte_order_mask(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

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions lib/sqlite3/encoding.rb
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions test/test_database_initialization.rb
Expand Up @@ -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
144 changes: 73 additions & 71 deletions 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
42 changes: 22 additions & 20 deletions test/test_database_queries_utf_8.rb
Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit 5ca2c4a

Please sign in to comment.