amalgalite 0.4.2-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +81 -0
- data/LICENSE +29 -0
- data/README +40 -0
- data/bin/amalgalite-pack-into-db +155 -0
- data/examples/a.rb +9 -0
- data/examples/blob.rb +105 -0
- data/examples/bootstrap.rb +36 -0
- data/examples/gem-db.rb +94 -0
- data/examples/requires.rb +54 -0
- data/examples/schema-info.rb +34 -0
- data/ext/amalgalite3.c +201 -0
- data/ext/amalgalite3.h +121 -0
- data/ext/amalgalite3_blob.c +241 -0
- data/ext/amalgalite3_constants.c +221 -0
- data/ext/amalgalite3_database.c +550 -0
- data/ext/amalgalite3_requires_bootstrap.c +210 -0
- data/ext/amalgalite3_statement.c +628 -0
- data/ext/extconf.rb +19 -0
- data/ext/gen_constants.rb +130 -0
- data/ext/rbconfig-mingw.rb +178 -0
- data/ext/sqlite3.c +97092 -0
- data/ext/sqlite3.h +6364 -0
- data/ext/sqlite3_options.h +4 -0
- data/ext/sqlite3ext.h +372 -0
- data/gemspec.rb +55 -0
- data/lib/amalgalite.rb +33 -0
- data/lib/amalgalite/blob.rb +186 -0
- data/lib/amalgalite/boolean.rb +42 -0
- data/lib/amalgalite/column.rb +86 -0
- data/lib/amalgalite/core_ext/kernel/require.rb +14 -0
- data/lib/amalgalite/database.rb +514 -0
- data/lib/amalgalite/index.rb +43 -0
- data/lib/amalgalite/paths.rb +70 -0
- data/lib/amalgalite/profile_tap.rb +130 -0
- data/lib/amalgalite/requires.rb +112 -0
- data/lib/amalgalite/schema.rb +115 -0
- data/lib/amalgalite/sqlite3.rb +6 -0
- data/lib/amalgalite/sqlite3/constants.rb +82 -0
- data/lib/amalgalite/sqlite3/database/status.rb +69 -0
- data/lib/amalgalite/sqlite3/status.rb +61 -0
- data/lib/amalgalite/sqlite3/version.rb +38 -0
- data/lib/amalgalite/statement.rb +394 -0
- data/lib/amalgalite/table.rb +36 -0
- data/lib/amalgalite/taps.rb +2 -0
- data/lib/amalgalite/taps/console.rb +27 -0
- data/lib/amalgalite/taps/io.rb +71 -0
- data/lib/amalgalite/trace_tap.rb +35 -0
- data/lib/amalgalite/type_map.rb +63 -0
- data/lib/amalgalite/type_maps/default_map.rb +167 -0
- data/lib/amalgalite/type_maps/storage_map.rb +41 -0
- data/lib/amalgalite/type_maps/text_map.rb +23 -0
- data/lib/amalgalite/version.rb +37 -0
- data/lib/amalgalite/view.rb +26 -0
- data/lib/amalgalite3.so +0 -0
- data/spec/amalgalite_spec.rb +4 -0
- data/spec/blob_spec.rb +81 -0
- data/spec/boolean_spec.rb +23 -0
- data/spec/database_spec.rb +238 -0
- data/spec/default_map_spec.rb +87 -0
- data/spec/integeration_spec.rb +111 -0
- data/spec/paths_spec.rb +28 -0
- data/spec/schema_spec.rb +60 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/sqlite3/constants_spec.rb +65 -0
- data/spec/sqlite3/database_status_spec.rb +36 -0
- data/spec/sqlite3/status_spec.rb +18 -0
- data/spec/sqlite3/version_spec.rb +14 -0
- data/spec/sqlite3_spec.rb +23 -0
- data/spec/statement_spec.rb +134 -0
- data/spec/storage_map_spec.rb +41 -0
- data/spec/tap_spec.rb +59 -0
- data/spec/text_map_spec.rb +23 -0
- data/spec/type_map_spec.rb +17 -0
- data/spec/version_spec.rb +9 -0
- data/tasks/announce.rake +39 -0
- data/tasks/config.rb +110 -0
- data/tasks/distribution.rake +53 -0
- data/tasks/documentation.rake +33 -0
- data/tasks/extension.rake +100 -0
- data/tasks/rspec.rake +32 -0
- data/tasks/rubyforge.rake +59 -0
- data/tasks/utils.rb +80 -0
- metadata +192 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
#
|
6
|
+
require 'amalgalite/type_map'
|
7
|
+
require 'amalgalite3'
|
8
|
+
|
9
|
+
module Amalgalite::TypeMaps
|
10
|
+
##
|
11
|
+
# An Amalagliate TypeMap that converts both bind parameters and result
|
12
|
+
# parameters to a String, no matter what.
|
13
|
+
#
|
14
|
+
class TextMap < ::Amalgalite::TypeMap
|
15
|
+
def bind_type_of( obj )
|
16
|
+
return ::Amalgalite::SQLite3::Constants::DataType::TEXT
|
17
|
+
end
|
18
|
+
|
19
|
+
def result_value_of( delcared_type, value )
|
20
|
+
return value.to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for licensingn details
|
4
|
+
#++
|
5
|
+
|
6
|
+
module Amalgalite
|
7
|
+
# Version information for Amagalite
|
8
|
+
module Version
|
9
|
+
|
10
|
+
MAJOR = 0
|
11
|
+
MINOR = 4
|
12
|
+
BUILD = 2
|
13
|
+
|
14
|
+
#
|
15
|
+
# return the Version as an array of MAJOR, MINOR, BUILD
|
16
|
+
#
|
17
|
+
def self.to_a
|
18
|
+
[MAJOR, MINOR, BUILD]
|
19
|
+
end
|
20
|
+
|
21
|
+
# return the Version as a dotted String MAJOR.MINOR.BUILD
|
22
|
+
def self.to_s
|
23
|
+
to_a.join(".")
|
24
|
+
end
|
25
|
+
|
26
|
+
# return the Vesion as a hash
|
27
|
+
def self.to_hash
|
28
|
+
{ :major => MAJOR, :minor => MINOR, :build => BUILD }
|
29
|
+
end
|
30
|
+
|
31
|
+
# Version string constant
|
32
|
+
STRING = Version.to_s.freeze
|
33
|
+
end
|
34
|
+
|
35
|
+
# Version string constant
|
36
|
+
VERSION = Version.to_s.freeze
|
37
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
|
6
|
+
module Amalgalite
|
7
|
+
#
|
8
|
+
# a class representing the meta information about an SQLite view
|
9
|
+
#
|
10
|
+
class View
|
11
|
+
# the schame this view is assciated with
|
12
|
+
attr_accessor :schema
|
13
|
+
|
14
|
+
# the table name
|
15
|
+
attr_reader :name
|
16
|
+
|
17
|
+
# the original sql that was used to create this table
|
18
|
+
attr_reader :sql
|
19
|
+
|
20
|
+
def initialize( name, sql )
|
21
|
+
@name = name
|
22
|
+
@sql = sql
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/lib/amalgalite3.so
ADDED
Binary file
|
data/spec/blob_spec.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__),"..","lib"))
|
5
|
+
require 'amalgalite'
|
6
|
+
|
7
|
+
describe Amalgalite::Blob do
|
8
|
+
DATA_FILE = File.expand_path( File.join( File.dirname(__FILE__), "iso-3166-country.txt" ) )
|
9
|
+
before(:each) do
|
10
|
+
@blob_db_name = File.join(File.dirname( __FILE__ ), "blob.db")
|
11
|
+
File.unlink @blob_db_name if File.exist?( @blob_db_name )
|
12
|
+
@db = Amalgalite::Database.new( @blob_db_name )
|
13
|
+
@schema_sql = <<-SQL
|
14
|
+
CREATE TABLE blobs(
|
15
|
+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
16
|
+
name VARCHAR(128) NOT NULL UNIQUE,
|
17
|
+
data TEXT );
|
18
|
+
SQL
|
19
|
+
@db.execute( @schema_sql )
|
20
|
+
@junk_file = File.join( File.dirname(__FILE__), "test_output")
|
21
|
+
end
|
22
|
+
|
23
|
+
after(:each) do
|
24
|
+
@db.close
|
25
|
+
File.unlink @blob_db_name if File.exist?( @blob_db_name )
|
26
|
+
File.unlink @junk_file if File.exist?( @junk_file )
|
27
|
+
end
|
28
|
+
|
29
|
+
{ :file => DATA_FILE,
|
30
|
+
:string => IO.read( DATA_FILE ),
|
31
|
+
:io => StringIO.new( IO.read( DATA_FILE ) ) }.each_pair do |style, data |
|
32
|
+
describe "inserts a blob from a #{style}" do
|
33
|
+
before(:each) do
|
34
|
+
column = @db.schema.tables['blobs'].columns['data']
|
35
|
+
@db.execute("INSERT INTO blobs(name, data) VALUES ($name, $data)",
|
36
|
+
{ "$name" => DATA_FILE,
|
37
|
+
"$data" => Amalgalite::Blob.new( style => data,
|
38
|
+
:column => column ) } )
|
39
|
+
@db.execute("VACUUM")
|
40
|
+
end
|
41
|
+
|
42
|
+
after(:each) do
|
43
|
+
@db.execute("DELETE FROM blobs")
|
44
|
+
data.rewind if data.respond_to?( :rewind )
|
45
|
+
end
|
46
|
+
|
47
|
+
it "and retrieves the data as a single value" do
|
48
|
+
all_rows = @db.execute("SELECT name,data FROM blobs")
|
49
|
+
all_rows.size.should == 1;
|
50
|
+
all_rows.first['name'].should == DATA_FILE
|
51
|
+
all_rows.first['data'].should_not be_incremental
|
52
|
+
all_rows.first['data'].to_string_io.string.should == IO.read( DATA_FILE )
|
53
|
+
end
|
54
|
+
|
55
|
+
it "and retrieves the data using incremental IO" do
|
56
|
+
all_rows = @db.execute("SELECT * FROM blobs")
|
57
|
+
all_rows.size.should == 1;
|
58
|
+
all_rows.first['name'].should == DATA_FILE
|
59
|
+
all_rows.first['data'].should be_incremental
|
60
|
+
all_rows.first['data'].to_string_io.string.should == IO.read( DATA_FILE )
|
61
|
+
end
|
62
|
+
|
63
|
+
it "writes the data to a file " do
|
64
|
+
all_rows = @db.execute("SELECT * FROM blobs")
|
65
|
+
all_rows.size.should == 1;
|
66
|
+
all_rows.first['name'].should == DATA_FILE
|
67
|
+
all_rows.first['data'].should be_incremental
|
68
|
+
all_rows.first['data'].write_to_file( @junk_file )
|
69
|
+
IO.read( @junk_file).should == IO.read( DATA_FILE )
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
it "raises an error if initialized incorrectly" do
|
77
|
+
lambda{ Amalgalite::Blob.new( :file => "/dev/null", :string => "foo" ) }.should raise_error( Amalgalite::Blob::Error )
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__),"..","lib"))
|
2
|
+
require 'amalgalite'
|
3
|
+
require 'amalgalite/boolean'
|
4
|
+
|
5
|
+
describe Amalgalite::Boolean do
|
6
|
+
%w[ True Y Yes T 1 ].each do |v|
|
7
|
+
it "converts #{v} to true" do
|
8
|
+
Amalgalite::Boolean.to_bool(v).should == true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
%w[ False F f No n 0 ].each do |v|
|
13
|
+
it "converts #{v} to false " do
|
14
|
+
Amalgalite::Boolean.to_bool(v).should == false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
%w[ other things nil ].each do |v|
|
19
|
+
it "converts #{v} to nil" do
|
20
|
+
Amalgalite::Boolean.to_bool(v).should == nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,238 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__),"..","lib"))
|
5
|
+
require 'amalgalite'
|
6
|
+
require 'amalgalite/taps/io'
|
7
|
+
require 'amalgalite/taps/console'
|
8
|
+
|
9
|
+
describe Amalgalite::Database do
|
10
|
+
before(:each) do
|
11
|
+
@schema = IO.read( SpecInfo.test_schema_file )
|
12
|
+
@iso_db_file = SpecInfo.make_iso_db
|
13
|
+
@iso_db = Amalgalite::Database.new( SpecInfo.make_iso_db )
|
14
|
+
end
|
15
|
+
|
16
|
+
after(:each) do
|
17
|
+
File.unlink SpecInfo.test_db if File.exist?( SpecInfo.test_db )
|
18
|
+
@iso_db.close
|
19
|
+
File.unlink @iso_db_file if File.exist?( @iso_db_file )
|
20
|
+
end
|
21
|
+
|
22
|
+
it "can create a new database" do
|
23
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
24
|
+
db.instance_of?(Amalgalite::Database)
|
25
|
+
db.api.instance_of?(Amalgalite::SQLite3::Database)
|
26
|
+
File.exist?( SpecInfo.test_db ).should == true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "creates a new UTF-8 database (need exec to check pragma encoding)" do
|
30
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
31
|
+
db.execute_batch( @schema );
|
32
|
+
db.should_not be_utf16
|
33
|
+
db.encoding.should == "UTF-8"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "creates a new UTF-16 database (need exec to check pragma encoding)"
|
37
|
+
|
38
|
+
it "raises an error if the file does not exist and the database is opened with a non-create mode" do
|
39
|
+
lambda { Amalgalite::Database.new( SpecInfo.test_db, "r") }.should raise_error(Amalgalite::SQLite3::Error)
|
40
|
+
lambda { Amalgalite::Database.new( SpecInfo.test_db, "r+") }.should raise_error(Amalgalite::SQLite3::Error)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "raises an error if an invalid mode is used" do
|
44
|
+
lambda { Amalgalite::Database.new( SpecInfo.test_db, "b+" ) }.should raise_error(Amalgalite::Database::InvalidModeError)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "can be in autocommit mode, and is by default" do
|
48
|
+
@iso_db.autocommit?.should == true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "reports false for autocommit? when inside a transaction" do
|
52
|
+
@iso_db.execute(" BEGIN ")
|
53
|
+
@iso_db.autocommit?.should == false
|
54
|
+
@iso_db.in_transaction?.should == true
|
55
|
+
@iso_db.execute(" COMMIT")
|
56
|
+
@iso_db.in_transaction?.should == false
|
57
|
+
end
|
58
|
+
|
59
|
+
it "prepares a statment" do
|
60
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
61
|
+
stmt = db.prepare("SELECT datetime()")
|
62
|
+
stmt.instance_of?(Amalgalite::Statement)
|
63
|
+
stmt.api.instance_of?(Amalgalite::SQLite3::Statement)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "raises an error on invalid syntax when preparing a bad sql statement" do
|
67
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
68
|
+
lambda { db.prepare("SELECT nothing FROM stuf") }.should raise_error(Amalgalite::SQLite3::Error)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "closes normally" do
|
72
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
73
|
+
lambda { db.close }.should_not raise_error( Amalgalite::SQLite3::Error )
|
74
|
+
end
|
75
|
+
|
76
|
+
it "returns the id of the last inserted row" do
|
77
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
78
|
+
db.last_insert_rowid.should == 0
|
79
|
+
end
|
80
|
+
|
81
|
+
it "is in autocommit mode by default" do
|
82
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
83
|
+
db.should be_autocommit
|
84
|
+
end
|
85
|
+
|
86
|
+
it "report the number of rows changed with an insert" do
|
87
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
88
|
+
db.execute_batch <<-sql
|
89
|
+
CREATE TABLE t1( x );
|
90
|
+
INSERT INTO t1( x ) values ( 1 );
|
91
|
+
INSERT INTO t1( x ) values ( 2 );
|
92
|
+
INSERT INTO t1( x ) values ( 3 );
|
93
|
+
sql
|
94
|
+
|
95
|
+
db.row_changes.should == 1
|
96
|
+
db.total_changes.should == 3
|
97
|
+
db.close
|
98
|
+
end
|
99
|
+
|
100
|
+
it "reports the number of rows deleted" do
|
101
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
102
|
+
db.execute_batch <<-sql
|
103
|
+
CREATE TABLE t1( x );
|
104
|
+
INSERT INTO t1( x ) values ( 1 );
|
105
|
+
INSERT INTO t1( x ) values ( 2 );
|
106
|
+
INSERT INTO t1( x ) values ( 3 );
|
107
|
+
DELETE FROM t1 where x < 3;
|
108
|
+
sql
|
109
|
+
db.row_changes.should == 2
|
110
|
+
db.close
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
it "can immediately execute an sql statement " do
|
116
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
117
|
+
db.execute( "CREATE TABLE t1( x, y, z )" ).should be_empty
|
118
|
+
end
|
119
|
+
|
120
|
+
it "can execute a batch of commands" do
|
121
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
122
|
+
db.execute_batch( @schema ).should == 5
|
123
|
+
end
|
124
|
+
|
125
|
+
it "traces the execution of code" do
|
126
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
127
|
+
sql = "CREATE TABLE trace_test( x, y, z)"
|
128
|
+
s = db.trace_tap = ::Amalgalite::Taps::StringIO.new
|
129
|
+
db.execute( sql )
|
130
|
+
db.trace_tap.string.should== "registered as trace tap\n#{sql}\n"
|
131
|
+
db.trace_tap = nil
|
132
|
+
s.string.should== "registered as trace tap\n#{sql}\nunregistered as trace tap\n"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "raises an exception if the wrong type of object is used for tracing" do
|
136
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
137
|
+
lambda { db.trace_tap = Object.new }.should raise_error(Amalgalite::Error)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "raises an exception if the wrong type of object is used for profile" do
|
141
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
142
|
+
lambda { db.profile_tap = Object.new }.should raise_error(Amalgalite::Error)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "profiles the execution of code" do
|
146
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
147
|
+
s = db.profile_tap = ::Amalgalite::Taps::StringIO.new
|
148
|
+
db.execute_batch( @schema )
|
149
|
+
db.profile_tap.samplers.size.should == 6
|
150
|
+
db.profile_tap = nil
|
151
|
+
s.string.should =~ /unregistered as profile tap/m
|
152
|
+
end
|
153
|
+
|
154
|
+
it "#execute yields each row when called with a block" do
|
155
|
+
count = 0
|
156
|
+
@iso_db.execute( "SELECT * FROM country LIMIT 10") do |row|
|
157
|
+
count += 1
|
158
|
+
end
|
159
|
+
count.should == 10
|
160
|
+
end
|
161
|
+
|
162
|
+
it "#pragma yields each row when called with a block" do
|
163
|
+
count = 0
|
164
|
+
@iso_db.pragma( "index_info( subcountry_country )" ) do |row|
|
165
|
+
count += 1
|
166
|
+
end
|
167
|
+
count.should == 1
|
168
|
+
end
|
169
|
+
|
170
|
+
it "can use something that responds to 'write' as a tap" do
|
171
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
172
|
+
s2 = db.trace_tap = StringIO.new
|
173
|
+
s2.string.should == "registered as trace tap"
|
174
|
+
end
|
175
|
+
|
176
|
+
it "can clear all registered taps" do
|
177
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
178
|
+
s = db.profile_tap = ::Amalgalite::Taps::StringIO.new
|
179
|
+
db.trace_tap = s
|
180
|
+
db.execute_batch( @schema )
|
181
|
+
db.profile_tap.samplers.size.should == 6
|
182
|
+
db.clear_taps!
|
183
|
+
s.string.should =~ /unregistered as trace tap/m
|
184
|
+
s.string.should =~ /unregistered as profile tap/m
|
185
|
+
end
|
186
|
+
|
187
|
+
it "allows nested transactions even if SQLite under the covers does not" do
|
188
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
189
|
+
r = db.transaction do |db2|
|
190
|
+
r2 = db.transaction { 42 }
|
191
|
+
r2.should == 42
|
192
|
+
r2
|
193
|
+
end
|
194
|
+
r.should == 42
|
195
|
+
end
|
196
|
+
|
197
|
+
it "returns the result of the transaction when a block is yielded" do
|
198
|
+
db = Amalgalite::Database.new( SpecInfo.test_db )
|
199
|
+
(db.transaction { 42 }).should == 42
|
200
|
+
end
|
201
|
+
|
202
|
+
it "#reload_schema!" do
|
203
|
+
@iso_db = Amalgalite::Database.new( SpecInfo.make_iso_db )
|
204
|
+
schema = @iso_db.schema
|
205
|
+
schema.instance_of?( Amalgalite::Schema ).should == true
|
206
|
+
s2 = @iso_db.reload_schema!
|
207
|
+
s2.object_id.should_not == schema.object_id
|
208
|
+
end
|
209
|
+
|
210
|
+
it "can rollback a transaction" do
|
211
|
+
@iso_db.transaction
|
212
|
+
r = @iso_db.execute("SELECT count(1) as cnt FROM country");
|
213
|
+
r.first['cnt'].should == 242
|
214
|
+
@iso_db.execute("DELETE FROM country")
|
215
|
+
r = @iso_db.execute("SELECT count(1) as cnt FROM country");
|
216
|
+
r.first['cnt'].should == 0
|
217
|
+
@iso_db.rollback
|
218
|
+
|
219
|
+
r = @iso_db.execute("SELECT count(1) as cnt FROM country");
|
220
|
+
r.first['cnt'].should == 242
|
221
|
+
end
|
222
|
+
|
223
|
+
it "rolls back if an exception happens during a transaction block" do
|
224
|
+
begin
|
225
|
+
@iso_db.transaction do |db|
|
226
|
+
r = db.execute("SELECT count(1) as cnt FROM country");
|
227
|
+
r.first['cnt'].should == 242
|
228
|
+
db.execute("DELETE FROM country")
|
229
|
+
db.in_transaction?.should == true
|
230
|
+
raise "testing rollback"
|
231
|
+
end
|
232
|
+
rescue => e
|
233
|
+
@iso_db.in_transaction?.should == false
|
234
|
+
@iso_db.execute("SELECT count(1) as cnt FROM country").first['cnt'].should == 242
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__),"..","lib"))
|
5
|
+
require 'amalgalite/type_maps/default_map'
|
6
|
+
|
7
|
+
describe Amalgalite::TypeMaps::DefaultMap do
|
8
|
+
before(:each) do
|
9
|
+
@map = Amalgalite::TypeMaps::DefaultMap.new
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#bind_type_of" do
|
13
|
+
|
14
|
+
it "Float is bound to DataType::FLOAT" do
|
15
|
+
@map.bind_type_of( 3.14 ).should == ::Amalgalite::SQLite3::Constants::DataType::FLOAT
|
16
|
+
end
|
17
|
+
|
18
|
+
it "Fixnum is bound to DataType::INTGER" do
|
19
|
+
@map.bind_type_of( 42 ).should == ::Amalgalite::SQLite3::Constants::DataType::INTEGER
|
20
|
+
end
|
21
|
+
|
22
|
+
it "nil is bound to DataType::NULL" do
|
23
|
+
@map.bind_type_of( nil ).should == ::Amalgalite::SQLite3::Constants::DataType::NULL
|
24
|
+
end
|
25
|
+
|
26
|
+
it "::Amalgalite::Blob is bound to DataType::BLOB" do
|
27
|
+
@map.bind_type_of( ::Amalgalite::Blob.new( :column => true, :string => "just a test" ) ).should == ::Amalgalite::SQLite3::Constants::DataType::BLOB
|
28
|
+
end
|
29
|
+
|
30
|
+
it "everything else is bound to DataType::TEXT" do
|
31
|
+
@map.bind_type_of( "everything else" ).should == ::Amalgalite::SQLite3::Constants::DataType::TEXT
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
describe "#result_value_of" do
|
38
|
+
|
39
|
+
it "Numeric's are returned" do
|
40
|
+
y = 42
|
41
|
+
x = @map.result_value_of( "INT", 42 )
|
42
|
+
x.object_id.should == y.object_id
|
43
|
+
end
|
44
|
+
|
45
|
+
it "Nil is returned" do
|
46
|
+
@map.result_value_of( "NULL", nil ).should == nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it "DateTime is returned for delcared types of 'datetime'" do
|
50
|
+
@map.result_value_of( "DaTeTiME", "2008-04-01 23:23:23" ).should be_kind_of(DateTime)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "Date is returned for declared types of 'date'" do
|
54
|
+
@map.result_value_of( "date", "2008-04-01 23:42:42" ).should be_kind_of(Date)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "Time is returned for declared types of 'time'" do
|
58
|
+
@map.result_value_of( "timestamp", "2008-04-01T23:42:42" ).should be_kind_of(Time)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "Float is returned for declared types of 'double'" do
|
62
|
+
@map.result_value_of( "double", "3.14" ).should be_kind_of(Float)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "Integer is returned for declared types of 'int'" do
|
66
|
+
@map.result_value_of( "int", "42" ).should be_kind_of(Integer)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "boolean is returned for declared types of 'bool'" do
|
70
|
+
@map.result_value_of( "bool", "True" ).should == true
|
71
|
+
end
|
72
|
+
|
73
|
+
it "Blob is returned for declated types of 'blob'" do
|
74
|
+
blob = @map.result_value_of( "blob", "stuff")
|
75
|
+
blob.to_s.should == "stuff"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "raises and error if an unknown sql type is returned" do
|
79
|
+
x = nil
|
80
|
+
lambda{ x = @map.result_value_of( "footype", "foo" ) }.should raise_error( ::Amalgalite::Error )
|
81
|
+
end
|
82
|
+
|
83
|
+
it "raises and error if an ruby class is attempted to be mapped" do
|
84
|
+
lambda{ @map.result_value_of( "footype", 1..3 ) }.should raise_error( ::Amalgalite::Error )
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|