sqlite3 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ module SQLite3
2
+ class Encoding
3
+ class << self
4
+ def find(encoding)
5
+ enc = encoding.to_s
6
+ if enc.downcase == "utf-16"
7
+ utf_16native
8
+ else
9
+ ::Encoding.find(enc).tap do |e|
10
+ if utf_16?(e) && e != utf_16native
11
+ raise ArgumentError, "requested to use byte order different than native"
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def utf_16?(str_or_enc)
18
+ enc = str_or_enc.kind_of?(::Encoding) ? str_or_enc : str_or_enc.encoding
19
+ [utf_16le, utf_16be].include?(enc)
20
+ end
21
+
22
+ def utf_16native
23
+ "Ruby".unpack("i")[0] == 2036495698 ? utf_16le : utf_16be
24
+ end
25
+
26
+ def us_ascii
27
+ ::Encoding::US_ASCII
28
+ end
29
+
30
+ def utf_8
31
+ ::Encoding::UTF_8
32
+ end
33
+
34
+ def utf_16le
35
+ ::Encoding::UTF_16LE
36
+ end
37
+
38
+ def utf_16be
39
+ ::Encoding::UTF_16BE
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,3 @@
1
- require "sqlite3/constants"
2
-
3
1
  module SQLite3
4
2
 
5
3
  class Exception < ::StandardError
@@ -1,5 +1,3 @@
1
- require "sqlite3/errors"
2
-
3
1
  module SQLite3
4
2
 
5
3
  # This module is intended for inclusion solely by the Database class. It
@@ -1,6 +1,3 @@
1
- require "sqlite3/constants"
2
- require "sqlite3/errors"
3
-
4
1
  module SQLite3
5
2
 
6
3
  # The ResultSet object encapsulates the enumerability of a query's output.
@@ -31,10 +28,11 @@ module SQLite3
31
28
 
32
29
  # Create a new ResultSet attached to the given database, using the
33
30
  # given sql text.
34
- def initialize(db, stmt)
31
+ def initialize(db, stmt, utf_16 = false)
35
32
  @db = db
36
33
  @driver = @db.driver
37
34
  @stmt = stmt
35
+ @utf_16 = utf_16
38
36
  commence
39
37
  end
40
38
 
@@ -103,14 +101,16 @@ module SQLite3
103
101
  @driver.data_count(@stmt.handle).times do |column|
104
102
  type = @driver.column_type(@stmt.handle, column)
105
103
 
106
- if type == Constants::ColumnType::TEXT
107
- row << @driver.column_text(@stmt.handle, column)
108
- elsif type == Constants::ColumnType::NULL
104
+ if type == Constants::ColumnType::NULL
109
105
  row << nil
110
106
  elsif type == Constants::ColumnType::BLOB
111
107
  row << @driver.column_blob(@stmt.handle, column)
108
+ elsif type == Constants::ColumnType::INTEGER
109
+ row << @driver.column_int64(@stmt.handle, column)
110
+ elsif type == Constants::ColumnType::FLOAT
111
+ row << @driver.column_double(@stmt.handle, column)
112
112
  else
113
- row << @driver.column_text(@stmt.handle, column)
113
+ row << @driver.column_text(@stmt.handle, column, @utf_16)
114
114
  end
115
115
  end
116
116
 
@@ -1,18 +1,5 @@
1
- require "sqlite3/errors"
2
- require "sqlite3/resultset"
3
-
4
- class String
5
- def to_blob
6
- SQLite3::Blob.new(self)
7
- end
8
- end
9
-
10
1
  module SQLite3
11
2
 
12
- # A class for differentiating between strings and blobs, when binding them
13
- # into statements.
14
- class Blob < String; end
15
-
16
3
  # A statement represents a prepared-but-unexecuted SQL query. It will rarely
17
4
  # (if ever) be instantiated directly by a client, and is most often obtained
18
5
  # via the Database#prepare method.
@@ -30,12 +17,13 @@ module SQLite3
30
17
  # encapsulates the given SQL text. If the text contains more than one
31
18
  # statement (i.e., separated by semicolons), then the #remainder property
32
19
  # will be set to the trailing text.
33
- def initialize(db, sql, utf16 = false)
20
+ def initialize(db, sql, utf_16 = false)
34
21
  raise ArgumentError, "nil argument passed as sql text" unless sql
35
22
  @db = db
36
23
  @driver = @db.driver
37
24
  @closed = false
38
25
  @results = @columns = nil
26
+ @utf_16 = utf_16
39
27
  result, @handle, @remainder = @driver.prepare(@db.handle, sql)
40
28
  Error.check(result, @db)
41
29
  end
@@ -98,12 +86,10 @@ module SQLite3
98
86
  end
99
87
  when Numeric then
100
88
  @driver.bind_double(@handle, param, value.to_f)
101
- when Blob then
102
- @driver.bind_blob(@handle, param, value)
103
89
  when nil then
104
90
  @driver.bind_null(@handle, param)
105
91
  else
106
- @driver.bind_text(@handle, param, value)
92
+ @driver.bind_string(@handle, param, value.to_s)
107
93
  end
108
94
  else
109
95
  param = param.to_s
@@ -133,7 +119,7 @@ module SQLite3
133
119
  reset! if active?
134
120
 
135
121
  bind_params(*bind_vars) unless bind_vars.empty?
136
- @results = ResultSet.new(@db, self)
122
+ @results = ResultSet.new(@db, self, @utf_16)
137
123
 
138
124
  if block_given?
139
125
  yield @results
@@ -1,6 +1,3 @@
1
- require "time"
2
- require "date"
3
-
4
1
  module SQLite3
5
2
 
6
3
  # The Translator class encapsulates the logic and callbacks necessary for
@@ -1,5 +1,3 @@
1
- require "sqlite3/constants"
2
-
3
1
  module SQLite3
4
2
 
5
3
  class Value
Binary file
@@ -23,4 +23,29 @@ class TestDatabaseInitialization < Test::Unit::TestCase
23
23
  @db.close
24
24
  assert @db.closed?
25
25
  end
26
+
27
+ def test_encoding_conversion_from_utf_16_to_utf_8
28
+ expected_string = "test"
29
+ db_filename = "test_database_encoding.db"
30
+ File.delete(db_filename) if File.exists?(db_filename)
31
+ db = SQLite3::Database.new(db_filename, :encoding => "utf-16le")
32
+ db.execute("CREATE TABLE t1(t TEXT)")
33
+ db.execute("INSERT INTO t1 VALUES (?)", expected_string.encode(Encoding::UTF_8))
34
+ db.execute("INSERT INTO t1 VALUES (?)", expected_string.encode(Encoding::UTF_16LE))
35
+ rows = db.execute("SELECT * FROM t1")
36
+ assert_equal 2, rows.size
37
+ assert_equal expected_string.encode(Encoding::UTF_16LE), rows[0][0]
38
+ assert_equal Encoding::UTF_16LE, rows[0][0].encoding
39
+ assert_equal expected_string.encode(Encoding::UTF_16LE), rows[1][0]
40
+ assert_equal Encoding::UTF_16LE, rows[1][0].encoding
41
+ db.close
42
+ db = SQLite3::Database.new(db_filename)
43
+ rows = db.execute("SELECT * FROM t1")
44
+ assert_equal 2, rows.size
45
+ assert_equal expected_string, rows[0][0]
46
+ assert_equal Encoding::UTF_8, rows[0][0].encoding
47
+ assert_equal expected_string, rows[1][0]
48
+ assert_equal Encoding::UTF_8, rows[1][0].encoding
49
+ File.delete(db_filename) if File.exists?(db_filename)
50
+ end
26
51
  end
@@ -0,0 +1,80 @@
1
+ require "helper"
2
+
3
+ class TestDatabaseQueriesUtf16 < Test::Unit::TestCase
4
+ def setup
5
+ @db = SQLite3::Database.new(":memory:", :encoding => "utf-16")
6
+ @db.execute("CREATE TABLE t1(id INTEGER PRIMARY KEY ASC, t TEXT, nu1 NUMERIC, i1 INTEGER, i2 INTEGER, no BLOB)")
7
+ end
8
+
9
+ def teardown
10
+ @db.close
11
+ end
12
+
13
+ def test_tables_empty
14
+ assert_equal [], @db.execute("SELECT * FROM t1")
15
+ end
16
+
17
+ def test_execute
18
+ @db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, 4294967296, NULL)")
19
+ rows = @db.execute("SELECT * FROM t1")
20
+ assert_equal 1, rows.size
21
+ row = rows[0]
22
+ assert_equal "text1".encode(Encoding::UTF_16LE), row[1]
23
+ assert_equal Encoding::UTF_16LE, row[1].encoding
24
+ assert_equal 1.22, row[2]
25
+ assert_equal 42, row[3]
26
+ assert_equal 4294967296, row[4]
27
+ assert_nil row[5]
28
+ end
29
+
30
+ def test_execute_with_bindings
31
+ blob = open("test/fixtures/SQLite.gif", "rb").read
32
+ @db.execute("INSERT INTO t1 VALUES(?, ?, ?, ?, ?, ?)", nil, "text1", 1.22, 42, 4294967296, blob)
33
+ rows = @db.execute("SELECT * FROM t1")
34
+ assert_equal 1, rows.size
35
+ row = rows[0]
36
+ assert_equal "text1".encode(Encoding::UTF_16LE), row[1]
37
+ assert_equal Encoding::UTF_16LE, row[1].encoding
38
+ assert_equal 1.22, row[2]
39
+ assert_equal 42, row[3]
40
+ assert_equal 4294967296, row[4]
41
+ assert_equal blob, row[5]
42
+ assert_equal Encoding::ASCII_8BIT, row[5].encoding
43
+ end
44
+
45
+ def test_execute_with_different_encodings
46
+ expected_string = "text1"
47
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::ASCII_8BIT))
48
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_8))
49
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16LE))
50
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16BE))
51
+ rows = @db.execute("SELECT * FROM t1")
52
+ assert_equal 4, rows.size
53
+ assert_equal expected_string, rows[0][1]
54
+ assert_equal expected_string.encode(Encoding::UTF_16LE), rows[1][1]
55
+ assert_equal expected_string.encode(Encoding::UTF_16LE), rows[2][1]
56
+ assert_equal expected_string.encode(Encoding::UTF_16LE), rows[3][1]
57
+ assert_equal Encoding::ASCII_8BIT, rows[0][1].encoding
58
+ assert_equal Encoding::UTF_16LE, rows[1][1].encoding
59
+ assert_equal Encoding::UTF_16LE, rows[2][1].encoding
60
+ assert_equal Encoding::UTF_16LE, rows[3][1].encoding
61
+ end
62
+
63
+ def test_execute_with_bad_query
64
+ assert_raise(SQLite3::SQLException) { @db.execute("bad query") }
65
+ assert_equal %Q{near "bad": syntax error}, @db.errmsg
66
+ assert_equal 1, @db.errcode
67
+ end
68
+
69
+ def test_last_insert_row_id
70
+ @db.execute("INSERT INTO t1 VALUES(NULL, NULL, NULL, NULL, NULL, NULL)")
71
+ id = @db.last_insert_row_id
72
+ rows = @db.execute("SELECT * FROM t1 WHERE id = #{id}")
73
+ assert_equal 1, rows.size
74
+ end
75
+
76
+ # def test_execute_with_closed_database
77
+ # @db.close
78
+ # @db.execute("SELECT * FROM t1")
79
+ # end
80
+ end
@@ -0,0 +1,80 @@
1
+ require "helper"
2
+
3
+ class TestDatabaseQueriesUtf8 < Test::Unit::TestCase
4
+ def setup
5
+ @db = SQLite3::Database.new(":memory:")
6
+ @db.execute("CREATE TABLE t1(id INTEGER PRIMARY KEY ASC, t TEXT, nu1 NUMERIC, i1 INTEGER, i2 INTEGER, no BLOB)")
7
+ end
8
+
9
+ def teardown
10
+ @db.close
11
+ end
12
+
13
+ def test_tables_empty
14
+ assert_equal [], @db.execute("SELECT * FROM t1")
15
+ end
16
+
17
+ def test_execute
18
+ @db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, 4294967296, NULL)")
19
+ rows = @db.execute("SELECT * FROM t1")
20
+ assert_equal 1, rows.size
21
+ row = rows[0]
22
+ assert_equal "text1", row[1]
23
+ assert_equal Encoding::UTF_8, row[1].encoding
24
+ assert_equal 1.22, row[2]
25
+ assert_equal 42, row[3]
26
+ assert_equal 4294967296, row[4]
27
+ assert_nil row[5]
28
+ end
29
+
30
+ def test_execute_with_bindings
31
+ blob = open("test/fixtures/SQLite.gif", "rb").read
32
+ @db.execute("INSERT INTO t1 VALUES(?, ?, ?, ?, ?, ?)", nil, "text1", 1.22, 42, 4294967296, blob)
33
+ rows = @db.execute("SELECT * FROM t1")
34
+ assert_equal 1, rows.size
35
+ row = rows[0]
36
+ assert_equal "text1", row[1]
37
+ assert_equal Encoding::UTF_8, row[1].encoding
38
+ assert_equal 1.22, row[2]
39
+ assert_equal 42, row[3]
40
+ assert_equal 4294967296, row[4]
41
+ assert_equal blob, row[5]
42
+ assert_equal Encoding::ASCII_8BIT, row[5].encoding
43
+ end
44
+
45
+ def test_execute_with_different_encodings
46
+ expected_string = "text1"
47
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::ASCII_8BIT))
48
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_8))
49
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16LE))
50
+ @db.execute("INSERT INTO t1 VALUES(NULL, ?, NULL, NULL, NULL, NULL)", expected_string.encode(Encoding::UTF_16BE))
51
+ rows = @db.execute("SELECT * FROM t1")
52
+ assert_equal 4, rows.size
53
+ assert_equal expected_string, rows[0][1]
54
+ assert_equal expected_string, rows[1][1]
55
+ assert_equal expected_string, rows[2][1]
56
+ assert_equal expected_string, rows[3][1]
57
+ assert_equal Encoding::ASCII_8BIT, rows[0][1].encoding
58
+ assert_equal Encoding::UTF_8, rows[1][1].encoding
59
+ assert_equal Encoding::UTF_8, rows[2][1].encoding
60
+ assert_equal Encoding::UTF_8, rows[3][1].encoding
61
+ end
62
+
63
+ def test_execute_with_bad_query
64
+ assert_raise(SQLite3::SQLException) { @db.execute("bad query") }
65
+ assert_equal %Q{near "bad": syntax error}, @db.errmsg
66
+ assert_equal 1, @db.errcode
67
+ end
68
+
69
+ def test_last_insert_row_id
70
+ @db.execute("INSERT INTO t1 VALUES(NULL, NULL, NULL, NULL, NULL, NULL)")
71
+ id = @db.last_insert_row_id
72
+ rows = @db.execute("SELECT * FROM t1 WHERE id = #{id}")
73
+ assert_equal 1, rows.size
74
+ end
75
+
76
+ # def test_execute_with_closed_database
77
+ # @db.close
78
+ # @db.execute("SELECT * FROM t1")
79
+ # end
80
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlite3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jakub Ku\xC5\xBAma"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-17 00:00:00 +01:00
12
+ date: 2009-11-20 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -53,6 +53,7 @@ files:
53
53
  - lib/sqlite3/database.rb
54
54
  - lib/sqlite3/driver/ffi/api.rb
55
55
  - lib/sqlite3/driver/ffi/driver.rb
56
+ - lib/sqlite3/encoding.rb
56
57
  - lib/sqlite3/errors.rb
57
58
  - lib/sqlite3/pragmas.rb
58
59
  - lib/sqlite3/resultset.rb
@@ -60,20 +61,26 @@ files:
60
61
  - lib/sqlite3/translator.rb
61
62
  - lib/sqlite3/value.rb
62
63
  - lib/sqlite3/version.rb
64
+ - test/fixtures/SQLite.gif
63
65
  - test/helper.rb
64
66
  - test/test_database_initialization.rb
65
- - test/test_database_queries.rb
67
+ - test/test_database_queries_utf_16.rb
68
+ - test/test_database_queries_utf_8.rb
66
69
  has_rdoc: true
67
70
  homepage: http://github.com/qoobaa/sqlite3
68
71
  licenses: []
69
72
 
70
- post_install_message: |+
71
-
72
- WARNING!
73
-
73
+ post_install_message: |
74
+ ==== WARNING ===================================================================
74
75
  This is an early alpha version of SQLite3/Ruby FFI bindings!
75
- If you need native, production ready bindings install sqlite3-ruby instead.
76
+ Currently we support Ruby 1.9 ONLY.
77
+
78
+ If you need native bindings for Ruby 1.8 - install sqlite3-ruby instead.
79
+ You may need to uninstall this sqlite3 gem as well.
76
80
 
81
+ Thank you for installing sqlite3 gem! Suggestions: qoobaa@gmail.com
82
+ ================================================================================
83
+
77
84
  rdoc_options:
78
85
  - --charset=UTF-8
79
86
  require_paths:
@@ -98,6 +105,7 @@ signing_key:
98
105
  specification_version: 3
99
106
  summary: SQLite3/Ruby FFI bindings
100
107
  test_files:
101
- - test/helper.rb
102
- - test/test_database_queries.rb
108
+ - test/test_database_queries_utf_8.rb
109
+ - test/test_database_queries_utf_16.rb
103
110
  - test/test_database_initialization.rb
111
+ - test/helper.rb
@@ -1,29 +0,0 @@
1
- require "helper"
2
-
3
- class TestDatabaseQueries < Test::Unit::TestCase
4
- def setup
5
- @db_filename = "test_database.db"
6
- File.delete(@db_filename) if File.exists?(@db_filename)
7
- @db = SQLite3::Database.new(@db_filename)
8
- @db.execute("CREATE TABLE t1(id INTEGER PRIMARY KEY ASC, t TEXT, nu NUMERIC, i INTEGER, no BLOB)")
9
- end
10
-
11
- def teardown
12
- File.delete(@db_filename) if File.exists?(@db_filename)
13
- end
14
-
15
- def test_tables_empty
16
- assert_equal [], @db.execute("SELECT * FROM t1")
17
- end
18
-
19
- def test_insert_and_select
20
- @db.execute("INSERT INTO t1 VALUES(NULL, 'text1', 1.22, 42, NULL)")
21
- rows = @db.execute("SELECT * FROM t1")
22
- assert_equal 1, rows.size
23
- row = rows[0]
24
- assert_equal "text1", row[1]
25
- assert_equal "1.22", row[2]
26
- assert_equal "42", row[3]
27
- assert_nil row[4]
28
- end
29
- end