knjrbfw 0.0.52 → 0.0.53

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.52
1
+ 0.0.53
data/knjrbfw.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjrbfw}
8
- s.version = "0.0.52"
8
+ s.version = "0.0.53"
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 = %q{2012-06-29}
12
+ s.date = %q{2012-07-04}
13
13
  s.description = %q{Including stuff for HTTP, SSH and much more.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
data/lib/knj/datarow.rb CHANGED
@@ -452,19 +452,19 @@ class Knj::Datarow
452
452
  return sql.to_s if d.args["return_sql"]
453
453
 
454
454
  if select_col_as_array
455
- ids = [] if !block
456
- d.db.q(sql, qargs) do |data|
457
- if block
458
- block.call(data[:id])
459
- else
460
- ids << data[:id]
455
+ enum = Enumerator.new do |yielder|
456
+ d.db.q(sql, qargs) do |data|
457
+ yielder << data[:id]
461
458
  end
462
459
  end
463
460
 
464
- if !block
465
- return ids
466
- else
461
+ if block
462
+ enum.each(&block)
467
463
  return nil
464
+ elsif d.ob.args[:array_enum]
465
+ return Array_enumerator.new(enum)
466
+ else
467
+ return enum.to_a
468
468
  end
469
469
  elsif count
470
470
  ret = d.db.query(sql).fetch
@@ -6,6 +6,12 @@ class Knj::Datarow_custom
6
6
  return true
7
7
  end
8
8
 
9
+ #Initializes variables on the class from objects.
10
+ def self.datarow_init(d)
11
+ @@ob = d.ob
12
+ @@db = d.db
13
+ end
14
+
9
15
  def self.has_one(arr)
10
16
  arr.each do |val|
11
17
  methodname = nil
@@ -28,13 +34,13 @@ class Knj::Datarow_custom
28
34
  colname = "#{classname.to_s.downcase}_id".to_sym if !colname
29
35
 
30
36
  define_method(methodname) do
31
- return @ob.get_try(self, colname, classname)
37
+ return @@ob.get_try(self, colname, classname)
32
38
  end
33
39
 
34
40
  methodname_html = "#{methodname.to_s}_html".to_sym
35
41
  define_method(methodname_html) do |*args|
36
42
  obj = self.send(methodname)
37
- return @ob.events.call(:no_html, classname) if !obj
43
+ return @@ob.events.call(:no_html, classname) if !obj
38
44
 
39
45
  raise "Class '#{classname}' does not have a 'html'-method." if !obj.respond_to?(:html)
40
46
  return obj.html(*args)
@@ -63,13 +69,10 @@ class Knj::Datarow_custom
63
69
  end
64
70
 
65
71
  def table
66
- return self.class.name.split("::").last
72
+ return self.class.table
67
73
  end
68
74
 
69
- def initialize(d)
70
- @ob = d.ob
71
- data = d.data
72
-
75
+ def initialize(data, args)
73
76
  if data.is_a?(Hash)
74
77
  @data = Knj::ArrayExt.hash_sym(data)
75
78
  @id = self.id
@@ -95,14 +98,8 @@ class Knj::Datarow_custom
95
98
 
96
99
  #Returns a key from the hash that this object is holding or raises an error if it doesnt exist.
97
100
  def [](key)
98
- if !@data
99
- raise "No data spawned on object."
100
- end
101
-
102
- if !@data.key?(key)
103
- raise "No such key: '#{key}'. Available keys are: '#{@data.keys.sort.join(", ")}'."
104
- end
105
-
101
+ raise "No data spawned on object." if !@data
102
+ raise "No such key: '#{key}'. Available keys are: '#{@data.keys.sort.join(", ")}'." if !@data.key?(key)
106
103
  return @data[key]
107
104
  end
108
105
 
@@ -39,9 +39,7 @@ class KnjDB_mysql::Tables
39
39
  ret = {} unless block_given?
40
40
 
41
41
  sql = "SHOW TABLE STATUS"
42
- if args[:name]
43
- sql << " WHERE `Name` = '#{@db.esc(args[:name])}'"
44
- end
42
+ sql << " WHERE `Name` = '#{@db.esc(args[:name])}'" if args[:name]
45
43
 
46
44
  @list_mutex.synchronize do
47
45
  @db.q(sql) do |d_tables|
@@ -154,26 +152,25 @@ class KnjDB_mysql::Tables::Table
154
152
  def column(name)
155
153
  name = name.to_s
156
154
 
157
- begin
155
+ if col = @list.get!(name)
158
156
  return @list[name]
159
- rescue Wref::Recycled
160
- #ignore.
161
157
  end
162
158
 
163
- self.columns do |col|
159
+ self.columns(:name => name) do |col|
164
160
  return col if col.name == name
165
161
  end
166
162
 
167
163
  raise Knj::Errors::NotFound.new("Column not found: '#{name}'.")
168
164
  end
169
165
 
170
- def columns
166
+ def columns(args = nil)
171
167
  @db.cols
172
168
  ret = {}
173
169
  sql = "SHOW FULL COLUMNS FROM `#{self.name}`"
170
+ sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args and args.key?(:name)
174
171
 
175
172
  @db.q(sql) do |d_cols|
176
- obj = @list.get!(d_cols[:Field])
173
+ obj = @list.get!(d_cols[:Field].to_s)
177
174
 
178
175
  if !obj
179
176
  obj = KnjDB_mysql::Columns::Column.new(
@@ -181,13 +178,13 @@ class KnjDB_mysql::Tables::Table
181
178
  :db => @db,
182
179
  :data => d_cols
183
180
  )
184
- @list[d_cols[:Field]] = obj
181
+ @list[d_cols[:Field].to_s] = obj
185
182
  end
186
183
 
187
184
  if block_given?
188
185
  yield(obj)
189
186
  else
190
- ret[d_cols[:Field]] = obj
187
+ ret[d_cols[:Field].to_s] = obj
191
188
  end
192
189
  end
193
190
 
@@ -198,14 +195,17 @@ class KnjDB_mysql::Tables::Table
198
195
  end
199
196
  end
200
197
 
201
- def indexes
198
+ def indexes(args = nil)
202
199
  @db.indexes
203
200
  ret = {}
204
201
 
205
- @db.q("SHOW INDEX FROM `#{self.name}`") do |d_indexes|
202
+ sql = "SHOW INDEX FROM `#{self.name}`"
203
+ sql << " WHERE `Key_name` = '#{@db.esc(args[:name])}'" if args and args.key?(:name)
204
+
205
+ @db.q(sql) do |d_indexes|
206
206
  next if d_indexes[:Key_name] == "PRIMARY"
207
207
 
208
- obj = @indexes_list.get!(d_indexes[:Key_name])
208
+ obj = @indexes_list.get!(d_indexes[:Key_name].to_s)
209
209
 
210
210
  if !obj
211
211
  obj = KnjDB_mysql::Indexes::Index.new(
@@ -214,13 +214,13 @@ class KnjDB_mysql::Tables::Table
214
214
  :data => d_indexes
215
215
  )
216
216
  obj.columns << d_indexes[:Column_name]
217
- @indexes_list[d_indexes[:Key_name]] = obj
217
+ @indexes_list[d_indexes[:Key_name].to_s] = obj
218
218
  end
219
219
 
220
220
  if block_given?
221
221
  yield(obj)
222
222
  else
223
- ret[d_indexes[:Key_name]] = obj
223
+ ret[d_indexes[:Key_name].to_s] = obj
224
224
  end
225
225
  end
226
226
 
@@ -234,14 +234,12 @@ class KnjDB_mysql::Tables::Table
234
234
  def index(name)
235
235
  name = name.to_s
236
236
 
237
- begin
238
- return @indexes_list[name]
239
- rescue Wref::Recycled
240
- #ignore.
237
+ if index = @indexes_list.get!(name)
238
+ return index
241
239
  end
242
240
 
243
- self.indexes do |index|
244
- return index if index.name == name
241
+ self.indexes(:name => name) do |index|
242
+ return index if index.name.to_s == name
245
243
  end
246
244
 
247
245
  raise Knj::Errors::NotFound.new("Index not found: #{name}.")
@@ -235,34 +235,38 @@ class Knj::Db
235
235
  # sql = db.insert(:users, {:name => "John", :lastname => "Doe"}, :return_sql => true) #=> "INSERT INTO `users` (`name`, `lastname`) VALUES ('John', 'Doe')"
236
236
  def insert(tablename, arr_insert, args = nil)
237
237
  self.conn_exec do |driver|
238
- sql = "INSERT INTO #{driver.escape_table}#{tablename.to_s}#{driver.escape_table} ("
238
+ sql = "INSERT INTO #{driver.escape_table}#{tablename.to_s}#{driver.escape_table}"
239
239
 
240
- first = true
241
- arr_insert.each do |key, value|
242
- if first
243
- first = false
244
- else
245
- sql << ", "
240
+ if arr_insert and !arr_insert.empty?
241
+ sql << " ("
242
+
243
+ first = true
244
+ arr_insert.each do |key, value|
245
+ if first
246
+ first = false
247
+ else
248
+ sql << ", "
249
+ end
250
+
251
+ sql << "#{driver.escape_col}#{key.to_s}#{driver.escape_col}"
246
252
  end
247
253
 
248
- sql << "#{driver.escape_col}#{key.to_s}#{driver.escape_col}"
249
- end
250
-
251
- sql << ") VALUES ("
252
-
253
- first = true
254
- arr_insert.each do |key, value|
255
- if first
256
- first = false
257
- else
258
- sql << ", "
254
+ sql << ") VALUES ("
255
+
256
+ first = true
257
+ arr_insert.each do |key, value|
258
+ if first
259
+ first = false
260
+ else
261
+ sql << ", "
262
+ end
263
+
264
+ sql << "#{driver.escape_val}#{driver.escape(value.to_s)}#{driver.escape_val}"
259
265
  end
260
266
 
261
- sql << "#{driver.escape_val}#{driver.escape(value.to_s)}#{driver.escape_val}"
267
+ sql << ")"
262
268
  end
263
269
 
264
- sql << ")"
265
-
266
270
  return sql if args and args[:return_sql]
267
271
  driver.query(sql)
268
272
  return driver.lastID if args and args[:return_id]
@@ -473,11 +477,11 @@ class Knj::Db
473
477
  # str = driver.escape('something̈́')
474
478
  # end
475
479
  def conn_exec
476
- if Thread.current[:knjdb]
480
+ if tcur = Thread.current and tcur[:knjdb]
477
481
  tid = self.__id__
478
482
 
479
- if Thread.current[:knjdb].key?(tid)
480
- yield(Thread.current[:knjdb][tid])
483
+ if tcur[:knjdb].key?(tid)
484
+ yield(tcur[:knjdb][tid])
481
485
  return nil
482
486
  end
483
487
  end
@@ -603,6 +607,7 @@ class Knj::Db
603
607
  #Yields a query-buffer and flushes at the end of the block given.
604
608
  def q_buffer(&block)
605
609
  Knj::Db::Query_buffer.new(:db => self, &block)
610
+ return nil
606
611
  end
607
612
 
608
613
  #Returns the last inserted ID.
@@ -12,7 +12,7 @@ class Knj::Db::Query_buffer
12
12
  begin
13
13
  yield(self)
14
14
  ensure
15
- self.flush if @queries_count > 0 or !@queries.empty? or !@inserts.empty?
15
+ self.flush
16
16
  end
17
17
  end
18
18
 
@@ -24,7 +24,7 @@ class Knj::Db::Query_buffer
24
24
  @queries_count += 1
25
25
  end
26
26
 
27
- self.flush if @queries_count > 1000
27
+ self.flush if @queries_count >= 1000
28
28
  return nil
29
29
  end
30
30
 
@@ -50,15 +50,15 @@ class Knj::Db::Query_buffer
50
50
  @queries_count += 1
51
51
  end
52
52
 
53
- self.flush if @queries_count > 1000
53
+ self.flush if @queries_count >= 1000
54
54
  return nil
55
55
  end
56
56
 
57
57
  #Flushes all queries out in a transaction. This will automatically be called for every 1000 queries.
58
58
  def flush
59
+ return nil if @queries_count <= 0
60
+
59
61
  @lock.synchronize do
60
- return nil if @queries_count <= 0 and @queries.empty? and @inserts.empty?
61
-
62
62
  @args[:db].transaction do
63
63
  @queries.shift(1000).each do |str|
64
64
  STDOUT.print "Executing via buffer: #{str}\n" if @debug
@@ -75,7 +75,8 @@ class Knj::Db::Query_buffer
75
75
 
76
76
  @inserts.clear
77
77
  @queries_count = 0
78
- return nil
79
78
  end
79
+
80
+ return nil
80
81
  end
81
82
  end
@@ -42,7 +42,12 @@ class Knj::Db::Revision
42
42
  raise "No tables given." if !schema.has_key?("tables")
43
43
 
44
44
  #Cache tables to avoid constant reloading.
45
- tables = db.tables.list
45
+ if !args["tables_cache"] or args["tables_cache"]
46
+ print "Caching tables-list.\n" if args["debug"]
47
+ tables = db.tables.list
48
+ else
49
+ print "Skipping tables-cache.\n" if args["debug"]
50
+ end
46
51
 
47
52
  schema["tables"].each do |table_name, table_data|
48
53
  begin
@@ -75,7 +80,7 @@ class Knj::Db::Revision
75
80
 
76
81
  actual_after = nil
77
82
  set_next = false
78
- table_obj.columns do |col_iter|
83
+ cols.each do |col_name, col_iter|
79
84
  if col_iter.name == col_obj.name
80
85
  break
81
86
  else
@@ -138,7 +143,13 @@ class Knj::Db::Revision
138
143
  end
139
144
  end
140
145
 
141
- col_obj.change(col_data) if dochange
146
+ if dochange
147
+ col_obj.change(col_data)
148
+
149
+ #Change has been made - update cache.
150
+ cols = table_obj.columns
151
+ end
152
+
142
153
  first_col = false
143
154
  rescue Knj::Errors::NotFound => e
144
155
  print "Column not found: #{table_obj.name}.#{col_data["name"]}.\n" if args["debug"]
@@ -161,6 +172,9 @@ class Knj::Db::Revision
161
172
 
162
173
  col_rename.change(col_data)
163
174
 
175
+ #Change has been made - update cache.
176
+ cols = table_obj.columns
177
+
164
178
  if col_data.is_a?(Hash) and col_data["on_after_rename"]
165
179
  col_data["on_after_rename"].call("db" => db, "table" => table_obj, "col" => col_rename, "col_data" => col_data)
166
180
  end
@@ -175,6 +189,10 @@ class Knj::Db::Revision
175
189
  oncreated = col_data["on_created"]
176
190
  col_data.delete("on_created") if col_data["oncreated"]
177
191
  col_obj = table_obj.create_columns([col_data])
192
+
193
+ #Change has been made - update cache.
194
+ cols = table_obj.columns
195
+
178
196
  oncreated.call("db" => db, "table" => table_obj) if oncreated
179
197
  end
180
198
  end
@@ -236,7 +254,7 @@ class Knj::Db::Revision
236
254
  if table_data.key?("renames")
237
255
  table_data["renames"].each do |table_name_rename|
238
256
  begin
239
- table_rename = db.tables[table_name_rename]
257
+ table_rename = db.tables[table_name_rename.to_sym]
240
258
  table_rename.rename(table_name)
241
259
  raise Knj::Errors::Retry
242
260
  rescue Knj::Errors::NotFound
@@ -255,7 +273,7 @@ class Knj::Db::Revision
255
273
  end
256
274
 
257
275
  db.tables.create(table_name, table_data)
258
- table_obj = db.tables[table_name]
276
+ table_obj = db.tables[table_name.to_sym]
259
277
 
260
278
  if table_data["on_create_after"]
261
279
  table_data["on_create_after"].call("db" => db, "table_name" => table_name, "table_data" => table_data)
@@ -282,7 +300,7 @@ class Knj::Db::Revision
282
300
 
283
301
 
284
302
  #Free cache.
285
- tables.clear
303
+ tables.clear if tables
286
304
  tables = nil
287
305
  end
288
306
 
data/lib/knj/objects.rb CHANGED
@@ -19,6 +19,7 @@ class Knj::Objects
19
19
  @lock_require = Monitor.new
20
20
 
21
21
  require "wref" if @args[:cache] == :weak and !Kernel.const_defined?(:Wref)
22
+ require "#{@args[:array_enumerator_path]}array_enumerator" if @args[:array_enum] and !Kernel.const_defined?(:Array_enumerator)
22
23
 
23
24
  #Set up various events.
24
25
  @events = Knj::Event_handler.new
@@ -562,18 +563,33 @@ class Knj::Objects
562
563
  end
563
564
  end
564
565
 
565
- @args[:db].q(sql, qargs) do |d_obs|
566
+ if @args[:array_enum]
567
+ enum = Enumerator.new do |yielder|
568
+ @args[:db].q(sql, qargs) do |d_obs|
569
+ yielder << self.get(classname, d_obs)
570
+ end
571
+ end
572
+
566
573
  if block
567
- block.call(self.get(classname, d_obs))
574
+ enum.each(&block)
575
+ return nil
568
576
  else
569
- ret << self.get(classname, d_obs)
577
+ return Array_enumerator.new(enum)
570
578
  end
571
- end
572
-
573
- if !block
574
- return ret
575
579
  else
576
- return nil
580
+ @args[:db].q(sql, qargs) do |d_obs|
581
+ if block
582
+ block.call(self.get(classname, d_obs))
583
+ else
584
+ ret << self.get(classname, d_obs)
585
+ end
586
+ end
587
+
588
+ if !block
589
+ return ret
590
+ else
591
+ return nil
592
+ end
577
593
  end
578
594
  end
579
595
 
@@ -725,7 +741,7 @@ class Knj::Objects
725
741
  #===Examples
726
742
  # user = ob.get(:User, 1)
727
743
  # ob.delete(user)
728
- def delete(object)
744
+ def delete(object, args = nil)
729
745
  #Return false if the object has already been deleted.
730
746
  return false if object.deleted?
731
747
  classname = object.class.classname.to_sym
@@ -739,7 +755,7 @@ class Knj::Objects
739
755
  #If autodelete is set by 'has_many'-method, go through it and delete the various objects first.
740
756
  object.class.autodelete_data.each do |adel_data|
741
757
  self.list(adel_data[:classname], {adel_data[:colname].to_s => object.id}) do |obj_del|
742
- self.delete(obj_del)
758
+ self.delete(obj_del, args)
743
759
  end
744
760
  end
745
761
 
@@ -756,12 +772,18 @@ class Knj::Objects
756
772
  _kas.trans_del(object)
757
773
  end
758
774
 
759
- @args[:db].delete(object.table, {:id => obj_id})
775
+ #If a buffer is given in arguments, then use that to delete the object.
776
+ if args and buffer = args[:db_buffer]
777
+ buffer.delete(object.table, {:id => obj_id})
778
+ else
779
+ @args[:db].delete(object.table, {:id => obj_id})
780
+ end
760
781
  end
761
782
 
762
783
  @ids_cache[classname].delete(obj_id.to_i) if @ids_cache_should.key?(classname)
763
784
  self.call("object" => object, "signal" => "delete")
764
785
  object.destroy
786
+ return nil
765
787
  end
766
788
 
767
789
  #Deletes several objects as one. If running datarow-mode it checks all objects before it starts to actually delete them. Its faster than deleting every single object by itself...
data/lib/knj/thread.rb CHANGED
@@ -9,9 +9,9 @@ class Knj::Thread < Thread
9
9
  super(*args) do
10
10
  begin
11
11
  yield(*args)
12
- rescue SystemExit
13
- exit
14
- rescue => e
12
+ rescue SystemExit, Interrupt
13
+ raise
14
+ rescue Exception => e
15
15
  print "#{Knj::Errors.error_str(e)}\n\n"
16
16
  end
17
17
  end
data/lib/knj/web.rb CHANGED
@@ -481,7 +481,6 @@ class Knj::Web
481
481
  if opthash.is_a?(Hash) or opthash.class.to_s == "Dictionary"
482
482
  opthash.each do |key, value|
483
483
  html << "<option"
484
-
485
484
  sel = false
486
485
 
487
486
  if curvalue.is_a?(Array) and curvalue.index(key) != nil
@@ -499,8 +498,8 @@ class Knj::Web
499
498
  opthash.each_index do |key|
500
499
  if opthash[key.to_i] != nil
501
500
  html << "<option"
502
- html << " selected=\"selected\"" if curvalue.to_i == key.to_i
503
- html << " value=\"#{key.to_s}\">#{opthash[key].to_s}</option>"
501
+ html << " selected=\"selected\"" if curvalue.to_s == key.to_s
502
+ html << " value=\"#{Knj::Web.html(key)}\">#{Knj::Web.html(opthash[key])}</option>"
504
503
  end
505
504
  end
506
505
  end
data/spec/objects_spec.rb CHANGED
@@ -2,11 +2,12 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Objects" do
4
4
  it "should be able to cache rows" do
5
+ require "~/Dev/Ruby/array_enumerator/lib/array_enumerator"
5
6
  require "sqlite3" if RUBY_ENGINE != "jruby"
6
7
 
7
8
  $db_path = "#{Knj::Os.tmpdir}/knjrbfw_objects_cache_test.sqlite3"
8
9
  File.unlink($db_path) if File.exists?($db_path)
9
- $db = Knj::Db.new(:type => :sqlite3, :path => $db_path, :return_keys => "symbols")
10
+ $db = Knj::Db.new(:type => :sqlite3, :path => $db_path, :return_keys => "symbols", :debug => false)
10
11
 
11
12
  schema = {
12
13
  "tables" => {
@@ -32,6 +33,7 @@ describe "Objects" do
32
33
  :db => $db,
33
34
  :datarow => true,
34
35
  :require => false,
36
+ :array_enum => true,
35
37
  :models => {
36
38
  :User => {
37
39
  :cache_ids => true
@@ -50,6 +52,7 @@ describe "Objects" do
50
52
  raise "Expected user-ID-cache to be 5 but it wasnt: #{$ob.ids_cache[:User].length}" if $ob.ids_cache[:User].length != 5
51
53
 
52
54
  user = $ob.get(:User, 4)
55
+ raise "No user returned." if !user
53
56
  $ob.delete(user)
54
57
  raise "Expected user-ID-cache to be 4 but it wasnt: #{$ob.ids_cache[:User].length} #{$ob.ids_cache}" if $ob.ids_cache[:User].length != 4
55
58
 
@@ -57,6 +60,11 @@ describe "Objects" do
57
60
  raise "Expected user-ID-cache to be 2 but it wasnt: #{$ob.ids_cache[:User].length} #{$ob.ids_cache}" if $ob.ids_cache[:User].length != 2
58
61
  end
59
62
 
63
+ it "should be able to do 'select_col_as_array'" do
64
+ res = $ob.list(:User, {"select_col_as_array" => "id"}).to_a
65
+ raise "Expected length of 2 but got: #{res.length}" if res.length != 2
66
+ end
67
+
60
68
  it "should work even though stressed by threads (thread-safe)." do
61
69
  userd = []
62
70
  10.upto(25) do |i|
@@ -64,7 +72,7 @@ describe "Objects" do
64
72
  end
65
73
 
66
74
  $ob.adds(:User, userd)
67
- users = $ob.list(:User)
75
+ users = $ob.list(:User).to_a
68
76
 
69
77
  #Stress it to test threadsafety...
70
78
  threads = []
@@ -72,16 +80,23 @@ describe "Objects" do
72
80
  threads << Knj::Thread.new do
73
81
  0.upto(10) do |ic|
74
82
  user = $ob.add(:User, {:username => "User #{tc}-#{ic}"})
83
+ raise "No user returned." if !user
75
84
  $ob.delete(user)
76
85
 
77
86
  user1 = $ob.add(:User, {:username => "User #{tc}-#{ic}-1"})
78
87
  user2 = $ob.add(:User, {:username => "User #{tc}-#{ic}-2"})
79
88
  user3 = $ob.add(:User, {:username => "User #{tc}-#{ic}-3"})
89
+
90
+ raise "Missing user?" if !user1 or !user2 or !user3 or user1.deleted? or user2.deleted? or user3.deleted?
80
91
  $ob.deletes([user1, user2, user3])
81
92
 
93
+ count = 0
82
94
  users.each do |user|
95
+ count += 1
83
96
  user[:username] = "#{user[:username]}." if !user.deleted?
84
97
  end
98
+
99
+ raise "Expected at least 15 users but got #{count}." if count != 18
85
100
  end
86
101
  end
87
102
  end
@@ -104,7 +119,8 @@ describe "Objects" do
104
119
  0.upto(10) do
105
120
  threads << Knj::Thread.new do
106
121
  0.upto(15) do
107
- $ob2.add(:Group, {:groupname => "User 1"}, {:skip_ret => true})
122
+ ret = $ob2.add(:Group, {:groupname => "User 1"}, {:skip_ret => true})
123
+ raise "Expected empty return but got something: #{ret}" if ret
108
124
  end
109
125
  end
110
126
  end
@@ -169,10 +185,14 @@ describe "Objects" do
169
185
  "name" => "Test project"
170
186
  })
171
187
 
188
+ count = 0
172
189
  $db.q("SELECT * FROM Project") do |d|
173
190
  raise "Somehow name was not 'Test project'" if d[:name] != "Test project"
174
191
  raise "ID was not set?" if d[:id].to_i <= 0
192
+ count += 1
175
193
  end
194
+
195
+ raise "Expected count of 1 but it wasnt: #{count}" if count != 1
176
196
  end
177
197
 
178
198
  it "should be able to automatic generate methods on datarow-classes (has_many, has_one)." do
@@ -267,6 +287,31 @@ describe "Objects" do
267
287
  raise "Unexpected person_html from task (should have been '[no person]')." if task.person_html != "[no person]"
268
288
  end
269
289
 
290
+ it "should be able to to multiple additions and delete objects through a buffer" do
291
+ objs = []
292
+ 0.upto(10000) do
293
+ objs << {:name => :Kasper}
294
+ end
295
+
296
+ $ob.adds(:Person, objs)
297
+ pers_length = $ob.list(:Person, "count" => true)
298
+
299
+ count = 0
300
+ $db.q_buffer do |buffer|
301
+ $ob.list(:Person) do |person|
302
+ count += 1
303
+ $ob.delete(person, :db_buffer => buffer)
304
+ end
305
+
306
+ buffer.flush
307
+ end
308
+
309
+ raise "Expected count to be #{pers_length} but it wasnt: #{count}" if count != pers_length
310
+
311
+ persons = $ob.list(:Person).to_a
312
+ raise "Expected persons count to be 0 but it wasnt: #{persons.map{|e| e.data} }" if persons.length > 0
313
+ end
314
+
270
315
  it "should delete the temp database again." do
271
316
  db_path = "#{Knj::Os.tmpdir}/knjrbfw_test_sqlite3.sqlite3"
272
317
  File.unlink(db_path) if File.exists?(db_path)
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: knjrbfw
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.52
5
+ version: 0.0.53
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kasper Johansen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-06-29 00:00:00 +02:00
13
+ date: 2012-07-04 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -379,7 +379,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
379
379
  requirements:
380
380
  - - ">="
381
381
  - !ruby/object:Gem::Version
382
- hash: -865176145479195506
382
+ hash: -3998058168416114798
383
383
  segments:
384
384
  - 0
385
385
  version: "0"