baza 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/baza.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "baza"
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
data/include/db.rb CHANGED
@@ -434,9 +434,9 @@ class Baza::Db
434
434
  end
435
435
  end
436
436
 
437
- #Checks if a given selector exists. If it does, updates it to match data. If not inserts the row.
438
- def upsert(table, selector, data, args = nil)
439
- row = self.select(table, selector, "limit" => 1).fetch
437
+ #Checks if a given terms exists. If it does, updates it to match data. If not inserts the row.
438
+ def upsert(table, data, terms, args = nil)
439
+ row = self.select(table, terms, "limit" => 1).fetch
440
440
 
441
441
  if args and args[:buffer]
442
442
  obj = args[:buffer]
@@ -445,9 +445,9 @@ class Baza::Db
445
445
  end
446
446
 
447
447
  if row
448
- obj.update(table, data, selector)
448
+ obj.update(table, data, terms)
449
449
  else
450
- obj.insert(table, selector.merge(data))
450
+ obj.insert(table, terms.merge(data))
451
451
  end
452
452
  end
453
453
 
@@ -34,9 +34,7 @@ class Baza::QueryBuffer
34
34
 
35
35
  #Delete as on a normal Baza::Db.
36
36
  #===Example
37
- # db.q_buffer do |buffer|
38
- # buffer.delete(:users, {:id => 5})
39
- # end
37
+ # buffer.delete(:users, {:id => 5})
40
38
  def delete(table, where)
41
39
  STDOUT.puts "Delete called on table #{table} with arguments: '#{where}'." if @debug
42
40
  self.query(@args[:db].delete(table, where, :return_sql => true))
@@ -45,19 +43,22 @@ class Baza::QueryBuffer
45
43
 
46
44
  #Update as on a normal Baza::Db.
47
45
  #===Example
48
- # db.q_buffer do |buffer|
49
- # buffer.update(:users, {:name => "Kasper"}, {:id => 5})
50
- # end
46
+ # buffer.update(:users, {:name => "Kasper"}, {:id => 5})
51
47
  def update(table, update, terms)
52
48
  STDOUT.puts "Update called on table #{table}." if @debug
53
49
  self.query(@args[:db].update(table, update, terms, :return_sql => true))
54
50
  end
55
51
 
52
+ #Shortcut to doing upsert through the buffer instead of through the db-object with the buffer as an argument.
53
+ #===Example
54
+ # buffer.upsert(:users, {:id => 5}, {:name => "Kasper"})
55
+ def upsert(table, data, terms)
56
+ @args[:db].upsert(table, data, terms, :buffer => self)
57
+ end
58
+
56
59
  #Plans to inset a hash into a table. It will only be inserted when flush is called.
57
60
  #===Examples
58
- # db.q_buffer do |buffer|
59
- # buffer.insert(:users, {:name => "John Doe"})
60
- # end
61
+ # buffer.insert(:users, {:name => "John Doe"})
61
62
  def insert(table, data)
62
63
  @lock.synchronize do
63
64
  @inserts[table] = [] if !@inserts.key?(table)
data/spec/baza_spec.rb CHANGED
@@ -116,14 +116,14 @@ describe "Baza" do
116
116
  table.reload
117
117
  rows_count = table.rows_count
118
118
 
119
- db.upsert(:test_table, sel, data)
119
+ db.upsert(:test_table, data, sel)
120
120
  row = db.select(:test_table, sel).fetch
121
121
  row[:name].should eql("upsert - Kasper Johansen")
122
122
 
123
123
  table.reload
124
124
  table.rows_count.should eql(rows_count + 1)
125
125
 
126
- db.upsert(:test_table, sel, data2)
126
+ db.upsert(:test_table, data2, sel)
127
127
  row = db.select(:test_table, sel).fetch
128
128
  row[:name].should eql("upsert - Kasper Nielsen Johansen")
129
129
 
@@ -301,20 +301,37 @@ describe "Baza" do
301
301
  ]
302
302
  })
303
303
 
304
+ upsert = false
304
305
  db.q_buffer do |buffer|
305
- 10000.times do |count|
306
- buffer.insert(:test_table, {:name => "Kasper #{count}"})
306
+ 2500.times do |count|
307
+ if upsert
308
+ buffer.upsert(:test_table, {:name => "Kasper #{count}"}, {:name => "Kasper #{count}"})
309
+ upsert = false
310
+ else
311
+ buffer.insert(:test_table, {:name => "Kasper #{count}"})
312
+ upsert = true
313
+ end
307
314
  end
308
315
  end
309
316
 
310
317
  test_table = db.tables[:test_table]
311
- test_table.rows_count.should eql(10000)
318
+ test_table.rows_count.should eql(2500)
312
319
 
313
320
  db.q_buffer do |buffer|
314
321
  count = 0
322
+ upsert = false
323
+
315
324
  db.select(:test_table, {}, :orderby => :id) do |row|
316
325
  row[:name].should eql("Kasper #{count}")
317
- buffer.update(:test_table, {:name => "Kasper #{count}-#{count}"}, {:id => row[:id]})
326
+
327
+ if upsert
328
+ buffer.upsert(:test_table, {:name => "Kasper #{count}-#{count}"}, {:id => row[:id]})
329
+ upsert = false
330
+ else
331
+ buffer.update(:test_table, {:name => "Kasper #{count}-#{count}"}, {:id => row[:id]})
332
+ upsert = true
333
+ end
334
+
318
335
  count += 1
319
336
  end
320
337
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: 2097963653998790721
221
+ hash: 3400246052753672673
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements: