baza 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -107,6 +107,18 @@ Or a specific column:
107
107
  end
108
108
  end
109
109
 
110
+ == Query Buffer
111
+ In order to speed things up, but without using transactions directly, you can use a query buffer. This stores various instructions in memory and flushes them every now and then through transactions or intelligent queries (like multi-insertion). The drawback is that it will not be possible to test the queries for errors before a flush is executed and it wont be possible to read results form any of the queries. It is fairly simple do however:
112
+ db.q_buffer do |buffer|
113
+ 100_000.times do |count|
114
+ buffer.insert(:table_name, {:name => "Kasper #{count}"})
115
+
116
+ buffer.query("UPDATE table SET ...")
117
+ buffer.query("DELETE FROM table WHERE ...")
118
+ end
119
+ end
120
+
121
+
110
122
  == Contributing to baza
111
123
 
112
124
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/baza.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "baza"
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
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"]
12
- s.date = "2013-04-16"
12
+ s.date = "2013-04-17"
13
13
  s.description = "A database abstraction layer, model framework and database framework."
14
14
  s.email = "kj@gfish.com"
15
15
  s.extra_rdoc_files = [
data/include/db.rb CHANGED
@@ -435,13 +435,19 @@ class Baza::Db
435
435
  end
436
436
 
437
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)
438
+ def upsert(table, selector, data, args = nil)
439
439
  row = self.select(table, selector, "limit" => 1).fetch
440
440
 
441
+ if args and args[:buffer]
442
+ obj = args[:buffer]
443
+ else
444
+ obj = self
445
+ end
446
+
441
447
  if row
442
- self.update(table, data, selector)
448
+ obj.update(table, data, selector)
443
449
  else
444
- self.insert(table, selector.merge(data))
450
+ obj.insert(table, selector.merge(data))
445
451
  end
446
452
  end
447
453
 
@@ -33,7 +33,7 @@ class Baza::QueryBuffer
33
33
  end
34
34
 
35
35
  #Delete as on a normal Baza::Db.
36
- #===Examples
36
+ #===Example
37
37
  # db.q_buffer do |buffer|
38
38
  # buffer.delete(:users, {:id => 5})
39
39
  # end
@@ -43,6 +43,16 @@ class Baza::QueryBuffer
43
43
  return nil
44
44
  end
45
45
 
46
+ #Update as on a normal Baza::Db.
47
+ #===Example
48
+ # db.q_buffer do |buffer|
49
+ # buffer.update(:users, {:name => "Kasper"}, {:id => 5})
50
+ # end
51
+ def update(table, update, terms)
52
+ STDOUT.puts "Update called on table #{table}." if @debug
53
+ self.query(@args[:db].update(table, update, terms, :return_sql => true))
54
+ end
55
+
46
56
  #Plans to inset a hash into a table. It will only be inserted when flush is called.
47
57
  #===Examples
48
58
  # db.q_buffer do |buffer|
@@ -64,20 +74,22 @@ class Baza::QueryBuffer
64
74
  return nil if @queries_count <= 0
65
75
 
66
76
  @lock.synchronize do
67
- @args[:db].transaction do
68
- @queries.shift(1000).each do |str|
69
- STDOUT.print "Executing via buffer: #{str}\n" if @debug
70
- @args[:db].q(str)
71
- end
72
-
73
- @inserts.each do |table, datas_arr|
74
- while !datas_arr.empty?
75
- datas_chunk_arr = datas_arr.shift(1000)
76
- @args[:db].insert_multi(table, datas_chunk_arr)
77
+ if !@queries.empty?
78
+ @args[:db].transaction do
79
+ @queries.shift(1000).each do |str|
80
+ STDOUT.print "Executing via buffer: #{str}\n" if @debug
81
+ @args[:db].q(str)
77
82
  end
78
83
  end
79
84
  end
80
85
 
86
+ @inserts.each do |table, datas_arr|
87
+ while !datas_arr.empty?
88
+ datas_chunk_arr = datas_arr.shift(1000)
89
+ @args[:db].insert_multi(table, datas_chunk_arr)
90
+ end
91
+ end
92
+
81
93
  @inserts.clear
82
94
  @queries_count = 0
83
95
  end
data/lib/baza.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  class Baza
2
2
  #Autoloader for subclasses.
3
3
  def self.const_missing(name)
4
- require "#{File.dirname(__FILE__)}/../include/#{name.to_s.downcase}.rb"
4
+ file_name = name.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase
5
+ require "#{File.dirname(__FILE__)}/../include/#{file_name}.rb"
5
6
  raise "Still not defined: '#{name}'." if !Baza.const_defined?(name)
6
7
  return Baza.const_get(name)
7
8
  end
data/spec/baza_spec.rb CHANGED
@@ -291,6 +291,49 @@ describe "Baza" do
291
291
  count_found.should eql(10000)
292
292
  end
293
293
  end
294
+
295
+ it "should be able to use query buffers" do
296
+ driver[:const].sample_db do |db|
297
+ db.tables.create(:test_table, {
298
+ :columns => [
299
+ {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
300
+ {:name => :name, :type => :varchar}
301
+ ]
302
+ })
303
+
304
+ db.q_buffer do |buffer|
305
+ 10000.times do |count|
306
+ buffer.insert(:test_table, {:name => "Kasper #{count}"})
307
+ end
308
+ end
309
+
310
+ test_table = db.tables[:test_table]
311
+ test_table.rows_count.should eql(10000)
312
+
313
+ db.q_buffer do |buffer|
314
+ count = 0
315
+ db.select(:test_table, {}, :orderby => :id) do |row|
316
+ row[:name].should eql("Kasper #{count}")
317
+ buffer.update(:test_table, {:name => "Kasper #{count}-#{count}"}, {:id => row[:id]})
318
+ count += 1
319
+ end
320
+ end
321
+
322
+ count = 0
323
+ db.select(:test_table, {}, :orderby => :id) do |row|
324
+ row[:name].should eql("Kasper #{count}-#{count}")
325
+ count += 1
326
+ end
327
+
328
+ db.q_buffer do |buffer|
329
+ db.select(:test_table) do |row|
330
+ buffer.delete(:test_table, {:id => row[:id]})
331
+ end
332
+ end
333
+
334
+ test_table.rows_count.should eql(0)
335
+ end
336
+ end
294
337
  end
295
338
 
296
339
  it "should be able to connect to mysql and do various stuff" do
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-16 00:00:00.000000000 Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: datet
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: -3309657141000385200
221
+ hash: 2097963653998790721
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements: