amalgalite 1.6.0-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +49 -0
  3. data/HISTORY.md +346 -0
  4. data/LICENSE +31 -0
  5. data/Manifest.txt +104 -0
  6. data/README.md +65 -0
  7. data/Rakefile +26 -0
  8. data/TODO.md +57 -0
  9. data/bin/amalgalite-pack +147 -0
  10. data/examples/a.rb +9 -0
  11. data/examples/blob.rb +88 -0
  12. data/examples/bootstrap.rb +36 -0
  13. data/examples/define_aggregate.rb +75 -0
  14. data/examples/define_function.rb +104 -0
  15. data/examples/fts5.rb +152 -0
  16. data/examples/gem-db.rb +94 -0
  17. data/examples/require_me.rb +11 -0
  18. data/examples/requires.rb +42 -0
  19. data/examples/schema-info.rb +34 -0
  20. data/ext/amalgalite/c/amalgalite.c +355 -0
  21. data/ext/amalgalite/c/amalgalite.h +151 -0
  22. data/ext/amalgalite/c/amalgalite_blob.c +240 -0
  23. data/ext/amalgalite/c/amalgalite_constants.c +1226 -0
  24. data/ext/amalgalite/c/amalgalite_database.c +1178 -0
  25. data/ext/amalgalite/c/amalgalite_requires_bootstrap.c +282 -0
  26. data/ext/amalgalite/c/amalgalite_statement.c +649 -0
  27. data/ext/amalgalite/c/extconf.rb +62 -0
  28. data/ext/amalgalite/c/gen_constants.rb +330 -0
  29. data/ext/amalgalite/c/notes.txt +134 -0
  30. data/ext/amalgalite/c/sqlite3.c +205352 -0
  31. data/ext/amalgalite/c/sqlite3.h +10727 -0
  32. data/ext/amalgalite/c/sqlite3_options.h +4 -0
  33. data/ext/amalgalite/c/sqlite3ext.h +578 -0
  34. data/lib/amalgalite.rb +51 -0
  35. data/lib/amalgalite/2.0/amalgalite.so +0 -0
  36. data/lib/amalgalite/2.1/amalgalite.so +0 -0
  37. data/lib/amalgalite/2.2/amalgalite.so +0 -0
  38. data/lib/amalgalite/2.3/amalgalite.so +0 -0
  39. data/lib/amalgalite/2.4/amalgalite.so +0 -0
  40. data/lib/amalgalite/aggregate.rb +67 -0
  41. data/lib/amalgalite/blob.rb +186 -0
  42. data/lib/amalgalite/boolean.rb +42 -0
  43. data/lib/amalgalite/busy_timeout.rb +47 -0
  44. data/lib/amalgalite/column.rb +99 -0
  45. data/lib/amalgalite/core_ext/kernel/require.rb +21 -0
  46. data/lib/amalgalite/csv_table_importer.rb +74 -0
  47. data/lib/amalgalite/database.rb +984 -0
  48. data/lib/amalgalite/function.rb +61 -0
  49. data/lib/amalgalite/index.rb +43 -0
  50. data/lib/amalgalite/memory_database.rb +15 -0
  51. data/lib/amalgalite/packer.rb +231 -0
  52. data/lib/amalgalite/paths.rb +80 -0
  53. data/lib/amalgalite/profile_tap.rb +131 -0
  54. data/lib/amalgalite/progress_handler.rb +21 -0
  55. data/lib/amalgalite/requires.rb +151 -0
  56. data/lib/amalgalite/schema.rb +225 -0
  57. data/lib/amalgalite/sqlite3.rb +6 -0
  58. data/lib/amalgalite/sqlite3/constants.rb +95 -0
  59. data/lib/amalgalite/sqlite3/database/function.rb +48 -0
  60. data/lib/amalgalite/sqlite3/database/status.rb +68 -0
  61. data/lib/amalgalite/sqlite3/status.rb +60 -0
  62. data/lib/amalgalite/sqlite3/version.rb +55 -0
  63. data/lib/amalgalite/statement.rb +418 -0
  64. data/lib/amalgalite/table.rb +91 -0
  65. data/lib/amalgalite/taps.rb +2 -0
  66. data/lib/amalgalite/taps/console.rb +27 -0
  67. data/lib/amalgalite/taps/io.rb +71 -0
  68. data/lib/amalgalite/trace_tap.rb +35 -0
  69. data/lib/amalgalite/type_map.rb +63 -0
  70. data/lib/amalgalite/type_maps/default_map.rb +166 -0
  71. data/lib/amalgalite/type_maps/storage_map.rb +38 -0
  72. data/lib/amalgalite/type_maps/text_map.rb +21 -0
  73. data/lib/amalgalite/version.rb +8 -0
  74. data/lib/amalgalite/view.rb +26 -0
  75. data/spec/aggregate_spec.rb +154 -0
  76. data/spec/amalgalite_spec.rb +4 -0
  77. data/spec/blob_spec.rb +78 -0
  78. data/spec/boolean_spec.rb +24 -0
  79. data/spec/busy_handler.rb +157 -0
  80. data/spec/data/iso-3166-country.txt +242 -0
  81. data/spec/data/iso-3166-schema.sql +22 -0
  82. data/spec/data/iso-3166-subcountry.txt +3995 -0
  83. data/spec/data/make-iso-db.sh +12 -0
  84. data/spec/database_spec.rb +508 -0
  85. data/spec/default_map_spec.rb +92 -0
  86. data/spec/function_spec.rb +78 -0
  87. data/spec/integeration_spec.rb +97 -0
  88. data/spec/iso_3166_database.rb +58 -0
  89. data/spec/packer_spec.rb +60 -0
  90. data/spec/paths_spec.rb +28 -0
  91. data/spec/progress_handler_spec.rb +91 -0
  92. data/spec/requires_spec.rb +54 -0
  93. data/spec/rtree_spec.rb +66 -0
  94. data/spec/schema_spec.rb +131 -0
  95. data/spec/spec_helper.rb +48 -0
  96. data/spec/sqlite3/constants_spec.rb +108 -0
  97. data/spec/sqlite3/database_status_spec.rb +36 -0
  98. data/spec/sqlite3/status_spec.rb +22 -0
  99. data/spec/sqlite3/version_spec.rb +28 -0
  100. data/spec/sqlite3_spec.rb +53 -0
  101. data/spec/statement_spec.rb +168 -0
  102. data/spec/storage_map_spec.rb +38 -0
  103. data/spec/tap_spec.rb +57 -0
  104. data/spec/text_map_spec.rb +20 -0
  105. data/spec/type_map_spec.rb +14 -0
  106. data/spec/version_spec.rb +8 -0
  107. data/tasks/custom.rake +102 -0
  108. data/tasks/default.rake +240 -0
  109. data/tasks/extension.rake +38 -0
  110. data/tasks/this.rb +208 -0
  111. metadata +318 -0
@@ -0,0 +1,12 @@
1
+ #!/bin/sh
2
+
3
+ DB="iso-3166.db"
4
+ SCHEMA="iso-3166-schema.sql"
5
+
6
+ rm -f ${DB}
7
+ sqlite3 ${DB} < ${SCHEMA}
8
+ echo ".import iso-3166-country.txt country" | sqlite3 ${DB}
9
+ echo ".import iso-3166-subcountry.txt subcountry" | sqlite3 ${DB}
10
+
11
+ sqlite3 ${DB} "select 'country_count', count(1) from country"
12
+ sqlite3 ${DB} "select 'subcountry_count', count(1) from subcountry"
@@ -0,0 +1,508 @@
1
+ require 'spec_helper'
2
+
3
+ require 'thread'
4
+ require 'amalgalite'
5
+ require 'amalgalite/taps/io'
6
+ require 'amalgalite/taps/console'
7
+ require 'amalgalite/database'
8
+
9
+ describe Amalgalite::Database do
10
+
11
+ it "can create a new database" do
12
+ db = Amalgalite::Database.new( SpecInfo.test_db )
13
+ db.instance_of?(Amalgalite::Database)
14
+ db.api.instance_of?(Amalgalite::SQLite3::Database)
15
+ File.exist?( SpecInfo.test_db ).should eql(true)
16
+ end
17
+
18
+ it "creates a new UTF-8 database (need exec to check pragma encoding)" do
19
+ db = Amalgalite::Database.new( SpecInfo.test_db )
20
+ db.execute_batch( @schema );
21
+ db.should_not be_utf16
22
+ db.encoding.should eql("UTF-8")
23
+ end
24
+
25
+ it "creates a new UTF-16 database (need exec to check pragma encoding)"
26
+
27
+ it "raises an error if the file does not exist and the database is opened with a non-create mode" do
28
+ lambda { Amalgalite::Database.new( SpecInfo.test_db, "r") }.should raise_error(Amalgalite::SQLite3::Error)
29
+ lambda { Amalgalite::Database.new( SpecInfo.test_db, "r+") }.should raise_error(Amalgalite::SQLite3::Error)
30
+ end
31
+
32
+ it "raises an error if an invalid mode is used" do
33
+ lambda { Amalgalite::Database.new( SpecInfo.test_db, "b+" ) }.should raise_error(Amalgalite::Database::InvalidModeError)
34
+ end
35
+
36
+ it "can be in autocommit mode, and is by default" do
37
+ @iso_db.autocommit?.should eql(true)
38
+ end
39
+
40
+ it "reports false for autocommit? when inside a transaction" do
41
+ @iso_db.execute(" BEGIN ")
42
+ @iso_db.autocommit?.should eql(false)
43
+ @iso_db.in_transaction?.should eql(true)
44
+ @iso_db.execute(" COMMIT")
45
+ @iso_db.in_transaction?.should eql(false)
46
+ end
47
+
48
+ it "prepares a statment" do
49
+ db = Amalgalite::Database.new( SpecInfo.test_db )
50
+ stmt = db.prepare("SELECT datetime()")
51
+ stmt.instance_of?(Amalgalite::Statement)
52
+ stmt.api.instance_of?(Amalgalite::SQLite3::Statement)
53
+ end
54
+
55
+ it "raises an error on invalid syntax when preparing a bad sql statement" do
56
+ db = Amalgalite::Database.new( SpecInfo.test_db )
57
+ lambda { db.prepare("SELECT nothing FROM stuf") }.should raise_error(Amalgalite::SQLite3::Error)
58
+ end
59
+
60
+ it "closes normally" do
61
+ db = Amalgalite::Database.new( SpecInfo.test_db )
62
+ expect { db.close }.not_to raise_error
63
+ end
64
+
65
+ it "returns the id of the last inserted row" do
66
+ db = Amalgalite::Database.new( SpecInfo.test_db )
67
+ db.last_insert_rowid.should eql(0)
68
+ end
69
+
70
+ it "is in autocommit mode by default" do
71
+ db = Amalgalite::Database.new( SpecInfo.test_db )
72
+ db.should be_autocommit
73
+ end
74
+
75
+ it "report the number of rows changed with an insert" do
76
+ db = Amalgalite::Database.new( SpecInfo.test_db )
77
+ db.execute_batch <<-sql
78
+ CREATE TABLE t1( x );
79
+ INSERT INTO t1( x ) values ( 1 );
80
+ INSERT INTO t1( x ) values ( 2 );
81
+ INSERT INTO t1( x ) values ( 3 );
82
+ sql
83
+
84
+ db.row_changes.should eql(1)
85
+ db.total_changes.should eql(3)
86
+ db.close
87
+ end
88
+
89
+ it "reports the number of rows deleted" do
90
+ db = Amalgalite::Database.new( SpecInfo.test_db )
91
+ db.execute_batch <<-sql
92
+ CREATE TABLE t1( x );
93
+ INSERT INTO t1( x ) values ( 1 );
94
+ INSERT INTO t1( x ) values ( 2 );
95
+ INSERT INTO t1( x ) values ( 3 );
96
+ DELETE FROM t1 where x < 3;
97
+ sql
98
+ db.row_changes.should eql(2)
99
+ db.close
100
+ end
101
+
102
+ it "can immediately execute an sql statement " do
103
+ db = Amalgalite::Database.new( SpecInfo.test_db )
104
+ db.execute( "CREATE TABLE t1( x, y, z )" ).should be_empty
105
+ end
106
+
107
+ it "can execute a batch of commands" do
108
+ db = Amalgalite::Database.new( SpecInfo.test_db )
109
+ db.execute_batch( @schema ).should eql(5)
110
+ end
111
+
112
+ it "returns an empty array when there are no results" do
113
+ row = @iso_db.execute("SELECT * from subcountry where country = 'Antarctica'")
114
+ row.should be_empty
115
+ end
116
+
117
+ it "traces the execution of code" do
118
+ db = Amalgalite::Database.new( SpecInfo.test_db )
119
+ sql = "CREATE TABLE trace_test( x, y, z)"
120
+ s = db.trace_tap = ::Amalgalite::Taps::StringIO.new
121
+ db.execute( sql )
122
+ db.trace_tap.string.should eql("registered as trace tap\n#{sql}\n")
123
+ db.trace_tap = nil
124
+ s.string.should eql("registered as trace tap\n#{sql}\nunregistered as trace tap\n")
125
+ end
126
+
127
+ it "raises an exception if the wrong type of object is used for tracing" do
128
+ db = Amalgalite::Database.new( SpecInfo.test_db )
129
+ lambda { db.trace_tap = Object.new }.should raise_error(Amalgalite::Error)
130
+ end
131
+
132
+ it "raises an exception if the wrong type of object is used for profile" do
133
+ db = Amalgalite::Database.new( SpecInfo.test_db )
134
+ lambda { db.profile_tap = Object.new }.should raise_error(Amalgalite::Error)
135
+ end
136
+
137
+ it "profiles the execution of code" do
138
+ db = Amalgalite::Database.new( SpecInfo.test_db )
139
+ s = db.profile_tap = ::Amalgalite::Taps::StringIO.new
140
+ db.execute_batch( @schema )
141
+ db.profile_tap.samplers.size.should eql(6)
142
+ db.profile_tap = nil
143
+ s.string.should =~ /unregistered as profile tap/m
144
+ end
145
+
146
+ it "#execute yields each row when called with a block" do
147
+ count = 0
148
+ @iso_db.execute( "SELECT * FROM country LIMIT 10") do |row|
149
+ count += 1
150
+ end
151
+ count.should eql(10)
152
+ end
153
+
154
+ it "#pragma yields each row when called with a block" do
155
+ count = 0
156
+ @iso_db.pragma( "index_info( subcountry_country )" ) do |row|
157
+ count += 1
158
+ end
159
+ count.should eql(1)
160
+ end
161
+
162
+ it "can use something that responds to 'write' as a tap" do
163
+ db = Amalgalite::Database.new( SpecInfo.test_db )
164
+ s2 = db.trace_tap = StringIO.new
165
+ s2.string.should eql("registered as trace tap")
166
+ end
167
+
168
+ it "can clear all registered taps" do
169
+ db = Amalgalite::Database.new( SpecInfo.test_db )
170
+ s = db.profile_tap = ::Amalgalite::Taps::StringIO.new
171
+ db.trace_tap = s
172
+ db.execute_batch( @schema )
173
+ db.profile_tap.samplers.size.should eql(6)
174
+ db.clear_taps!
175
+ s.string.should =~ /unregistered as trace tap/m
176
+ s.string.should =~ /unregistered as profile tap/m
177
+ end
178
+
179
+ it "allows nested transactions even if SQLite under the covers does not" do
180
+ db = Amalgalite::Database.new( SpecInfo.test_db )
181
+ r = db.transaction do |db2|
182
+ r2 = db.transaction { 42 }
183
+ r2.should eql(42)
184
+ r2
185
+ end
186
+ r.should eql(42)
187
+ end
188
+
189
+ %w[ transaction deferred_transaction immediate_transaction exclusive_transaction ].each do |trans|
190
+ it "returns the result of the #{trans} when a block is yielded" do
191
+ db = Amalgalite::Database.new( SpecInfo.test_db )
192
+ (db.send( trans ){ 42 }).should eql(42)
193
+ end
194
+ end
195
+
196
+ it "#reload_schema!" do
197
+ schema = @iso_db.schema
198
+ schema.instance_of?( Amalgalite::Schema ).should eql(true)
199
+ s2 = @iso_db.reload_schema!
200
+ s2.object_id.should_not eql(schema.object_id)
201
+ end
202
+
203
+ it "can rollback a transaction" do
204
+ @iso_db.transaction
205
+ r = @iso_db.execute("SELECT count(1) as cnt FROM country");
206
+ r.first['cnt'].should eql(242)
207
+ @iso_db.execute("DELETE FROM country")
208
+ r = @iso_db.execute("SELECT count(1) as cnt FROM country");
209
+ r.first['cnt'].should eql(0)
210
+ @iso_db.rollback
211
+
212
+ r = @iso_db.execute("SELECT count(1) as cnt FROM country");
213
+ r.first['cnt'].should eql(242)
214
+ end
215
+
216
+ it "rolls back if an exception happens during a transaction block" do
217
+ begin
218
+ @iso_db.transaction do |db|
219
+ r = db.execute("SELECT count(1) as cnt FROM country");
220
+ r.first['cnt'].should eql(242)
221
+ db.execute("DELETE FROM country")
222
+ db.in_transaction?.should eql(true)
223
+ raise "testing rollback"
224
+ end
225
+ rescue
226
+ @iso_db.in_transaction?.should eql(false)
227
+ @iso_db.execute("SELECT count(1) as cnt FROM country").first['cnt'].should eql(242)
228
+ end
229
+ end
230
+
231
+ it "commits if an exception happens during a transaction block but is rescued within the block" do
232
+ @iso_db.transaction do |db|
233
+ begin
234
+ r = db.execute("SELECT count(1) as cnt FROM country");
235
+ r.first['cnt'].should eql(242)
236
+ db.execute("DELETE FROM country")
237
+ db.in_transaction?.should eql(true)
238
+ raise "testing rollback"
239
+ rescue => e
240
+ e.message.should == "testing rollback"
241
+ end
242
+ $!.should == nil
243
+ end
244
+ @iso_db.in_transaction?.should eql(false)
245
+ @iso_db.first_value_from("select count(1) as cnt from country").should eql(0)
246
+ end
247
+
248
+ it "does not reraise an exception that exits before the transaction starts" do
249
+ class MyExceptionTest < RuntimeError; end
250
+ db = Amalgalite::Database.new( ":memory:" )
251
+
252
+ lambda {
253
+ begin
254
+ raise MyExceptionTest, "James pointed this out"
255
+ rescue MyExceptionTest
256
+ db.transaction("EXCLUSIVE") { }
257
+ end
258
+ }.should_not raise_error
259
+ end
260
+
261
+ describe "#define_function" do
262
+ it "does not allow mixing of arbitrary and mandatory arguments to an SQL function" do
263
+ class DBFunctionTest2 < ::Amalgalite::Function
264
+ def initialize
265
+ super( 'ftest2', -2 )
266
+ end
267
+ def call( a, *args ); end
268
+ end
269
+ lambda { @iso_db.define_function("ftest2", DBFunctionTest2.new ) }.should raise_error( ::Amalgalite::Database::FunctionError )
270
+ end
271
+
272
+ it "does not allow outrageous arity" do
273
+ class DBFunctionTest3 < ::Amalgalite::Function
274
+ def initialize
275
+ super( 'ftest3', 128 )
276
+ end
277
+ def call( *args) ; end
278
+ end
279
+ lambda { @iso_db.define_function("ftest3", DBFunctionTest3.new ) }.should raise_error( ::Amalgalite::SQLite3::Error )
280
+ end
281
+
282
+ end
283
+
284
+ describe "#remove_function" do
285
+ it "unregisters a single function by name and arity" do
286
+ @iso_db.define_function( "rtest1" ) do
287
+ "rtest1 called"
288
+ end
289
+
290
+ @iso_db.functions.size.should eql(1 )
291
+
292
+ r = @iso_db.execute( "select rtest1() AS r" )
293
+ r.first['r'].should eql("rtest1 called")
294
+ @iso_db.remove_function("rtest1")
295
+
296
+ lambda { @iso_db.execute( "select rtest1() as r" )}.should raise_error( ::Amalgalite::SQLite3::Error, /no such function: rtest1/ )
297
+ @iso_db.functions.size.should eql(0)
298
+ end
299
+
300
+ it "unregisters a function by instances" do
301
+ class FunctionTest5 < ::Amalgalite::Function
302
+ def initialize
303
+ super( 'ftest5', 0)
304
+ end
305
+ def call( *args) "ftest5 called"; end
306
+ end
307
+ @iso_db.define_function("ftest5", FunctionTest5.new )
308
+ @iso_db.functions.size.should eql(1)
309
+ r = @iso_db.execute( "select ftest5() AS r" )
310
+ r.first['r'].should eql("ftest5 called")
311
+ @iso_db.remove_function("ftest5", FunctionTest5.new )
312
+ lambda { @iso_db.execute( "select ftest5() as r" )}.should raise_error( ::Amalgalite::SQLite3::Error, /no such function: ftest5/ )
313
+ @iso_db.functions.size.should eql(0)
314
+ end
315
+
316
+ it "unregisters all functions with the same name" do
317
+ @iso_db.function( "rtest" ) do |x|
318
+ "rtest #{x} called"
319
+ end
320
+
321
+ @iso_db.function( "rtest" ) do ||
322
+ "rtest/0 called"
323
+ end
324
+
325
+ @iso_db.functions.size.should eql(2)
326
+ r = @iso_db.execute( "select rtest(1) AS r")
327
+ r.first['r'].should eql("rtest 1 called")
328
+ r = @iso_db.execute( "select rtest() AS r")
329
+ r.first['r'].should eql("rtest/0 called")
330
+ @iso_db.remove_function( 'rtest' )
331
+ lambda { @iso_db.execute( "select rtest(1) AS r") }.should raise_error( ::Amalgalite::SQLite3::Error )
332
+ lambda { @iso_db.execute( "select rtest() AS r") }.should raise_error( ::Amalgalite::SQLite3::Error )
333
+ @iso_db.functions.size.should eql(0)
334
+ end
335
+ end
336
+
337
+ it "can interrupt another thread that is also running in this database" do
338
+ executions = 0
339
+ control_queue = Queue.new
340
+ other = Thread.new( @iso_db ) do |db|
341
+ looping_sent = false
342
+ c = 0
343
+ loop do
344
+ begin
345
+ db.execute("select * from subcountry")
346
+ executions += 1
347
+ if not looping_sent then
348
+ control_queue.enq :looping
349
+ looping_sent = true
350
+ end
351
+ if c > 20_000 then
352
+ break
353
+ end
354
+ c += 1
355
+ rescue => e
356
+ Thread.current[:loop_count] = c
357
+ Thread.current[:had_error] = e.dup
358
+ control_queue.enq :boom
359
+ break
360
+ end
361
+ end
362
+ end
363
+
364
+ rudeness = Thread.new( @iso_db ) do |db|
365
+ control_queue.deq
366
+ count = 0
367
+ loop do
368
+ @iso_db.interrupt!
369
+ break unless control_queue.empty?
370
+ if count > 20_000 then
371
+ break
372
+ end
373
+ count += 1
374
+ end
375
+ end
376
+
377
+ rudeness.join
378
+ other.join
379
+
380
+ #$stdout.puts " looped #{other[:loop_count]} times"
381
+ other[:had_error].should be_an_instance_of( ::Amalgalite::SQLite3::Error )
382
+ other[:had_error].message.should =~ / interrupted/
383
+ end
384
+
385
+ it "savepoints are considered 'in_transaction'" do
386
+ @iso_db.savepoint( 'test1' ) do |db|
387
+ db.should be_in_transaction
388
+ end
389
+ end
390
+
391
+ it "releases a savepoint" do
392
+ us_sub = @iso_db.execute( "select count(1) as cnt from subcountry where country = 'US'" ).first['cnt']
393
+ us_sub.should eql(57)
394
+ other_sub = @iso_db.execute( "select count(1) as cnt from subcountry where country != 'US'" ).first['cnt']
395
+
396
+ @iso_db.transaction
397
+ @iso_db.savepoint( "t1" ) do |s|
398
+ s.execute("DELETE FROM subcountry where country = 'US'")
399
+ end
400
+
401
+ all_sub = @iso_db.execute("SELECT count(*) as cnt from subcountry").first['cnt']
402
+
403
+ all_sub.should eql(other_sub)
404
+ @iso_db.rollback
405
+ all_sub = @iso_db.execute("SELECT count(*) as cnt from subcountry").first['cnt']
406
+ all_sub.should eql(( us_sub + other_sub ))
407
+
408
+ end
409
+
410
+ it "rolls back a savepoint" do
411
+ us_sub = @iso_db.execute("SELECT count(*) as cnt from subcountry where country = 'US'").first['cnt']
412
+ lambda {
413
+ @iso_db.savepoint( "t1" ) do |s|
414
+ s.execute("DELETE FROM subcountry where country = 'US'")
415
+ as = @iso_db.execute("SELECT count(*) as cnt from subcountry where country = 'US'").first['cnt']
416
+ as.should be == 0
417
+ raise "sample error"
418
+ end
419
+ }.should raise_error( StandardError, /sample error/ )
420
+
421
+ @iso_db.execute("SELECT count(*) as cnt from subcountry where country = 'US'").first['cnt'].should eql(us_sub)
422
+ end
423
+
424
+ it "rolling back the outermost savepoint is still 'in_transaction'" do
425
+ @iso_db.savepoint( "t1" )
426
+ @iso_db.execute("DELETE FROM subcountry where country = 'US'")
427
+ @iso_db.rollback_to( "t1" )
428
+ @iso_db.should be_in_transaction
429
+ @iso_db.rollback
430
+ @iso_db.should_not be_in_transaction
431
+ end
432
+
433
+ it "can escape quoted strings" do
434
+ @iso_db.escape( "It's a happy day!" ).should eql("It''s a happy day!")
435
+ end
436
+
437
+ it "can quote and escape single quoted strings" do
438
+ @iso_db.quote( "It's a happy day!" ).should eql("'It''s a happy day!'")
439
+ end
440
+
441
+ it "can escape a symbol" do
442
+ @iso_db.escape( :stuff ).should eql("stuff")
443
+ end
444
+
445
+ it "can quote a symbol" do
446
+ @iso_db.quote( :stuff ).should eql("'stuff'")
447
+ end
448
+
449
+ it "returns the first row of results as a convenience" do
450
+ row = @iso_db.first_row_from("SELECT c.name, c.two_letter, count(*) AS count
451
+ FROM country c
452
+ JOIN subcountry sc
453
+ ON c.two_letter = sc.country
454
+ GROUP BY c.name, c.two_letter
455
+ ORDER BY count DESC")
456
+ row.length.should eql(3)
457
+ row[0].should eql("United Kingdom")
458
+ row[1].should eql("GB")
459
+ row[2].should eql(232)
460
+ row['name'].should eql("United Kingdom")
461
+ row['two_letter'].should eql("GB")
462
+ row['count'].should eql(232)
463
+ end
464
+
465
+ it "returns and empty row if there are no results for the first row" do
466
+ row = @iso_db.first_row_from("SELECT * from subcountry where country = 'Antarctica'")
467
+ row.should be_empty
468
+ end
469
+
470
+ it "returns nil if there is no value in the first value" do
471
+ val = @iso_db.first_value_from("select * from subcountry where country = 'Antarctica'" )
472
+ val.should eql(nil)
473
+ end
474
+
475
+ it "returns the first value of results as a conveinience" do
476
+ val = @iso_db.first_value_from("SELECT count(*) from subcountry ")
477
+ val.should eql(3995)
478
+ end
479
+
480
+ it "replicates a database to memory" do
481
+ mem_db = @iso_db.replicate_to( ":memory:" )
482
+ @iso_db.close
483
+ val = mem_db.first_value_from("SELECT count(*) from subcountry" )
484
+ val.should eql(3995)
485
+ end
486
+
487
+ it "replicates a database to a database file" do
488
+ all_sub = @iso_db.execute("SELECT count(*) as cnt from subcountry").first['cnt']
489
+
490
+ fdb = Amalgalite::Database.new( SpecInfo.test_db )
491
+ @iso_db.replicate_to( fdb )
492
+ @iso_db.close
493
+
494
+ File.exist?( SpecInfo.test_db ).should be == true
495
+ fdb.execute("SELECT count(*) as cnt from subcountry").first['cnt'].should == all_sub
496
+ end
497
+
498
+ it "raises an error if it is given an invalid location to replicate to" do
499
+ lambda { @iso_db.replicate_to( false ) }.should raise_error( ArgumentError, /must be a String or a Database/ )
500
+ end
501
+
502
+ it "imports batch statements" do
503
+ db = Amalgalite::Database.new(":memory:")
504
+ db.import("CREATE TABLE things(stuff TEXT); INSERT INTO things (stuff) VALUES (\"foobar\");").should be_truthy
505
+ db.first_value_from("SELECT stuff FROM things").should == "foobar"
506
+ end
507
+
508
+ end