sqlite3-ruby 1.2.5-x86-mingw32 → 1.3.0.beta.1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/API_CHANGES.rdoc +48 -0
  2. data/{History.txt → CHANGELOG.rdoc} +24 -0
  3. data/Manifest.txt +14 -14
  4. data/{README.txt → README.rdoc} +1 -6
  5. data/Rakefile +8 -3
  6. data/ext/sqlite3/database.c +687 -0
  7. data/ext/sqlite3/database.h +15 -0
  8. data/ext/sqlite3/exception.c +94 -0
  9. data/ext/sqlite3/exception.h +8 -0
  10. data/ext/sqlite3/extconf.rb +26 -0
  11. data/ext/sqlite3/sqlite3.c +33 -0
  12. data/ext/sqlite3/sqlite3_ruby.h +43 -0
  13. data/ext/sqlite3/statement.c +412 -0
  14. data/ext/sqlite3/statement.h +16 -0
  15. data/lib/sqlite3/1.8/sqlite3_native.so +0 -0
  16. data/lib/sqlite3/1.9/sqlite3_native.so +0 -0
  17. data/lib/sqlite3/database.rb +94 -302
  18. data/lib/sqlite3/errors.rb +0 -24
  19. data/lib/sqlite3/pragmas.rb +16 -7
  20. data/lib/sqlite3/resultset.rb +25 -81
  21. data/lib/sqlite3/statement.rb +22 -107
  22. data/lib/sqlite3/version.rb +4 -4
  23. data/lib/sqlite3.rb +9 -0
  24. data/setup.rb +2 -2
  25. data/tasks/native.rake +13 -17
  26. data/tasks/vendor_sqlite3.rake +10 -7
  27. data/test/helper.rb +1 -65
  28. data/test/test_database.rb +239 -189
  29. data/test/test_encoding.rb +115 -0
  30. data/test/test_integration.rb +38 -35
  31. data/test/test_integration_open_close.rb +1 -1
  32. data/test/test_integration_pending.rb +6 -4
  33. data/test/test_integration_resultset.rb +20 -8
  34. data/test/test_integration_statement.rb +1 -2
  35. data/test/test_sqlite3.rb +9 -0
  36. data/test/test_statement.rb +193 -0
  37. metadata +84 -49
  38. data/ext/sqlite3_api/extconf.rb +0 -10
  39. data/ext/sqlite3_api/sqlite3_api.i +0 -362
  40. data/ext/sqlite3_api/sqlite3_api_wrap.c +0 -5018
  41. data/lib/1.8/sqlite3_api.so +0 -0
  42. data/lib/1.9/sqlite3_api.so +0 -0
  43. data/lib/sqlite3/driver/dl/api.rb +0 -152
  44. data/lib/sqlite3/driver/dl/driver.rb +0 -307
  45. data/lib/sqlite3/driver/native/driver.rb +0 -219
  46. data/tasks/benchmark.rake +0 -9
  47. data/tasks/gem.rake +0 -32
  48. data/test/bm.rb +0 -140
  49. data/test/driver/dl/tc_driver.rb +0 -292
  50. data/test/native-vs-dl.rb +0 -126
  51. data/test/test_errors.rb +0 -17
data/setup.rb CHANGED
@@ -481,7 +481,7 @@ module FileOperations
481
481
  end
482
482
 
483
483
  def extdir?(dir)
484
- File.exist?(dir + '/MANIFEST')
484
+ File.exist?(dir + '/MANIFEST') or File.exist?("#{dir}/extconf.rb")
485
485
  end
486
486
 
487
487
  def all_files_in(dirname)
@@ -1149,7 +1149,7 @@ class Installer
1149
1149
  def install_dir_ext(rel)
1150
1150
  return unless extdir?(curr_srcdir())
1151
1151
  install_files ruby_extentions('.'),
1152
- "#{config('so-dir')}/#{File.dirname(rel)}",
1152
+ "#{config('so-dir')}/#{rel}",
1153
1153
  0555
1154
1154
  end
1155
1155
 
data/tasks/native.rake CHANGED
@@ -1,19 +1,22 @@
1
1
  # use rake-compiler for building the extension
2
2
  require 'rake/extensiontask'
3
3
 
4
- # build sqlite3_api C extension
5
- Rake::ExtensionTask.new('sqlite3_api', HOE.spec) do |ext|
4
+ # build sqlite3_native C extension
5
+ Rake::ExtensionTask.new('sqlite3_native', HOE.spec) do |ext|
6
+ # where to locate the extension
7
+ ext.ext_dir = 'ext/sqlite3'
8
+
9
+ # where native extension will be copied (matches makefile)
10
+ ext.lib_dir = "lib/sqlite3"
11
+
6
12
  # reference to the sqlite3 library
7
13
  sqlite3_lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor', 'sqlite3'))
8
14
 
9
- # define target for extension (supporting fat binaries)
10
- if RUBY_PLATFORM =~ /mingw/ then
11
- ruby_ver = RUBY_VERSION.match(/(\d+\.\d+)/)[1]
12
- ext.lib_dir = "lib/#{ruby_ver}"
13
- end
14
-
15
15
  # automatically add build options to avoid need of manual input
16
16
  if RUBY_PLATFORM =~ /mswin|mingw/ then
17
+ # define target for extension (supporting fat binaries)
18
+ RUBY_VERSION =~ /(\d+\.\d+)/
19
+ ext.lib_dir = "lib/sqlite3/#{$1}"
17
20
  ext.config_options << "--with-sqlite3-dir=#{sqlite3_lib}"
18
21
  else
19
22
  ext.cross_compile = true
@@ -22,14 +25,7 @@ Rake::ExtensionTask.new('sqlite3_api', HOE.spec) do |ext|
22
25
  end
23
26
  end
24
27
 
25
- # C wrapper depends on swig file to be generated
26
- file 'ext/sqlite3_api/sqlite3_api_wrap.c' => ['ext/sqlite3_api/sqlite3_api.i'] do |t|
27
- begin
28
- sh "swig -ruby -o #{t.name} #{t.prerequisites.first}"
29
- rescue
30
- fail "could not build wrapper via swig (perhaps swig is not installed?)"
31
- end
32
- end
33
-
34
28
  # ensure things are compiled prior testing
35
29
  task :test => [:compile]
30
+
31
+ # vim: syntax=ruby
@@ -25,14 +25,15 @@ def dlltool(dllname, deffile, libfile)
25
25
  end
26
26
  end
27
27
 
28
- version = '3_6_16'
28
+ SQLITE_VERSION = '3.6.22'
29
+ url_version = SQLITE_VERSION.gsub('.', '_')
29
30
 
30
31
  # required folder structure for --with-sqlite3-dir (include + lib)
31
32
  directory "vendor/sqlite3/lib"
32
33
  directory "vendor/sqlite3/include"
33
34
 
34
35
  # download amalgamation version (for include files)
35
- file "vendor/sqlite-amalgamation-#{version}.zip" => ['vendor'] do |t|
36
+ file "vendor/sqlite-amalgamation-#{url_version}.zip" => ['vendor'] do |t|
36
37
  url = "http://www.sqlite.org/#{File.basename(t.name)}"
37
38
  when_writing "downloading #{t.name}" do
38
39
  cd File.dirname(t.name) do
@@ -42,7 +43,7 @@ file "vendor/sqlite-amalgamation-#{version}.zip" => ['vendor'] do |t|
42
43
  end
43
44
 
44
45
  # download dll binaries
45
- file "vendor/sqlitedll-#{version}.zip" => ['vendor'] do |t|
46
+ file "vendor/sqlitedll-#{url_version}.zip" => ['vendor'] do |t|
46
47
  url = "http://www.sqlite.org/#{File.basename(t.name)}"
47
48
  when_writing "downloading #{t.name}" do
48
49
  cd File.dirname(t.name) do
@@ -52,7 +53,7 @@ file "vendor/sqlitedll-#{version}.zip" => ['vendor'] do |t|
52
53
  end
53
54
 
54
55
  # extract header files into include folder
55
- file "vendor/sqlite3/include/sqlite3.h" => ['vendor/sqlite3/include', "vendor/sqlite-amalgamation-#{version}.zip"] do |t|
56
+ file "vendor/sqlite3/include/sqlite3.h" => ['vendor/sqlite3/include', "vendor/sqlite-amalgamation-#{url_version}.zip"] do |t|
56
57
  full_file = File.expand_path(t.prerequisites.last)
57
58
  when_writing "creating #{t.name}" do
58
59
  cd File.dirname(t.name) do
@@ -64,7 +65,7 @@ file "vendor/sqlite3/include/sqlite3.h" => ['vendor/sqlite3/include', "vendor/sq
64
65
  end
65
66
 
66
67
  # extract dll files into lib folder
67
- file "vendor/sqlite3/lib/sqlite3.dll" => ['vendor/sqlite3/lib', "vendor/sqlitedll-#{version}.zip"] do |t|
68
+ file "vendor/sqlite3/lib/sqlite3.dll" => ['vendor/sqlite3/lib', "vendor/sqlitedll-#{url_version}.zip"] do |t|
68
69
  full_file = File.expand_path(t.prerequisites.last)
69
70
  when_writing "creating #{t.name}" do
70
71
  cd File.dirname(t.name) do
@@ -88,7 +89,7 @@ end
88
89
 
89
90
  # clean and clobber actions
90
91
  # All the uncompressed files must be removed at clean
91
- CLEAN.include('vendor/sqlite3')
92
+ CLEAN.include('vendor/sqlite3')
92
93
 
93
94
  # clobber vendored packages
94
95
  CLOBBER.include('vendor')
@@ -100,5 +101,7 @@ task 'vendor:sqlite3' => ["vendor/sqlite3/lib/sqlite3.lib", "vendor/sqlite3/incl
100
101
  if RUBY_PLATFORM =~ /mingw|mswin/ then
101
102
  Rake::Task['compile'].prerequisites.unshift 'vendor:sqlite3'
102
103
  else
103
- Rake::Task['cross'].prerequisites.unshift 'vendor:sqlite3'
104
+ if Rake::Task.task_defined?(:cross)
105
+ Rake::Task['cross'].prerequisites.unshift 'vendor:sqlite3'
106
+ end
104
107
  end
data/test/helper.rb CHANGED
@@ -1,67 +1,3 @@
1
- # add lib folder to the path
2
- $:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
3
-
4
1
  require 'sqlite3'
5
-
6
- require 'rubygems'
7
2
  require 'test/unit'
8
-
9
- # define mocks to be used
10
- require 'mocha'
11
-
12
- class Driver < Mocha::Mock
13
- def initialize
14
- super
15
- stubs( :open ).returns([0, 'cookie'])
16
- stubs( :close ).returns(0)
17
- stubs( :complete? ).returns(0)
18
- stubs( :errmsg ).returns('')
19
- stubs( :errcode ).returns(0)
20
- stubs( :trace ).returns(nil)
21
- stubs( :set_authorizer ).returns(0)
22
- stubs( :prepare ).returns([0, 'stmt', 'remainder'])
23
- stubs( :finalize ).returns(0)
24
- stubs( :changes ).returns(14)
25
- stubs( :total_changes ).returns(28)
26
- stubs( :interrupt ).returns(0)
27
- end
28
- end
29
-
30
- class MockResultSet < Mocha::Mock
31
- def initialize
32
- super
33
- stubs( :each ).yields(['foo'])
34
- stubs( :columns ).returns(['name'])
35
- end
36
- end
37
-
38
- class Statement < Mocha::Mock
39
- attr_reader :handle
40
- attr_reader :sql
41
- attr_reader :last_result
42
-
43
- def initialize( handle, sql )
44
- super()
45
- @handle = handle
46
- @sql = sql
47
- stubs( :close ).returns(0)
48
- stubs( :remainder ).returns('')
49
- stubs( :execute ).returns(MockResultSet.new)
50
- end
51
- end
52
-
53
- # UTF conversion extensions
54
- class String
55
- def to_utf16(terminate=false)
56
- self.split(//).map { |c| c[0] }.pack("v*") +
57
- (terminate ? "\0\0" : "")
58
- end
59
-
60
- def from_utf16
61
- result = ""
62
- length.times do |i|
63
- result << self[i,1] if i % 2 == 0 && self[i] != 0
64
- end
65
- result
66
- end
67
- end
3
+ require 'iconv'
@@ -1,217 +1,267 @@
1
- require File.join(File.dirname(__FILE__), 'helper')
2
-
3
- class TC_Database_Init < Test::Unit::TestCase
4
- def test_new
5
- # any_instance fails here...
6
- driver = Driver.new
7
- driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
8
- Driver.stubs(:new).returns(driver)
9
- db = SQLite3::Database.new( 'foo.db', :driver => Driver )
10
- assert !db.closed?
11
- assert !db.results_as_hash
12
- assert !db.type_translation
13
- end
14
-
15
- def test_new_with_block
16
- driver = Driver.new
17
- driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
18
- Driver.stubs(:new).returns(driver)
19
- returned_db = SQLite3::Database.new( "foo.db", :driver => Driver ) do |db|
20
- assert !db.closed?
21
- assert !db.results_as_hash
22
- assert !db.type_translation
23
- end
24
- assert returned_db.closed?
25
- end
1
+ require 'helper'
2
+ require 'iconv'
26
3
 
27
- def test_open
28
- driver = Driver.new
29
- driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
30
- Driver.stubs(:new).returns(driver)
31
- db = SQLite3::Database.open( "foo.db", :driver => Driver )
32
- assert !db.closed?
33
- assert !db.results_as_hash
34
- assert !db.type_translation
35
- end
36
-
37
- def test_open_with_block
38
- driver = Driver.new
39
- driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
40
- Driver.stubs(:new).returns(driver)
41
- returned_db = SQLite3::Database.open( "foo.db", :driver => Driver ) do |db|
42
- assert !db.closed?
43
- assert !db.results_as_hash
44
- assert !db.type_translation
45
- end
46
- assert returned_db.closed?
47
- end
48
-
49
- def test_with_type_translation
50
- db = SQLite3::Database.open( "foo.db", :driver => Driver,
51
- :type_translation => true )
52
- assert db.type_translation
53
- end
54
-
55
- def test_with_results_as_hash
56
- db = SQLite3::Database.open( "foo.db", :driver => Driver,
57
- :results_as_hash => true )
58
- assert db.results_as_hash
59
- end
60
-
61
- def test_with_type_translation_and_results_as_hash
62
- db = SQLite3::Database.open( "foo.db", :driver => Driver,
63
- :results_as_hash => true,
64
- :type_translation => true )
65
- assert db.results_as_hash
66
- assert db.type_translation
67
- end
68
- end
4
+ module SQLite3
5
+ class TestDatabase < Test::Unit::TestCase
6
+ def setup
7
+ @db = SQLite3::Database.new(':memory:')
8
+ end
69
9
 
70
- class TC_Database < Test::Unit::TestCase
71
- def setup
72
- @db = SQLite3::Database.open( "foo.db",
73
- :driver => Driver, :statement_factory => Statement )
74
- end
10
+ def test_new
11
+ db = SQLite3::Database.new(':memory:')
12
+ assert db
13
+ end
75
14
 
76
- def test_quote
77
- assert_equal "''one''two''three''", SQLite3::Database.quote(
78
- "'one'two'three'" )
79
- end
15
+ def test_new_yields_self
16
+ thing = nil
17
+ SQLite3::Database.new(':memory:') do |db|
18
+ thing = db
19
+ end
20
+ assert_instance_of(SQLite3::Database, thing)
21
+ end
80
22
 
81
- def test_complete
82
- Driver.any_instance.expects(:complete?)
83
- @db.complete? "foo"
84
- end
23
+ def test_new_with_options
24
+ db = SQLite3::Database.new(Iconv.conv('UTF-16LE', 'UTF-8', ':memory:'),
25
+ :utf16 => true)
26
+ assert db
27
+ end
85
28
 
86
- def test_errmsg
87
- Driver.any_instance.expects(:errmsg)
88
- @db.errmsg
89
- end
29
+ def test_close
30
+ db = SQLite3::Database.new(':memory:')
31
+ db.close
32
+ assert db.closed?
33
+ end
90
34
 
91
- def test_errcode
92
- Driver.any_instance.expects(:errcode)
93
- @db.errcode
94
- end
35
+ def test_block_closes_self
36
+ thing = nil
37
+ SQLite3::Database.new(':memory:') do |db|
38
+ thing = db
39
+ assert !thing.closed?
40
+ end
41
+ assert thing.closed?
42
+ end
95
43
 
96
- def test_translator
97
- translator = @db.translator
98
- assert_instance_of SQLite3::Translator, translator
99
- end
44
+ def test_prepare
45
+ db = SQLite3::Database.new(':memory:')
46
+ stmt = db.prepare('select "hello world"')
47
+ assert_instance_of(SQLite3::Statement, stmt)
48
+ end
100
49
 
101
- def test_close
102
- Driver.any_instance.expects(:close).returns(0)
103
- @db.close
104
- assert @db.closed?
105
- Driver.any_instance.expects(:close).never
106
- @db.close
107
- end
50
+ def test_total_changes
51
+ db = SQLite3::Database.new(':memory:')
52
+ db.execute("create table foo ( a integer primary key, b text )")
53
+ db.execute("insert into foo (b) values ('hello')")
54
+ assert_equal 1, db.total_changes
55
+ end
108
56
 
109
- def test_trace
110
- Driver.any_instance.expects(:trace).with('cookie', 15)
111
- @db.trace( 15 ) { "foo" }
112
- # assert_equal 1, driver.mock_blocks[:trace].length
113
- end
57
+ def test_execute_returns_list_of_hash
58
+ db = SQLite3::Database.new(':memory:', :results_as_hash => true)
59
+ db.execute("create table foo ( a integer primary key, b text )")
60
+ db.execute("insert into foo (b) values ('hello')")
61
+ rows = db.execute("select * from foo")
62
+ assert_equal [{0=>1, "a"=>1, "b"=>"hello", 1=>"hello"}], rows
63
+ end
114
64
 
115
- def test_authorizer
116
- Driver.any_instance.expects(:set_authorizer).with('cookie', 15).returns(0)
117
- @db.authorizer( 15 ) { "foo" }
118
- # assert_equal 1, driver.mock_blocks[:set_authorizer].length
119
- end
65
+ def test_execute_yields_hash
66
+ db = SQLite3::Database.new(':memory:', :results_as_hash => true)
67
+ db.execute("create table foo ( a integer primary key, b text )")
68
+ db.execute("insert into foo (b) values ('hello')")
69
+ db.execute("select * from foo") do |row|
70
+ assert_equal({0=>1, "a"=>1, "b"=>"hello", 1=>"hello"}, row)
71
+ end
72
+ end
120
73
 
121
- def test_prepare_no_block
122
- Statement.any_instance.expects(:close).never
123
- assert_nothing_raised { @db.prepare( "foo" ) }
124
- end
74
+ def test_table_info
75
+ db = SQLite3::Database.new(':memory:', :results_as_hash => true)
76
+ db.execute("create table foo ( a integer primary key, b text )")
77
+ info = [{
78
+ "name" => "a",
79
+ "pk" => 1,
80
+ "notnull" => 0,
81
+ "type" => "integer",
82
+ "dflt_value" => nil,
83
+ "cid" => 0
84
+ },
85
+ {
86
+ "name" => "b",
87
+ "pk" => 0,
88
+ "notnull" => 0,
89
+ "type" => "text",
90
+ "dflt_value" => nil,
91
+ "cid" => 1
92
+ }]
93
+ assert_equal info, db.table_info('foo')
94
+ end
125
95
 
126
- def test_prepare_with_block
127
- called = false
128
- # any_instance fails here...
129
- statement = Statement.new('cookie', 'foo')
130
- statement.expects(:close).once
131
- Statement.stubs(:new).returns(statement)
132
- @db.prepare( "foo" ) { |stmt| called = true }
133
- assert called
134
- end
96
+ def test_total_changes_closed
97
+ db = SQLite3::Database.new(':memory:')
98
+ db.close
99
+ assert_raise(SQLite3::Exception) do
100
+ db.total_changes
101
+ end
102
+ end
135
103
 
136
- def test_execute_no_block
137
- # any_instance fails here...
138
- statement = Statement.new('cookie', 'foo')
139
- statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
140
- Statement.stubs(:new).returns(statement)
141
- MockResultSet.any_instance.stubs(:inject).returns([['foo']])
142
- result = @db.execute( "foo", "bar", "baz" )
143
- assert_equal [["foo"]], result
144
- end
104
+ def test_trace_requires_opendb
105
+ @db.close
106
+ assert_raise(SQLite3::Exception) do
107
+ @db.trace { |x| }
108
+ end
109
+ end
145
110
 
146
- def test_execute_with_block
147
- called = false
148
- # any_instance fails here...
149
- statement = Statement.new('cookie', 'foo')
150
- statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
151
- Statement.stubs(:new).returns(statement)
152
- @db.execute( "foo", "bar", "baz" ) do |row|
153
- called = true
154
- assert_equal ["foo"], row
111
+ def test_trace_with_block
112
+ result = nil
113
+ @db.trace { |sql| result = sql }
114
+ @db.execute "select 'foo'"
115
+ assert_equal "select 'foo'", result
155
116
  end
156
117
 
157
- assert called
158
- end
118
+ def test_trace_with_object
119
+ obj = Class.new {
120
+ attr_accessor :result
121
+ def call sql; @result = sql end
122
+ }.new
159
123
 
160
- def test_execute2_no_block
161
- # any_instance fails here...
162
- statement = Statement.new('cookie', 'foo')
163
- statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
164
- Statement.stubs(:new).returns(statement)
165
- MockResultSet.any_instance.stubs(:inject).returns([['name'], ['foo']])
166
- result = @db.execute2( "foo", "bar", "baz" )
167
- assert_equal [["name"],["foo"]], result
168
- end
124
+ @db.trace(obj)
125
+ @db.execute "select 'foo'"
126
+ assert_equal "select 'foo'", obj.result
127
+ end
169
128
 
170
- def test_execute2_with_block
171
- called = false
172
- parts = [ ["name"],["foo"] ]
173
- # any_instance fails here...
174
- statement = Statement.new('cookie', 'foo')
175
- statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
176
- Statement.stubs(:new).returns(statement)
177
- @db.execute2( "foo", "bar", "baz" ) do |row|
178
- called = true
179
- assert_equal parts.shift, row
129
+ def test_trace_takes_nil
130
+ @db.trace(nil)
131
+ @db.execute "select 'foo'"
180
132
  end
181
133
 
182
- assert called
183
- end
134
+ def test_last_insert_row_id_closed
135
+ @db.close
136
+ assert_raise(SQLite3::Exception) do
137
+ @db.last_insert_row_id
138
+ end
139
+ end
184
140
 
185
- def test_execute_batch
186
- # any_instance fails here...
187
- statement = Statement.new('cookie', 'foo')
188
- statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
189
- Statement.stubs(:new).returns(statement)
190
- @db.execute_batch( "foo", "bar", "baz" )
191
- end
141
+ def test_define_function
142
+ called_with = nil
143
+ @db.define_function("hello") do |value|
144
+ called_with = value
145
+ end
146
+ @db.execute("select hello(10)")
147
+ assert_equal 10, called_with
148
+ end
192
149
 
193
- def test_get_first_row
194
- result = @db.get_first_row( "foo", "bar", "baz" )
195
- assert_equal ["foo"], result
196
- end
150
+ def test_call_func_arg_type
151
+ called_with = nil
152
+ @db.define_function("hello") do |b, c, d|
153
+ called_with = [b, c, d]
154
+ nil
155
+ end
156
+ @db.execute("select hello(2.2, 'foo', NULL)")
157
+ assert_equal [2.2, 'foo', nil], called_with
158
+ end
197
159
 
198
- def test_get_first_value
199
- result = @db.get_first_value( "foo", "bar", "baz" )
200
- assert_equal "foo", result
201
- end
160
+ def test_define_varargs
161
+ called_with = nil
162
+ @db.define_function("hello") do |*args|
163
+ called_with = args
164
+ nil
165
+ end
166
+ @db.execute("select hello(2.2, 'foo', NULL)")
167
+ assert_equal [2.2, 'foo', nil], called_with
168
+ end
202
169
 
203
- def test_changes
204
- Driver.any_instance.expects(:changes).returns(14)
205
- assert_equal 14, @db.changes
206
- end
170
+ def test_function_return
171
+ @db.define_function("hello") { |a| 10 }
172
+ assert_equal [10], @db.execute("select hello('world')").first
173
+ end
207
174
 
208
- def test_total_changes
209
- Driver.any_instance.expects(:total_changes).returns(28)
210
- assert_equal 28, @db.total_changes
211
- end
175
+ def test_function_return_types
176
+ [10, 2.2, nil, "foo"].each do |thing|
177
+ @db.define_function("hello") { |a| thing }
178
+ assert_equal [thing], @db.execute("select hello('world')").first
179
+ end
180
+ end
181
+
182
+ def test_define_function_closed
183
+ @db.close
184
+ assert_raise(SQLite3::Exception) do
185
+ @db.define_function('foo') { }
186
+ end
187
+ end
188
+
189
+ def test_inerrupt_closed
190
+ @db.close
191
+ assert_raise(SQLite3::Exception) do
192
+ @db.interrupt
193
+ end
194
+ end
195
+
196
+ def test_define_aggregate
197
+ @db.execute "create table foo ( a integer primary key, b text )"
198
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
199
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
200
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
201
+
202
+ acc = Class.new {
203
+ attr_reader :sum
204
+ alias :finalize :sum
205
+ def initialize
206
+ @sum = 0
207
+ end
208
+
209
+ def step a
210
+ @sum += a
211
+ end
212
+ }.new
213
+
214
+ @db.define_aggregator("accumulate", acc)
215
+ value = @db.get_first_value( "select accumulate(a) from foo" )
216
+ assert_equal 6, value
217
+ end
218
+
219
+ def test_authorizer_ok
220
+ @db.authorizer = Class.new {
221
+ def call action, a, b, c, d; true end
222
+ }.new
223
+ @db.prepare("select 'fooooo'")
224
+
225
+ @db.authorizer = Class.new {
226
+ def call action, a, b, c, d; 0 end
227
+ }.new
228
+ @db.prepare("select 'fooooo'")
229
+ end
212
230
 
213
- def test_interrupt
214
- Driver.any_instance.expects(:interrupt)
215
- @db.interrupt
231
+ def test_authorizer_ignore
232
+ @db.authorizer = Class.new {
233
+ def call action, a, b, c, d; nil end
234
+ }.new
235
+ stmt = @db.prepare("select 'fooooo'")
236
+ assert_equal nil, stmt.step
237
+ end
238
+
239
+ def test_authorizer_fail
240
+ @db.authorizer = Class.new {
241
+ def call action, a, b, c, d; false end
242
+ }.new
243
+ assert_raises(SQLite3::AuthorizationException) do
244
+ @db.prepare("select 'fooooo'")
245
+ end
246
+ end
247
+
248
+ def test_remove_auth
249
+ @db.authorizer = Class.new {
250
+ def call action, a, b, c, d; false end
251
+ }.new
252
+ assert_raises(SQLite3::AuthorizationException) do
253
+ @db.prepare("select 'fooooo'")
254
+ end
255
+
256
+ @db.authorizer = nil
257
+ @db.prepare("select 'fooooo'")
258
+ end
259
+
260
+ def test_close_with_open_statements
261
+ stmt = @db.prepare("select 'foo'")
262
+ assert_raises(SQLite3::BusyException) do
263
+ @db.close
264
+ end
265
+ end
216
266
  end
217
267
  end