baza 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +58 -13
- data/VERSION +1 -1
- data/baza.gemspec +15 -3
- data/include/db.rb +871 -865
- data/include/drivers/mysql/mysql.rb +104 -297
- data/include/drivers/mysql/mysql_column.rb +133 -0
- data/include/drivers/mysql/mysql_columns.rb +4 -127
- data/include/drivers/mysql/mysql_index.rb +76 -0
- data/include/drivers/mysql/mysql_indexes.rb +0 -73
- data/include/drivers/mysql/mysql_result.rb +42 -0
- data/include/drivers/mysql/mysql_result_java.rb +61 -0
- data/include/drivers/mysql/mysql_result_mysql2.rb +26 -0
- data/include/drivers/mysql/mysql_result_unbuffered.rb +72 -0
- data/include/drivers/mysql/mysql_sqlspecs.rb +1 -1
- data/include/drivers/mysql/mysql_table.rb +361 -0
- data/include/drivers/mysql/mysql_tables.rb +23 -381
- data/include/drivers/sqlite3/libknjdb_java_sqlite3.rb +17 -22
- data/include/drivers/sqlite3/libknjdb_sqlite3_ironruby.rb +13 -13
- data/include/drivers/sqlite3/sqlite3.rb +39 -105
- data/include/drivers/sqlite3/sqlite3_column.rb +146 -0
- data/include/drivers/sqlite3/sqlite3_columns.rb +17 -149
- data/include/drivers/sqlite3/sqlite3_index.rb +55 -0
- data/include/drivers/sqlite3/sqlite3_indexes.rb +0 -52
- data/include/drivers/sqlite3/sqlite3_result.rb +35 -0
- data/include/drivers/sqlite3/sqlite3_result_java.rb +39 -0
- data/include/drivers/sqlite3/sqlite3_table.rb +399 -0
- data/include/drivers/sqlite3/sqlite3_tables.rb +7 -403
- data/include/idquery.rb +19 -19
- data/include/model.rb +139 -139
- data/include/model_handler_sqlhelper.rb +74 -74
- data/spec/support/driver_columns_collection.rb +17 -0
- metadata +14 -2
@@ -0,0 +1,361 @@
|
|
1
|
+
class Baza::Driver::Mysql::Table
|
2
|
+
attr_reader :list, :name
|
3
|
+
|
4
|
+
def initialize(args)
|
5
|
+
@args = args
|
6
|
+
@db = args[:db]
|
7
|
+
@data = args[:data]
|
8
|
+
@subtype = @db.opts[:subtype]
|
9
|
+
@list = Wref_map.new
|
10
|
+
@indexes_list = Wref_map.new
|
11
|
+
@name = @data[:Name].to_sym
|
12
|
+
@tables = args[:tables]
|
13
|
+
|
14
|
+
raise "Could not figure out name from: '#{@data}'." if @data[:Name].to_s.strip.empty?
|
15
|
+
end
|
16
|
+
|
17
|
+
def reload
|
18
|
+
@data = @db.q("SHOW TABLE STATUS WHERE `Name` = '#{@db.esc(self.name)}'").fetch
|
19
|
+
end
|
20
|
+
|
21
|
+
#Used to validate in Knj::Wrap_map.
|
22
|
+
def __object_unique_id__
|
23
|
+
return @data[:Name]
|
24
|
+
end
|
25
|
+
|
26
|
+
def drop
|
27
|
+
raise "Cant drop native table: '#{self.name}'." if self.native?
|
28
|
+
@db.query("DROP TABLE `#{@db.esc_table(self.name)}`")
|
29
|
+
@tables.__send__(:remove_from_list, self)
|
30
|
+
return nil
|
31
|
+
end
|
32
|
+
|
33
|
+
#Returns true if the table is safe to drop.
|
34
|
+
def native?
|
35
|
+
return true if @db.q("SELECT DATABASE() AS db").fetch[:db] == "mysql"
|
36
|
+
return false
|
37
|
+
end
|
38
|
+
|
39
|
+
def optimize
|
40
|
+
@db.query("OPTIMIZE TABLE `#{@db.esc_table(self.name)}`")
|
41
|
+
return self
|
42
|
+
end
|
43
|
+
|
44
|
+
def rows_count
|
45
|
+
return @db.q("SELECT COUNT(*) AS count FROM `#{@db.esc_table(self.name)}`").fetch[:count].to_i
|
46
|
+
end
|
47
|
+
|
48
|
+
def column(name)
|
49
|
+
name = name.to_sym
|
50
|
+
|
51
|
+
if col = @list.get!(name)
|
52
|
+
return @list[name]
|
53
|
+
end
|
54
|
+
|
55
|
+
self.columns(:name => name) do |col|
|
56
|
+
return col if col.name == name
|
57
|
+
end
|
58
|
+
|
59
|
+
raise Errno::ENOENT, "Column not found: '#{name}'."
|
60
|
+
end
|
61
|
+
|
62
|
+
def columns(args = nil)
|
63
|
+
@db.cols
|
64
|
+
ret = {}
|
65
|
+
sql = "SHOW FULL COLUMNS FROM `#{@db.esc_table(name)}`"
|
66
|
+
sql << " WHERE `Field` = '#{@db.esc(args[:name])}'" if args and args.key?(:name)
|
67
|
+
|
68
|
+
@db.q(sql) do |d_cols|
|
69
|
+
column_name = d_cols[:Field].to_sym
|
70
|
+
obj = @list.get!(name)
|
71
|
+
|
72
|
+
unless obj
|
73
|
+
obj = Baza::Driver::Mysql::Column.new(
|
74
|
+
table_name: name,
|
75
|
+
db: @db,
|
76
|
+
data: d_cols
|
77
|
+
)
|
78
|
+
@list[column_name] = obj
|
79
|
+
end
|
80
|
+
|
81
|
+
if block_given?
|
82
|
+
yield(obj)
|
83
|
+
else
|
84
|
+
ret[column_name] = obj
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
if block_given?
|
89
|
+
return nil
|
90
|
+
else
|
91
|
+
return ret
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def indexes args = nil
|
96
|
+
@db.indexes
|
97
|
+
ret = {}
|
98
|
+
|
99
|
+
sql = "SHOW INDEX FROM `#{@db.esc_table(name)}`"
|
100
|
+
sql << " WHERE `Key_name` = '#{@db.esc(args[:name])}'" if args && args.key?(:name)
|
101
|
+
|
102
|
+
@db.q(sql) do |d_indexes|
|
103
|
+
next if d_indexes[:Key_name] == "PRIMARY"
|
104
|
+
obj = @indexes_list.get!(d_indexes[:Key_name].to_s)
|
105
|
+
|
106
|
+
unless obj
|
107
|
+
obj = Baza::Driver::Mysql::Index.new(
|
108
|
+
table_name: name,
|
109
|
+
db: @db,
|
110
|
+
data: d_indexes
|
111
|
+
)
|
112
|
+
obj.columns << d_indexes[:Column_name]
|
113
|
+
@indexes_list[d_indexes[:Key_name].to_s] = obj
|
114
|
+
end
|
115
|
+
|
116
|
+
if block_given?
|
117
|
+
yield obj
|
118
|
+
else
|
119
|
+
ret[d_indexes[:Key_name].to_s] = obj
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
if block_given?
|
124
|
+
return nil
|
125
|
+
else
|
126
|
+
return ret
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def index(name)
|
131
|
+
name = name.to_s
|
132
|
+
|
133
|
+
if index = @indexes_list.get!(name)
|
134
|
+
return index
|
135
|
+
end
|
136
|
+
|
137
|
+
indexes(name: name) do |index|
|
138
|
+
return index if index.name.to_s == name
|
139
|
+
end
|
140
|
+
|
141
|
+
raise Errno::ENOENT, "Index not found: #{name}."
|
142
|
+
end
|
143
|
+
|
144
|
+
def create_columns(col_arr)
|
145
|
+
@db.transaction do
|
146
|
+
col_arr.each do |col_data|
|
147
|
+
sql = "ALTER TABLE `#{self.name}` ADD COLUMN #{@db.cols.data_sql(col_data)};"
|
148
|
+
@db.query(sql)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def create_indexes(index_arr, args = {})
|
154
|
+
return Baza::Driver::Mysql::Table.create_indexes(index_arr, args.merge(:table_name => self.name, :db => @db))
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.create_indexes(index_arr, args = {})
|
158
|
+
db = args[:db]
|
159
|
+
|
160
|
+
if args[:return_sql]
|
161
|
+
sql = ""
|
162
|
+
first = true
|
163
|
+
end
|
164
|
+
|
165
|
+
index_arr.each do |index_data|
|
166
|
+
if !args[:return_sql]
|
167
|
+
sql = ""
|
168
|
+
end
|
169
|
+
|
170
|
+
if args[:create] or !args.key?(:create)
|
171
|
+
sql << "CREATE"
|
172
|
+
end
|
173
|
+
|
174
|
+
if index_data.is_a?(String) || index_data.is_a?(Symbol)
|
175
|
+
index_data = {name: index_data, columns: [index_data]}
|
176
|
+
end
|
177
|
+
|
178
|
+
raise "No name was given: '#{index_data}'." if !index_data.key?(:name) || index_data[:name].to_s.strip.empty?
|
179
|
+
raise "No columns was given on index: '#{index_data[:name]}'." if !index_data[:columns] || index_data[:columns].empty?
|
180
|
+
|
181
|
+
if args[:return_sql]
|
182
|
+
if first
|
183
|
+
first = false
|
184
|
+
else
|
185
|
+
sql << ", "
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
sql << " UNIQUE" if index_data[:unique]
|
190
|
+
sql << " INDEX `#{db.esc_col(index_data[:name])}`"
|
191
|
+
|
192
|
+
if args[:on_table] || !args.key?(:on_table)
|
193
|
+
sql << " ON `#{db.esc_table(args[:table_name])}`"
|
194
|
+
end
|
195
|
+
|
196
|
+
sql << " ("
|
197
|
+
|
198
|
+
first = true
|
199
|
+
index_data[:columns].each do |col_name|
|
200
|
+
sql << ", " if !first
|
201
|
+
first = false if first
|
202
|
+
|
203
|
+
sql << "`#{db.esc_col(col_name)}`"
|
204
|
+
end
|
205
|
+
|
206
|
+
sql << ")"
|
207
|
+
|
208
|
+
if !args[:return_sql]
|
209
|
+
db.query(sql)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
if args[:return_sql]
|
214
|
+
return sql
|
215
|
+
else
|
216
|
+
return nil
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def rename newname
|
221
|
+
newname = newname.to_sym
|
222
|
+
oldname = name
|
223
|
+
|
224
|
+
@tables.__send__(:remove_from_list, self)
|
225
|
+
@db.query("ALTER TABLE `#{@db.esc_table(oldname)}` RENAME TO `#{@db.esc_table(newname)}`")
|
226
|
+
|
227
|
+
@data[:Name] = newname
|
228
|
+
@name = newname
|
229
|
+
@tables.__send__(:add_to_list, self)
|
230
|
+
|
231
|
+
@list.each do |name, column|
|
232
|
+
column.args[:table_name] = newname
|
233
|
+
end
|
234
|
+
|
235
|
+
@indexes_list.each do |name, index|
|
236
|
+
index.args[:table_name] = newname
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
def truncate
|
241
|
+
@db.query("TRUNCATE `#{@db.esc_table(self.name)}`")
|
242
|
+
return self
|
243
|
+
end
|
244
|
+
|
245
|
+
def data
|
246
|
+
ret = {
|
247
|
+
name: self.name,
|
248
|
+
columns: [],
|
249
|
+
indexes: []
|
250
|
+
}
|
251
|
+
|
252
|
+
columns.each do |name, column|
|
253
|
+
ret[:columns] << column.data
|
254
|
+
end
|
255
|
+
|
256
|
+
indexes.each do |name, index|
|
257
|
+
ret[:indexes] << index.data if name != "PRIMARY"
|
258
|
+
end
|
259
|
+
|
260
|
+
return ret
|
261
|
+
end
|
262
|
+
|
263
|
+
def insert(data)
|
264
|
+
@db.insert(self.name, data)
|
265
|
+
end
|
266
|
+
|
267
|
+
def clone(newname, args = {})
|
268
|
+
raise "Invalid name." if newname.to_s.strip.empty?
|
269
|
+
|
270
|
+
sql = "CREATE TABLE `#{@db.esc_table(newname)}` ("
|
271
|
+
first = true
|
272
|
+
pkey_found = false
|
273
|
+
pkeys = []
|
274
|
+
|
275
|
+
columns do |col|
|
276
|
+
sql << ", " unless first
|
277
|
+
first = false if first
|
278
|
+
|
279
|
+
col_data = col.data
|
280
|
+
pkey_found = true if !pkey_found && col_data[:primarykey] && args[:force_single_pkey]
|
281
|
+
|
282
|
+
if args[:no_pkey] || (pkey_found && col_data[:primarykey] && args[:force_single_pkey])
|
283
|
+
col_data[:primarykey] = false
|
284
|
+
end
|
285
|
+
|
286
|
+
if col_data[:primarykey]
|
287
|
+
pkeys << col_data[:name]
|
288
|
+
col_data.delete(:primarykey)
|
289
|
+
end
|
290
|
+
|
291
|
+
if args[:all_cols_storage]
|
292
|
+
col_data[:storage] = args[:all_cols_storage]
|
293
|
+
end
|
294
|
+
|
295
|
+
sql << @db.cols.data_sql(col_data)
|
296
|
+
end
|
297
|
+
|
298
|
+
unless pkeys.empty?
|
299
|
+
sql << ", PRIMARY KEY ("
|
300
|
+
|
301
|
+
first = true
|
302
|
+
pkeys.each do |pkey|
|
303
|
+
sql << ", " unless first
|
304
|
+
first = false if first
|
305
|
+
sql << "`#{@db.esc_col(pkey)}`"
|
306
|
+
end
|
307
|
+
|
308
|
+
sql << ")"
|
309
|
+
end
|
310
|
+
|
311
|
+
sql << ")"
|
312
|
+
sql << " TABLESPACE #{args[:tablespace]}" if args[:tablespace]
|
313
|
+
sql << " ENGINE=#{args[:engine]}" if args[:engine]
|
314
|
+
sql << ";"
|
315
|
+
|
316
|
+
#Create table.
|
317
|
+
@db.query(sql)
|
318
|
+
|
319
|
+
|
320
|
+
#Insert data of previous data in a single query.
|
321
|
+
@db.query("INSERT INTO `#{@db.esc_table(newname)}` SELECT * FROM `#{@db.esc_table(self.name)}`")
|
322
|
+
|
323
|
+
|
324
|
+
#Create indexes.
|
325
|
+
new_table = @db.tables[newname]
|
326
|
+
indexes_list = []
|
327
|
+
indexes do |index|
|
328
|
+
indexes_list << index.data unless index.primary?
|
329
|
+
end
|
330
|
+
|
331
|
+
new_table.create_indexes(indexes_list)
|
332
|
+
|
333
|
+
|
334
|
+
#Return new table.
|
335
|
+
return new_table
|
336
|
+
end
|
337
|
+
|
338
|
+
#Returns the current engine of the table.
|
339
|
+
def engine
|
340
|
+
return @data[:Engine]
|
341
|
+
end
|
342
|
+
|
343
|
+
#Changes the engine for a table.
|
344
|
+
def engine=(newengine)
|
345
|
+
raise "Invalid engine: '#{newengine}'." unless newengine.to_s.match(/^[A-z]+$/)
|
346
|
+
@db.query("ALTER TABLE `#{@db.esc_table(self.name)}` ENGINE = #{newengine}") if self.engine.to_s != newengine.to_s
|
347
|
+
@data[:Engine] = newengine
|
348
|
+
end
|
349
|
+
|
350
|
+
private
|
351
|
+
|
352
|
+
def remove_column_from_list(col)
|
353
|
+
raise "Column not found: '#{col.name}'." unless @list.key?(col.name)
|
354
|
+
@list.delete(col.name)
|
355
|
+
end
|
356
|
+
|
357
|
+
def add_column_to_list(col)
|
358
|
+
raise "Column already exists: '#{col.name}'." if @list.key?(col.name)
|
359
|
+
@list[col.name] = col
|
360
|
+
end
|
361
|
+
end
|