sqlite_magic 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -3,8 +3,8 @@ rvm:
3
3
  # - "1.8.7"
4
4
  - "1.9.2"
5
5
  - "1.9.3"
6
- - jruby-18mode # JRuby in 1.8 mode
7
- - jruby-19mode # JRuby in 1.9 mode
6
+ # - jruby-18mode # JRuby in 1.8 mode
7
+ # - jruby-19mode # JRuby in 1.9 mode
8
8
  # - rbx-18mode
9
9
  - rbx-19mode
10
10
  # uncomment this line if your project needs to run something other than `rake`:
data/README.md CHANGED
@@ -23,6 +23,10 @@ Or install it yourself as:
23
23
 
24
24
  TODO: Write usage instructions here
25
25
 
26
+ ## Changes
27
+ Allow options to be passed when initializing connection (which will be passed to Sqlite)
28
+ insert\_or\_update no defers to #save\_data when hitting error (which caused a problem when missing field)
29
+
26
30
  ## Contributing
27
31
 
28
32
  1. Fork it
data/lib/sqlite_magic.rb CHANGED
@@ -11,8 +11,8 @@ module SqliteMagic
11
11
 
12
12
  class Connection
13
13
  attr_reader :database
14
- def initialize(db_loc='sqlite.db')
15
- @database = SQLite3::Database.new(db_loc)
14
+ def initialize(db_loc='sqlite.db', options={})
15
+ @database = SQLite3::Database.new(db_loc, options)
16
16
  end
17
17
 
18
18
  def add_columns(tbl_name, col_names)
@@ -54,10 +54,10 @@ module SqliteMagic
54
54
 
55
55
  # This is an (expensive) convenience method to insert a row (for given unique keys), or if the row already exists
56
56
  #
57
- def insert_or_update(uniq_keys, values_hash, tbl_name='ocdata')
58
- field_names_as_symbol_string = values_hash.keys.map{ |k| ":#{k}" }.join(',') # need to appear as symbols
59
-
60
- sql_statement = "INSERT INTO #{tbl_name} (#{values_hash.keys.join(',')}) VALUES (#{field_names_as_symbol_string})"
57
+ def insert_or_update(uniq_keys, values_hash, tbl_name='main_table')
58
+ all_field_names = values_hash.keys
59
+ field_names_as_symbol_string = all_field_names.map{ |k| ":#{k}" }.join(',') # need to appear as symbols
60
+ sql_statement = "INSERT INTO #{tbl_name} (#{all_field_names.join(',')}) VALUES (#{field_names_as_symbol_string})"
61
61
  database.execute(sql_statement, values_hash)
62
62
  rescue SQLite3::ConstraintException => e
63
63
  unique_key_constraint = uniq_keys.map { |k| "#{k}=:#{k}" }.join(' AND ')
@@ -65,9 +65,17 @@ module SqliteMagic
65
65
  sql_statement = "UPDATE #{tbl_name} SET #{update_keys} WHERE #{unique_key_constraint}"
66
66
  database.execute sql_statement, values_hash
67
67
  rescue SQLite3::SQLException => e
68
- puts "Exception (#{e.inspect}) raised: #{sql_statement}" if verbose?
69
- # defer to save_data, which can handle missing tables, columns etc
70
- save_data(uniq_keys, values_hash, tbl_name)
68
+ puts "Exception (#{e.inspect}) raised" if verbose?
69
+ case e.message
70
+ when /no such table/
71
+ create_table(tbl_name, all_field_names, uniq_keys)
72
+ retry
73
+ when /has no column/
74
+ add_columns(tbl_name, all_field_names)
75
+ retry
76
+ else
77
+ raise e
78
+ end
71
79
  end
72
80
 
73
81
  # #save data into the database
@@ -1,3 +1,3 @@
1
1
  module SqliteMagic
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -16,7 +16,7 @@ describe SqliteMagic do
16
16
 
17
17
  describe SqliteMagic::Connection do
18
18
  before do
19
- @dummy_db = stub('dummy_sqlite3_database')
19
+ @dummy_db = double('dummy_sqlite3_database')
20
20
  SQLite3::Database.stub(:new).and_return(@dummy_db)
21
21
  @connection = SqliteMagic::Connection.new
22
22
  end
@@ -28,15 +28,20 @@ describe SqliteMagic do
28
28
  end
29
29
 
30
30
  it "should open up a Sqlite3 database with default name" do
31
- SQLite3::Database.should_receive(:new).with('sqlite.db').and_return(@dummy_db)
31
+ SQLite3::Database.should_receive(:new).with('sqlite.db',{}).and_return(@dummy_db)
32
32
  connection = SqliteMagic::Connection.new
33
33
  end
34
34
 
35
35
  it "should use given db_name when setting up Sqlite3" do
36
- SQLite3::Database.should_receive(:new).with('path/to/mynew.db').and_return(@dummy_db)
36
+ SQLite3::Database.should_receive(:new).with('path/to/mynew.db',{}).and_return(@dummy_db)
37
37
  connection = SqliteMagic::Connection.new('path/to/mynew.db')
38
38
  end
39
39
 
40
+ it "should pass options when opening Sqlite3 db" do
41
+ SQLite3::Database.should_receive(:new).with('path/to/mynew.db', :foo => 'bar').and_return(@dummy_db)
42
+ connection = SqliteMagic::Connection.new('path/to/mynew.db', :foo => 'bar')
43
+ end
44
+
40
45
  it "should store Sqlite3 database in @database instance variable" do
41
46
  connection = SqliteMagic::Connection.new
42
47
  connection.instance_variable_get(:@database).should == @dummy_db
@@ -256,10 +261,10 @@ describe SqliteMagic do
256
261
 
257
262
  context 'and no table name given' do
258
263
  before do
259
- @expected_query = "INSERT INTO ocdata (foo,foo2,foo3,foo4) VALUES (:foo,:foo2,:foo3,:foo4)"
264
+ @expected_query = "INSERT INTO main_table (foo,foo2,foo3,foo4) VALUES (:foo,:foo2,:foo3,:foo4)"
260
265
  end
261
266
 
262
- it 'should use ocdata table by default' do
267
+ it 'should use main_table table by default' do
263
268
  @dummy_db.should_receive(:execute).with(@expected_query, @datum)
264
269
  @connection.insert_or_update(@unique_keys, @datum)
265
270
  end
@@ -267,7 +272,7 @@ describe SqliteMagic do
267
272
 
268
273
  context 'and data already exists' do
269
274
  before do
270
- @dummy_db.should_receive(:execute).with(/INSERT/, anything).and_raise(SQLite3::ConstraintException.new('constraint failed'))
275
+ @dummy_db.stub(:execute).with(/INSERT/, anything).and_raise(SQLite3::ConstraintException.new('constraint failed'))
271
276
  @expected_update_query = "UPDATE foo_table SET foo=:foo, foo4=:foo4 WHERE foo2=:foo2 AND foo3=:foo3"
272
277
  end
273
278
 
@@ -275,34 +280,57 @@ describe SqliteMagic do
275
280
  @dummy_db.should_receive(:execute).with(@expected_update_query, @datum)
276
281
  @connection.insert_or_update(@unique_keys, @datum, 'foo_table')
277
282
  end
283
+
278
284
  context 'and no table name given' do
279
285
  before do
280
- @expected_update_query = "UPDATE ocdata SET foo=:foo, foo4=:foo4 WHERE foo2=:foo2 AND foo3=:foo3"
286
+ @expected_update_query = "UPDATE main_table SET foo=:foo, foo4=:foo4 WHERE foo2=:foo2 AND foo3=:foo3"
281
287
  end
282
288
 
283
- it 'should use ocdata table by default' do
289
+ it 'should use main_table table by default' do
284
290
  @dummy_db.should_receive(:execute).with(@expected_update_query, @datum)
285
291
  @connection.insert_or_update(@unique_keys, @datum)
286
292
  end
287
293
  end
288
294
 
289
- end
295
+ context 'and some columns do not exist' do
296
+ before do
297
+ @dummy_db.should_receive(:execute).with(@expected_query, @datum).
298
+ and_raise(SQLite3::SQLException.new("table mynewtable has no column named foo") )
299
+ @expected_update_query = "UPDATE foo_table SET foo=:foo, foo4=:foo4 WHERE foo2=:foo2 AND foo3=:foo3"
300
+ end
290
301
 
291
- context 'and SQLite3::SQLException raised' do
292
- before do
293
- @dummy_db.stub(:execute) # default
294
- # raise just once
295
- @dummy_db.should_receive(:execute).
296
- and_raise(SQLite3::SQLException )
297
- end
302
+ it 'should create missing fields using all field names and unique keys' do
303
+ @connection.should_receive(:add_columns).with('foo_table', [:foo,:foo2,:foo3, :foo4])
304
+ @dummy_db.stub(:execute).with(@expected_update_query, @datum)
298
305
 
299
- it 'should defer to save_data' do
300
- @connection.stub(:create_table)
301
- @connection.should_receive(:save_data).with(@unique_keys, @datum, 'foo_table')
306
+ @connection.insert_or_update(@unique_keys, @datum, 'foo_table')
307
+ end
302
308
 
303
- @connection.insert_or_update(@unique_keys, @datum, 'foo_table')
309
+ it 'should update data' do
310
+ @connection.stub(:add_columns)
311
+ @dummy_db.should_receive(:execute).with(@expected_update_query, @datum)
312
+
313
+ @connection.insert_or_update(@unique_keys, @datum, 'foo_table')
314
+ end
304
315
  end
316
+
305
317
  end
318
+
319
+ # context 'and SQLite3::SQLException raised' do
320
+ # before do
321
+ # @dummy_db.stub(:execute) # default
322
+ # # raise just once
323
+ # @dummy_db.should_receive(:execute).
324
+ # and_raise(SQLite3::SQLException )
325
+ # end
326
+ #
327
+ # it 'should defer to save_data' do
328
+ # @connection.stub(:create_table)
329
+ # @connection.should_receive(:save_data).with(@unique_keys, @datum, 'foo_table')
330
+ #
331
+ # @connection.insert_or_update(@unique_keys, @datum, 'foo_table')
332
+ # end
333
+ # end
306
334
  end
307
335
 
308
336
  describe '#verbose?' do
metadata CHANGED
@@ -1,81 +1,88 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sqlite_magic
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 2
9
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Chris Taggart
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2013-07-03 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2013-11-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: sqlite3
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
30
22
  type: :runtime
31
- version_requirements: *id001
32
- - !ruby/object:Gem::Dependency
33
- name: bundler
34
23
  prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- requirements:
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
37
35
  - - ~>
38
- - !ruby/object:Gem::Version
39
- segments:
40
- - 1
41
- - 3
42
- version: "1.3"
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
43
38
  type: :development
44
- version_requirements: *id002
45
- - !ruby/object:Gem::Dependency
46
- name: rake
47
39
  prerelease: false
48
- requirement: &id003 !ruby/object:Gem::Requirement
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- segments:
53
- - 0
54
- version: "0"
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
55
54
  type: :development
56
- version_requirements: *id003
57
- - !ruby/object:Gem::Dependency
58
- name: rspec
59
55
  prerelease: false
60
- requirement: &id004 !ruby/object:Gem::Requirement
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- segments:
65
- - 0
66
- version: "0"
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
67
70
  type: :development
68
- version_requirements: *id004
69
- description: Sprinkles some magic onto Sqlite3 gem. Sort of extracted from Scraperwiki gem
70
- email:
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Sprinkles some magic onto Sqlite3 gem. Sort of extracted from Scraperwiki
79
+ gem
80
+ email:
71
81
  - info@opencorporates.com
72
82
  executables: []
73
-
74
83
  extensions: []
75
-
76
84
  extra_rdoc_files: []
77
-
78
- files:
85
+ files:
79
86
  - .gitignore
80
87
  - .travis.yml
81
88
  - Gemfile
@@ -88,37 +95,32 @@ files:
88
95
  - spec/lib/sqlite_magic_spec.rb
89
96
  - spec/spec_helper.rb
90
97
  - sqlite_magic.gemspec
91
- has_rdoc: true
92
98
  homepage: https://github.com/openc/sqlite_magic
93
- licenses:
99
+ licenses:
94
100
  - MIT
95
101
  post_install_message:
96
102
  rdoc_options: []
97
-
98
- require_paths:
103
+ require_paths:
99
104
  - lib
100
- required_ruby_version: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- segments:
105
- - 0
106
- version: "0"
107
- required_rubygems_version: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- segments:
112
- - 0
113
- version: "0"
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
114
117
  requirements: []
115
-
116
118
  rubyforge_project:
117
- rubygems_version: 1.3.6
119
+ rubygems_version: 1.8.25
118
120
  signing_key:
119
121
  specification_version: 3
120
122
  summary: Sprinkles some magic onto Sqlite3 gem
121
- test_files:
123
+ test_files:
122
124
  - spec/.DS_Store
123
125
  - spec/lib/sqlite_magic_spec.rb
124
126
  - spec/spec_helper.rb