sqlite3-ruby 1.2.4 → 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.cvs +88 -0
- data/History.txt +68 -0
- data/LICENSE +27 -0
- data/Manifest.txt +41 -0
- data/{README.rdoc → README.txt} +6 -1
- data/Rakefile +5 -0
- data/ext/sqlite3_api/extconf.rb +3 -9
- data/ext/sqlite3_api/sqlite3_api.i +4 -0
- data/ext/sqlite3_api/sqlite3_api_wrap.c +4977 -3061
- data/{doc/faq → faq}/faq.rb +0 -0
- data/{doc/faq → faq}/faq.yml +0 -0
- data/lib/sqlite3/database.rb +13 -7
- data/lib/sqlite3/driver/dl/api.rb +1 -1
- data/lib/sqlite3/driver/native/driver.rb +9 -1
- data/lib/sqlite3/resultset.rb +5 -1
- data/lib/sqlite3/statement.rb +3 -2
- data/lib/sqlite3/version.rb +4 -2
- data/setup.rb +1333 -0
- data/tasks/benchmark.rake +9 -0
- data/tasks/faq.rake +9 -0
- data/tasks/gem.rake +32 -0
- data/tasks/native.rake +35 -0
- data/tasks/vendor_sqlite3.rake +104 -0
- data/test/{mocks.rb → helper.rb} +23 -1
- data/test/{tc_database.rb → test_database.rb} +25 -6
- data/test/{tc_errors.rb → test_errors.rb} +1 -5
- data/test/test_integration.rb +542 -0
- data/test/test_integration_open_close.rb +30 -0
- data/test/test_integration_pending.rb +111 -0
- data/test/test_integration_resultset.rb +159 -0
- data/test/test_integration_statement.rb +195 -0
- metadata +86 -39
- data/doc/faq/faq.html +0 -408
- data/ext/sqlite3_api/MANIFEST +0 -4
- data/ext/sqlite3_api/Makefile +0 -146
- data/ext/sqlite3_api/mkmf.log +0 -71
- data/ext/sqlite3_api/win32/build.bat +0 -7
- data/test/tc_integration.rb +0 -1044
- data/test/tests.rb +0 -6
data/tasks/faq.rake
ADDED
data/tasks/gem.rake
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems/package_task'
|
2
|
+
require 'hoe'
|
3
|
+
|
4
|
+
HOE = Hoe.spec 'sqlite3-ruby' do
|
5
|
+
self.rubyforge_name = 'sqlite-ruby'
|
6
|
+
self.author = ['Jamis Buck']
|
7
|
+
self.email = %w[jamis@37signals.com]
|
8
|
+
self.readme_file = 'README.txt'
|
9
|
+
self.need_tar = false
|
10
|
+
self.need_zip = false
|
11
|
+
|
12
|
+
spec_extras[:required_ruby_version] = Gem::Requirement.new('> 1.8.5')
|
13
|
+
|
14
|
+
spec_extras[:extensions] = ["ext/sqlite3_api/extconf.rb"]
|
15
|
+
|
16
|
+
extra_dev_deps << 'mocha'
|
17
|
+
extra_dev_deps << ['rake-compiler', "~> 0.5.0"]
|
18
|
+
|
19
|
+
spec_extras['rdoc_options'] = proc do |rdoc_options|
|
20
|
+
rdoc_options << "--main=README.txt"
|
21
|
+
end
|
22
|
+
|
23
|
+
clean_globs.push('**/test.db')
|
24
|
+
end
|
25
|
+
|
26
|
+
file "#{HOE.spec.name}.gemspec" => ['Rakefile', 'tasks/gem.rake', 'lib/sqlite3/version.rb'] do |t|
|
27
|
+
puts "Generating #{t.name}"
|
28
|
+
File.open(t.name, 'w') { |f| f.puts HOE.spec.to_yaml }
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "Generate or update the standalone gemspec file for the project"
|
32
|
+
task :gemspec => ["#{HOE.spec.name}.gemspec"]
|
data/tasks/native.rake
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# use rake-compiler for building the extension
|
2
|
+
require 'rake/extensiontask'
|
3
|
+
|
4
|
+
# build sqlite3_api C extension
|
5
|
+
Rake::ExtensionTask.new('sqlite3_api', HOE.spec) do |ext|
|
6
|
+
# reference to the sqlite3 library
|
7
|
+
sqlite3_lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor', 'sqlite3'))
|
8
|
+
|
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
|
+
# automatically add build options to avoid need of manual input
|
16
|
+
if RUBY_PLATFORM =~ /mswin|mingw/ then
|
17
|
+
ext.config_options << "--with-sqlite3-dir=#{sqlite3_lib}"
|
18
|
+
else
|
19
|
+
ext.cross_compile = true
|
20
|
+
ext.cross_platform = ['i386-mswin32', 'i386-mingw32']
|
21
|
+
ext.cross_config_options << "--with-sqlite3-dir=#{sqlite3_lib}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
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
|
+
# ensure things are compiled prior testing
|
35
|
+
task :test => [:compile]
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'rake/clean'
|
2
|
+
require 'rake/extensioncompiler'
|
3
|
+
|
4
|
+
# download sqlite3 library and headers
|
5
|
+
|
6
|
+
# only on Windows or cross platform compilation
|
7
|
+
def dlltool(dllname, deffile, libfile)
|
8
|
+
# define if we are using GCC or not
|
9
|
+
if Rake::ExtensionCompiler.mingw_gcc_executable then
|
10
|
+
dir = File.dirname(Rake::ExtensionCompiler.mingw_gcc_executable)
|
11
|
+
tool = case RUBY_PLATFORM
|
12
|
+
when /mingw/
|
13
|
+
File.join(dir, 'dlltool.exe')
|
14
|
+
when /linux|darwin/
|
15
|
+
File.join(dir, "#{Rake::ExtensionCompiler.mingw_host}-dlltool")
|
16
|
+
end
|
17
|
+
return "#{tool} --dllname #{dllname} --def #{deffile} --output-lib #{libfile}"
|
18
|
+
else
|
19
|
+
if RUBY_PLATFORM =~ /mswin/ then
|
20
|
+
tool = 'lib.exe'
|
21
|
+
else
|
22
|
+
fail "Unsupported platform for cross-compilation (please, contribute some patches)."
|
23
|
+
end
|
24
|
+
return "#{tool} /DEF:#{deffile} /OUT:#{libfile}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
version = '3_6_16'
|
29
|
+
|
30
|
+
# required folder structure for --with-sqlite3-dir (include + lib)
|
31
|
+
directory "vendor/sqlite3/lib"
|
32
|
+
directory "vendor/sqlite3/include"
|
33
|
+
|
34
|
+
# download amalgamation version (for include files)
|
35
|
+
file "vendor/sqlite-amalgamation-#{version}.zip" => ['vendor'] do |t|
|
36
|
+
url = "http://www.sqlite.org/#{File.basename(t.name)}"
|
37
|
+
when_writing "downloading #{t.name}" do
|
38
|
+
cd File.dirname(t.name) do
|
39
|
+
system "wget -c #{url} || curl -C - -O #{url}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# download dll binaries
|
45
|
+
file "vendor/sqlitedll-#{version}.zip" => ['vendor'] do |t|
|
46
|
+
url = "http://www.sqlite.org/#{File.basename(t.name)}"
|
47
|
+
when_writing "downloading #{t.name}" do
|
48
|
+
cd File.dirname(t.name) do
|
49
|
+
system "wget -c #{url} || curl -C - -O #{url}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# extract header files into include folder
|
55
|
+
file "vendor/sqlite3/include/sqlite3.h" => ['vendor/sqlite3/include', "vendor/sqlite-amalgamation-#{version}.zip"] do |t|
|
56
|
+
full_file = File.expand_path(t.prerequisites.last)
|
57
|
+
when_writing "creating #{t.name}" do
|
58
|
+
cd File.dirname(t.name) do
|
59
|
+
sh "unzip #{full_file}"
|
60
|
+
# update file timestamp to avoid Rake perform this extraction again.
|
61
|
+
touch File.basename(t.name)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# extract dll files into lib folder
|
67
|
+
file "vendor/sqlite3/lib/sqlite3.dll" => ['vendor/sqlite3/lib', "vendor/sqlitedll-#{version}.zip"] do |t|
|
68
|
+
full_file = File.expand_path(t.prerequisites.last)
|
69
|
+
when_writing "creating #{t.name}" do
|
70
|
+
cd File.dirname(t.name) do
|
71
|
+
sh "unzip #{full_file}"
|
72
|
+
# update file timestamp to avoid Rake perform this extraction again.
|
73
|
+
touch File.basename(t.name)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# generate import library from definition and dll file
|
79
|
+
file "vendor/sqlite3/lib/sqlite3.lib" => ["vendor/sqlite3/lib/sqlite3.dll"] do |t|
|
80
|
+
libfile = t.name
|
81
|
+
dllname = libfile.ext('dll')
|
82
|
+
deffile = libfile.ext('def')
|
83
|
+
|
84
|
+
when_writing "creating #{t.name}" do
|
85
|
+
sh dlltool(dllname, deffile, libfile)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# clean and clobber actions
|
90
|
+
# All the uncompressed files must be removed at clean
|
91
|
+
CLEAN.include('vendor/sqlite3')
|
92
|
+
|
93
|
+
# clobber vendored packages
|
94
|
+
CLOBBER.include('vendor')
|
95
|
+
|
96
|
+
# vendor:sqlite3
|
97
|
+
task 'vendor:sqlite3' => ["vendor/sqlite3/lib/sqlite3.lib", "vendor/sqlite3/include/sqlite3.h"]
|
98
|
+
|
99
|
+
# hook into cross compilation vendored sqlite3 dependency
|
100
|
+
if RUBY_PLATFORM =~ /mingw|mswin/ then
|
101
|
+
Rake::Task['compile'].prerequisites.unshift 'vendor:sqlite3'
|
102
|
+
else
|
103
|
+
Rake::Task['cross'].prerequisites.unshift 'vendor:sqlite3'
|
104
|
+
end
|
data/test/{mocks.rb → helper.rb}
RENAMED
@@ -1,6 +1,12 @@
|
|
1
|
+
# add lib folder to the path
|
2
|
+
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'sqlite3'
|
5
|
+
|
1
6
|
require 'rubygems'
|
2
|
-
|
7
|
+
require 'test/unit'
|
3
8
|
|
9
|
+
# define mocks to be used
|
4
10
|
require 'mocha'
|
5
11
|
|
6
12
|
class Driver < Mocha::Mock
|
@@ -43,3 +49,19 @@ class Statement < Mocha::Mock
|
|
43
49
|
stubs( :execute ).returns(MockResultSet.new)
|
44
50
|
end
|
45
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
|
@@ -1,9 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'sqlite3/database'
|
4
|
-
require 'test/unit'
|
5
|
-
|
6
|
-
require 'mocks'
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
7
2
|
|
8
3
|
class TC_Database_Init < Test::Unit::TestCase
|
9
4
|
def test_new
|
@@ -16,6 +11,18 @@ class TC_Database_Init < Test::Unit::TestCase
|
|
16
11
|
assert !db.results_as_hash
|
17
12
|
assert !db.type_translation
|
18
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
|
19
26
|
|
20
27
|
def test_open
|
21
28
|
driver = Driver.new
|
@@ -27,6 +34,18 @@ class TC_Database_Init < Test::Unit::TestCase
|
|
27
34
|
assert !db.type_translation
|
28
35
|
end
|
29
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
|
+
|
30
49
|
def test_with_type_translation
|
31
50
|
db = SQLite3::Database.open( "foo.db", :driver => Driver,
|
32
51
|
:type_translation => true )
|
@@ -0,0 +1,542 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
class TC_Database_Integration < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@db = SQLite3::Database.new( "test.db" )
|
6
|
+
@db.transaction do
|
7
|
+
@db.execute "create table foo ( a integer primary key, b text )"
|
8
|
+
@db.execute "insert into foo ( b ) values ( 'foo' )"
|
9
|
+
@db.execute "insert into foo ( b ) values ( 'bar' )"
|
10
|
+
@db.execute "insert into foo ( b ) values ( 'baz' )"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
@db.close
|
16
|
+
File.delete( "test.db" )
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_table_info_with_type_translation_active
|
20
|
+
@db.type_translation = true
|
21
|
+
assert_nothing_raised { @db.table_info("foo") }
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_table_info_with_defaults_for_version_3_3_8_and_higher
|
25
|
+
@db.transaction do
|
26
|
+
@db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
|
27
|
+
data = @db.table_info( "defaults_test" )
|
28
|
+
assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => "0", "cid" => "0", "pk" => "0"},
|
29
|
+
data[0])
|
30
|
+
assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => "0", "cid" => "1", "pk" => "0"},
|
31
|
+
data[1])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_table_info_without_defaults_for_version_3_3_8_and_higher
|
36
|
+
@db.transaction do
|
37
|
+
@db.execute "create table no_defaults_test ( a integer default 1, b integer )"
|
38
|
+
data = @db.table_info( "no_defaults_test" )
|
39
|
+
assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => "0", "cid" => "0", "pk" => "0"},
|
40
|
+
data[0])
|
41
|
+
assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => "0", "cid" => "1", "pk" => "0"},
|
42
|
+
data[1])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_complete_fail
|
47
|
+
assert !@db.complete?( "select * from foo" )
|
48
|
+
end
|
49
|
+
def test_complete_success
|
50
|
+
assert @db.complete?( "select * from foo;" )
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_complete_fail_utf16
|
54
|
+
assert !@db.complete?( "select * from foo".to_utf16(false), true )
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_complete_success_utf16
|
58
|
+
assert @db.complete?( "select * from foo;".to_utf16(true), true )
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_errmsg
|
62
|
+
assert_equal "not an error", @db.errmsg
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_errmsg_utf16
|
66
|
+
assert_equal "not an error".to_utf16, @db.errmsg(true)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_errcode
|
70
|
+
assert_equal 0, @db.errcode
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_trace
|
74
|
+
result = nil
|
75
|
+
@db.trace( "data" ) { |data,sql| result = [ data, sql ]; 0 }
|
76
|
+
@db.execute "select * from foo"
|
77
|
+
assert_equal ["data","select * from foo"], result
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_authorizer_okay
|
81
|
+
@db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
|
82
|
+
rows = @db.execute "select * from foo"
|
83
|
+
assert_equal 3, rows.length
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_authorizer_error
|
87
|
+
@db.authorizer( "data" ) { |data,type,a,b,c,d| 1 }
|
88
|
+
assert_raise( SQLite3::AuthorizationException ) do
|
89
|
+
@db.execute "select * from foo"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_authorizer_silent
|
94
|
+
@db.authorizer( "data" ) { |data,type,a,b,c,d| 2 }
|
95
|
+
rows = @db.execute "select * from foo"
|
96
|
+
assert rows.empty?
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_prepare_invalid_syntax
|
100
|
+
assert_raise( SQLite3::SQLException ) do
|
101
|
+
@db.prepare "select from foo"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_prepare_invalid_column
|
106
|
+
assert_raise( SQLite3::SQLException ) do
|
107
|
+
@db.prepare "select k from foo"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_prepare_invalid_table
|
112
|
+
assert_raise( SQLite3::SQLException ) do
|
113
|
+
@db.prepare "select * from barf"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_prepare_no_block
|
118
|
+
stmt = @db.prepare "select * from foo"
|
119
|
+
assert stmt.respond_to?(:execute)
|
120
|
+
stmt.close
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_prepare_with_block
|
124
|
+
called = false
|
125
|
+
@db.prepare "select * from foo" do |stmt|
|
126
|
+
called = true
|
127
|
+
assert stmt.respond_to?(:execute)
|
128
|
+
end
|
129
|
+
assert called
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_execute_no_block_no_bind_no_match
|
133
|
+
rows = @db.execute( "select * from foo where a > 100" )
|
134
|
+
assert rows.empty?
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_execute_with_block_no_bind_no_match
|
138
|
+
called = false
|
139
|
+
@db.execute( "select * from foo where a > 100" ) do |row|
|
140
|
+
called = true
|
141
|
+
end
|
142
|
+
assert !called
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_execute_no_block_with_bind_no_match
|
146
|
+
rows = @db.execute( "select * from foo where a > ?", 100 )
|
147
|
+
assert rows.empty?
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_execute_with_block_with_bind_no_match
|
151
|
+
called = false
|
152
|
+
@db.execute( "select * from foo where a > ?", 100 ) do |row|
|
153
|
+
called = true
|
154
|
+
end
|
155
|
+
assert !called
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_execute_no_block_no_bind_with_match
|
159
|
+
rows = @db.execute( "select * from foo where a = 1" )
|
160
|
+
assert_equal 1, rows.length
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_execute_with_block_no_bind_with_match
|
164
|
+
called = 0
|
165
|
+
@db.execute( "select * from foo where a = 1" ) do |row|
|
166
|
+
called += 1
|
167
|
+
end
|
168
|
+
assert_equal 1, called
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_execute_no_block_with_bind_with_match
|
172
|
+
rows = @db.execute( "select * from foo where a = ?", 1 )
|
173
|
+
assert_equal 1, rows.length
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_execute_with_block_with_bind_with_match
|
177
|
+
called = 0
|
178
|
+
@db.execute( "select * from foo where a = ?", 1 ) do |row|
|
179
|
+
called += 1
|
180
|
+
end
|
181
|
+
assert_equal 1, called
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_execute2_no_block_no_bind_no_match
|
185
|
+
columns, *rows = @db.execute2( "select * from foo where a > 100" )
|
186
|
+
assert rows.empty?
|
187
|
+
assert [ "a", "b" ], columns
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_execute2_with_block_no_bind_no_match
|
191
|
+
called = 0
|
192
|
+
@db.execute2( "select * from foo where a > 100" ) do |row|
|
193
|
+
assert [ "a", "b" ], row unless called == 0
|
194
|
+
called += 1
|
195
|
+
end
|
196
|
+
assert_equal 1, called
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_execute2_no_block_with_bind_no_match
|
200
|
+
columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
|
201
|
+
assert rows.empty?
|
202
|
+
assert [ "a", "b" ], columns
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_execute2_with_block_with_bind_no_match
|
206
|
+
called = 0
|
207
|
+
@db.execute2( "select * from foo where a > ?", 100 ) do |row|
|
208
|
+
assert [ "a", "b" ], row unless called == 0
|
209
|
+
called += 1
|
210
|
+
end
|
211
|
+
assert_equal 1, called
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_execute2_no_block_no_bind_with_match
|
215
|
+
columns, *rows = @db.execute2( "select * from foo where a = 1" )
|
216
|
+
assert_equal 1, rows.length
|
217
|
+
assert [ "a", "b" ], columns
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_execute2_with_block_no_bind_with_match
|
221
|
+
called = 0
|
222
|
+
@db.execute2( "select * from foo where a = 1" ) do |row|
|
223
|
+
assert [ "a", "b" ], row unless called == 0
|
224
|
+
called += 1
|
225
|
+
end
|
226
|
+
assert_equal 2, called
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_execute2_no_block_with_bind_with_match
|
230
|
+
columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
|
231
|
+
assert_equal 1, rows.length
|
232
|
+
assert [ "a", "b" ], columns
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_execute2_with_block_with_bind_with_match
|
236
|
+
called = 0
|
237
|
+
@db.execute2( "select * from foo where a = ?", 1 ) do |row|
|
238
|
+
called += 1
|
239
|
+
end
|
240
|
+
assert_equal 2, called
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_execute_batch_empty
|
244
|
+
assert_nothing_raised { @db.execute_batch "" }
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_execute_batch_no_bind
|
248
|
+
@db.transaction do
|
249
|
+
@db.execute_batch <<-SQL
|
250
|
+
create table bar ( a, b, c );
|
251
|
+
insert into bar values ( 'one', 2, 'three' );
|
252
|
+
insert into bar values ( 'four', 5, 'six' );
|
253
|
+
insert into bar values ( 'seven', 8, 'nine' );
|
254
|
+
SQL
|
255
|
+
end
|
256
|
+
rows = @db.execute( "select * from bar" )
|
257
|
+
assert_equal 3, rows.length
|
258
|
+
end
|
259
|
+
|
260
|
+
def test_execute_batch_with_bind
|
261
|
+
@db.execute_batch( <<-SQL, 1 )
|
262
|
+
create table bar ( a, b, c );
|
263
|
+
insert into bar values ( 'one', 2, ? );
|
264
|
+
insert into bar values ( 'four', 5, ? );
|
265
|
+
insert into bar values ( 'seven', 8, ? );
|
266
|
+
SQL
|
267
|
+
rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
|
268
|
+
assert_equal %w{1 1 1}, rows
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_query_no_block_no_bind_no_match
|
272
|
+
result = @db.query( "select * from foo where a > 100" )
|
273
|
+
assert_nil result.next
|
274
|
+
result.close
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_query_with_block_no_bind_no_match
|
278
|
+
r = nil
|
279
|
+
@db.query( "select * from foo where a > 100" ) do |result|
|
280
|
+
assert_nil result.next
|
281
|
+
r = result
|
282
|
+
end
|
283
|
+
assert r.closed?
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_query_no_block_with_bind_no_match
|
287
|
+
result = @db.query( "select * from foo where a > ?", 100 )
|
288
|
+
assert_nil result.next
|
289
|
+
result.close
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_query_with_block_with_bind_no_match
|
293
|
+
r = nil
|
294
|
+
@db.query( "select * from foo where a > ?", 100 ) do |result|
|
295
|
+
assert_nil result.next
|
296
|
+
r = result
|
297
|
+
end
|
298
|
+
assert r.closed?
|
299
|
+
end
|
300
|
+
|
301
|
+
def test_query_no_block_no_bind_with_match
|
302
|
+
result = @db.query( "select * from foo where a = 1" )
|
303
|
+
assert_not_nil result.next
|
304
|
+
assert_nil result.next
|
305
|
+
result.close
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_query_with_block_no_bind_with_match
|
309
|
+
r = nil
|
310
|
+
@db.query( "select * from foo where a = 1" ) do |result|
|
311
|
+
assert_not_nil result.next
|
312
|
+
assert_nil result.next
|
313
|
+
r = result
|
314
|
+
end
|
315
|
+
assert r.closed?
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_query_no_block_with_bind_with_match
|
319
|
+
result = @db.query( "select * from foo where a = ?", 1 )
|
320
|
+
assert_not_nil result.next
|
321
|
+
assert_nil result.next
|
322
|
+
result.close
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_query_with_block_with_bind_with_match
|
326
|
+
r = nil
|
327
|
+
@db.query( "select * from foo where a = ?", 1 ) do |result|
|
328
|
+
assert_not_nil result.next
|
329
|
+
assert_nil result.next
|
330
|
+
r = result
|
331
|
+
end
|
332
|
+
assert r.closed?
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_get_first_row_no_bind_no_match
|
336
|
+
result = @db.get_first_row( "select * from foo where a=100" )
|
337
|
+
assert_nil result
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_get_first_row_no_bind_with_match
|
341
|
+
result = @db.get_first_row( "select * from foo where a=1" )
|
342
|
+
assert_equal [ "1", "foo" ], result
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_get_first_row_with_bind_no_match
|
346
|
+
result = @db.get_first_row( "select * from foo where a=?", 100 )
|
347
|
+
assert_nil result
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_get_first_row_with_bind_with_match
|
351
|
+
result = @db.get_first_row( "select * from foo where a=?", 1 )
|
352
|
+
assert_equal [ "1", "foo" ], result
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_get_first_value_no_bind_no_match
|
356
|
+
result = @db.get_first_value( "select b, a from foo where a=100" )
|
357
|
+
assert_nil result
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_get_first_value_no_bind_with_match
|
361
|
+
result = @db.get_first_value( "select b, a from foo where a=1" )
|
362
|
+
assert_equal "foo", result
|
363
|
+
end
|
364
|
+
|
365
|
+
def test_get_first_value_with_bind_no_match
|
366
|
+
result = @db.get_first_value( "select b, a from foo where a=?", 100 )
|
367
|
+
assert_nil result
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_get_first_value_with_bind_with_match
|
371
|
+
result = @db.get_first_value( "select b, a from foo where a=?", 1 )
|
372
|
+
assert_equal "foo", result
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_last_insert_row_id
|
376
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
377
|
+
assert_equal 4, @db.last_insert_row_id
|
378
|
+
@db.execute "insert into foo ( b ) values ( 'again' )"
|
379
|
+
assert_equal 5, @db.last_insert_row_id
|
380
|
+
end
|
381
|
+
|
382
|
+
def test_changes
|
383
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
384
|
+
assert_equal 1, @db.changes
|
385
|
+
@db.execute "delete from foo where 1=1"
|
386
|
+
assert_equal 4, @db.changes
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_total_changes
|
390
|
+
assert_equal 3, @db.total_changes
|
391
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
392
|
+
@db.execute "delete from foo where 1=1"
|
393
|
+
assert_equal 8, @db.total_changes
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_transaction_nest
|
397
|
+
assert_raise( SQLite3::SQLException ) do
|
398
|
+
@db.transaction do
|
399
|
+
@db.transaction do
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_transaction_rollback
|
406
|
+
@db.transaction
|
407
|
+
@db.execute_batch <<-SQL
|
408
|
+
insert into foo (b) values ( 'test1' );
|
409
|
+
insert into foo (b) values ( 'test2' );
|
410
|
+
insert into foo (b) values ( 'test3' );
|
411
|
+
insert into foo (b) values ( 'test4' );
|
412
|
+
SQL
|
413
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
414
|
+
@db.rollback
|
415
|
+
assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
|
416
|
+
end
|
417
|
+
|
418
|
+
def test_transaction_commit
|
419
|
+
@db.transaction
|
420
|
+
@db.execute_batch <<-SQL
|
421
|
+
insert into foo (b) values ( 'test1' );
|
422
|
+
insert into foo (b) values ( 'test2' );
|
423
|
+
insert into foo (b) values ( 'test3' );
|
424
|
+
insert into foo (b) values ( 'test4' );
|
425
|
+
SQL
|
426
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
427
|
+
@db.commit
|
428
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
429
|
+
end
|
430
|
+
|
431
|
+
def test_transaction_rollback_in_block
|
432
|
+
assert_raise( SQLite3::SQLException ) do
|
433
|
+
@db.transaction do
|
434
|
+
@db.rollback
|
435
|
+
end
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_transaction_commit_in_block
|
440
|
+
assert_raise( SQLite3::SQLException ) do
|
441
|
+
@db.transaction do
|
442
|
+
@db.commit
|
443
|
+
end
|
444
|
+
end
|
445
|
+
end
|
446
|
+
|
447
|
+
def test_transaction_active
|
448
|
+
assert !@db.transaction_active?
|
449
|
+
@db.transaction
|
450
|
+
assert @db.transaction_active?
|
451
|
+
@db.commit
|
452
|
+
assert !@db.transaction_active?
|
453
|
+
end
|
454
|
+
|
455
|
+
def test_interrupt
|
456
|
+
@db.create_function( "abort", 1 ) do |func,x|
|
457
|
+
@db.interrupt
|
458
|
+
func.result = x
|
459
|
+
end
|
460
|
+
|
461
|
+
assert_raise( SQLite3::SQLException ) do
|
462
|
+
@db.execute "select abort(a) from foo"
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
def test_create_function
|
467
|
+
@db.create_function( "munge", 1 ) do |func,x|
|
468
|
+
func.result = ">>>#{x}<<<"
|
469
|
+
end
|
470
|
+
|
471
|
+
value = @db.get_first_value( "select munge(b) from foo where a=1" )
|
472
|
+
assert_match( />>>.*<<</, value )
|
473
|
+
end
|
474
|
+
|
475
|
+
def test_create_aggregate_without_block
|
476
|
+
step = proc do |ctx,a|
|
477
|
+
ctx[:sum] ||= 0
|
478
|
+
ctx[:sum] += a.to_i
|
479
|
+
end
|
480
|
+
|
481
|
+
final = proc { |ctx| ctx.result = ctx[:sum] }
|
482
|
+
|
483
|
+
@db.create_aggregate( "accumulate", 1, step, final )
|
484
|
+
|
485
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
486
|
+
assert_equal "6", value
|
487
|
+
end
|
488
|
+
|
489
|
+
def test_create_aggregate_with_block
|
490
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
491
|
+
step do |ctx,a|
|
492
|
+
ctx[:sum] ||= 0
|
493
|
+
ctx[:sum] += a.to_i
|
494
|
+
end
|
495
|
+
|
496
|
+
finalize { |ctx| ctx.result = ctx[:sum] }
|
497
|
+
end
|
498
|
+
|
499
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
500
|
+
assert_equal "6", value
|
501
|
+
end
|
502
|
+
|
503
|
+
def test_create_aggregate_with_no_data
|
504
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
505
|
+
step do |ctx,a|
|
506
|
+
ctx[:sum] ||= 0
|
507
|
+
ctx[:sum] += a.to_i
|
508
|
+
end
|
509
|
+
|
510
|
+
finalize { |ctx| ctx.result = ctx[:sum] || 0 }
|
511
|
+
end
|
512
|
+
|
513
|
+
value = @db.get_first_value(
|
514
|
+
"select accumulate(a) from foo where a = 100" )
|
515
|
+
assert_equal "0", value
|
516
|
+
end
|
517
|
+
|
518
|
+
def test_create_aggregate_handler
|
519
|
+
handler = Class.new do
|
520
|
+
class << self
|
521
|
+
def arity; 1; end
|
522
|
+
def text_rep; SQLite3::Constants::TextRep::ANY; end
|
523
|
+
def name; "multiply"; end
|
524
|
+
end
|
525
|
+
def step(ctx, a)
|
526
|
+
ctx[:buffer] ||= 1
|
527
|
+
ctx[:buffer] *= a.to_i
|
528
|
+
end
|
529
|
+
def finalize(ctx); ctx.result = ctx[:buffer]; end
|
530
|
+
end
|
531
|
+
|
532
|
+
@db.create_aggregate_handler( handler )
|
533
|
+
value = @db.get_first_value( "select multiply(a) from foo" )
|
534
|
+
assert_equal "6", value
|
535
|
+
end
|
536
|
+
|
537
|
+
def test_bind_array_parameter
|
538
|
+
result = @db.get_first_value( "select b from foo where a=? and b=?",
|
539
|
+
[ 1, "foo" ] )
|
540
|
+
assert_equal "foo", result
|
541
|
+
end
|
542
|
+
end
|