sqlite3 0.1.1 → 1.3.3.beta.1

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.
Files changed (55) hide show
  1. data/API_CHANGES.rdoc +50 -0
  2. data/CHANGELOG.rdoc +177 -0
  3. data/ChangeLog.cvs +88 -0
  4. data/LICENSE +24 -17
  5. data/Manifest.txt +50 -0
  6. data/README.rdoc +100 -4
  7. data/Rakefile +10 -0
  8. data/ext/sqlite3/backup.c +164 -0
  9. data/ext/sqlite3/backup.h +15 -0
  10. data/ext/sqlite3/database.c +762 -0
  11. data/ext/sqlite3/database.h +15 -0
  12. data/ext/sqlite3/exception.c +94 -0
  13. data/ext/sqlite3/exception.h +8 -0
  14. data/ext/sqlite3/extconf.rb +47 -0
  15. data/ext/sqlite3/sqlite3.c +36 -0
  16. data/ext/sqlite3/sqlite3_ruby.h +44 -0
  17. data/ext/sqlite3/statement.c +419 -0
  18. data/ext/sqlite3/statement.h +16 -0
  19. data/faq/faq.rb +145 -0
  20. data/faq/faq.yml +426 -0
  21. data/lib/sqlite3.rb +9 -12
  22. data/lib/sqlite3/constants.rb +46 -72
  23. data/lib/sqlite3/database.rb +415 -140
  24. data/lib/sqlite3/errors.rb +1 -56
  25. data/lib/sqlite3/pragmas.rb +246 -63
  26. data/lib/sqlite3/resultset.rb +30 -112
  27. data/lib/sqlite3/statement.rb +42 -108
  28. data/lib/sqlite3/translator.rb +118 -0
  29. data/lib/sqlite3/value.rb +57 -0
  30. data/lib/sqlite3/version.rb +23 -1
  31. data/setup.rb +1333 -0
  32. data/tasks/faq.rake +9 -0
  33. data/tasks/gem.rake +31 -0
  34. data/tasks/native.rake +61 -0
  35. data/tasks/vendor_sqlite3.rake +104 -0
  36. data/test/helper.rb +3 -0
  37. data/test/test_backup.rb +33 -0
  38. data/test/test_collation.rb +82 -0
  39. data/test/test_database.rb +312 -0
  40. data/test/test_database_readonly.rb +29 -0
  41. data/test/test_deprecated.rb +37 -0
  42. data/test/test_encoding.rb +119 -0
  43. data/test/test_integration.rb +544 -0
  44. data/test/test_integration_open_close.rb +30 -0
  45. data/test/test_integration_pending.rb +115 -0
  46. data/test/test_integration_resultset.rb +156 -0
  47. data/test/test_integration_statement.rb +194 -0
  48. data/test/test_sqlite3.rb +9 -0
  49. data/test/test_statement.rb +207 -0
  50. data/test/test_statement_execute.rb +35 -0
  51. metadata +124 -67
  52. data/lib/sqlite3/api.rb +0 -52
  53. data/lib/sqlite3/driver.rb +0 -138
  54. data/lib/sqlite3/encoding.rb +0 -43
  55. data/lib/sqlite3/extensions.rb +0 -27
@@ -1,52 +0,0 @@
1
- module SQLite3
2
- module API
3
- extend FFI::Library
4
-
5
- lib_paths = Array(ENV["SQLITE3_LIB"] || Dir["/{opt,usr}/{,local/}lib{,64}/libsqlite3.{dylib,so*}"])
6
- fallback_names = ["libsqlite3", "sqlite3"]
7
- ffi_lib(lib_paths + fallback_names)
8
-
9
- attach_function :sqlite3_bind_blob, [:pointer, :int, :pointer, :int, :pointer], :int
10
- attach_function :sqlite3_bind_double, [:pointer, :int, :double], :int
11
- attach_function :sqlite3_bind_int, [:pointer, :int, :int], :int
12
- attach_function :sqlite3_bind_int64, [:pointer, :int, :int64], :int
13
- attach_function :sqlite3_bind_null, [:pointer, :int], :int
14
- attach_function :sqlite3_bind_parameter_index, [:pointer, :string], :int
15
- attach_function :sqlite3_bind_text, [:pointer, :int, :string, :int, :pointer], :int
16
- attach_function :sqlite3_bind_text16, [:pointer, :int, :pointer, :int, :pointer], :int
17
- attach_function :sqlite3_busy_timeout, [:pointer, :int], :int
18
- attach_function :sqlite3_changes, [:pointer], :int
19
- attach_function :sqlite3_close, [:pointer], :int
20
- attach_function :sqlite3_column_blob, [:pointer, :int], :pointer
21
- attach_function :sqlite3_column_bytes, [:pointer, :int], :int
22
- attach_function :sqlite3_column_bytes16, [:pointer, :int], :int
23
- attach_function :sqlite3_column_count, [:pointer], :int
24
- attach_function :sqlite3_column_decltype, [:pointer, :int], :string
25
- attach_function :sqlite3_column_double, [:pointer, :int], :double
26
- attach_function :sqlite3_column_int64, [:pointer, :int], :int64
27
- attach_function :sqlite3_column_name, [:pointer, :int], :string
28
- attach_function :sqlite3_column_text, [:pointer, :int], :string
29
- attach_function :sqlite3_column_text16, [:pointer, :int], :pointer
30
- attach_function :sqlite3_column_type, [:pointer, :int], :int
31
- attach_function :sqlite3_data_count, [:pointer], :int
32
- attach_function :sqlite3_errcode, [:pointer], :int
33
- attach_function :sqlite3_errmsg, [:pointer], :string
34
- attach_function :sqlite3_errmsg16, [:pointer], :pointer
35
- attach_function :sqlite3_finalize, [:pointer], :int
36
- attach_function :sqlite3_last_insert_rowid, [:pointer], :int64
37
- attach_function :sqlite3_libversion, [], :string
38
- attach_function :sqlite3_open, [:string, :pointer], :int
39
- attach_function :sqlite3_open16, [:pointer, :pointer], :int
40
- attach_function :sqlite3_prepare, [:pointer, :string, :int, :pointer, :pointer], :int
41
- attach_function :sqlite3_reset, [:pointer], :int
42
- attach_function :sqlite3_step, [:pointer], :int
43
-
44
- begin
45
- attach_function :sqlite3_enable_load_extension, [:pointer, :int], :int
46
- attach_function :sqlite3_load_extension, [:pointer, :string, :string, :string], :int
47
- EXTENSION_SUPPORT = true
48
- rescue FFI::NotFoundError
49
- EXTENSION_SUPPORT = false
50
- end
51
- end
52
- end
@@ -1,138 +0,0 @@
1
- module SQLite3
2
- class Driver
3
- TRANSIENT = FFI::Pointer.new(-1)
4
-
5
- def open(filename, utf_16 = false)
6
- handle = FFI::MemoryPointer.new(:pointer)
7
- if utf_16
8
- filename = filename.encode(Encoding.utf_16native) if filename.respond_to?(:encode)
9
- result = API.sqlite3_open16(c_string(filename), handle)
10
- else
11
- filename = filename.encode(Encoding.utf_8) if filename.respond_to?(:encode)
12
- result = API.sqlite3_open(filename, handle)
13
- end
14
- [result, handle.get_pointer(0)]
15
- end
16
-
17
- def errmsg(db, utf_16 = false)
18
- if utf_16
19
- ptr = API.sqlite3_errmsg16(db)
20
- result = get_string_utf_16(ptr)
21
- result.force_encoding(Encoding.utf_16native) if result.respond_to?(:force_encoding)
22
- result
23
- else
24
- result = API.sqlite3_errmsg(db)
25
- result.force_encoding(Encoding.utf_8) if result.respond_to?(:force_encoding)
26
- result
27
- end
28
- end
29
-
30
- def prepare(db, sql)
31
- handle = FFI::MemoryPointer.new(:pointer)
32
- remainder = FFI::MemoryPointer.new(:pointer)
33
-
34
- if Encoding.utf_16?(sql)
35
- str = c_string(sql)
36
- result = API.sqlite3_prepare16(db, str, str.bytesize, handle, remainder)
37
- remainder_string = get_string_utf_16(remainder.get_pointer(0))
38
- else
39
- result = API.sqlite3_prepare(db, sql, sql.bytesize, handle, remainder)
40
- remainder_string = remainder.get_pointer(0).get_string(0)
41
- end
42
-
43
- [result, handle.get_pointer(0), remainder_string]
44
- end
45
-
46
- def bind_string(stmt, index, value)
47
- case value.encoding
48
- when Encoding.utf_8, Encoding.us_ascii
49
- API.sqlite3_bind_text(stmt, index, value, value.bytesize, TRANSIENT)
50
- when Encoding.utf_16le, Encoding.utf_16be
51
- value = add_byte_order_mask(value)
52
- API.sqlite3_bind_text16(stmt, index, value, value.bytesize, TRANSIENT)
53
- else
54
- API.sqlite3_bind_blob(stmt, index, value, value.bytesize, TRANSIENT)
55
- end
56
- end
57
-
58
- def column_blob(stmt, column)
59
- blob = API.sqlite3_column_blob(stmt, column)
60
- length = API.sqlite3_column_bytes(stmt, column)
61
- blob.get_bytes(0, length) # free?
62
- end
63
-
64
- def column_text(stmt, column, utf_16 = false)
65
- if utf_16
66
- ptr = API.sqlite3_column_text16(stmt, column)
67
- length = API.sqlite3_column_bytes16(stmt, column)
68
- ptr.get_bytes(0, length).force_encoding(Encoding.utf_16native) # free?
69
- else
70
- API.sqlite3_column_text(stmt, column).force_encoding(Encoding.utf_8)
71
- end
72
- end
73
-
74
- def extension_support?
75
- API::EXTENSION_SUPPORT
76
- end
77
-
78
- def load_extension(db, name, entry_point = nil)
79
- result = API.sqlite3_load_extension(db, name, entry_point, nil)
80
- [result, nil]
81
- end
82
-
83
- def enable_load_extension(db, onoff = false)
84
- API.sqlite3_enable_load_extension(db, (onoff ? 1 : 0))
85
- end
86
-
87
- def self.api_delegate(name)
88
- define_method(name) { |*args| API.send("sqlite3_#{name}", *args) }
89
- end
90
-
91
- api_delegate :bind_double
92
- api_delegate :bind_int
93
- api_delegate :bind_int64
94
- api_delegate :bind_null
95
- api_delegate :bind_parameter_index
96
- api_delegate :busy_timeout
97
- api_delegate :changes
98
- api_delegate :close
99
- api_delegate :column_count
100
- api_delegate :column_decltype
101
- api_delegate :column_double
102
- api_delegate :column_int64
103
- api_delegate :column_name
104
- api_delegate :column_type
105
- api_delegate :data_count
106
- api_delegate :errcode
107
- api_delegate :finalize
108
- api_delegate :last_insert_rowid
109
- api_delegate :libversion
110
- api_delegate :reset
111
- api_delegate :step
112
-
113
- private
114
-
115
- def c_string(string)
116
- if Encoding.utf_16?(string)
117
- result = add_byte_order_mask(string)
118
- terminate_string!(result)
119
- else
120
- string # FFI does the job
121
- end
122
- end
123
-
124
- def add_byte_order_mask(string)
125
- "\uFEFF".encode(string.encoding) + string
126
- end
127
-
128
- def terminate_string!(string)
129
- string << "\0\0".force_encoding(string.encoding)
130
- end
131
-
132
- def get_string_utf_16(ptr)
133
- length = 0
134
- length += 2 until ptr.get_bytes(length, 2) == "\0\0"
135
- ptr.get_bytes(0, length)
136
- end
137
- end
138
- end
@@ -1,43 +0,0 @@
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,27 +0,0 @@
1
- module SQLite3
2
- module Extensions
3
- def enable_load_extension(onoff=true)
4
- must_support_extensions!
5
- result = @driver.enable_load_extension(@handle, onoff)
6
- Error.check(result, self)
7
- end
8
-
9
- def disable_load_extension
10
- must_support_extensions!
11
- result = @driver.enable_load_extension(@handle, false)
12
- Error.check(result, self)
13
- end
14
-
15
- def load_extension(name, entry_point=nil)
16
- must_support_extensions!
17
- result, message = @driver.load_extension(@handle, name, entry_point)
18
- Error.check(result, self, message)
19
- end
20
-
21
- private
22
-
23
- def must_support_extensions!
24
- raise SQLite3::Exception, "extensions API not supported" unless @driver.extension_support?
25
- end
26
- end
27
- end