baza 0.0.0 → 0.0.1
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/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/README.rdoc +106 -1
- data/VERSION +1 -1
- data/baza.gemspec +103 -0
- data/include/db.rb +108 -29
- data/include/drivers/mysql/mysql.rb +52 -47
- data/include/drivers/mysql/mysql_columns.rb +48 -41
- data/include/drivers/mysql/mysql_indexes.rb +2 -2
- data/include/drivers/mysql/mysql_tables.rb +91 -59
- data/include/drivers/sqlite3/sqlite3.rb +66 -45
- data/include/drivers/sqlite3/sqlite3_columns.rb +41 -39
- data/include/drivers/sqlite3/sqlite3_indexes.rb +2 -2
- data/include/drivers/sqlite3/sqlite3_tables.rb +86 -54
- data/include/dump.rb +5 -2
- data/include/revision.rb +130 -90
- data/spec/baza_spec.rb +312 -263
- data/spec/info_mysql_example.rb +6 -0
- data/spec/info_sqlite3.rb +20 -0
- metadata +22 -3
@@ -21,7 +21,7 @@ class Baza::Driver::Mysql::Tables
|
|
21
21
|
|
22
22
|
#Returns a table by the given table-name.
|
23
23
|
def [](table_name)
|
24
|
-
table_name = table_name.
|
24
|
+
table_name = table_name.to_sym
|
25
25
|
|
26
26
|
begin
|
27
27
|
return @list[table_name]
|
@@ -45,20 +45,22 @@ class Baza::Driver::Mysql::Tables
|
|
45
45
|
|
46
46
|
@list_mutex.synchronize do
|
47
47
|
@db.q(sql) do |d_tables|
|
48
|
-
|
48
|
+
name = d_tables[:Name].to_sym
|
49
|
+
obj = @list.get!(name)
|
49
50
|
|
50
51
|
if !obj
|
51
52
|
obj = Baza::Driver::Mysql::Tables::Table.new(
|
52
53
|
:db => @db,
|
53
|
-
:data => d_tables
|
54
|
+
:data => d_tables,
|
55
|
+
:tables => self
|
54
56
|
)
|
55
|
-
@list[
|
57
|
+
@list[name] = obj
|
56
58
|
end
|
57
59
|
|
58
60
|
if block_given?
|
59
61
|
yield(obj)
|
60
62
|
else
|
61
|
-
ret[
|
63
|
+
ret[name] = obj
|
62
64
|
end
|
63
65
|
end
|
64
66
|
end
|
@@ -70,25 +72,26 @@ class Baza::Driver::Mysql::Tables
|
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
75
|
+
CREATE_ALLOWED_KEYS = [:columns, :indexes, :temp, :return_sql]
|
73
76
|
#Creates a new table by the given name and data.
|
74
77
|
def create(name, data, args = nil)
|
75
|
-
raise "No columns was given for '#{name}'." if !data[
|
78
|
+
raise "No columns was given for '#{name}'." if !data[:columns] or data[:columns].empty?
|
76
79
|
|
77
80
|
sql = "CREATE"
|
78
|
-
sql << " TEMPORARY" if data[
|
81
|
+
sql << " TEMPORARY" if data[:temp]
|
79
82
|
sql << " TABLE `#{name}` ("
|
80
83
|
|
81
84
|
first = true
|
82
|
-
data[
|
85
|
+
data[:columns].each do |col_data|
|
83
86
|
sql << ", " if !first
|
84
87
|
first = false if first
|
85
|
-
col_data.delete(
|
88
|
+
col_data.delete(:after) if col_data[:after]
|
86
89
|
sql << @db.cols.data_sql(col_data)
|
87
90
|
end
|
88
91
|
|
89
|
-
if data[
|
92
|
+
if data[:indexes] and !data[:indexes].empty?
|
90
93
|
sql << ", "
|
91
|
-
sql << Baza::Driver::Mysql::Tables::Table.create_indexes(data[
|
94
|
+
sql << Baza::Driver::Mysql::Tables::Table.create_indexes(data[:indexes], {
|
92
95
|
:db => @db,
|
93
96
|
:return_sql => true,
|
94
97
|
:create => false,
|
@@ -102,10 +105,22 @@ class Baza::Driver::Mysql::Tables
|
|
102
105
|
return [sql] if args and args[:return_sql]
|
103
106
|
@db.query(sql)
|
104
107
|
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def add_to_list(table)
|
112
|
+
raise "Already exists: '#{table.name}'." if @list.key?(table.name) and @list[table.name].__id__ != table.__id__
|
113
|
+
@list[table.name] = table
|
114
|
+
end
|
115
|
+
|
116
|
+
def remove_from_list(table)
|
117
|
+
raise "Table not in list: '#{table.name}'." if !@list.key?(table.name)
|
118
|
+
@list.delete(table.name)
|
119
|
+
end
|
105
120
|
end
|
106
121
|
|
107
122
|
class Baza::Driver::Mysql::Tables::Table
|
108
|
-
attr_reader :list
|
123
|
+
attr_reader :list, :name
|
109
124
|
|
110
125
|
def initialize(args)
|
111
126
|
@args = args
|
@@ -114,6 +129,8 @@ class Baza::Driver::Mysql::Tables::Table
|
|
114
129
|
@subtype = @db.opts[:subtype]
|
115
130
|
@list = Wref_map.new
|
116
131
|
@indexes_list = Wref_map.new
|
132
|
+
@name = @data[:Name].to_sym
|
133
|
+
@tables = args[:tables]
|
117
134
|
|
118
135
|
raise "Could not figure out name from: '#{@data}'." if @data[:Name].to_s.strip.length <= 0
|
119
136
|
end
|
@@ -127,13 +144,11 @@ class Baza::Driver::Mysql::Tables::Table
|
|
127
144
|
return @data[:Name]
|
128
145
|
end
|
129
146
|
|
130
|
-
def name
|
131
|
-
return @data[:Name]
|
132
|
-
end
|
133
|
-
|
134
147
|
def drop
|
135
148
|
raise "Cant drop native table: '#{self.name}'." if self.native?
|
136
|
-
@db.query("DROP TABLE `#{self.name}`")
|
149
|
+
@db.query("DROP TABLE `#{@db.esc_table(self.name)}`")
|
150
|
+
@tables.__send__(:remove_from_list, self)
|
151
|
+
return nil
|
137
152
|
end
|
138
153
|
|
139
154
|
#Returns true if the table is safe to drop.
|
@@ -143,16 +158,16 @@ class Baza::Driver::Mysql::Tables::Table
|
|
143
158
|
end
|
144
159
|
|
145
160
|
def optimize
|
146
|
-
@db.query("OPTIMIZE TABLE `#{self.name}`")
|
161
|
+
@db.query("OPTIMIZE TABLE `#{@db.esc_table(self.name)}`")
|
147
162
|
return self
|
148
163
|
end
|
149
164
|
|
150
165
|
def rows_count
|
151
|
-
return @
|
166
|
+
return @db.q("SELECT COUNT(*) AS count FROM `#{@db.esc_table(self.name)}`").fetch[:count].to_i
|
152
167
|
end
|
153
168
|
|
154
169
|
def column(name)
|
155
|
-
name = name.
|
170
|
+
name = name.to_sym
|
156
171
|
|
157
172
|
if col = @list.get!(name)
|
158
173
|
return @list[name]
|
@@ -168,11 +183,12 @@ class Baza::Driver::Mysql::Tables::Table
|
|
168
183
|
def columns(args = nil)
|
169
184
|
@db.cols
|
170
185
|
ret = {}
|
171
|
-
sql = "SHOW FULL COLUMNS FROM `#{self.name}`"
|
186
|
+
sql = "SHOW FULL COLUMNS FROM `#{@db.esc_table(self.name)}`"
|
172
187
|
sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args and args.key?(:name)
|
173
188
|
|
174
189
|
@db.q(sql) do |d_cols|
|
175
|
-
|
190
|
+
name = d_cols[:Field].to_sym
|
191
|
+
obj = @list.get!(name)
|
176
192
|
|
177
193
|
if !obj
|
178
194
|
obj = Baza::Driver::Mysql::Columns::Column.new(
|
@@ -180,13 +196,13 @@ class Baza::Driver::Mysql::Tables::Table
|
|
180
196
|
:db => @db,
|
181
197
|
:data => d_cols
|
182
198
|
)
|
183
|
-
@list[
|
199
|
+
@list[name] = obj
|
184
200
|
end
|
185
201
|
|
186
202
|
if block_given?
|
187
203
|
yield(obj)
|
188
204
|
else
|
189
|
-
ret[
|
205
|
+
ret[name] = obj
|
190
206
|
end
|
191
207
|
end
|
192
208
|
|
@@ -201,7 +217,7 @@ class Baza::Driver::Mysql::Tables::Table
|
|
201
217
|
@db.indexes
|
202
218
|
ret = {}
|
203
219
|
|
204
|
-
sql = "SHOW INDEX FROM `#{self.name}`"
|
220
|
+
sql = "SHOW INDEX FROM `#{@db.esc_table(self.name)}`"
|
205
221
|
sql << " WHERE `Key_name` = '#{@db.esc(args[:name])}'" if args and args.key?(:name)
|
206
222
|
|
207
223
|
@db.q(sql) do |d_indexes|
|
@@ -248,9 +264,11 @@ class Baza::Driver::Mysql::Tables::Table
|
|
248
264
|
end
|
249
265
|
|
250
266
|
def create_columns(col_arr)
|
251
|
-
|
252
|
-
|
253
|
-
|
267
|
+
@db.transaction do
|
268
|
+
col_arr.each do |col_data|
|
269
|
+
sql = "ALTER TABLE `#{self.name}` ADD COLUMN #{@db.cols.data_sql(col_data)};"
|
270
|
+
@db.query(sql)
|
271
|
+
end
|
254
272
|
end
|
255
273
|
end
|
256
274
|
|
@@ -275,12 +293,12 @@ class Baza::Driver::Mysql::Tables::Table
|
|
275
293
|
sql << "CREATE"
|
276
294
|
end
|
277
295
|
|
278
|
-
if index_data.is_a?(String)
|
279
|
-
index_data = {
|
296
|
+
if index_data.is_a?(String) or index_data.is_a?(Symbol)
|
297
|
+
index_data = {:name => index_data, :columns => [index_data]}
|
280
298
|
end
|
281
299
|
|
282
|
-
raise "No name was given." if !index_data.key?(
|
283
|
-
raise "No columns was given on index: '#{index_data[
|
300
|
+
raise "No name was given: '#{index_data}'." if !index_data.key?(:name) or index_data[:name].to_s.strip.empty?
|
301
|
+
raise "No columns was given on index: '#{index_data[:name]}'." if !index_data[:columns] or index_data[:columns].empty?
|
284
302
|
|
285
303
|
if args[:return_sql]
|
286
304
|
if first
|
@@ -290,8 +308,8 @@ class Baza::Driver::Mysql::Tables::Table
|
|
290
308
|
end
|
291
309
|
end
|
292
310
|
|
293
|
-
sql << " UNIQUE" if index_data[
|
294
|
-
sql << " INDEX `#{db.esc_col(index_data[
|
311
|
+
sql << " UNIQUE" if index_data[:unique]
|
312
|
+
sql << " INDEX `#{db.esc_col(index_data[:name])}`"
|
295
313
|
|
296
314
|
if args[:on_table] or !args.key?(:on_table)
|
297
315
|
sql << " ON `#{db.esc_table(args[:table_name])}`"
|
@@ -300,7 +318,7 @@ class Baza::Driver::Mysql::Tables::Table
|
|
300
318
|
sql << " ("
|
301
319
|
|
302
320
|
first = true
|
303
|
-
index_data[
|
321
|
+
index_data[:columns].each do |col_name|
|
304
322
|
sql << ", " if !first
|
305
323
|
first = false if first
|
306
324
|
|
@@ -322,31 +340,35 @@ class Baza::Driver::Mysql::Tables::Table
|
|
322
340
|
end
|
323
341
|
|
324
342
|
def rename(newname)
|
343
|
+
newname = newname.to_sym
|
325
344
|
oldname = self.name
|
326
|
-
|
327
|
-
@
|
328
|
-
@db.
|
345
|
+
|
346
|
+
@tables.__send__(:remove_from_list, self)
|
347
|
+
@db.query("ALTER TABLE `#{@db.esc_table(oldname)}` RENAME TO `#{@db.esc_table(newname)}`")
|
348
|
+
|
329
349
|
@data[:Name] = newname
|
350
|
+
@name = newname
|
351
|
+
@tables.__send__(:add_to_list, self)
|
330
352
|
end
|
331
353
|
|
332
354
|
def truncate
|
333
|
-
@db.query("TRUNCATE `#{self.name}`")
|
355
|
+
@db.query("TRUNCATE `#{@db.esc_table(self.name)}`")
|
334
356
|
return self
|
335
357
|
end
|
336
358
|
|
337
359
|
def data
|
338
360
|
ret = {
|
339
|
-
|
340
|
-
|
341
|
-
|
361
|
+
:name => self.name,
|
362
|
+
:columns => [],
|
363
|
+
:indexes => []
|
342
364
|
}
|
343
365
|
|
344
366
|
columns.each do |name, column|
|
345
|
-
ret[
|
367
|
+
ret[:columns] << column.data
|
346
368
|
end
|
347
369
|
|
348
370
|
indexes.each do |name, index|
|
349
|
-
ret[
|
371
|
+
ret[:indexes] << index.data if name != "PRIMARY"
|
350
372
|
end
|
351
373
|
|
352
374
|
return ret
|
@@ -356,11 +378,6 @@ class Baza::Driver::Mysql::Tables::Table
|
|
356
378
|
@db.insert(self.name, data)
|
357
379
|
end
|
358
380
|
|
359
|
-
#Returns the current engine of the table.
|
360
|
-
def engine
|
361
|
-
return @data[:Engine]
|
362
|
-
end
|
363
|
-
|
364
381
|
def clone(newname, args = {})
|
365
382
|
raise "Invalid name." if newname.to_s.strip.empty?
|
366
383
|
|
@@ -374,19 +391,19 @@ class Baza::Driver::Mysql::Tables::Table
|
|
374
391
|
first = false if first
|
375
392
|
|
376
393
|
col_data = col.data
|
377
|
-
pkey_found = true if !pkey_found and col_data[
|
394
|
+
pkey_found = true if !pkey_found and col_data[:primarykey] and args[:force_single_pkey]
|
378
395
|
|
379
|
-
if args[:no_pkey] or (pkey_found and col_data[
|
380
|
-
col_data[
|
396
|
+
if args[:no_pkey] or (pkey_found and col_data[:primarykey] and args[:force_single_pkey])
|
397
|
+
col_data[:primarykey] = false
|
381
398
|
end
|
382
399
|
|
383
|
-
if col_data[
|
384
|
-
pkeys << col_data[
|
385
|
-
col_data.delete(
|
400
|
+
if col_data[:primarykey]
|
401
|
+
pkeys << col_data[:name]
|
402
|
+
col_data.delete(:primarykey)
|
386
403
|
end
|
387
404
|
|
388
405
|
if args[:all_cols_storage]
|
389
|
-
col_data[
|
406
|
+
col_data[:storage] = args[:all_cols_storage]
|
390
407
|
end
|
391
408
|
|
392
409
|
sql << @db.cols.data_sql(col_data)
|
@@ -410,14 +427,12 @@ class Baza::Driver::Mysql::Tables::Table
|
|
410
427
|
sql << " ENGINE=#{args[:engine]}" if args[:engine]
|
411
428
|
sql << ";"
|
412
429
|
|
413
|
-
puts sql
|
414
|
-
|
415
430
|
#Create table.
|
416
431
|
@db.query(sql)
|
417
432
|
|
418
433
|
|
419
434
|
#Insert data of previous data in a single query.
|
420
|
-
@db.query("INSERT INTO `#{newname}` SELECT * FROM `#{self.name}`")
|
435
|
+
@db.query("INSERT INTO `#{@db.esc_table(newname)}` SELECT * FROM `#{@db.esc_table(self.name)}`")
|
421
436
|
|
422
437
|
|
423
438
|
#Create indexes.
|
@@ -434,10 +449,27 @@ class Baza::Driver::Mysql::Tables::Table
|
|
434
449
|
return new_table
|
435
450
|
end
|
436
451
|
|
452
|
+
#Returns the current engine of the table.
|
453
|
+
def engine
|
454
|
+
return @data[:Engine]
|
455
|
+
end
|
456
|
+
|
437
457
|
#Changes the engine for a table.
|
438
458
|
def engine=(newengine)
|
439
459
|
raise "Invalid engine: '#{newengine}'." if !newengine.to_s.match(/^[A-z]+$/)
|
440
460
|
@db.query("ALTER TABLE `#{@db.esc_table(self.name)}` ENGINE = #{newengine}") if self.engine.to_s != newengine.to_s
|
441
461
|
@data[:Engine] = newengine
|
442
462
|
end
|
463
|
+
|
464
|
+
private
|
465
|
+
|
466
|
+
def remove_column_from_list(col)
|
467
|
+
raise "Column not found: '#{col.name}'." if !@list.key?(col.name)
|
468
|
+
@list.delete(col.name)
|
469
|
+
end
|
470
|
+
|
471
|
+
def add_column_to_list(col)
|
472
|
+
raise "Column already exists: '#{col.name}'." if @list.key?(col.name)
|
473
|
+
@list[col.name] = col
|
474
|
+
end
|
443
475
|
end
|
@@ -3,6 +3,21 @@ class Baza::Driver::Sqlite3
|
|
3
3
|
attr_reader :knjdb, :conn, :sep_table, :sep_col, :sep_val, :symbolize
|
4
4
|
attr_accessor :tables, :cols, :indexes
|
5
5
|
|
6
|
+
#Helper to enable automatic registering of database using Baza::Db.from_object
|
7
|
+
def self.from_object(args)
|
8
|
+
if args[:object].class.name == "SQLite3::Database"
|
9
|
+
return {
|
10
|
+
:type => :success,
|
11
|
+
:args => {
|
12
|
+
:type => "sqlite3",
|
13
|
+
:conn => args[:object]
|
14
|
+
}
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
return nil
|
19
|
+
end
|
20
|
+
|
6
21
|
#Constructor. This should not be called manually.
|
7
22
|
def initialize(knjdb_ob)
|
8
23
|
@sep_table = "`"
|
@@ -12,52 +27,51 @@ class Baza::Driver::Sqlite3
|
|
12
27
|
@knjdb = knjdb_ob
|
13
28
|
@path = @knjdb.opts[:path] if @knjdb.opts[:path]
|
14
29
|
@path = @knjdb.opts["path"] if @knjdb.opts["path"]
|
15
|
-
@symbolize = true if !@knjdb.opts.key?(:return_keys) or @knjdb.opts[:return_keys] == "symbols"
|
16
30
|
|
17
31
|
@knjdb.opts[:subtype] = "java" if !@knjdb.opts.key?(:subtype) and RUBY_ENGINE == "jruby"
|
18
|
-
raise "No path was given." if !@path
|
19
32
|
|
20
|
-
if @knjdb.opts[:
|
21
|
-
|
22
|
-
|
33
|
+
if @knjdb.opts[:conn]
|
34
|
+
@conn = @knjdb.opts[:conn]
|
35
|
+
else
|
36
|
+
raise "No path was given." if !@path
|
37
|
+
|
38
|
+
if @knjdb.opts[:subtype] == "java"
|
39
|
+
if @knjdb.opts[:sqlite_driver]
|
40
|
+
require @knjdb.opts[:sqlite_driver]
|
41
|
+
else
|
42
|
+
require "#{File.dirname(__FILE__)}/../../../jruby/sqlitejdbc-v056.jar"
|
43
|
+
end
|
44
|
+
|
45
|
+
require "java"
|
46
|
+
import "org.sqlite.JDBC"
|
47
|
+
@conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@knjdb.opts[:path]}")
|
48
|
+
@stat = @conn.createStatement
|
49
|
+
elsif @knjdb.opts[:subtype] == "rhodes"
|
50
|
+
@conn = SQLite3::Database.new(@path, @path)
|
23
51
|
else
|
24
|
-
|
52
|
+
@conn = SQLite3::Database.open(@path)
|
53
|
+
@conn.results_as_hash = true
|
54
|
+
@conn.type_translation = false
|
25
55
|
end
|
26
|
-
|
27
|
-
require "java"
|
28
|
-
import "org.sqlite.JDBC"
|
29
|
-
@conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@knjdb.opts[:path]}")
|
30
|
-
@stat = @conn.createStatement
|
31
|
-
elsif @knjdb.opts[:subtype] == "rhodes"
|
32
|
-
@conn = SQLite3::Database.new(@path, @path)
|
33
|
-
else
|
34
|
-
@conn = SQLite3::Database.open(@path)
|
35
|
-
@conn.results_as_hash = true
|
36
|
-
@conn.type_translation = false
|
37
56
|
end
|
38
57
|
end
|
39
58
|
|
40
59
|
#Executes a query against the driver.
|
41
60
|
def query(string)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
raise e
|
53
|
-
end
|
61
|
+
if @knjdb.opts[:subtype] == :rhodes
|
62
|
+
return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(string, string))
|
63
|
+
elsif @knjdb.opts[:subtype] == :java
|
64
|
+
begin
|
65
|
+
return Baza::Driver::Sqlite3::ResultJava.new(self, @stat.executeQuery(string))
|
66
|
+
rescue java.sql.SQLException => e
|
67
|
+
if e.message.to_s.index("query does not return ResultSet") != nil
|
68
|
+
return Baza::Driver::Sqlite3::ResultJava.new(self, nil)
|
69
|
+
else
|
70
|
+
raise e
|
54
71
|
end
|
55
|
-
else
|
56
|
-
return Baza::Driver::Sqlite3_result.new(self, @conn.execute(string))
|
57
72
|
end
|
58
|
-
|
59
|
-
|
60
|
-
raise e.class, "#{e.message} (SQL: #{string})"
|
73
|
+
else
|
74
|
+
return Baza::Driver::Sqlite3::Result.new(self, @conn.execute(string))
|
61
75
|
end
|
62
76
|
end
|
63
77
|
|
@@ -98,10 +112,24 @@ class Baza::Driver::Sqlite3
|
|
98
112
|
yield(@knjdb)
|
99
113
|
end
|
100
114
|
end
|
115
|
+
|
116
|
+
def insert_multi(tablename, arr_hashes, args = nil)
|
117
|
+
sql = [] if args and args[:return_sql]
|
118
|
+
|
119
|
+
@knjdb.transaction do
|
120
|
+
arr_hashes.each do |hash|
|
121
|
+
res = @knjdb.insert(tablename, hash, args)
|
122
|
+
sql << res if args and args[:return_sql]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
return sql if args and args[:return_sql]
|
127
|
+
return nil
|
128
|
+
end
|
101
129
|
end
|
102
130
|
|
103
131
|
#This class handels results when running in JRuby.
|
104
|
-
class Baza::Driver::
|
132
|
+
class Baza::Driver::Sqlite3::ResultJava
|
105
133
|
def initialize(driver, rs)
|
106
134
|
@index = 0
|
107
135
|
retkeys = driver.knjdb.opts[:return_keys]
|
@@ -114,8 +142,7 @@ class Baza::Driver::Sqlite3_result_java
|
|
114
142
|
while rs.next
|
115
143
|
row_data = {}
|
116
144
|
for i in (1..columns_count)
|
117
|
-
col_name = metadata.getColumnName(i)
|
118
|
-
col_name = col_name.to_s.to_sym if retkeys == "symbols"
|
145
|
+
col_name = metadata.getColumnName(i).to_sym
|
119
146
|
row_data[col_name] = rs.getString(i)
|
120
147
|
end
|
121
148
|
|
@@ -142,17 +169,11 @@ class Baza::Driver::Sqlite3_result_java
|
|
142
169
|
end
|
143
170
|
|
144
171
|
#This class handels the result when running MRI (or others).
|
145
|
-
class Baza::Driver::
|
172
|
+
class Baza::Driver::Sqlite3::Result
|
146
173
|
#Constructor. This should not be called manually.
|
147
174
|
def initialize(driver, result_array)
|
148
175
|
@result_array = result_array
|
149
176
|
@index = 0
|
150
|
-
|
151
|
-
if driver.knjdb.opts[:return_keys] == "symbols"
|
152
|
-
@symbols = true
|
153
|
-
else
|
154
|
-
@symbols = false
|
155
|
-
end
|
156
177
|
end
|
157
178
|
|
158
179
|
#Returns a single result.
|
@@ -165,7 +186,7 @@ class Baza::Driver::Sqlite3_result
|
|
165
186
|
result_hash.each do |key, val|
|
166
187
|
if (Float(key) rescue false)
|
167
188
|
#do nothing.
|
168
|
-
elsif
|
189
|
+
elsif !key.is_a?(Symbol)
|
169
190
|
ret[key.to_sym] = val
|
170
191
|
else
|
171
192
|
ret[key] = val
|
@@ -7,37 +7,39 @@ class Baza::Driver::Sqlite3::Columns
|
|
7
7
|
@args = args
|
8
8
|
end
|
9
9
|
|
10
|
+
DATA_SQL_ALLOWED_KEYS = [:name, :type, :maxlength, :autoincr, :primarykey, :null, :default, :default_func, :renames]
|
10
11
|
#Returns SQL for a knjdb-compatible hash.
|
11
12
|
def data_sql(data)
|
12
|
-
|
13
|
-
|
13
|
+
data.each do |key, val|
|
14
|
+
raise "Invalid key: '#{key}' (#{key.class.name})." unless DATA_SQL_ALLOWED_KEYS.include?(key)
|
15
|
+
end
|
16
|
+
|
17
|
+
raise "No type given." if !data[:type]
|
18
|
+
type = data[:type].to_sym
|
14
19
|
|
15
|
-
if type ==
|
16
|
-
type =
|
17
|
-
data.delete(
|
20
|
+
if type == :enum
|
21
|
+
type = :varchar
|
22
|
+
data.delete(:maxlength)
|
18
23
|
end
|
19
24
|
|
20
|
-
data[
|
21
|
-
data[
|
22
|
-
type =
|
25
|
+
data[:maxlength] = 255 if type == :varchar and !data.key?(:maxlength)
|
26
|
+
data[:maxlength] = 11 if type == :int and !data.key?(:maxlength) and !data[:autoincr] and !data[:primarykey]
|
27
|
+
type = :integer if @args[:db].int_types.index(type) and (data[:autoincr] or data[:primarykey])
|
23
28
|
|
24
|
-
sql = "`#{data[
|
25
|
-
sql << "(#{data[
|
26
|
-
sql << " PRIMARY KEY" if data[
|
27
|
-
sql << " AUTOINCREMENT" if data[
|
29
|
+
sql = "`#{data[:name]}` #{type}"
|
30
|
+
sql << "(#{data[:maxlength]})" if data[:maxlength] and !data[:autoincr]
|
31
|
+
sql << " PRIMARY KEY" if data[:primarykey]
|
32
|
+
sql << " AUTOINCREMENT" if data[:autoincr]
|
28
33
|
|
29
|
-
if !data[
|
34
|
+
if !data[:null] and data.key?(:null)
|
30
35
|
sql << " NOT NULL"
|
31
|
-
|
32
|
-
if !data.key?("default") or !data["default"]
|
33
|
-
data["default"] = 0 if type == "int"
|
34
|
-
end
|
36
|
+
data[:default] = 0 if type == :int if !data.key?(:default) or !data[:default]
|
35
37
|
end
|
36
38
|
|
37
|
-
if data.key?(
|
38
|
-
sql << " DEFAULT #{data[
|
39
|
-
elsif data.key?(
|
40
|
-
sql << " DEFAULT '#{@args[:db].escape(data[
|
39
|
+
if data.key?(:default_func)
|
40
|
+
sql << " DEFAULT #{data[:default_func]}"
|
41
|
+
elsif data.key?(:default) and data[:default] != false
|
42
|
+
sql << " DEFAULT '#{@args[:db].escape(data[:default])}'"
|
41
43
|
end
|
42
44
|
|
43
45
|
return sql
|
@@ -67,13 +69,13 @@ class Baza::Driver::Sqlite3::Columns::Column
|
|
67
69
|
#Returns the data of the column as a hash in knjdb-format.
|
68
70
|
def data
|
69
71
|
return {
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
72
|
+
:type => self.type,
|
73
|
+
:name => self.name,
|
74
|
+
:null => self.null?,
|
75
|
+
:maxlength => self.maxlength,
|
76
|
+
:default => self.default,
|
77
|
+
:primarykey => self.primarykey?,
|
78
|
+
:autoincr => self.autoincr?
|
77
79
|
}
|
78
80
|
end
|
79
81
|
|
@@ -133,7 +135,7 @@ class Baza::Driver::Sqlite3::Columns::Column
|
|
133
135
|
def_val = def_val.to_s.slice(0, def_val.length - 1)
|
134
136
|
end
|
135
137
|
|
136
|
-
return false if @args[:data][:dflt_value].to_s.
|
138
|
+
return false if @args[:data][:dflt_value].to_s.empty?
|
137
139
|
return def_val
|
138
140
|
end
|
139
141
|
|
@@ -145,32 +147,32 @@ class Baza::Driver::Sqlite3::Columns::Column
|
|
145
147
|
|
146
148
|
#Returns true if the column is auto-increasing.
|
147
149
|
def autoincr?
|
148
|
-
return true if @args[:data][:pk].to_i == 1 and @args[:data][:type].
|
150
|
+
return true if @args[:data][:pk].to_i == 1 and @args[:data][:type].to_sym == :integer
|
149
151
|
return false
|
150
152
|
end
|
151
153
|
|
152
154
|
#Drops the column from the table.
|
153
155
|
def drop
|
154
|
-
self.table.copy(
|
156
|
+
self.table.copy(:drops => self.name)
|
155
157
|
end
|
156
158
|
|
157
159
|
#Changes data on the column. Like the name, type, maxlength or whatever.
|
158
160
|
def change(data)
|
159
161
|
newdata = data.clone
|
160
162
|
|
161
|
-
newdata[
|
162
|
-
newdata[
|
163
|
-
newdata[
|
164
|
-
newdata[
|
165
|
-
newdata[
|
166
|
-
newdata[
|
163
|
+
newdata[:name] = self.name if !newdata.key?(:name)
|
164
|
+
newdata[:type] = self.type if !newdata.key?(:type)
|
165
|
+
newdata[:maxlength] = self.maxlength if !newdata.key?(:maxlength) and self.maxlength
|
166
|
+
newdata[:null] = self.null? if !newdata.key?(:null)
|
167
|
+
newdata[:default] = self.default if !newdata.key?(:default)
|
168
|
+
newdata[:primarykey] = self.primarykey? if !newdata.key?(:primarykey)
|
167
169
|
|
168
170
|
@type = nil
|
169
171
|
@maxlength = nil
|
170
172
|
|
171
173
|
new_table = self.table.copy(
|
172
|
-
|
173
|
-
self.name.
|
174
|
+
:alter_columns => {
|
175
|
+
self.name.to_sym => newdata
|
174
176
|
}
|
175
177
|
)
|
176
178
|
end
|