baza 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +58 -13
  3. data/VERSION +1 -1
  4. data/baza.gemspec +15 -3
  5. data/include/db.rb +871 -865
  6. data/include/drivers/mysql/mysql.rb +104 -297
  7. data/include/drivers/mysql/mysql_column.rb +133 -0
  8. data/include/drivers/mysql/mysql_columns.rb +4 -127
  9. data/include/drivers/mysql/mysql_index.rb +76 -0
  10. data/include/drivers/mysql/mysql_indexes.rb +0 -73
  11. data/include/drivers/mysql/mysql_result.rb +42 -0
  12. data/include/drivers/mysql/mysql_result_java.rb +61 -0
  13. data/include/drivers/mysql/mysql_result_mysql2.rb +26 -0
  14. data/include/drivers/mysql/mysql_result_unbuffered.rb +72 -0
  15. data/include/drivers/mysql/mysql_sqlspecs.rb +1 -1
  16. data/include/drivers/mysql/mysql_table.rb +361 -0
  17. data/include/drivers/mysql/mysql_tables.rb +23 -381
  18. data/include/drivers/sqlite3/libknjdb_java_sqlite3.rb +17 -22
  19. data/include/drivers/sqlite3/libknjdb_sqlite3_ironruby.rb +13 -13
  20. data/include/drivers/sqlite3/sqlite3.rb +39 -105
  21. data/include/drivers/sqlite3/sqlite3_column.rb +146 -0
  22. data/include/drivers/sqlite3/sqlite3_columns.rb +17 -149
  23. data/include/drivers/sqlite3/sqlite3_index.rb +55 -0
  24. data/include/drivers/sqlite3/sqlite3_indexes.rb +0 -52
  25. data/include/drivers/sqlite3/sqlite3_result.rb +35 -0
  26. data/include/drivers/sqlite3/sqlite3_result_java.rb +39 -0
  27. data/include/drivers/sqlite3/sqlite3_table.rb +399 -0
  28. data/include/drivers/sqlite3/sqlite3_tables.rb +7 -403
  29. data/include/idquery.rb +19 -19
  30. data/include/model.rb +139 -139
  31. data/include/model_handler_sqlhelper.rb +74 -74
  32. data/spec/support/driver_columns_collection.rb +17 -0
  33. metadata +14 -2
@@ -30,14 +30,14 @@ class Baza::Driver::Sqlite3::Tables
30
30
  ret = {} unless block_given?
31
31
 
32
32
  @list_mutex.synchronize do
33
- q_tables = @db.select("sqlite_master", {"type" => "table"}, {:orderby => "name"}) do |d_tables|
33
+ q_tables = @db.select("sqlite_master", {"type" => "table"}, {orderby: "name"}) do |d_tables|
34
34
  next if d_tables[:name] == "sqlite_sequence"
35
35
 
36
36
  tname = d_tables[:name].to_sym
37
37
  obj = @list.get!(tname)
38
38
 
39
39
  if !obj
40
- obj = Baza::Driver::Sqlite3::Tables::Table.new(
40
+ obj = Baza::Driver::Sqlite3::Table.new(
41
41
  db: @db,
42
42
  data: d_tables,
43
43
  tables: self
@@ -77,7 +77,7 @@ class Baza::Driver::Sqlite3::Tables
77
77
  CREATE_ALLOWED_KEYS = [:indexes, :columns]
78
78
  def create(name, data, args = nil)
79
79
  data.each do |key, val|
80
- raise "Invalid key: '#{key}' (#{key.class.name})." if !CREATE_ALLOWED_KEYS.include?(key)
80
+ raise "Invalid key: '#{key}' (#{key.class.name})." unless CREATE_ALLOWED_KEYS.include?(key)
81
81
  end
82
82
 
83
83
  sql = "CREATE TABLE `#{name}` ("
@@ -91,382 +91,19 @@ class Baza::Driver::Sqlite3::Tables
91
91
 
92
92
  sql << ")"
93
93
 
94
- if args and args[:return_sql]
94
+ if args && args[:return_sql]
95
95
  ret = [sql]
96
96
  else
97
97
  @db.query(sql)
98
98
  end
99
99
 
100
- if data.key?(:indexes) and data[:indexes]
100
+ if data.key?(:indexes) && data[:indexes]
101
101
  table_obj = self[name]
102
102
 
103
- if args and args[:return_sql]
104
- ret += table_obj.create_indexes(data[:indexes], :return_sql => true)
105
- else
106
- table_obj.create_indexes(data[:indexes])
107
- end
108
- end
109
-
110
- if args and args[:return_sql]
111
- return ret
112
- else
113
- return nil
114
- end
115
- end
116
- end
117
-
118
- class Baza::Driver::Sqlite3::Tables::Table
119
- attr_reader :name, :type
120
-
121
- def initialize(args)
122
- @db = args[:db]
123
- @data = args[:data]
124
- @name = @data[:name].to_sym
125
- @type = @data[:type].to_sym
126
- @tables = args[:tables]
127
-
128
- @list = Wref_map.new
129
- @indexes_list = Wref_map.new
130
- end
131
-
132
- def maxlength
133
- return @data[:maxlength]
134
- end
135
-
136
- def reload
137
- @data = @db.select("sqlite_master", {type: "table", name: name}, {:orderby => "name"}).fetch
138
- end
139
-
140
- def rows_count
141
- data = @db.q("SELECT COUNT(*) AS count FROM `#{name}`").fetch
142
- return data[:count].to_i
143
- end
144
-
145
- #Drops the table from the database.
146
- def drop
147
- raise "Cant drop native table: '#{name}'." if native?
148
- @db.query("DROP TABLE `#{name}`")
149
- @tables.remove_from_list(self) if @tables.exists_in_list?(self)
150
- end
151
-
152
- #Returns true if the table is safe to drop.
153
- def native?
154
- return true if name.to_s == "sqlite_sequence"
155
- return false
156
- end
157
-
158
- def optimize
159
- # Not possible in SQLite3.
160
- end
161
-
162
- def rename(newname)
163
- newname = newname.to_sym
164
-
165
- @tables.remove_from_list(self)
166
- newtable = clone(newname)
167
- @db.tables.remove_from_list(newtable)
168
- drop
169
- @data[:name] = newname
170
- @name = newname
171
- @tables.add_to_list(self)
172
-
173
- #Rename table on all columns and indexes.
174
- @list.each do |name, column|
175
- column.args[:table_name] = newname
176
- end
177
-
178
- @indexes_list.each do |name, index|
179
- index.args[:table_name] = newname
180
- end
181
- end
182
-
183
- def truncate
184
- @db.query("DELETE FROM `#{name}` WHERE 1=1")
185
- return nil
186
- end
187
-
188
- def table
189
- return @db.tables[@table_name]
190
- end
191
-
192
- def column(name)
193
- list = self.columns
194
- return list[name] if list[name]
195
- raise Errno::ENOENT.new("Column not found: #{name}.")
196
- end
197
-
198
- def columns
199
- @db.cols
200
- ret = {}
201
-
202
- @db.q("PRAGMA table_info(`#{@db.esc_table(self.name)}`)") do |d_cols|
203
- name = d_cols[:name].to_sym
204
- obj = @list.get!(name)
205
-
206
- if !obj
207
- obj = Baza::Driver::Sqlite3::Columns::Column.new(
208
- table_name: self.name,
209
- db: @db,
210
- data: d_cols
211
- )
212
- @list[name] = obj
213
- end
214
-
215
- if block_given?
216
- yield(obj)
217
- else
218
- ret[name] = obj
219
- end
220
- end
221
-
222
- if block_given?
223
- return nil
224
- else
225
- return ret
226
- end
227
- end
228
-
229
- def create_columns(col_arr)
230
- col_arr.each do |col_data|
231
- #if col_data.key?("after")
232
- # self.create_column_programmatic(col_data)
233
- #else
234
- @db.query("ALTER TABLE `#{self.name}` ADD COLUMN #{@db.cols.data_sql(col_data)};")
235
- #end
236
- end
237
- end
238
-
239
- def create_column_programmatic(col_data)
240
- temp_name = "temptable_#{Time.now.to_f.to_s.hash}"
241
- cloned_tabled = self.clone(temp_name)
242
- cols_cur = self.columns
243
- @db.query("DROP TABLE `#{self.name}`")
244
-
245
- sql = "CREATE TABLE `#{self.name}` ("
246
- first = true
247
- cols_cur.each do |name, col|
248
- sql << ", " if !first
249
- first = false if first
250
- sql << @db.cols.data_sql(col.data)
251
-
252
- if col_data[:after] and col_data[:after] == name
253
- sql << ", #{@db.cols.data_sql(col_data)}"
254
- end
255
- end
256
- sql << ");"
257
- @db.query(sql)
258
-
259
- sql = "INSERT INTO `#{self.name}` SELECT "
260
- first = true
261
- cols_cur.each do |name, col|
262
- sql << ", " if !first
263
- first = false if first
264
-
265
- sql << "`#{name}`"
266
-
267
- if col_data[:after] and col_data[:after] == name
268
- sql << ", ''"
269
- end
270
- end
271
- sql << " FROM `#{temp_name}`"
272
- @db.query(sql)
273
- @db.query("DROP TABLE `#{temp_name}`")
274
- end
275
-
276
- def clone(newname, args = nil)
277
- raise "Invalid name." if newname.to_s.strip.length <= 0
278
-
279
- sql = "CREATE TABLE `#{newname}` ("
280
- first = true
281
- columns.each do |name, col|
282
- sql << ", " unless first
283
- first = false if first
284
- sql << @db.cols.data_sql(col.data)
285
- end
286
-
287
- sql << ");"
288
- @db.query(sql)
289
- @db.query("INSERT INTO `#{newname}` SELECT * FROM `#{name}`")
290
-
291
- indexes_to_create = []
292
- new_table = @db.tables[newname.to_sym]
293
- indexes.each do |name, index|
294
- index_name = name.to_s
295
-
296
- if @db.opts[:index_append_table_name] && match = index_name.match(/\A(.+?)__(.+)\Z/)
297
- index_name = match[2]
298
- end
299
-
300
- create_data = index.data
301
- create_data[:name] = index_name
302
-
303
- indexes_to_create << create_data
304
- end
305
-
306
- new_table.create_indexes(indexes_to_create)
307
-
308
- if args && args[:return_table] == false
309
- return nil
310
- else
311
- return new_table
312
- end
313
- end
314
-
315
- def copy(args = {})
316
- temp_name = "temptable_#{Time.now.to_f.to_s.hash}"
317
- cloned_tabled = self.clone(temp_name)
318
- cols_cur = self.columns
319
- @db.query("DROP TABLE `#{self.name}`")
320
-
321
- sql = "CREATE TABLE `#{self.name}` ("
322
- first = true
323
- cols_cur.each do |name, col|
324
- next if args[:drops] and args[:drops].index(name) != nil
325
-
326
- sql << ", " if !first
327
- first = false if first
328
-
329
- if args.key?(:alter_columns) and args[:alter_columns][name.to_sym]
330
- sql << @db.cols.data_sql(args[:alter_columns][name.to_sym])
331
- else
332
- sql << @db.cols.data_sql(col.data)
333
- end
334
-
335
- if args[:new]
336
- args[:new].each do |col_data|
337
- if col_data[:after] and col_data[:after] == name
338
- sql << ", #{@db.cols.data_sql(col_data)}"
339
- end
340
- end
341
- end
342
- end
343
- sql << ");"
344
- @db.query(sql)
345
-
346
- sql = "INSERT INTO `#{self.name}` SELECT "
347
- first = true
348
- cols_cur.each do |name, col|
349
- next if args[:drops] and args[:drops].index(name) != nil
350
-
351
- sql << ", " if !first
352
- first = false if first
353
-
354
- sql << "`#{name}`"
355
-
356
- if args[:news]
357
- args[:news].each do |col_data|
358
- if col_data[:after] and col_data[:after] == name
359
- sql << ", ''"
360
- end
361
- end
362
- end
363
- end
364
-
365
- sql << " FROM `#{temp_name}`"
366
- @db.query(sql)
367
- @db.query("DROP TABLE `#{temp_name}`")
368
- end
369
-
370
- def index index_name
371
- index_name = index_name.to_sym
372
-
373
- begin
374
- return @indexes_list[index_name]
375
- rescue Wref::Recycled
376
- if @db.opts[:index_append_table_name]
377
- tryname = "#{name}__#{index_name}"
378
-
379
- begin
380
- return @indexes_list[tryname]
381
- rescue Wref::Recycled
382
- #ignore.
383
- end
384
- end
385
- end
386
-
387
- indexes do |index|
388
- if index.name.to_s == "#{name}__#{index_name}"
389
- return index
390
- end
391
-
392
- return index if index.name.to_s == index_name.to_s
393
- end
394
-
395
- raise Errno::ENOENT, "Index not found: #{index_name}."
396
- end
397
-
398
- def indexes
399
- @db.indexes
400
- ret = {} unless block_given?
401
-
402
- @db.q("PRAGMA index_list(`#{@db.esc_table(name)}`)") do |d_indexes|
403
- next if d_indexes[:Key_name] == "PRIMARY"
404
- obj = @indexes_list.get!(d_indexes[:name])
405
-
406
- unless obj
407
- obj = Baza::Driver::Sqlite3::Indexes::Index.new(
408
- table_name: name,
409
- db: @db,
410
- data: d_indexes
411
- )
412
-
413
- @indexes_list[d_indexes[:name].to_sym] = obj
414
-
415
- # Get columns from index.
416
- index_master_data = @db.single(:sqlite_master, type: "index", name: d_indexes[:name])
417
- parse_columns_from_sql(index_master_data[:sql]).each do |column|
418
- obj.columns << column
419
- end
420
- end
421
-
422
- if block_given?
423
- yield(obj)
424
- else
425
- ret[d_indexes[:name].to_sym] = obj
426
- end
427
- end
428
-
429
- if block_given?
430
- return nil
431
- else
432
- return ret
433
- end
434
- end
435
-
436
- def create_indexes(index_arr, args = nil)
437
- if args && args[:return_sql]
438
- ret = []
439
- end
440
-
441
- index_arr.each do |index_data|
442
- if index_data.is_a?(String) or index_data.is_a?(Symbol)
443
- index_data = {name: index_data, columns: [index_data]}
444
- end
445
-
446
- raise "No name was given in data: '#{index_data}'." if !index_data.key?(:name) or index_data[:name].to_s.strip.empty?
447
- raise "No columns was given on index #{index_data[:name]}." if index_data[:columns].empty?
448
-
449
- name = index_data[:name]
450
- name = "#{self.name}__#{name}" if @db.opts[:index_append_table_name]
451
-
452
- sql = "CREATE"
453
- sql << " UNIQUE" if index_data[:unique]
454
- sql << " INDEX '#{@db.esc_col(name)}' ON `#{@db.esc_table(self.name)}` ("
455
-
456
- first = true
457
- index_data[:columns].each do |col_name|
458
- sql << ", " if !first
459
- first = false if first
460
-
461
- sql << "`#{@db.esc_col(col_name)}`"
462
- end
463
-
464
- sql << ")"
465
-
466
103
  if args && args[:return_sql]
467
- ret << sql
104
+ ret += table_obj.create_indexes(data[:indexes], return_sql: true)
468
105
  else
469
- @db.query(sql)
106
+ table_obj.create_indexes(data[:indexes])
470
107
  end
471
108
  end
472
109
 
@@ -476,37 +113,4 @@ class Baza::Driver::Sqlite3::Tables::Table
476
113
  return nil
477
114
  end
478
115
  end
479
-
480
- def data
481
- ret = {
482
- name: name,
483
- columns: [],
484
- indexes: []
485
- }
486
-
487
- columns.each do |name, column|
488
- ret[:columns] << column.data
489
- end
490
-
491
- indexes.each do |name, index|
492
- ret[:indexes] << index.data if name != "PRIMARY"
493
- end
494
-
495
- return ret
496
- end
497
-
498
- def insert(data)
499
- @db.insert(self.name, data)
500
- end
501
-
502
- def to_s
503
- "#<Baza::Driver::Sqlite3::Table name: \"#{name}\">"
504
- end
505
-
506
- private
507
-
508
- def parse_columns_from_sql sql
509
- columns_sql = sql.match(/\((.+?)\)\Z/)[1]
510
- return columns_sql.split(",").map{ |column| column[1, column.length - 2] }
511
- end
512
116
  end
data/include/idquery.rb CHANGED
@@ -2,10 +2,10 @@
2
2
  class Baza::Idquery
3
3
  #An array containing all the IDs that will be looked up.
4
4
  attr_reader :ids
5
-
5
+
6
6
  #Constructor.
7
7
  #===Examples
8
- # idq = Baza::Idquery(:db => db, :table => :users)
8
+ # idq = Baza::Idquery(db: db, table: :users)
9
9
  # idq.ids + [1, 5, 9]
10
10
  # idq.each do |user|
11
11
  # print "Name: #{user[:name]}\n"
@@ -14,11 +14,11 @@ class Baza::Idquery
14
14
  @args = args
15
15
  @ids = []
16
16
  @debug = @args[:debug]
17
-
17
+
18
18
  if @args[:query]
19
19
  @args[:db].q(@args[:query]) do |data|
20
20
  @args[:col] = data.keys.first if !@args[:col]
21
-
21
+
22
22
  if data.is_a?(Array)
23
23
  @ids << data.first
24
24
  else
@@ -26,43 +26,43 @@ class Baza::Idquery
26
26
  end
27
27
  end
28
28
  end
29
-
29
+
30
30
  @args[:col] = :id if !@args[:col]
31
31
  @args[:size] = 200 if !@args[:size]
32
-
32
+
33
33
  if block
34
34
  raise "No query was given but a block was." if !@args[:query]
35
35
  self.each(&block)
36
36
  end
37
37
  end
38
-
38
+
39
39
  #Fetches results.
40
40
  #===Examples
41
41
  # data = idq.fetch #=> Hash
42
42
  def fetch
43
43
  return nil if !@args
44
-
44
+
45
45
  if @res
46
46
  data = @res.fetch if @res
47
47
  @res = nil if !data
48
48
  return data if data
49
49
  end
50
-
50
+
51
51
  @res = new_res if !@res
52
52
  if !@res
53
53
  destroy
54
54
  return nil
55
55
  end
56
-
56
+
57
57
  data = @res.fetch
58
58
  if !data
59
59
  destroy
60
60
  return nil
61
61
  end
62
-
62
+
63
63
  return data
64
64
  end
65
-
65
+
66
66
  #Yields a block for every result.
67
67
  #===Examples
68
68
  # idq.each do |data|
@@ -73,33 +73,33 @@ class Baza::Idquery
73
73
  yield(data)
74
74
  end
75
75
  end
76
-
76
+
77
77
  private
78
-
78
+
79
79
  #Spawns a new database-result to read from.
80
80
  def new_res
81
81
  table_esc = "`#{@args[:db].esc_table(@args[:table])}`"
82
82
  col_esc = "`#{@args[:db].esc_col(@args[:col])}`"
83
83
  ids = @ids.shift(@args[:size])
84
-
84
+
85
85
  if ids.empty?
86
86
  destroy
87
87
  return nil
88
88
  end
89
-
89
+
90
90
  ids_sql = Knj::ArrayExt.join(
91
91
  :arr => ids,
92
92
  :callback => proc{|val| @args[:db].esc(val)},
93
93
  :sep => ",",
94
94
  :surr => "'"
95
95
  )
96
-
96
+
97
97
  query_str = "SELECT * FROM #{table_esc} WHERE #{table_esc}.#{col_esc} IN (#{ids_sql})"
98
98
  print "Query: #{query_str}\n" if @debug
99
-
99
+
100
100
  return @args[:db].q(query_str)
101
101
  end
102
-
102
+
103
103
  #Removes all variables on the object. This is done when no more results are available.
104
104
  def destroy
105
105
  @args = nil