dawanda-sqlite3 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/API_CHANGES.rdoc +50 -0
- data/CHANGELOG.rdoc +184 -0
- data/ChangeLog.cvs +88 -0
- data/LICENSE +27 -0
- data/Manifest.txt +50 -0
- data/README.rdoc +95 -0
- data/Rakefile +10 -0
- data/ext/sqlite3/backup.c +168 -0
- data/ext/sqlite3/backup.h +15 -0
- data/ext/sqlite3/database.c +762 -0
- data/ext/sqlite3/database.h +15 -0
- data/ext/sqlite3/exception.c +94 -0
- data/ext/sqlite3/exception.h +8 -0
- data/ext/sqlite3/extconf.rb +41 -0
- data/ext/sqlite3/sqlite3.c +40 -0
- data/ext/sqlite3/sqlite3_ruby.h +44 -0
- data/ext/sqlite3/statement.c +418 -0
- data/ext/sqlite3/statement.h +16 -0
- data/faq/faq.rb +145 -0
- data/faq/faq.yml +426 -0
- data/lib/sqlite3.rb +10 -0
- data/lib/sqlite3/constants.rb +49 -0
- data/lib/sqlite3/database.rb +587 -0
- data/lib/sqlite3/errors.rb +44 -0
- data/lib/sqlite3/pragmas.rb +280 -0
- data/lib/sqlite3/resultset.rb +126 -0
- data/lib/sqlite3/statement.rb +148 -0
- data/lib/sqlite3/translator.rb +118 -0
- data/lib/sqlite3/value.rb +57 -0
- data/lib/sqlite3/version.rb +25 -0
- data/setup.rb +1333 -0
- data/tasks/faq.rake +9 -0
- data/tasks/gem.rake +31 -0
- data/tasks/native.rake +61 -0
- data/tasks/vendor_sqlite3.rake +104 -0
- data/test/helper.rb +3 -0
- data/test/test_backup.rb +33 -0
- data/test/test_collation.rb +82 -0
- data/test/test_database.rb +319 -0
- data/test/test_database_readonly.rb +29 -0
- data/test/test_deprecated.rb +37 -0
- data/test/test_encoding.rb +119 -0
- data/test/test_integration.rb +544 -0
- data/test/test_integration_open_close.rb +30 -0
- data/test/test_integration_pending.rb +115 -0
- data/test/test_integration_resultset.rb +156 -0
- data/test/test_integration_statement.rb +194 -0
- data/test/test_sqlite3.rb +9 -0
- data/test/test_statement.rb +213 -0
- data/test/test_statement_execute.rb +35 -0
- metadata +184 -0
data/tasks/faq.rake
ADDED
data/tasks/gem.rake
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
begin
|
2
|
+
require 'hoe'
|
3
|
+
rescue LoadError
|
4
|
+
# try with rubygems?
|
5
|
+
require 'rubygems'
|
6
|
+
require 'hoe'
|
7
|
+
end
|
8
|
+
|
9
|
+
Hoe.plugin :debugging, :doofus, :git
|
10
|
+
|
11
|
+
HOE = Hoe.spec 'dawanda-sqlite3' do
|
12
|
+
developer 'Jamis Buck', 'jamis@37signals.com'
|
13
|
+
developer 'Luis Lavena', 'luislavena@gmail.com'
|
14
|
+
developer 'Aaron Patterson', 'aaron@tenderlovemaking.com'
|
15
|
+
|
16
|
+
self.readme_file = 'README.rdoc'
|
17
|
+
self.history_file = 'CHANGELOG.rdoc'
|
18
|
+
self.extra_rdoc_files = FileList['*.rdoc', 'ext/**/*.c']
|
19
|
+
|
20
|
+
spec_extras[:required_ruby_version] = Gem::Requirement.new('>= 1.8.7')
|
21
|
+
spec_extras[:required_rubygems_version] = '>= 1.3.5'
|
22
|
+
spec_extras[:extensions] = ["ext/sqlite3/extconf.rb"]
|
23
|
+
|
24
|
+
extra_dev_deps << ['rake-compiler', "~> 0.7.0"]
|
25
|
+
|
26
|
+
clean_globs.push('**/test.db')
|
27
|
+
end
|
28
|
+
|
29
|
+
Hoe.add_include_dirs '.'
|
30
|
+
|
31
|
+
# vim: syntax=ruby
|
data/tasks/native.rake
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# use rake-compiler for building the extension
|
2
|
+
require 'rake/extensiontask'
|
3
|
+
|
4
|
+
# NOTE: version used by cross compilation of Windows native extension
|
5
|
+
# It do not affect compilation under other operating systems
|
6
|
+
# The version indicated is the minimum DLL suggested for correct functionality
|
7
|
+
BINARY_VERSION = '3.7.3'
|
8
|
+
URL_VERSION = BINARY_VERSION.gsub('.', '_')
|
9
|
+
|
10
|
+
# build sqlite3_native C extension
|
11
|
+
Rake::ExtensionTask.new('sqlite3_native', HOE.spec) do |ext|
|
12
|
+
# where to locate the extension
|
13
|
+
ext.ext_dir = 'ext/sqlite3'
|
14
|
+
|
15
|
+
# where native extension will be copied (matches makefile)
|
16
|
+
ext.lib_dir = "lib/sqlite3"
|
17
|
+
|
18
|
+
# reference to the sqlite3 library
|
19
|
+
sqlite3_lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor', 'sqlite3'))
|
20
|
+
|
21
|
+
# clean binary folders always
|
22
|
+
CLEAN.include("#{ext.lib_dir}/?.?")
|
23
|
+
|
24
|
+
# automatically add build options to avoid need of manual input
|
25
|
+
if RUBY_PLATFORM =~ /mswin|mingw/ then
|
26
|
+
# define target for extension (supporting fat binaries)
|
27
|
+
RUBY_VERSION =~ /(\d+\.\d+)/
|
28
|
+
ext.lib_dir = "lib/sqlite3/#{$1}"
|
29
|
+
ext.config_options << "--with-sqlite3-dir=#{sqlite3_lib}"
|
30
|
+
else
|
31
|
+
ext.cross_compile = true
|
32
|
+
ext.cross_platform = ['i386-mswin32-60', 'i386-mingw32']
|
33
|
+
ext.cross_config_options << "--with-sqlite3-dir=#{sqlite3_lib}"
|
34
|
+
ext.cross_compiling do |gemspec|
|
35
|
+
gemspec.post_install_message = <<-POST_INSTALL_MESSAGE
|
36
|
+
|
37
|
+
=============================================================================
|
38
|
+
|
39
|
+
You've installed the binary version of #{gemspec.name}.
|
40
|
+
It was built using SQLite3 version #{BINARY_VERSION}.
|
41
|
+
It's recommended to use the exact same version to avoid potential issues.
|
42
|
+
|
43
|
+
At the time of building this gem, the necessary DLL files where available
|
44
|
+
in the following download:
|
45
|
+
|
46
|
+
http://www.sqlite.org/sqlitedll-#{URL_VERSION}.zip
|
47
|
+
|
48
|
+
You can put the sqlite3.dll available in this package in your Ruby bin
|
49
|
+
directory, for example C:\\Ruby\\bin
|
50
|
+
|
51
|
+
=============================================================================
|
52
|
+
|
53
|
+
POST_INSTALL_MESSAGE
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# ensure things are compiled prior testing
|
59
|
+
task :test => [:compile]
|
60
|
+
|
61
|
+
# vim: syntax=ruby
|
@@ -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
|
+
# required folder structure for --with-sqlite3-dir (include + lib)
|
29
|
+
directory "vendor/sqlite3/lib"
|
30
|
+
directory "vendor/sqlite3/include"
|
31
|
+
|
32
|
+
# download amalgamation version (for include files)
|
33
|
+
file "vendor/sqlite-amalgamation-#{URL_VERSION}.zip" => ['vendor'] do |t|
|
34
|
+
url = "http://www.sqlite.org/#{File.basename(t.name)}"
|
35
|
+
when_writing "downloading #{t.name}" do
|
36
|
+
cd File.dirname(t.name) do
|
37
|
+
system "wget -c #{url} || curl -C - -O #{url}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# download dll binaries
|
43
|
+
file "vendor/sqlitedll-#{URL_VERSION}.zip" => ['vendor'] do |t|
|
44
|
+
url = "http://www.sqlite.org/#{File.basename(t.name)}"
|
45
|
+
when_writing "downloading #{t.name}" do
|
46
|
+
cd File.dirname(t.name) do
|
47
|
+
system "wget -c #{url} || curl -C - -O #{url}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# extract header files into include folder
|
53
|
+
file "vendor/sqlite3/include/sqlite3.h" => ['vendor/sqlite3/include', "vendor/sqlite-amalgamation-#{URL_VERSION}.zip"] do |t|
|
54
|
+
full_file = File.expand_path(t.prerequisites.last)
|
55
|
+
when_writing "creating #{t.name}" do
|
56
|
+
cd File.dirname(t.name) do
|
57
|
+
sh "unzip #{full_file}"
|
58
|
+
# update file timestamp to avoid Rake perform this extraction again.
|
59
|
+
touch File.basename(t.name)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# extract dll files into lib folder
|
65
|
+
file "vendor/sqlite3/lib/sqlite3.dll" => ['vendor/sqlite3/lib', "vendor/sqlitedll-#{URL_VERSION}.zip"] do |t|
|
66
|
+
full_file = File.expand_path(t.prerequisites.last)
|
67
|
+
when_writing "creating #{t.name}" do
|
68
|
+
cd File.dirname(t.name) do
|
69
|
+
sh "unzip #{full_file}"
|
70
|
+
# update file timestamp to avoid Rake perform this extraction again.
|
71
|
+
touch File.basename(t.name)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# generate import library from definition and dll file
|
77
|
+
file "vendor/sqlite3/lib/sqlite3.lib" => ["vendor/sqlite3/lib/sqlite3.dll"] do |t|
|
78
|
+
libfile = t.name
|
79
|
+
dllname = libfile.ext('dll')
|
80
|
+
deffile = libfile.ext('def')
|
81
|
+
|
82
|
+
when_writing "creating #{t.name}" do
|
83
|
+
sh dlltool(dllname, deffile, libfile)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# clean and clobber actions
|
88
|
+
# All the uncompressed files must be removed at clean
|
89
|
+
CLEAN.include('vendor/sqlite3')
|
90
|
+
|
91
|
+
# clobber vendored packages
|
92
|
+
CLOBBER.include('vendor')
|
93
|
+
|
94
|
+
# vendor:sqlite3
|
95
|
+
task 'vendor:sqlite3' => ["vendor/sqlite3/lib/sqlite3.lib", "vendor/sqlite3/include/sqlite3.h"]
|
96
|
+
|
97
|
+
# hook into cross compilation vendored sqlite3 dependency
|
98
|
+
if RUBY_PLATFORM =~ /mingw|mswin/ then
|
99
|
+
Rake::Task['compile'].prerequisites.unshift 'vendor:sqlite3'
|
100
|
+
else
|
101
|
+
if Rake::Task.task_defined?(:cross)
|
102
|
+
Rake::Task['cross'].prerequisites.unshift 'vendor:sqlite3'
|
103
|
+
end
|
104
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_backup.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
module SQLite3
|
4
|
+
class TestBackup < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@sdb = SQLite3::Database.new(':memory:')
|
7
|
+
@ddb = SQLite3::Database.new(':memory:')
|
8
|
+
@sdb.execute('CREATE TABLE foo (idx, val);');
|
9
|
+
@data = ('A'..'Z').map{|x|x * 40}
|
10
|
+
@data.each_with_index do |v, i|
|
11
|
+
@sdb.execute('INSERT INTO foo (idx, val) VALUES (?, ?);', [i, v])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_backup_step
|
16
|
+
b = SQLite3::Backup.new(@ddb, 'main', @sdb, 'main')
|
17
|
+
while b.step(1) == SQLite3::Constants::ErrorCode::OK
|
18
|
+
assert_not_equal(0, b.remaining)
|
19
|
+
end
|
20
|
+
assert_equal(0, b.remaining)
|
21
|
+
b.finish
|
22
|
+
assert_equal(@data.length, @ddb.execute('SELECT * FROM foo;').length)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_backup_all
|
26
|
+
b = SQLite3::Backup.new(@ddb, 'main', @sdb, 'main')
|
27
|
+
assert_equal(SQLite3::Constants::ErrorCode::DONE, b.step(-1))
|
28
|
+
assert_equal(0, b.remaining)
|
29
|
+
b.finish
|
30
|
+
assert_equal(@data.length, @ddb.execute('SELECT * FROM foo;').length)
|
31
|
+
end
|
32
|
+
end if defined?(SQLite3::Backup)
|
33
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
module SQLite3
|
6
|
+
class TestCollation < Test::Unit::TestCase
|
7
|
+
class Comparator
|
8
|
+
attr_reader :calls
|
9
|
+
def initialize
|
10
|
+
@calls = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def compare left, right
|
14
|
+
@calls << [left, right]
|
15
|
+
left <=> right
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def setup
|
20
|
+
@db = SQLite3::Database.new(':memory:')
|
21
|
+
@create = "create table ex(id int, data string)"
|
22
|
+
@db.execute(@create);
|
23
|
+
[ [1, 'hello'], [2, 'world'] ].each do |vals|
|
24
|
+
@db.execute('insert into ex (id, data) VALUES (?, ?)', vals)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_custom_collation
|
29
|
+
comparator = Comparator.new
|
30
|
+
|
31
|
+
@db.collation 'foo', comparator
|
32
|
+
|
33
|
+
assert_equal comparator, @db.collations['foo']
|
34
|
+
@db.execute('select data from ex order by 1 collate foo')
|
35
|
+
assert_equal 1, comparator.calls.length
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_remove_collation
|
39
|
+
comparator = Comparator.new
|
40
|
+
|
41
|
+
@db.collation 'foo', comparator
|
42
|
+
@db.collation 'foo', nil
|
43
|
+
|
44
|
+
assert_nil @db.collations['foo']
|
45
|
+
assert_raises(SQLite3::SQLException) do
|
46
|
+
@db.execute('select data from ex order by 1 collate foo')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
if RUBY_VERSION >= '1.9.1'
|
51
|
+
def test_encoding
|
52
|
+
comparator = Comparator.new
|
53
|
+
@db.collation 'foo', comparator
|
54
|
+
@db.execute('select data from ex order by 1 collate foo')
|
55
|
+
|
56
|
+
a, b = *comparator.calls.first
|
57
|
+
|
58
|
+
assert_equal Encoding.find('UTF-8'), a.encoding
|
59
|
+
assert_equal Encoding.find('UTF-8'), b.encoding
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_encoding_default_internal
|
63
|
+
warn_before = $-w
|
64
|
+
$-w = false
|
65
|
+
before_enc = Encoding.default_internal
|
66
|
+
|
67
|
+
Encoding.default_internal = 'EUC-JP'
|
68
|
+
comparator = Comparator.new
|
69
|
+
@db.collation 'foo', comparator
|
70
|
+
@db.execute('select data from ex order by 1 collate foo')
|
71
|
+
|
72
|
+
a, b = *comparator.calls.first
|
73
|
+
|
74
|
+
assert_equal Encoding.find('EUC-JP'), a.encoding
|
75
|
+
assert_equal Encoding.find('EUC-JP'), b.encoding
|
76
|
+
ensure
|
77
|
+
Encoding.default_internal = before_enc
|
78
|
+
$-w = warn_before
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,319 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'iconv'
|
3
|
+
|
4
|
+
module SQLite3
|
5
|
+
class TestDatabase < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@db = SQLite3::Database.new(':memory:')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_blob
|
11
|
+
@db.execute("CREATE TABLE blobs ( id INTEGER, hash BLOB(10) )")
|
12
|
+
str = "\0foo"
|
13
|
+
@db.execute("INSERT INTO blobs VALUES (0, ?)", [str])
|
14
|
+
assert_equal [[0, str]], @db.execute("SELECT * FROM blobs")
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_get_first_row
|
18
|
+
assert_equal [1], @db.get_first_row('SELECT 1')
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_get_first_row_with_type_translation_and_hash_results
|
22
|
+
@db.results_as_hash = true
|
23
|
+
assert_equal({0=>1, "1"=>1}, @db.get_first_row('SELECT 1'))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_execute_with_type_translation_and_hash
|
27
|
+
@db.results_as_hash = true
|
28
|
+
rows = []
|
29
|
+
@db.execute('SELECT 1') { |row| rows << row }
|
30
|
+
|
31
|
+
assert_equal({0=>1, "1"=>1}, rows.first)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_encoding
|
35
|
+
assert @db.encoding, 'database has encoding'
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_changes
|
39
|
+
@db.execute("CREATE TABLE items (id integer PRIMARY KEY AUTOINCREMENT, number integer)")
|
40
|
+
assert_equal 0, @db.changes
|
41
|
+
@db.execute("INSERT INTO items (number) VALUES (10)")
|
42
|
+
assert_equal 1, @db.changes
|
43
|
+
@db.execute_batch(
|
44
|
+
"UPDATE items SET number = (number + :nn) WHERE (number = :n)",
|
45
|
+
{"nn" => 20, "n" => 10})
|
46
|
+
assert_equal 1, @db.changes
|
47
|
+
assert_equal [[30]], @db.execute("select number from items")
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_new
|
51
|
+
db = SQLite3::Database.new(':memory:')
|
52
|
+
assert db
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_new_yields_self
|
56
|
+
thing = nil
|
57
|
+
SQLite3::Database.new(':memory:') do |db|
|
58
|
+
thing = db
|
59
|
+
end
|
60
|
+
assert_instance_of(SQLite3::Database, thing)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_new_with_options
|
64
|
+
db = SQLite3::Database.new(Iconv.conv('UTF-16LE', 'UTF-8', ':memory:'),
|
65
|
+
:utf16 => true)
|
66
|
+
assert db
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_close
|
70
|
+
db = SQLite3::Database.new(':memory:')
|
71
|
+
db.close
|
72
|
+
assert db.closed?
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_block_closes_self
|
76
|
+
thing = nil
|
77
|
+
SQLite3::Database.new(':memory:') do |db|
|
78
|
+
thing = db
|
79
|
+
assert !thing.closed?
|
80
|
+
end
|
81
|
+
assert thing.closed?
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_prepare
|
85
|
+
db = SQLite3::Database.new(':memory:')
|
86
|
+
stmt = db.prepare('select "hello world"')
|
87
|
+
assert_instance_of(SQLite3::Statement, stmt)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_total_changes
|
91
|
+
db = SQLite3::Database.new(':memory:')
|
92
|
+
db.execute("create table foo ( a integer primary key, b text )")
|
93
|
+
db.execute("insert into foo (b) values ('hello')")
|
94
|
+
assert_equal 1, db.total_changes
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_execute_returns_list_of_hash
|
98
|
+
db = SQLite3::Database.new(':memory:', :results_as_hash => true)
|
99
|
+
db.execute("create table foo ( a integer primary key, b text )")
|
100
|
+
db.execute("insert into foo (b) values ('hello')")
|
101
|
+
rows = db.execute("select * from foo")
|
102
|
+
assert_equal [{0=>1, "a"=>1, "b"=>"hello", 1=>"hello"}], rows
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_execute_yields_hash
|
106
|
+
db = SQLite3::Database.new(':memory:', :results_as_hash => true)
|
107
|
+
db.execute("create table foo ( a integer primary key, b text )")
|
108
|
+
db.execute("insert into foo (b) values ('hello')")
|
109
|
+
db.execute("select * from foo") do |row|
|
110
|
+
assert_equal({0=>1, "a"=>1, "b"=>"hello", 1=>"hello"}, row)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_table_info
|
115
|
+
db = SQLite3::Database.new(':memory:', :results_as_hash => true)
|
116
|
+
db.execute("create table foo ( a integer primary key, b text )")
|
117
|
+
info = [{
|
118
|
+
"name" => "a",
|
119
|
+
"pk" => 1,
|
120
|
+
"notnull" => 0,
|
121
|
+
"type" => "integer",
|
122
|
+
"dflt_value" => nil,
|
123
|
+
"cid" => 0
|
124
|
+
},
|
125
|
+
{
|
126
|
+
"name" => "b",
|
127
|
+
"pk" => 0,
|
128
|
+
"notnull" => 0,
|
129
|
+
"type" => "text",
|
130
|
+
"dflt_value" => nil,
|
131
|
+
"cid" => 1
|
132
|
+
}]
|
133
|
+
assert_equal info, db.table_info('foo')
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_total_changes_closed
|
137
|
+
db = SQLite3::Database.new(':memory:')
|
138
|
+
db.close
|
139
|
+
assert_raise(SQLite3::Exception) do
|
140
|
+
db.total_changes
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_trace_requires_opendb
|
145
|
+
@db.close
|
146
|
+
assert_raise(SQLite3::Exception) do
|
147
|
+
@db.trace { |x| }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_trace_with_block
|
152
|
+
result = nil
|
153
|
+
@db.trace { |sql| result = sql }
|
154
|
+
@db.execute "select 'foo'"
|
155
|
+
assert_equal "select 'foo'", result
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_trace_with_object
|
159
|
+
obj = Class.new {
|
160
|
+
attr_accessor :result
|
161
|
+
def call sql; @result = sql end
|
162
|
+
}.new
|
163
|
+
|
164
|
+
@db.trace(obj)
|
165
|
+
@db.execute "select 'foo'"
|
166
|
+
assert_equal "select 'foo'", obj.result
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_trace_takes_nil
|
170
|
+
@db.trace(nil)
|
171
|
+
@db.execute "select 'foo'"
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_last_insert_row_id_closed
|
175
|
+
@db.close
|
176
|
+
assert_raise(SQLite3::Exception) do
|
177
|
+
@db.last_insert_row_id
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_define_function
|
182
|
+
called_with = nil
|
183
|
+
@db.define_function("hello") do |value|
|
184
|
+
called_with = value
|
185
|
+
end
|
186
|
+
@db.execute("select hello(10)")
|
187
|
+
assert_equal 10, called_with
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_call_func_arg_type
|
191
|
+
called_with = nil
|
192
|
+
@db.define_function("hello") do |b, c, d|
|
193
|
+
called_with = [b, c, d]
|
194
|
+
nil
|
195
|
+
end
|
196
|
+
@db.execute("select hello(2.2, 'foo', NULL)")
|
197
|
+
assert_equal [2.2, 'foo', nil], called_with
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_define_varargs
|
201
|
+
called_with = nil
|
202
|
+
@db.define_function("hello") do |*args|
|
203
|
+
called_with = args
|
204
|
+
nil
|
205
|
+
end
|
206
|
+
@db.execute("select hello(2.2, 'foo', NULL)")
|
207
|
+
assert_equal [2.2, 'foo', nil], called_with
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_function_return
|
211
|
+
@db.define_function("hello") { |a| 10 }
|
212
|
+
assert_equal [10], @db.execute("select hello('world')").first
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_function_return_types
|
216
|
+
[10, 2.2, nil, "foo"].each do |thing|
|
217
|
+
@db.define_function("hello") { |a| thing }
|
218
|
+
assert_equal [thing], @db.execute("select hello('world')").first
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_define_function_closed
|
223
|
+
@db.close
|
224
|
+
assert_raise(SQLite3::Exception) do
|
225
|
+
@db.define_function('foo') { }
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_inerrupt_closed
|
230
|
+
@db.close
|
231
|
+
assert_raise(SQLite3::Exception) do
|
232
|
+
@db.interrupt
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_define_aggregate
|
237
|
+
@db.execute "create table foo ( a integer primary key, b text )"
|
238
|
+
@db.execute "insert into foo ( b ) values ( 'foo' )"
|
239
|
+
@db.execute "insert into foo ( b ) values ( 'bar' )"
|
240
|
+
@db.execute "insert into foo ( b ) values ( 'baz' )"
|
241
|
+
|
242
|
+
acc = Class.new {
|
243
|
+
attr_reader :sum
|
244
|
+
alias :finalize :sum
|
245
|
+
def initialize
|
246
|
+
@sum = 0
|
247
|
+
end
|
248
|
+
|
249
|
+
def step a
|
250
|
+
@sum += a
|
251
|
+
end
|
252
|
+
}.new
|
253
|
+
|
254
|
+
@db.define_aggregator("accumulate", acc)
|
255
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
256
|
+
assert_equal 6, value
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_authorizer_ok
|
260
|
+
@db.authorizer = Class.new {
|
261
|
+
def call action, a, b, c, d; true end
|
262
|
+
}.new
|
263
|
+
@db.prepare("select 'fooooo'")
|
264
|
+
|
265
|
+
@db.authorizer = Class.new {
|
266
|
+
def call action, a, b, c, d; 0 end
|
267
|
+
}.new
|
268
|
+
@db.prepare("select 'fooooo'")
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_authorizer_ignore
|
272
|
+
@db.authorizer = Class.new {
|
273
|
+
def call action, a, b, c, d; nil end
|
274
|
+
}.new
|
275
|
+
stmt = @db.prepare("select 'fooooo'")
|
276
|
+
assert_equal nil, stmt.step
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_authorizer_fail
|
280
|
+
@db.authorizer = Class.new {
|
281
|
+
def call action, a, b, c, d; false end
|
282
|
+
}.new
|
283
|
+
assert_raises(SQLite3::AuthorizationException) do
|
284
|
+
@db.prepare("select 'fooooo'")
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_remove_auth
|
289
|
+
@db.authorizer = Class.new {
|
290
|
+
def call action, a, b, c, d; false end
|
291
|
+
}.new
|
292
|
+
assert_raises(SQLite3::AuthorizationException) do
|
293
|
+
@db.prepare("select 'fooooo'")
|
294
|
+
end
|
295
|
+
|
296
|
+
@db.authorizer = nil
|
297
|
+
@db.prepare("select 'fooooo'")
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_close_with_open_statements
|
301
|
+
stmt = @db.prepare("select 'foo'")
|
302
|
+
assert_raises(SQLite3::BusyException) do
|
303
|
+
@db.close
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_execute_with_empty_bind_params
|
308
|
+
assert_equal [['foo']], @db.execute("select 'foo'", [])
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_query_with_named_bind_params
|
312
|
+
assert_equal [['foo']], @db.query("select :n", {'n' => 'foo'}).to_a
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_execute_with_named_bind_params
|
316
|
+
assert_equal [['foo']], @db.execute("select :n", {'n' => 'foo'})
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|