sqlite_magic 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjEzZTViZTI2MTgwOTRiODQ4YzEzYzQyN2FkY2I0YmQ1Yjg4YzEzNA==
5
+ data.tar.gz: !binary |-
6
+ ZWIxMGEzZTliNWRiODI5MTFlYTNmYTQxOWYwZDJjOTAzZjliZWEzYQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NzU4M2IwZGQ5ZTRiYmY1NzdmMTFjN2UzY2QyMTkxMzk0NTNhNzMwYzBiMGM5
10
+ MzRhNjcyZDBiZDgwYmY2ZjdmM2FmODhhNTBjYTY3MDY4MTdjYzliYzUyMGY5
11
+ MjY2MjdiM2I0NDIzOGU2MTVmMjg2YWFlOTNhMzFmYjUzMjYyNjg=
12
+ data.tar.gz: !binary |-
13
+ NzkxMDI5MTQ1YzhhN2U1NDJiMzA2ZGQ3ZWI5NzVmYTJlZmUzYzNhZWI3Y2Q4
14
+ YzkyZWViZGFlZjZjNDY3YjU0OTNmYTk0MmUzMjEwMWM1MTc3YzdjMGFjODhi
15
+ OGU2M2EzZDZkYWViY2QzOGJhZGRmY2UyYTdjOWU4NmYxNTY0Y2M=
data/.travis.yml CHANGED
@@ -6,6 +6,6 @@ rvm:
6
6
  # - jruby-18mode # JRuby in 1.8 mode
7
7
  # - jruby-19mode # JRuby in 1.9 mode
8
8
  # - rbx-18mode
9
- - rbx-19mode
9
+ # - rbx-19mode
10
10
  # uncomment this line if your project needs to run something other than `rake`:
11
11
  script: bundle exec rspec spec
@@ -1,3 +1,3 @@
1
1
  module SqliteMagic
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/sqlite_magic.rb CHANGED
@@ -36,6 +36,11 @@ module SqliteMagic
36
36
  query = unique_keys ? "CREATE TABLE #{tbl_name} (#{col_names.join(',')}, UNIQUE (#{unique_keys.join(',')}))" :
37
37
  "CREATE TABLE #{tbl_name} (#{col_names.join(',')})"
38
38
  database.execute query
39
+ if unique_keys && !unique_keys.empty?
40
+ query = "CREATE UNIQUE INDEX IF NOT EXISTS #{unique_keys.join('_')} " +
41
+ "ON #{tbl_name} (#{unique_keys.join(',')})"
42
+ database.execute query
43
+ end
39
44
  end
40
45
 
41
46
  def execute(query,data=nil)
@@ -54,15 +59,17 @@ module SqliteMagic
54
59
 
55
60
  # This is an (expensive) convenience method to insert a row (for given unique keys), or if the row already exists
56
61
  #
57
- def insert_or_update(uniq_keys, values_hash, tbl_name='main_table')
62
+ def insert_or_update(uniq_keys, values_hash, tbl_name='main_table', opts={})
58
63
  all_field_names = values_hash.keys
59
64
  field_names_as_symbol_string = all_field_names.map{ |k| ":#{k}" }.join(',') # need to appear as symbols
60
65
  sql_statement = "INSERT INTO #{tbl_name} (#{all_field_names.join(',')}) VALUES (#{field_names_as_symbol_string})"
61
66
  database.execute(sql_statement, values_hash)
62
67
  rescue SQLite3::ConstraintException => e
63
68
  unique_key_constraint = uniq_keys.map { |k| "#{k}=:#{k}" }.join(' AND ')
64
- update_keys = (values_hash.keys - uniq_keys).map { |k| "#{k}=:#{k}" }.join(', ')
65
- sql_statement = "UPDATE #{tbl_name} SET #{update_keys} WHERE #{unique_key_constraint}"
69
+ update_keys = values_hash.keys
70
+ update_keys -= uniq_keys if !opts[:update_unique_keys]
71
+ update_sql = update_keys.map { |k| "#{k}=:#{k}" }.join(', ')
72
+ sql_statement = "UPDATE #{tbl_name} SET #{update_sql} WHERE #{unique_key_constraint}"
66
73
  database.execute sql_statement, values_hash
67
74
  rescue SQLite3::SQLException => e
68
75
  puts "Exception (#{e.inspect}) raised" if verbose?
@@ -181,4 +188,3 @@ module SqliteMagic
181
188
  # end
182
189
 
183
190
  end
184
-
@@ -214,9 +214,11 @@ describe SqliteMagic do
214
214
  end
215
215
 
216
216
  context 'and unique keys are given' do
217
- it 'should add constraint for given keys' do
218
- expected_query = "CREATE TABLE some_table (foo,bar,baz, UNIQUE (foo,baz))"
219
- @dummy_db.should_receive(:execute).with(expected_query)
217
+ it 'should add constraint and index for given keys' do
218
+ expected_query_1 = "CREATE TABLE some_table (foo,bar,baz, UNIQUE (foo,baz))"
219
+ expected_query_2 = "CREATE UNIQUE INDEX IF NOT EXISTS foo_baz ON some_table (foo,baz)"
220
+ @dummy_db.should_receive(:execute).with(expected_query_1)
221
+ @dummy_db.should_receive(:execute).with(expected_query_2)
220
222
  @connection.create_table(:some_table, [:foo,:bar,:baz], [:foo,:baz])
221
223
  end
222
224
  end
@@ -281,6 +283,14 @@ describe SqliteMagic do
281
283
  @connection.insert_or_update(@unique_keys, @datum, 'foo_table')
282
284
  end
283
285
 
286
+ context "and :update_unique_keys specified in opts" do
287
+ it 'should update all columns including unique keys' do
288
+ @expected_update_query = "UPDATE foo_table SET foo=:foo, foo2=:foo2, foo3=:foo3, foo4=:foo4 WHERE foo2=:foo2 AND foo3=:foo3"
289
+ @dummy_db.should_receive(:execute).with(@expected_update_query, @datum)
290
+ @connection.insert_or_update(@unique_keys, @datum, 'foo_table', :update_unique_keys => true)
291
+ end
292
+ end
293
+
284
294
  context 'and no table name given' do
285
295
  before do
286
296
  @expected_update_query = "UPDATE main_table SET foo=:foo, foo4=:foo4 WHERE foo2=:foo2 AND foo3=:foo3"
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlite_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Chris Taggart
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-04 00:00:00.000000000 Z
11
+ date: 2015-05-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: sqlite3
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -98,27 +89,26 @@ files:
98
89
  homepage: https://github.com/openc/sqlite_magic
99
90
  licenses:
100
91
  - MIT
92
+ metadata: {}
101
93
  post_install_message:
102
94
  rdoc_options: []
103
95
  require_paths:
104
96
  - lib
105
97
  required_ruby_version: !ruby/object:Gem::Requirement
106
- none: false
107
98
  requirements:
108
99
  - - ! '>='
109
100
  - !ruby/object:Gem::Version
110
101
  version: '0'
111
102
  required_rubygems_version: !ruby/object:Gem::Requirement
112
- none: false
113
103
  requirements:
114
104
  - - ! '>='
115
105
  - !ruby/object:Gem::Version
116
106
  version: '0'
117
107
  requirements: []
118
108
  rubyforge_project:
119
- rubygems_version: 1.8.25
109
+ rubygems_version: 2.4.5
120
110
  signing_key:
121
- specification_version: 3
111
+ specification_version: 4
122
112
  summary: Sprinkles some magic onto Sqlite3 gem
123
113
  test_files:
124
114
  - spec/.DS_Store