baza 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ class Baza::Driver::Sqlite3::Tables
10
10
  end
11
11
 
12
12
  def [](table_name)
13
- table_name = table_name.to_s
13
+ table_name = table_name.to_sym
14
14
 
15
15
  begin
16
16
  ret = @list[table_name]
@@ -20,7 +20,7 @@ class Baza::Driver::Sqlite3::Tables
20
20
  end
21
21
 
22
22
  self.list do |table_obj|
23
- return table_obj if table_obj.name.to_s == table_name.to_s
23
+ return table_obj if table_obj.name == table_name
24
24
  end
25
25
 
26
26
  raise Errno::ENOENT, "Table was not found: #{table_name}."
@@ -30,23 +30,25 @@ 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
- obj = @list.get!(d_tables[:name])
36
+ tname = d_tables[:name].to_sym
37
+ obj = @list.get!(tname)
37
38
 
38
39
  if !obj
39
40
  obj = Baza::Driver::Sqlite3::Tables::Table.new(
40
41
  :db => @db,
41
- :data => d_tables
42
+ :data => d_tables,
43
+ :tables => self
42
44
  )
43
- @list[d_tables[:name]] = obj
45
+ @list[tname] = obj
44
46
  end
45
47
 
46
48
  if block_given?
47
49
  yield(obj)
48
50
  else
49
- ret[d_tables[:name]] = obj
51
+ ret[tname] = obj
50
52
  end
51
53
  end
52
54
  end
@@ -58,11 +60,25 @@ class Baza::Driver::Sqlite3::Tables
58
60
  end
59
61
  end
60
62
 
63
+ def remove_from_list(table)
64
+ @list.delete(table.name)
65
+ end
66
+
67
+ def add_to_list(table)
68
+ raise "Already exists: '#{table.name}'." if @list.key?(table.name) and @list[table.name].__id__ != table.__id__
69
+ @list[table.name] = table
70
+ end
71
+
72
+ CREATE_ALLOWED_KEYS = [:indexes, :columns]
61
73
  def create(name, data, args = nil)
74
+ data.each do |key, val|
75
+ raise "Invalid key: '#{key}' (#{key.class.name})." if !CREATE_ALLOWED_KEYS.include?(key)
76
+ end
77
+
62
78
  sql = "CREATE TABLE `#{name}` ("
63
79
 
64
80
  first = true
65
- data["columns"].each do |col_data|
81
+ data[:columns].each do |col_data|
66
82
  sql << ", " if !first
67
83
  first = false if first
68
84
  sql << @db.cols.data_sql(col_data)
@@ -76,13 +92,13 @@ class Baza::Driver::Sqlite3::Tables
76
92
  @db.query(sql)
77
93
  end
78
94
 
79
- if data.key?("indexes") and data["indexes"]
95
+ if data.key?(:indexes) and data[:indexes]
80
96
  table_obj = self[name]
81
97
 
82
98
  if args and args[:return_sql]
83
- ret += table_obj.create_indexes(data["indexes"], :return_sql => true)
99
+ ret += table_obj.create_indexes(data[:indexes], :return_sql => true)
84
100
  else
85
- table_obj.create_indexes(data["indexes"])
101
+ table_obj.create_indexes(data[:indexes])
86
102
  end
87
103
  end
88
104
 
@@ -95,28 +111,25 @@ class Baza::Driver::Sqlite3::Tables
95
111
  end
96
112
 
97
113
  class Baza::Driver::Sqlite3::Tables::Table
114
+ attr_reader :name, :type
115
+
98
116
  def initialize(args)
99
117
  @db = args[:db]
100
118
  @data = args[:data]
119
+ @name = @data[:name].to_sym
120
+ @type = @data[:type].to_sym
121
+ @tables = args[:tables]
101
122
 
102
123
  @list = Wref_map.new
103
124
  @indexes_list = Wref_map.new
104
125
  end
105
126
 
106
- def name
107
- return @data[:name]
108
- end
109
-
110
- def type
111
- return @data[:type]
112
- end
113
-
114
127
  def maxlength
115
128
  return @data[:maxlength]
116
129
  end
117
130
 
118
131
  def reload
119
- @data = @db.select("sqlite_master", {"type" => "table", "name" => self.name}, {"orderby" => "name"}).fetch
132
+ @data = @db.select("sqlite_master", {"type" => "table", "name" => self.name}, {:orderby => "name"}).fetch
120
133
  end
121
134
 
122
135
  def rows_count
@@ -128,6 +141,7 @@ class Baza::Driver::Sqlite3::Tables::Table
128
141
  def drop
129
142
  raise "Cant drop native table: '#{self.name}'." if self.native?
130
143
  @db.query("DROP TABLE `#{self.name}`")
144
+ @tables.remove_from_list(self)
131
145
  end
132
146
 
133
147
  #Returns true if the table is safe to drop.
@@ -141,8 +155,20 @@ class Baza::Driver::Sqlite3::Tables::Table
141
155
  end
142
156
 
143
157
  def rename(newname)
144
- self.clone(newname)
158
+ newname = newname.to_sym
159
+
160
+ @tables.remove_from_list(self)
161
+ self.clone(newname, :return_table => false)
145
162
  self.drop
163
+ @data[:name] = newname
164
+ @name = newname
165
+ @tables.add_to_list(self)
166
+
167
+ #Rename table on all columns.
168
+ #FIXME: This should only be done for columns that exists in memory. However a reference to the table should not be set, at this would force the table to stay in memory, when the column is still referenced...
169
+ self.columns.each do |name, col|
170
+ col.args[:table_name] = newname
171
+ end
146
172
  end
147
173
 
148
174
  def truncate
@@ -165,7 +191,8 @@ class Baza::Driver::Sqlite3::Tables::Table
165
191
  ret = {}
166
192
 
167
193
  @db.q("PRAGMA table_info(`#{@db.esc_table(self.name)}`)") do |d_cols|
168
- obj = @list.get!(d_cols[:name])
194
+ name = d_cols[:name].to_sym
195
+ obj = @list.get!(name)
169
196
 
170
197
  if !obj
171
198
  obj = Baza::Driver::Sqlite3::Columns::Column.new(
@@ -173,13 +200,13 @@ class Baza::Driver::Sqlite3::Tables::Table
173
200
  :db => @db,
174
201
  :data => d_cols
175
202
  )
176
- @list[d_cols[:name]] = obj
203
+ @list[name] = obj
177
204
  end
178
205
 
179
206
  if block_given?
180
207
  yield(obj)
181
208
  else
182
- ret[d_cols[:name]] = obj
209
+ ret[name] = obj
183
210
  end
184
211
  end
185
212
 
@@ -213,7 +240,7 @@ class Baza::Driver::Sqlite3::Tables::Table
213
240
  first = false if first
214
241
  sql << @db.cols.data_sql(col.data)
215
242
 
216
- if col_data["after"] and col_data["after"] == name
243
+ if col_data[:after] and col_data[:after] == name
217
244
  sql << ", #{@db.cols.data_sql(col_data)}"
218
245
  end
219
246
  end
@@ -228,7 +255,7 @@ class Baza::Driver::Sqlite3::Tables::Table
228
255
 
229
256
  sql << "`#{name}`"
230
257
 
231
- if col_data["after"] and col_data["after"] == name
258
+ if col_data[:after] and col_data[:after] == name
232
259
  sql << ", ''"
233
260
  end
234
261
  end
@@ -237,7 +264,7 @@ class Baza::Driver::Sqlite3::Tables::Table
237
264
  @db.query("DROP TABLE `#{temp_name}`")
238
265
  end
239
266
 
240
- def clone(newname)
267
+ def clone(newname, args = nil)
241
268
  raise "Invalid name." if newname.to_s.strip.length <= 0
242
269
  cols_cur = self.columns
243
270
 
@@ -254,7 +281,12 @@ class Baza::Driver::Sqlite3::Tables::Table
254
281
 
255
282
  sql = "INSERT INTO `#{newname}` SELECT * FROM `#{self.name}`"
256
283
  @db.query(sql)
257
- return @db.tables[newname]
284
+
285
+ if args and args[:return_table] == false
286
+ return nil
287
+ else
288
+ return @db.tables[newname]
289
+ end
258
290
  end
259
291
 
260
292
  def copy(args = {})
@@ -266,20 +298,20 @@ class Baza::Driver::Sqlite3::Tables::Table
266
298
  sql = "CREATE TABLE `#{self.name}` ("
267
299
  first = true
268
300
  cols_cur.each do |name, col|
269
- next if args["drops"] and args["drops"].index(name) != nil
301
+ next if args[:drops] and args[:drops].index(name) != nil
270
302
 
271
303
  sql << ", " if !first
272
304
  first = false if first
273
305
 
274
- if args.key?("alter_columns") and args["alter_columns"][name.to_s]
275
- sql << @db.cols.data_sql(args["alter_columns"][name.to_s])
306
+ if args.key?(:alter_columns) and args[:alter_columns][name.to_sym]
307
+ sql << @db.cols.data_sql(args[:alter_columns][name.to_sym])
276
308
  else
277
309
  sql << @db.cols.data_sql(col.data)
278
310
  end
279
311
 
280
- if args["new"]
281
- args["new"].each do |col_data|
282
- if col_data["after"] and col_data["after"] == name
312
+ if args[:new]
313
+ args[:new].each do |col_data|
314
+ if col_data[:after] and col_data[:after] == name
283
315
  sql << ", #{@db.cols.data_sql(col_data)}"
284
316
  end
285
317
  end
@@ -291,16 +323,16 @@ class Baza::Driver::Sqlite3::Tables::Table
291
323
  sql = "INSERT INTO `#{self.name}` SELECT "
292
324
  first = true
293
325
  cols_cur.each do |name, col|
294
- next if args["drops"] and args["drops"].index(name) != nil
326
+ next if args[:drops] and args[:drops].index(name) != nil
295
327
 
296
328
  sql << ", " if !first
297
329
  first = false if first
298
330
 
299
331
  sql << "`#{name}`"
300
332
 
301
- if args["news"]
302
- args["news"].each do |col_data|
303
- if col_data["after"] and col_data["after"] == name
333
+ if args[:news]
334
+ args[:news].each do |col_data|
335
+ if col_data[:after] and col_data[:after] == name
304
336
  sql << ", ''"
305
337
  end
306
338
  end
@@ -313,7 +345,7 @@ class Baza::Driver::Sqlite3::Tables::Table
313
345
  end
314
346
 
315
347
  def index(name)
316
- name = name.to_s
348
+ name = name.to_sym
317
349
 
318
350
  begin
319
351
  return @indexes_list[name]
@@ -352,12 +384,12 @@ class Baza::Driver::Sqlite3::Tables::Table
352
384
  match_name = d_indexes[:name].match(/__(.+)$/)
353
385
 
354
386
  if match_name
355
- name = match_name[1]
387
+ name = match_name[1].to_sym
356
388
  else
357
- name = d_indexes[:name]
389
+ name = d_indexes[:name].to_sym
358
390
  end
359
391
  else
360
- name = d_indexes[:name]
392
+ name = d_indexes[:name].to_sym
361
393
  end
362
394
 
363
395
  obj = Baza::Driver::Sqlite3::Indexes::Index.new(
@@ -366,13 +398,13 @@ class Baza::Driver::Sqlite3::Tables::Table
366
398
  :data => d_indexes
367
399
  )
368
400
  obj.columns << name
369
- @indexes_list[d_indexes[:name]] = obj
401
+ @indexes_list[d_indexes[:name].to_sym] = obj
370
402
  end
371
403
 
372
404
  if block_given?
373
405
  yield(obj)
374
406
  else
375
- ret[d_indexes[:name]] = obj
407
+ ret[d_indexes[:name].to_sym] = obj
376
408
  end
377
409
  end
378
410
 
@@ -390,19 +422,19 @@ class Baza::Driver::Sqlite3::Tables::Table
390
422
 
391
423
  index_arr.each do |index_data|
392
424
  if index_data.is_a?(String)
393
- index_data = {"name" => index_data, "columns" => [index_data]}
425
+ index_data = {:name => index_data, :columns => [index_data]}
394
426
  end
395
427
 
396
- raise "No name was given." if !index_data.key?("name") or index_data["name"].strip.length <= 0
397
- raise "No columns was given on index #{index_data["name"]}." if index_data["columns"].empty?
428
+ raise "No name was given in data: '#{index_data}'." if !index_data.key?(:name) or index_data[:name].strip.empty?
429
+ raise "No columns was given on index #{index_data[:name]}." if index_data[:columns].empty?
398
430
 
399
- name = index_data["name"]
431
+ name = index_data[:name]
400
432
  name = "#{self.name}__#{name}" if @db.opts[:index_append_table_name]
401
433
 
402
434
  sql = "CREATE INDEX '#{@db.esc_col(name)}' ON `#{@db.esc_table(self.name)}` ("
403
435
 
404
436
  first = true
405
- index_data["columns"].each do |col_name|
437
+ index_data[:columns].each do |col_name|
406
438
  sql << ", " if !first
407
439
  first = false if first
408
440
 
@@ -427,17 +459,17 @@ class Baza::Driver::Sqlite3::Tables::Table
427
459
 
428
460
  def data
429
461
  ret = {
430
- "name" => name,
431
- "columns" => [],
432
- "indexes" => []
462
+ :name => name,
463
+ :columns => [],
464
+ :indexes => []
433
465
  }
434
466
 
435
467
  columns.each do |name, column|
436
- ret["columns"] << column.data
468
+ ret[:columns] << column.data
437
469
  end
438
470
 
439
471
  indexes.each do |name, index|
440
- ret["indexes"] << index.data if name != "PRIMARY"
472
+ ret[:indexes] << index.data if name != "PRIMARY"
441
473
  end
442
474
 
443
475
  return ret
data/include/dump.rb CHANGED
@@ -50,7 +50,7 @@ class Baza::Dump
50
50
 
51
51
  @table_obj = table_obj
52
52
  self.update_status
53
- print "Dumping table: '#{table_obj.name}'.\n" if @debug
53
+ puts "Dumping table: '#{table_obj.name}'." if @debug
54
54
  self.dump_table(io, table_obj)
55
55
  end
56
56
  end
@@ -62,8 +62,11 @@ class Baza::Dump
62
62
 
63
63
  #Dumps the given table into the given IO.
64
64
  def dump_table(io, table_obj)
65
+ create_data = table_obj.data
66
+ create_data.delete(:name)
67
+
65
68
  #Get SQL for creating table and add it to IO.
66
- sqls = @args[:db].tables.create(table_obj.name, table_obj.data, :return_sql => true)
69
+ sqls = @args[:db].tables.create(table_obj.name, create_data, :return_sql => true)
67
70
  sqls.each do |sql|
68
71
  io.write("#{sql};\n")
69
72
  end