knjrbfw 0.0.23 → 0.0.24
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/knjrbfw.gemspec +7 -4
- data/lib/knj/autoload.rb +1 -61
- data/lib/knj/datarow.rb +7 -10
- data/lib/knj/datarow_custom.rb +12 -2
- data/lib/knj/datet.rb +107 -0
- data/lib/knj/eruby.rb +21 -12
- data/lib/knj/gettext_threadded.rb +1 -1
- data/lib/knj/http2.rb +27 -9
- data/lib/knj/image.rb +10 -0
- data/lib/knj/includes/require_info.rb +3 -3
- data/lib/knj/knj.rb +16 -9
- data/lib/knj/knj_controller.rb +10 -1
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb +26 -9
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb +11 -8
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_indexes.rb +5 -0
- data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb +83 -26
- data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb +3 -3
- data/lib/knj/knjdb/libknjdb.rb +19 -20
- data/lib/knj/knjdb/revision.rb +9 -2
- data/lib/knj/kvm.rb +100 -0
- data/lib/knj/locale_strings.rb +32 -3
- data/lib/knj/locales.rb +1 -4
- data/lib/knj/memory_analyzer.rb +335 -0
- data/lib/knj/objects/objects_sqlhelper.rb +2 -2
- data/lib/knj/objects.rb +44 -11
- data/lib/knj/opts.rb +8 -8
- data/lib/knj/os.rb +13 -0
- data/lib/knj/php.rb +18 -33
- data/lib/knj/process.rb +3 -0
- data/lib/knj/process_meta.rb +1 -1
- data/lib/knj/rhodes/mutex.rb +2 -1
- data/lib/knj/rhodes/rhodes.js +5 -1
- data/lib/knj/rhodes/rhodes.rb +15 -15
- data/lib/knj/rhodes/youtube_embed.erb +12 -0
- data/lib/knj/rhodes/youtube_open.erb +45 -0
- data/lib/knj/strings.rb +2 -1
- data/lib/knj/translations.rb +3 -3
- data/lib/knj/unix_proc.rb +15 -4
- data/lib/knj/web.rb +17 -247
- data/lib/knj/webscripts/image.rhtml +9 -3
- data/lib/knj/wref.rb +109 -70
- data/spec/datet_spec.rb +30 -0
- data/spec/http2_spec.rb +23 -0
- data/spec/php_spec.rb +3 -0
- metadata +20 -17
- data/lib/knj/rhodes/delegate.rb +0 -414
- data/lib/knj/rhodes/weakref.rb +0 -80
@@ -71,6 +71,8 @@ class KnjDB_mysql
|
|
71
71
|
args[key] = @knjdb.opts[key] if @knjdb.opts.key?(key)
|
72
72
|
end
|
73
73
|
|
74
|
+
args[:as] = :array if @opts[:result] == "array"
|
75
|
+
|
74
76
|
tries = 0
|
75
77
|
begin
|
76
78
|
tries += 1
|
@@ -93,7 +95,7 @@ class KnjDB_mysql
|
|
93
95
|
@jdbc_loaded = true
|
94
96
|
end
|
95
97
|
|
96
|
-
@conn = java.sql::DriverManager.getConnection("jdbc:mysql://#{@knjdb.opts[:host]}:#{@port}/#{@knjdb.opts[:db]}?user=#{@knjdb.opts[:user]}&password=#{@knjdb.opts[:pass]}&populateInsertRowWithDefaultValues=true&zeroDateTimeBehavior=round&characterEncoding=#{@encoding}")
|
98
|
+
@conn = java.sql::DriverManager.getConnection("jdbc:mysql://#{@knjdb.opts[:host]}:#{@port}/#{@knjdb.opts[:db]}?user=#{@knjdb.opts[:user]}&password=#{@knjdb.opts[:pass]}&populateInsertRowWithDefaultValues=true&zeroDateTimeBehavior=round&characterEncoding=#{@encoding}&holdResultsOpenOverStatementClose=true")
|
97
99
|
self.query("SET SQL_MODE = ''")
|
98
100
|
else
|
99
101
|
raise "Unknown subtype: #{@subtype}"
|
@@ -128,18 +130,25 @@ class KnjDB_mysql
|
|
128
130
|
|
129
131
|
return nil
|
130
132
|
else
|
133
|
+
id = nil
|
134
|
+
|
131
135
|
begin
|
132
136
|
res = stmt.execute_query(str)
|
133
137
|
ret = KnjDB_java_mysql_result.new(@knjdb, @opts, res)
|
138
|
+
id = ret.__id__
|
139
|
+
|
140
|
+
#If ID is being reused we have to free the result.
|
141
|
+
self.java_mysql_resultset_killer(id) if @java_rs_data.key?(id)
|
134
142
|
|
135
143
|
#Save reference to result and statement, so we can close them when they are garbage collected.
|
136
|
-
@java_rs_data[
|
144
|
+
@java_rs_data[id] = {:res => res, :stmt => stmt}
|
137
145
|
ObjectSpace.define_finalizer(ret, self.method("java_mysql_resultset_killer"))
|
138
146
|
|
139
147
|
return ret
|
140
148
|
rescue => e
|
141
149
|
res.close if res
|
142
150
|
stmt.close
|
151
|
+
@java_rs_data.delete(id) if ret and id
|
143
152
|
raise e
|
144
153
|
end
|
145
154
|
end
|
@@ -162,7 +171,7 @@ class KnjDB_mysql
|
|
162
171
|
end
|
163
172
|
end
|
164
173
|
|
165
|
-
print str
|
174
|
+
#print str
|
166
175
|
raise e
|
167
176
|
end
|
168
177
|
end
|
@@ -175,7 +184,7 @@ class KnjDB_mysql
|
|
175
184
|
@conn.query_with_result = false
|
176
185
|
return KnjDB_mysql_unbuffered_result.new(@conn, @opts, @conn.query(str))
|
177
186
|
when "mysql2"
|
178
|
-
|
187
|
+
return KnjDB_mysql2_result.new(@conn.query(str, @query_args.merge(:stream => true)))
|
179
188
|
when "java"
|
180
189
|
if str.match(/^\s*(delete|update|create|drop|insert\s+into)\s+/i)
|
181
190
|
stmt = @conn.createStatement
|
@@ -206,7 +215,6 @@ class KnjDB_mysql
|
|
206
215
|
raise e
|
207
216
|
end
|
208
217
|
end
|
209
|
-
raise "Not implemented yet."
|
210
218
|
else
|
211
219
|
raise "Unknown subtype: '#{@subtype}'"
|
212
220
|
end
|
@@ -458,8 +466,13 @@ class KnjDB_mysql2_result
|
|
458
466
|
end
|
459
467
|
end
|
460
468
|
|
461
|
-
def each
|
462
|
-
@result.each
|
469
|
+
def each
|
470
|
+
@result.each do |res|
|
471
|
+
#This sometimes happens when streaming results...
|
472
|
+
next if !res
|
473
|
+
|
474
|
+
yield(res)
|
475
|
+
end
|
463
476
|
end
|
464
477
|
end
|
465
478
|
|
@@ -491,11 +504,15 @@ class KnjDB_java_mysql_result
|
|
491
504
|
|
492
505
|
def fetch
|
493
506
|
return false if !@result
|
494
|
-
|
495
507
|
self.read_meta if !@keys
|
496
508
|
status = @result.next
|
497
509
|
|
498
|
-
|
510
|
+
if !status
|
511
|
+
@result = nil
|
512
|
+
@keys = nil
|
513
|
+
@count = nil
|
514
|
+
return false
|
515
|
+
end
|
499
516
|
|
500
517
|
if @as_hash
|
501
518
|
ret = {}
|
@@ -1,10 +1,9 @@
|
|
1
1
|
class KnjDB_mysql::Columns
|
2
|
-
attr_reader :db
|
2
|
+
attr_reader :db
|
3
3
|
|
4
4
|
def initialize(args)
|
5
5
|
@args = args
|
6
6
|
@db = @args[:db]
|
7
|
-
@driver = @args[:driver]
|
8
7
|
end
|
9
8
|
|
10
9
|
def data_sql(data)
|
@@ -39,12 +38,17 @@ class KnjDB_mysql::Columns::Column
|
|
39
38
|
@args = args
|
40
39
|
end
|
41
40
|
|
41
|
+
#Used to validate in Knj::Wrap_map.
|
42
|
+
def __object_unique_id__
|
43
|
+
return @args[:data][:Field]
|
44
|
+
end
|
45
|
+
|
42
46
|
def name
|
43
47
|
return @args[:data][:Field]
|
44
48
|
end
|
45
49
|
|
46
50
|
def table
|
47
|
-
return @
|
51
|
+
return @db.tables[@args[:table_name]]
|
48
52
|
end
|
49
53
|
|
50
54
|
def data
|
@@ -112,13 +116,12 @@ class KnjDB_mysql::Columns::Column
|
|
112
116
|
end
|
113
117
|
|
114
118
|
def drop
|
115
|
-
@
|
119
|
+
@db.query("ALTER TABLE `#{@args[:table_name]}` DROP COLUMN `#{self.name}`")
|
116
120
|
end
|
117
121
|
|
118
122
|
def change(data)
|
119
|
-
|
120
|
-
|
121
|
-
table_escape = "#{@args[:driver].escape_table}#{@args[:driver].esc_table(self.table.name)}#{@args[:driver].escape_table}"
|
123
|
+
col_escaped = "#{@db.enc_col}#{@db.esc_col(self.name)}#{@db.enc_col}"
|
124
|
+
table_escape = "#{@db.enc_table}#{@db.esc_table(self.table.name)}#{@db.enc_table}"
|
122
125
|
newdata = data.clone
|
123
126
|
|
124
127
|
newdata["name"] = self.name if !newdata.key?("name")
|
@@ -129,6 +132,6 @@ class KnjDB_mysql::Columns::Column
|
|
129
132
|
newdata.delete("primarykey") if newdata.key?("primarykey")
|
130
133
|
|
131
134
|
type_s = newdata["type"].to_s
|
132
|
-
@
|
135
|
+
@db.query("ALTER TABLE #{table_escape} CHANGE #{col_escaped} #{@db.cols.data_sql(newdata)}")
|
133
136
|
end
|
134
137
|
end
|
@@ -1,12 +1,11 @@
|
|
1
1
|
require "#{$knjpath}wref"
|
2
2
|
|
3
3
|
class KnjDB_mysql::Tables
|
4
|
-
attr_reader :db, :
|
4
|
+
attr_reader :db, :list
|
5
5
|
|
6
6
|
def initialize(args)
|
7
7
|
@args = args
|
8
8
|
@db = @args[:db]
|
9
|
-
@driver = @args[:driver]
|
10
9
|
@subtype = @db.opts[:subtype]
|
11
10
|
@list_mutex = Mutex.new
|
12
11
|
@list = Knj::Wref_map.new
|
@@ -23,11 +22,11 @@ class KnjDB_mysql::Tables
|
|
23
22
|
|
24
23
|
begin
|
25
24
|
return @list[table_name]
|
26
|
-
rescue
|
25
|
+
rescue Knj::Wref::Recycled
|
27
26
|
#ignore.
|
28
27
|
end
|
29
28
|
|
30
|
-
self.list do |table_obj|
|
29
|
+
self.list(:name => table_name) do |table_obj|
|
31
30
|
return table_obj if table_obj.name == table_name
|
32
31
|
end
|
33
32
|
|
@@ -37,16 +36,19 @@ class KnjDB_mysql::Tables
|
|
37
36
|
def list(args = {})
|
38
37
|
ret = {} unless block_given?
|
39
38
|
|
39
|
+
sql = "SHOW TABLE STATUS"
|
40
|
+
if args[:name]
|
41
|
+
sql << " WHERE `Name` = '#{@db.esc(args[:name])}'"
|
42
|
+
end
|
43
|
+
|
40
44
|
@list_mutex.synchronize do
|
41
|
-
@db.q(
|
45
|
+
@db.q(sql) do |d_tables|
|
42
46
|
obj = @list.get!(d_tables[:Name])
|
43
47
|
|
44
48
|
if !obj
|
45
49
|
obj = KnjDB_mysql::Tables::Table.new(
|
46
50
|
:db => @db,
|
47
|
-
:
|
48
|
-
:data => d_tables,
|
49
|
-
:tables => self
|
51
|
+
:data => d_tables
|
50
52
|
)
|
51
53
|
@list[d_tables[:Name]] = obj
|
52
54
|
end
|
@@ -79,14 +81,19 @@ class KnjDB_mysql::Tables
|
|
79
81
|
sql << @db.cols.data_sql(col_data)
|
80
82
|
end
|
81
83
|
|
82
|
-
sql << ")"
|
83
|
-
|
84
|
-
@db.query(sql)
|
85
|
-
|
86
84
|
if data["indexes"]
|
87
|
-
|
88
|
-
|
85
|
+
sql << ", "
|
86
|
+
sql << KnjDB_mysql::Tables::Table.create_indexes(data["indexes"], {
|
87
|
+
:db => @db,
|
88
|
+
:return_sql => true,
|
89
|
+
:create => false,
|
90
|
+
:on_table => false,
|
91
|
+
:table_name => name
|
92
|
+
})
|
89
93
|
end
|
94
|
+
|
95
|
+
sql << ")"
|
96
|
+
@db.query(sql)
|
90
97
|
end
|
91
98
|
end
|
92
99
|
|
@@ -96,7 +103,6 @@ class KnjDB_mysql::Tables::Table
|
|
96
103
|
def initialize(args)
|
97
104
|
@args = args
|
98
105
|
@db = args[:db]
|
99
|
-
@driver = args[:driver]
|
100
106
|
@data = args[:data]
|
101
107
|
@subtype = @db.opts[:subtype]
|
102
108
|
@list = Knj::Wref_map.new
|
@@ -105,6 +111,15 @@ class KnjDB_mysql::Tables::Table
|
|
105
111
|
raise "Could not figure out name from: '#{@data}'." if !@data[:Name]
|
106
112
|
end
|
107
113
|
|
114
|
+
def reload
|
115
|
+
@data = @db.q("SHOW TABLE STATUS WHERE `Name` = '#{@db.esc(self.name)}'").fetch
|
116
|
+
end
|
117
|
+
|
118
|
+
#Used to validate in Knj::Wrap_map.
|
119
|
+
def __object_unique_id__
|
120
|
+
return @data[:Name]
|
121
|
+
end
|
122
|
+
|
108
123
|
def name
|
109
124
|
return @data[:Name]
|
110
125
|
end
|
@@ -119,12 +134,16 @@ class KnjDB_mysql::Tables::Table
|
|
119
134
|
return self
|
120
135
|
end
|
121
136
|
|
137
|
+
def rows_count
|
138
|
+
return @data[:Rows].to_i
|
139
|
+
end
|
140
|
+
|
122
141
|
def column(name)
|
123
142
|
name = name.to_s
|
124
143
|
|
125
144
|
begin
|
126
145
|
return @list[name]
|
127
|
-
rescue
|
146
|
+
rescue Knj::Wref::Recycled
|
128
147
|
#ignore.
|
129
148
|
end
|
130
149
|
|
@@ -147,7 +166,6 @@ class KnjDB_mysql::Tables::Table
|
|
147
166
|
obj = KnjDB_mysql::Columns::Column.new(
|
148
167
|
:table_name => self.name,
|
149
168
|
:db => @db,
|
150
|
-
:driver => @driver,
|
151
169
|
:data => d_cols
|
152
170
|
)
|
153
171
|
@list[d_cols[:Field]] = obj
|
@@ -180,7 +198,6 @@ class KnjDB_mysql::Tables::Table
|
|
180
198
|
obj = KnjDB_mysql::Indexes::Index.new(
|
181
199
|
:table_name => self.name,
|
182
200
|
:db => @db,
|
183
|
-
:driver => @driver,
|
184
201
|
:data => d_indexes
|
185
202
|
)
|
186
203
|
obj.columns << d_indexes[:Column_name]
|
@@ -206,7 +223,7 @@ class KnjDB_mysql::Tables::Table
|
|
206
223
|
|
207
224
|
begin
|
208
225
|
return @indexes_list[name]
|
209
|
-
rescue
|
226
|
+
rescue Knj::Wref::Recycled
|
210
227
|
#ignore.
|
211
228
|
end
|
212
229
|
|
@@ -224,8 +241,27 @@ class KnjDB_mysql::Tables::Table
|
|
224
241
|
end
|
225
242
|
end
|
226
243
|
|
227
|
-
def create_indexes(index_arr)
|
244
|
+
def create_indexes(index_arr, args = {})
|
245
|
+
return KnjDB_mysql::Tables::Table.create_indexes(index_arr, args.merge(:table_name => self.name, :db => @db))
|
246
|
+
end
|
247
|
+
|
248
|
+
def self.create_indexes(index_arr, args = {})
|
249
|
+
db = args[:db]
|
250
|
+
|
251
|
+
if args[:return_sql]
|
252
|
+
sql = ""
|
253
|
+
first = true
|
254
|
+
end
|
255
|
+
|
228
256
|
index_arr.each do |index_data|
|
257
|
+
if !args[:return_sql]
|
258
|
+
sql = ""
|
259
|
+
end
|
260
|
+
|
261
|
+
if args[:create] or !args.key?(:create)
|
262
|
+
sql << "CREATE"
|
263
|
+
end
|
264
|
+
|
229
265
|
if index_data.is_a?(String)
|
230
266
|
index_data = {"name" => index_data, "columns" => [index_data]}
|
231
267
|
end
|
@@ -233,29 +269,50 @@ class KnjDB_mysql::Tables::Table
|
|
233
269
|
raise "No name was given." if !index_data.key?("name") or index_data["name"].strip.length <= 0
|
234
270
|
raise "No columns was given on index: '#{index_data["name"]}'." if !index_data["columns"] or index_data["columns"].empty?
|
235
271
|
|
236
|
-
|
272
|
+
if args[:return_sql]
|
273
|
+
if first
|
274
|
+
first = false
|
275
|
+
else
|
276
|
+
sql << ", "
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
237
280
|
sql << " UNIQUE" if index_data["unique"]
|
238
|
-
sql << " INDEX #{
|
281
|
+
sql << " INDEX #{db.escape_col}#{db.esc_col(index_data["name"])}#{db.escape_col}"
|
282
|
+
|
283
|
+
if args[:on_table] or !args.key?(:on_table)
|
284
|
+
sql << " ON #{db.escape_table}#{db.esc_table(args[:table_name])}#{db.escape_table}"
|
285
|
+
end
|
286
|
+
|
287
|
+
sql << " ("
|
239
288
|
|
240
289
|
first = true
|
241
290
|
index_data["columns"].each do |col_name|
|
242
291
|
sql << ", " if !first
|
243
292
|
first = false if first
|
244
293
|
|
245
|
-
sql << "#{
|
294
|
+
sql << "#{db.escape_col}#{db.esc_col(col_name)}#{db.escape_col}"
|
246
295
|
end
|
247
296
|
|
248
297
|
sql << ")"
|
249
298
|
|
250
|
-
|
299
|
+
if !args[:return_sql]
|
300
|
+
db.query(sql)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
if args[:return_sql]
|
305
|
+
return sql
|
306
|
+
else
|
307
|
+
return nil
|
251
308
|
end
|
252
309
|
end
|
253
310
|
|
254
311
|
def rename(newname)
|
255
312
|
oldname = self.name
|
256
313
|
@db.query("ALTER TABLE `#{oldname}` RENAME TO `#{newname}`")
|
257
|
-
@
|
258
|
-
@
|
314
|
+
@db.tables.list[newname] = self
|
315
|
+
@db.tables.list.delete(oldname)
|
259
316
|
@data[:Name] = newname
|
260
317
|
end
|
261
318
|
|
@@ -17,7 +17,7 @@ class KnjDB_sqlite3::Tables
|
|
17
17
|
|
18
18
|
begin
|
19
19
|
return @list[table_name]
|
20
|
-
rescue
|
20
|
+
rescue Knj::Wref::Recycled
|
21
21
|
#ignore.
|
22
22
|
end
|
23
23
|
|
@@ -271,13 +271,13 @@ class KnjDB_sqlite3::Tables::Table
|
|
271
271
|
|
272
272
|
begin
|
273
273
|
return @indexes_list[name]
|
274
|
-
rescue
|
274
|
+
rescue Knj::Wref::Recycled
|
275
275
|
if @db.opts[:index_append_table_name]
|
276
276
|
tryname = "#{self.name}__#{name}"
|
277
277
|
|
278
278
|
begin
|
279
279
|
return @indexes_list[tryname]
|
280
|
-
rescue
|
280
|
+
rescue Knj::Wref::Recycled
|
281
281
|
#ignore.
|
282
282
|
end
|
283
283
|
else
|
data/lib/knj/knjdb/libknjdb.rb
CHANGED
@@ -68,7 +68,7 @@ class Knj::Db
|
|
68
68
|
|
69
69
|
fpaths = [
|
70
70
|
"drivers/#{@opts[:type]}/knjdb_#{@opts[:type]}.rb",
|
71
|
-
"libknjdb_
|
71
|
+
"libknjdb_#{@opts[:type]}.rb"
|
72
72
|
]
|
73
73
|
fpaths.each do |fpath|
|
74
74
|
rpath = "#{File.dirname(__FILE__)}/#{fpath}"
|
@@ -370,11 +370,10 @@ class Knj::Db
|
|
370
370
|
#Executes a query and returns the result.
|
371
371
|
def query(string)
|
372
372
|
if @debug
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
print "
|
377
|
-
print e.backtrace.join("\n")
|
373
|
+
print "SQL: #{string}\n"
|
374
|
+
|
375
|
+
if @debug.is_a?(Fixnum) and @debug >= 2
|
376
|
+
print caller.join("\n")
|
378
377
|
print "\n"
|
379
378
|
end
|
380
379
|
end
|
@@ -402,15 +401,7 @@ class Knj::Db
|
|
402
401
|
|
403
402
|
#Clones the connection, executes a unbuffered query and closes the connection again.
|
404
403
|
def cloned_conn(args = nil, &block)
|
405
|
-
subtype = @opts[:subtype]
|
406
|
-
|
407
|
-
#MySQL2-driver doesnt support unbuffered queries yet.
|
408
|
-
if @opts[:type] == "mysql" and @opts[:subtype] == "mysql2"
|
409
|
-
subtype = "mysql"
|
410
|
-
end
|
411
|
-
|
412
404
|
clone_conn_args = {
|
413
|
-
:subtype => subtype,
|
414
405
|
:threadsafe => false
|
415
406
|
}
|
416
407
|
|
@@ -513,15 +504,14 @@ class Knj::Db
|
|
513
504
|
#Returns the table-module and spawns it if it isnt already spawned.
|
514
505
|
def tables
|
515
506
|
conn_exec do |driver|
|
516
|
-
if
|
507
|
+
if !@tables
|
517
508
|
require "#{File.dirname(__FILE__)}/drivers/#{@opts[:type]}/knjdb_#{@opts[:type]}_tables" if (!@opts.key?(:require) or @opts[:require])
|
518
|
-
|
519
|
-
:driver => driver,
|
509
|
+
@tables = Kernel.const_get("KnjDB_#{@opts[:type]}".to_sym).const_get(:Tables).new(
|
520
510
|
:db => self
|
521
511
|
)
|
522
512
|
end
|
523
513
|
|
524
|
-
return
|
514
|
+
return @tables
|
525
515
|
end
|
526
516
|
end
|
527
517
|
|
@@ -530,7 +520,6 @@ class Knj::Db
|
|
530
520
|
if !@cols
|
531
521
|
require "#{File.dirname(__FILE__)}/drivers/#{@opts[:type]}/knjdb_#{@opts[:type]}_columns" if (!@opts.key?(:require) or @opts[:require])
|
532
522
|
@cols = Kernel.const_get("KnjDB_#{@opts[:type]}".to_sym).const_get(:Columns).new(
|
533
|
-
:driver => @conn,
|
534
523
|
:db => self
|
535
524
|
)
|
536
525
|
end
|
@@ -543,7 +532,6 @@ class Knj::Db
|
|
543
532
|
if !@indexes
|
544
533
|
require "#{File.dirname(__FILE__)}/drivers/#{@opts[:type]}/knjdb_#{@opts[:type]}_indexes" if (!@opts.key?(:require) or @opts[:require])
|
545
534
|
@indexes = Kernel.const_get("KnjDB_#{@opts[:type]}".to_sym).const_get(:Indexes).new(
|
546
|
-
:driver => @conn,
|
547
535
|
:db => self
|
548
536
|
)
|
549
537
|
end
|
@@ -560,4 +548,15 @@ class Knj::Db
|
|
560
548
|
|
561
549
|
raise "Method not found: #{method_name}"
|
562
550
|
end
|
551
|
+
|
552
|
+
#Beings a transaction and commits when the block ends.
|
553
|
+
def transaction
|
554
|
+
self.query("START TRANSACTION")
|
555
|
+
|
556
|
+
begin
|
557
|
+
yield(self)
|
558
|
+
ensure
|
559
|
+
self.query("COMMIT")
|
560
|
+
end
|
561
|
+
end
|
563
562
|
end
|
data/lib/knj/knjdb/revision.rb
CHANGED
@@ -5,6 +5,7 @@ class Knj::Db::Revision
|
|
5
5
|
|
6
6
|
#This method checks if certain rows are present in a table based on a hash.
|
7
7
|
def rows_init(args)
|
8
|
+
db = args["db"]
|
8
9
|
table = args["table"]
|
9
10
|
|
10
11
|
args["rows"].each do |row_data|
|
@@ -37,7 +38,6 @@ class Knj::Db::Revision
|
|
37
38
|
def init_db(args)
|
38
39
|
schema = args["schema"]
|
39
40
|
db = args["db"]
|
40
|
-
@db = db
|
41
41
|
|
42
42
|
#Check for normal bugs and raise apropiate error.
|
43
43
|
raise "'schema' argument was not a Hash: '#{schema.class.name}'." if !schema.is_a?(Hash)
|
@@ -50,6 +50,7 @@ class Knj::Db::Revision
|
|
50
50
|
schema["tables"].each do |table_name, table_data|
|
51
51
|
begin
|
52
52
|
begin
|
53
|
+
raise Knj::Errors::NotFound if !tables.key?(table_name)
|
53
54
|
table_obj = db.tables[table_name]
|
54
55
|
|
55
56
|
#Cache indexes- and column-objects to avoid constant reloading.
|
@@ -224,11 +225,12 @@ class Knj::Db::Revision
|
|
224
225
|
end
|
225
226
|
end
|
226
227
|
|
227
|
-
self.rows_init("table" => table_obj, "rows" => table_data["rows"]) if table_data and table_data["rows"]
|
228
|
+
self.rows_init("db" => db, "table" => table_obj, "rows" => table_data["rows"]) if table_data and table_data["rows"]
|
228
229
|
rescue Knj::Errors::NotFound => e
|
229
230
|
if table_data["renames"]
|
230
231
|
table_data["renames"].each do |table_name_rename|
|
231
232
|
begin
|
233
|
+
raise Knj::Errors::NotFound if !tables.key?(table_name)
|
232
234
|
table_rename = db.tables[table_name_rename]
|
233
235
|
table_rename.rename(table_name)
|
234
236
|
raise Knj::Errors::Retry
|
@@ -272,5 +274,10 @@ class Knj::Db::Revision
|
|
272
274
|
end
|
273
275
|
end
|
274
276
|
end
|
277
|
+
|
278
|
+
|
279
|
+
#Free cache.
|
280
|
+
tables.clear
|
281
|
+
tables = nil
|
275
282
|
end
|
276
283
|
end
|
data/lib/knj/kvm.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
class Knj::Kvm
|
2
|
+
def self.list
|
3
|
+
list = []
|
4
|
+
|
5
|
+
Knj::Unix_proc.list("grep" => "kvm") do |proc_obj|
|
6
|
+
next if !proc_obj["cmd"].match(/^\/usr\/bin\/kvm\s+/)
|
7
|
+
|
8
|
+
args = {
|
9
|
+
:pid => proc_obj["pid"]
|
10
|
+
}
|
11
|
+
|
12
|
+
if mac_match = proc_obj["cmd"].match(/mac=(.+?)(,|\s+|$)/)
|
13
|
+
args[:mac] = mac_match[1]
|
14
|
+
end
|
15
|
+
|
16
|
+
if name_match = proc_obj["cmd"].match(/\-name\s+(.+?)(,|\s+|$)/)
|
17
|
+
args[:name] = name_match[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
if args.length > 0 and args[:name].to_s.length > 0
|
21
|
+
machine = Knj::Kvm::Machine.new(args)
|
22
|
+
if block_given?
|
23
|
+
yield(machine)
|
24
|
+
else
|
25
|
+
list << machine
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if block_given?
|
31
|
+
return nil
|
32
|
+
else
|
33
|
+
return list
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Knj::Kvm::Machine
|
39
|
+
def initialize(args)
|
40
|
+
@args = args
|
41
|
+
end
|
42
|
+
|
43
|
+
def pid
|
44
|
+
return @args[:pid]
|
45
|
+
end
|
46
|
+
|
47
|
+
def name
|
48
|
+
return @args[:name]
|
49
|
+
end
|
50
|
+
|
51
|
+
def mac
|
52
|
+
raise "No MAC-address has been registered for this machine." if !@args.key?(:mac)
|
53
|
+
return @args[:mac]
|
54
|
+
end
|
55
|
+
|
56
|
+
def iface
|
57
|
+
if !@iface
|
58
|
+
res = Knj::Os.shellcmd("ifconfig | grep \"#{self.mac[3, self.mac.length]}\"")
|
59
|
+
|
60
|
+
if net_match = res.match(/^vnet(\d+)/)
|
61
|
+
@iface = net_match[0]
|
62
|
+
else
|
63
|
+
raise "Could not figure out iface from '#{res}' for '#{self.name}'."
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
return @iface
|
68
|
+
end
|
69
|
+
|
70
|
+
def net_status
|
71
|
+
res = Knj::Os.shellcmd("ifconfig \"#{self.iface}\"")
|
72
|
+
|
73
|
+
ret = {}
|
74
|
+
|
75
|
+
if tx_bytes_match = res.match(/TX\s*bytes:\s*(\d+)/)
|
76
|
+
ret[:tx] = tx_bytes_match[1].to_i
|
77
|
+
end
|
78
|
+
|
79
|
+
if rx_bytes_match = res.match(/RX\s*bytes:\s*(\d+)/)
|
80
|
+
ret[:rx] = rx_bytes_match[1].to_i
|
81
|
+
end
|
82
|
+
|
83
|
+
return ret
|
84
|
+
end
|
85
|
+
|
86
|
+
def io_status
|
87
|
+
io_status = File.read("/proc/#{self.pid}/io")
|
88
|
+
|
89
|
+
if !matches = io_status.scan(/^(.+): (\d+)$/)
|
90
|
+
raise "Could not match IO-status from: '#{io_status}'."
|
91
|
+
end
|
92
|
+
|
93
|
+
ret = {}
|
94
|
+
matches.each do |match|
|
95
|
+
ret[match[0].to_sym] = match[1].to_i
|
96
|
+
end
|
97
|
+
|
98
|
+
return ret
|
99
|
+
end
|
100
|
+
end
|
data/lib/knj/locale_strings.rb
CHANGED
@@ -1,3 +1,32 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
#This file can be symlinked and thereby easily translated by POEdit. Further more the static methods can be used for stuff.
|
2
|
+
module Knj::Locales
|
3
|
+
def self.days_arr
|
4
|
+
return [_("Monday"), _("Tuesday"), _("Wednesday"), _("Thursday"), _("Friday"), _("Saturday"), _("Sunday")]
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.days_short_arr
|
8
|
+
return [_("Mon"), _("Tue"), _("Wed"), _("Thu"), _("Fri"), _("Sat"), _("Sun")]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.months_arr
|
12
|
+
return [_("January"), _("February"), _("March"), _("April"), _("May"), _("June"), _("July"), _("August"), _("September"), _("October"), _("November"), _("December")]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.ago_strings
|
16
|
+
return {
|
17
|
+
:year_ago_str => _("%s year ago"),
|
18
|
+
:years_ago_str => _("%s years ago"),
|
19
|
+
:month_ago_str => _("%s month ago"),
|
20
|
+
:months_ago_str => _("%s months ago"),
|
21
|
+
:day_ago_str => _("%s day ago"),
|
22
|
+
:days_ago_str => _("%s days ago"),
|
23
|
+
:hour_ago_str => _("%s hour ago"),
|
24
|
+
:hours_ago_str => _("%s hours ago"),
|
25
|
+
:min_ago_str => _("%s minute ago"),
|
26
|
+
:mins_ago_str => _("%s minutes ago"),
|
27
|
+
:sec_ago_str => _("%s second ago"),
|
28
|
+
:secs_ago_str => _("%s seconds ago"),
|
29
|
+
:right_now_str => _("right now")
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|