knjrbfw 0.0.39 → 0.0.40

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.39
1
+ 0.0.40
data/knjrbfw.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{knjrbfw}
8
- s.version = "0.0.39"
8
+ s.version = "0.0.40"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = %q{2012-05-21}
12
+ s.date = %q{2012-05-25}
13
13
  s.description = %q{Including stuff for HTTP, SSH and much more.}
14
14
  s.email = %q{k@spernj.org}
15
15
  s.extra_rdoc_files = [
@@ -173,10 +173,13 @@ Gem::Specification.new do |s|
173
173
  "lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_indexes.rb",
174
174
  "lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_sqlspecs.rb",
175
175
  "lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb",
176
+ "lib/knj/knjdb/dump.rb",
177
+ "lib/knj/knjdb/idquery.rb",
176
178
  "lib/knj/knjdb/libknjdb.rb",
177
179
  "lib/knj/knjdb/libknjdb_java_sqlite3.rb",
178
180
  "lib/knj/knjdb/libknjdb_row.rb",
179
181
  "lib/knj/knjdb/libknjdb_sqlite3_ironruby.rb",
182
+ "lib/knj/knjdb/query_buffer.rb",
180
183
  "lib/knj/knjdb/revision.rb",
181
184
  "lib/knj/knjdb/sqlspecs.rb",
182
185
  "lib/knj/kvm.rb",
data/lib/knj/datarow.rb CHANGED
@@ -150,7 +150,7 @@ class Knj::Datarow
150
150
  self.joined_tables(
151
151
  classname => {
152
152
  :where => {
153
- colname.to_s => {:type => "col", :name => :id}
153
+ colname.to_s => {:type => :col, :name => :id}
154
154
  }
155
155
  }
156
156
  )
@@ -220,7 +220,7 @@ class Knj::Datarow
220
220
  self.joined_tables(
221
221
  classname => {
222
222
  :where => {
223
- "id" => {:type => "col", :name => colname}
223
+ "id" => {:type => :col, :name => colname}
224
224
  }
225
225
  }
226
226
  )
@@ -251,7 +251,7 @@ class Knj::Datarow
251
251
  table_name => {
252
252
  :where => {
253
253
  "object_class" => self.name,
254
- "object_id" => {:type => "col", :name => "id"},
254
+ "object_id" => {:type => :col, :name => "id"},
255
255
  "key" => val.to_s,
256
256
  "locale" => proc{|d| _session[:locale]}
257
257
  },
@@ -335,33 +335,33 @@ class Knj::Datarow
335
335
  d.db.tables[table].columns do |col_obj|
336
336
  col_name = col_obj.name
337
337
  col_type = col_obj.type
338
- col_type = "int" if col_type == "bigint" or col_type == "tinyint" or col_type == "mediumint" or col_type == "smallint"
338
+ col_type = :int if col_type == :bigint or col_type == :tinyint or col_type == :mediumint or col_type == :smallint
339
339
  sqlhelper_args[:cols][col_name] = true
340
340
 
341
341
  self.define_bool_methods(:inst_methods => inst_methods, :col_name => col_name)
342
342
 
343
- if col_type == "enum" and col_obj.maxlength == "'0','1'"
343
+ if col_type == :enum and col_obj.maxlength == "'0','1'"
344
344
  sqlhelper_args[:cols_bools] << col_name
345
- elsif col_type == "int" and col_name.slice(-3, 3) == "_id"
345
+ elsif col_type == :int and col_name.slice(-3, 3) == "_id"
346
346
  sqlhelper_args[:cols_dbrows] << col_name
347
- elsif col_type == "int" or col_type == "bigint" or col_type == "decimal"
347
+ elsif col_type == :int or col_type == :decimal
348
348
  sqlhelper_args[:cols_num] << col_name
349
- elsif col_type == "varchar" or col_type == "text" or col_type == "enum"
349
+ elsif col_type == :varchar or col_type == :text or col_type == :enum
350
350
  sqlhelper_args[:cols_str] << col_name
351
- elsif col_type == "date" or col_type == "datetime"
351
+ elsif col_type == :date or col_type == :datetime
352
352
  sqlhelper_args[:cols_date] << col_name
353
353
  self.define_date_methods(:inst_methods => inst_methods, :col_name => col_name)
354
354
  end
355
355
 
356
- if col_type == "int" or col_type == "decimal"
356
+ if col_type == :int or col_type == :decimal
357
357
  self.define_numeric_methods(:inst_methods => inst_methods, :col_name => col_name)
358
358
  end
359
359
 
360
- if col_type == "int" or col_type == "varchar"
360
+ if col_type == :int or col_type == :varchar
361
361
  self.define_text_methods(:inst_methods => inst_methods, :col_name => col_name)
362
362
  end
363
363
 
364
- if col_type == "time"
364
+ if col_type == :time
365
365
  self.define_time_methods(:inst_methods => inst_methods, :col_name => col_name)
366
366
  end
367
367
  end
@@ -369,7 +369,7 @@ class Knj::Datarow
369
369
  if @columns_joined_tables
370
370
  @columns_joined_tables.each do |table_name, table_data|
371
371
  table_data[:where].each do |key, val|
372
- val[:table] = self.table.to_sym if val.is_a?(Hash) and !val.key?(:table) and val[:type] == "col"
372
+ val[:table] = self.table.to_sym if val.is_a?(Hash) and !val.key?(:table) and val[:type].to_sym == :col
373
373
  end
374
374
 
375
375
  table_data[:datarow] = @ob.args[:module].const_get(table_name.to_sym) if !table_data.key?(:datarow)
@@ -425,7 +425,7 @@ class Knj::Datarow
425
425
  when :cloned_ubuf
426
426
  qargs = {:cloned_ubuf => true}
427
427
  else
428
- raise "Invalid key: '#{key}' for '#{self.name}'. Valid keys are: '#{@columns_sqlhelper_args[:cols].keys.sort}'."
428
+ raise "Invalid key: '#{key}' for '#{self.name}'. Valid keys are: '#{@columns_sqlhelper_args[:cols].keys.sort}'. Date-keys: '#{@columns_sqlhelper_args[:cols_date]}'."
429
429
  end
430
430
  end
431
431
 
@@ -523,7 +523,10 @@ class Knj::Datarow
523
523
  raise Knj::Errors::InvalidData, "Could not figure out the data from '#{data.class.name}'."
524
524
  end
525
525
 
526
- raise "Invalid ID: '#{@id}'." if @id.to_i <= 0
526
+ if @id.to_i <= 0
527
+ raise "Invalid ID: '#{@id}' from '#{@data}'."if @data
528
+ raise "Invalid ID: '#{@id}'."
529
+ end
527
530
  end
528
531
 
529
532
  #Reloads the data from the database.
data/lib/knj/errors.rb CHANGED
@@ -39,9 +39,12 @@ module Knj::Errors
39
39
  err.backtrace.each do |bt|
40
40
  str << "#{Knj::Web.html(bt)}<br />\n"
41
41
  end
42
+
43
+ str << "<br />\n<br />\n"
42
44
  else
43
45
  str << "#{err.class.name}: #{err.message}\n\n"
44
46
  str << err.backtrace.join("\n")
47
+ str << "\n\n"
45
48
  end
46
49
 
47
50
  return str
data/lib/knj/http2.rb CHANGED
@@ -165,6 +165,7 @@ class Knj::Http2
165
165
  header_str << "#{@nl}"
166
166
 
167
167
  print "Http2: Writing headers.\n" if @debug
168
+ print "Header str: #{header_str}\n" if @debug
168
169
  self.write(header_str)
169
170
 
170
171
  print "Http2: Reading response.\n" if @debug
@@ -209,25 +210,46 @@ class Knj::Http2
209
210
  if !@args.key?(:encoding_gzip) or @args[:encoding_gzip]
210
211
  headers["Accept-Encoding"] = "gzip"
211
212
  else
212
- headers["Accept-Encoding"] = "none"
213
+ #headers["Accept-Encoding"] = "none"
213
214
  end
214
215
 
215
216
  return headers
216
217
  end
217
218
 
218
- def self.post_convert_data(pdata)
219
+ #This is used to convert a hash to valid post-data recursivly.
220
+ def self.post_convert_data(pdata, args = nil)
219
221
  praw = ""
220
222
 
221
223
  if pdata.is_a?(Hash)
222
224
  pdata.each do |key, val|
223
225
  praw << "&" if praw != ""
224
- praw << "#{Knj::Web.urlenc(Knj::Http2.post_convert_data(key))}=#{Knj::Web.urlenc(Knj::Http2.post_convert_data(val))}"
226
+
227
+ if args and args[:orig_key]
228
+ key = "#{args[:orig_key]}[#{key}]"
229
+ end
230
+
231
+ if val.is_a?(Hash) or val.is_a?(Array)
232
+ praw << self.post_convert_data(val, {:orig_key => key})
233
+ else
234
+ praw << "#{Knj::Web.urlenc(key)}=#{Knj::Web.urlenc(Knj::Http2.post_convert_data(val))}"
235
+ end
225
236
  end
226
237
  elsif pdata.is_a?(Array)
227
238
  count = 0
228
239
  pdata.each do |val|
240
+ if args and args[:orig_key]
241
+ key = "#{args[:orig_key]}[#{count}]"
242
+ else
243
+ key = count
244
+ end
245
+
246
+ if val.is_a?(Hash) or val.is_a?(Array)
247
+ praw << self.post_convert_data(val, {:orig_key => key})
248
+ else
249
+ praw << "#{Knj::Web.urlenc(key)}=#{Knj::Web.urlenc(Knj::Http2.post_convert_data(val))}"
250
+ end
251
+
229
252
  count += 1
230
- praw << "#{count}=#{Knj::Web.urlenc(Knj::Http2.post_convert_data(val))}"
231
253
  end
232
254
  else
233
255
  return pdata.to_s
@@ -246,7 +268,7 @@ class Knj::Http2
246
268
  praw = Knj::Http2.post_convert_data(pdata)
247
269
 
248
270
  header_str = "POST /#{addr} HTTP/1.1#{@nl}"
249
- header_str << self.header_str(self.default_headers(args).merge("Content-Length" => praw.length), args)
271
+ header_str << self.header_str(self.default_headers(args).merge("Content-Type" => "application/x-www-form-urlencoded", "Content-Length" => praw.length), args)
250
272
  header_str << "#{@nl}"
251
273
  header_str << praw
252
274
 
@@ -266,45 +288,89 @@ class Knj::Http2
266
288
  @mutex.synchronize do
267
289
  boundary = Digest::MD5.hexdigest(Time.now.to_f.to_s)
268
290
 
269
- praw = ""
270
- pdata.each do |key, val|
271
- praw << "--#{boundary}#{@nl}"
272
-
273
- if val.class.name == "Tempfile" and val.respond_to?("original_filename")
274
- praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val.original_filename}\";#{@nl}"
275
- praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
276
- elsif val.is_a?(Hash) and val[:filename]
277
- praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val[:filename]}\";#{@nl}"
278
- praw << "Content-Length: #{val[:content].to_s.bytesize}#{@nl}"
279
- else
280
- praw << "Content-Disposition: form-data; name=\"#{key}\";#{@nl}"
281
- praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
291
+ #Generate 'praw'-variable with post-content.
292
+ tmp_path = "#{Knj::Os.tmpdir}/knj_http2_post_multiepart_tmp_#{boundary}"
293
+
294
+ begin
295
+ File.open(tmp_path, "w") do |praw|
296
+ pdata.each do |key, val|
297
+ praw << "--#{boundary}#{@nl}"
298
+
299
+ if val.class.name == "Tempfile" and val.respond_to?("original_filename")
300
+ praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val.original_filename}\";#{@nl}"
301
+ praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
302
+ elsif val.is_a?(Hash) and val[:filename]
303
+ praw << "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val[:filename]}\";#{@nl}"
304
+
305
+ if val[:content]
306
+ praw << "Content-Length: #{val[:content].to_s.bytesize}#{@nl}"
307
+ elsif val[:fpath]
308
+ praw << "Content-Length: #{File.size(val[:fpath])}#{@nl}"
309
+ else
310
+ raise "Could not figure out where to get content from."
311
+ end
312
+ else
313
+ praw << "Content-Disposition: form-data; name=\"#{key}\";#{@nl}"
314
+ praw << "Content-Length: #{val.to_s.bytesize}#{@nl}"
315
+ end
316
+
317
+ praw << "Content-Type: text/plain#{@nl}"
318
+ praw << @nl
319
+
320
+ if val.is_a?(StringIO)
321
+ praw << val.read
322
+ elsif val.is_a?(Hash) and val[:content]
323
+ praw << val[:content].to_s
324
+ elsif val.is_a?(Hash) and val[:fpath]
325
+ File.open(val[:fpath], "r") do |fp|
326
+ begin
327
+ while data = fp.sysread(4096)
328
+ praw << data
329
+ end
330
+ rescue EOFError
331
+ #ignore.
332
+ end
333
+ end
334
+ else
335
+ praw << val.to_s
336
+ end
337
+
338
+ praw << @nl
339
+ end
340
+
341
+ praw << "--#{boundary}--"
282
342
  end
283
343
 
284
- praw << "Content-Type: text/plain#{@nl}"
285
- praw << @nl
286
344
 
287
- if val.is_a?(StringIO)
288
- praw << val.read
289
- elsif val.is_a?(Hash) and val[:content]
290
- praw << val[:content].to_s
291
- else
292
- praw << val.to_s
345
+ #Generate header-string containing 'praw'-variable.
346
+ header_str = "POST /#{addr} HTTP/1.1#{@nl}"
347
+ header_str << self.header_str(self.default_headers(args).merge(
348
+ "Content-Type" => "multipart/form-data; boundary=#{boundary}",
349
+ "Content-Length" => File.size(tmp_path)
350
+ ), args)
351
+ header_str << @nl
352
+
353
+
354
+ #Debug.
355
+ print "Headerstr: #{header_str}\n" if @debug
356
+
357
+
358
+ #Write and return.
359
+ self.write(header_str)
360
+ File.open(tmp_path, "r") do |fp|
361
+ begin
362
+ while data = fp.sysread(4096)
363
+ @sock.write(data)
364
+ end
365
+ rescue EOFError
366
+ #ignore.
367
+ end
293
368
  end
294
369
 
295
- praw << @nl
370
+ return self.read_response(args)
371
+ ensure
372
+ File.unlink(tmp_path) if File.exists?(tmp_path)
296
373
  end
297
-
298
- header_str = "POST /#{addr} HTTP/1.1#{@nl}"
299
- header_str << self.header_str(self.default_headers(args).merge("Content-Type" => "multipart/form-data; boundary=#{boundary}", "Content-Length" => praw.bytesize), args)
300
- header_str << "#{@nl}"
301
- header_str << praw
302
- header_str << "--#{boundary}--"
303
-
304
- print "Headerstr: #{header_str}\n" if @debug
305
-
306
- self.write(header_str)
307
- return self.read_response(args)
308
374
  end
309
375
  end
310
376
 
@@ -40,10 +40,12 @@ class KnjDB_mysql
40
40
  @java_rs_data.delete(id)
41
41
  end
42
42
 
43
+ #Cleans the wref-map holding the tables.
43
44
  def clean
44
45
  self.tables.clean if self.tables
45
46
  end
46
47
 
48
+ #Respawns the connection to the MySQL-database.
47
49
  def reconnect
48
50
  case @subtype
49
51
  when "mysql"
@@ -243,7 +245,7 @@ class KnjDB_mysql
243
245
  when "\n" then "\\n"
244
246
  when "\r" then "\\r"
245
247
  when "\032" then "\\Z"
246
- else "\\" + $1
248
+ else "\\#{$1}"
247
249
  end
248
250
  end
249
251
  end
@@ -296,10 +298,18 @@ class KnjDB_mysql
296
298
 
297
299
  #Inserts multiple rows in a table. Can return the inserted IDs if asked to in arguments.
298
300
  def insert_multi(tablename, arr_hashes, args = nil)
299
- sql = "INSERT INTO `#{self.esc_table(tablename)}` ("
301
+ sql = "INSERT INTO `#{tablename}` ("
300
302
 
301
303
  first = true
302
- arr_hashes.first.keys.each do |col_name|
304
+ if args and args[:keys]
305
+ keys = args[:keys]
306
+ elsif arr_hashes.first.is_a?(Hash)
307
+ keys = arr_hashes.first.keys
308
+ else
309
+ raise "Could not figure out keys."
310
+ end
311
+
312
+ keys.each do |col_name|
303
313
  sql << "," if !first
304
314
  first = false if first
305
315
  sql << "`#{self.esc_col(col_name)}`"
@@ -316,19 +326,33 @@ class KnjDB_mysql
316
326
  end
317
327
 
318
328
  first_key = true
319
- hash.each do |key, val|
320
- if first_key
321
- first_key = false
322
- else
323
- sql << ","
329
+ if hash.is_a?(Array)
330
+ hash.each do |val|
331
+ if first_key
332
+ first_key = false
333
+ else
334
+ sql << ","
335
+ end
336
+
337
+ sql << "'#{self.escape(val)}'"
338
+ end
339
+ else
340
+ hash.each do |key, val|
341
+ if first_key
342
+ first_key = false
343
+ else
344
+ sql << ","
345
+ end
346
+
347
+ sql << "'#{self.escape(val)}'"
324
348
  end
325
-
326
- sql << "'#{self.escape(val)}'"
327
349
  end
328
350
  end
329
351
 
330
352
  sql << ")"
331
353
 
354
+ return sql if args and args[:return_sql]
355
+
332
356
  self.query(sql)
333
357
 
334
358
  if args and args[:return_id]
@@ -348,9 +372,22 @@ class KnjDB_mysql
348
372
  return nil
349
373
  end
350
374
  end
375
+
376
+ #Starts a transaction, yields the database and commits at the end.
377
+ def transaction
378
+ @knjdb.q("START TRANSACTION")
379
+
380
+ begin
381
+ yield(@knjdb)
382
+ ensure
383
+ @knjdb.q("COMMIT")
384
+ end
385
+ end
351
386
  end
352
387
 
388
+ #This class controls the results for the normal MySQL-driver.
353
389
  class KnjDB_mysql_result
390
+ #Constructor. This should not be called manually.
354
391
  def initialize(driver, result)
355
392
  @driver = driver
356
393
  @result = result
@@ -365,17 +402,20 @@ class KnjDB_mysql_result
365
402
  end
366
403
  end
367
404
 
405
+ #Returns a single result.
368
406
  def fetch
369
407
  return self.fetch_hash_symbols if @driver.knjdb.opts[:return_keys] == "symbols"
370
408
  return self.fetch_hash_strings
371
409
  end
372
410
 
411
+ #Returns a single result as a hash with strings as keys.
373
412
  def fetch_hash_strings
374
413
  @mutex.synchronize do
375
414
  return @result.fetch_hash
376
415
  end
377
416
  end
378
417
 
418
+ #Returns a single result as a hash with symbols as keys.
379
419
  def fetch_hash_symbols
380
420
  fetched = nil
381
421
  @mutex.synchronize do
@@ -394,6 +434,7 @@ class KnjDB_mysql_result
394
434
  return ret
395
435
  end
396
436
 
437
+ #Loops over every result yielding it.
397
438
  def each
398
439
  while data = self.fetch_hash_symbols
399
440
  yield(data)
@@ -401,7 +442,9 @@ class KnjDB_mysql_result
401
442
  end
402
443
  end
403
444
 
445
+ #This class controls the unbuffered result for the normal MySQL-driver.
404
446
  class KnjDB_mysql_unbuffered_result
447
+ #Constructor. This should not be called manually.
405
448
  def initialize(conn, opts, result)
406
449
  @conn = conn
407
450
  @result = result
@@ -415,6 +458,7 @@ class KnjDB_mysql_unbuffered_result
415
458
  end
416
459
  end
417
460
 
461
+ #Lods the keys for the object.
418
462
  def load_keys
419
463
  @keys = []
420
464
  keys = @res.fetch_fields
@@ -423,6 +467,7 @@ class KnjDB_mysql_unbuffered_result
423
467
  end
424
468
  end
425
469
 
470
+ #Returns a single result.
426
471
  def fetch
427
472
  if @enum
428
473
  begin
@@ -462,6 +507,7 @@ class KnjDB_mysql_unbuffered_result
462
507
  end
463
508
  end
464
509
 
510
+ #Loops over every single result yielding it.
465
511
  def each
466
512
  while data = self.fetch
467
513
  yield(data)
@@ -469,11 +515,14 @@ class KnjDB_mysql_unbuffered_result
469
515
  end
470
516
  end
471
517
 
518
+ #This class controls the result for the MySQL2 driver.
472
519
  class KnjDB_mysql2_result
520
+ #Constructor. This should not be called manually.
473
521
  def initialize(result)
474
522
  @result = result
475
523
  end
476
524
 
525
+ #Returns a single result.
477
526
  def fetch
478
527
  @enum = @result.to_enum if !@enum
479
528
 
@@ -484,17 +533,19 @@ class KnjDB_mysql2_result
484
533
  end
485
534
  end
486
535
 
536
+ #Loops over every single result yielding it.
487
537
  def each
488
538
  @result.each do |res|
489
539
  #This sometimes happens when streaming results...
490
540
  next if !res
491
-
492
541
  yield(res)
493
542
  end
494
543
  end
495
544
  end
496
545
 
546
+ #This class controls the result for the Java-MySQL-driver.
497
547
  class KnjDB_java_mysql_result
548
+ #Constructor. This should not be called manually.
498
549
  def initialize(knjdb, opts, result)
499
550
  @knjdb = knjdb
500
551
  @result = result
@@ -1,10 +1,11 @@
1
+ #This class handels various MySQL-column-specific operations.
1
2
  class KnjDB_mysql::Columns
2
- attr_reader :db
3
-
3
+ #Constructor. Should not be called manually.
4
4
  def initialize(args)
5
5
  @args = args
6
6
  end
7
7
 
8
+ #Returns the SQL for this column.
8
9
  def data_sql(data)
9
10
  raise "No type given." if !data["type"]
10
11
 
@@ -30,9 +31,11 @@ class KnjDB_mysql::Columns
30
31
  end
31
32
  end
32
33
 
34
+ #This class handels every MySQL-column, that can be returned from a table-object.
33
35
  class KnjDB_mysql::Columns::Column
34
36
  attr_reader :args
35
37
 
38
+ #Constructor. Should not be called manually.
36
39
  def initialize(args)
37
40
  @args = args
38
41
  end
@@ -42,14 +45,17 @@ class KnjDB_mysql::Columns::Column
42
45
  return @args[:data][:Field]
43
46
  end
44
47
 
48
+ #Returns the name of the column.
45
49
  def name
46
50
  return @args[:data][:Field]
47
51
  end
48
52
 
53
+ #Returns the table-object that this column belongs to.
49
54
  def table
50
55
  return @args[:db].tables[@args[:table_name]]
51
56
  end
52
57
 
58
+ #Returns all data of the column in the knjdb-format.
53
59
  def data
54
60
  return {
55
61
  "type" => self.type,
@@ -62,37 +68,43 @@ class KnjDB_mysql::Columns::Column
62
68
  }
63
69
  end
64
70
 
71
+ #Returns the type of the column (integer, varchar etc.).
65
72
  def type
66
73
  if !@type
67
74
  if match = @args[:data][:Type].match(/^([A-z]+)$/)
68
75
  @maxlength = false
69
- @type = match[0]
76
+ @type = match[0].to_sym
70
77
  elsif match = @args[:data][:Type].match(/^decimal\((\d+),(\d+)\)$/)
71
78
  @maxlength = "#{match[1]},#{match[2]}"
72
- @type = "decimal"
79
+ @type = :decimal
73
80
  elsif match = @args[:data][:Type].match(/^enum\((.+)\)$/)
74
81
  @maxlength = match[1]
75
- @type = "enum"
76
- elsif match = @args[:data][:Type].match(/^(.+)\((\d+)\)$/)
77
- @maxlength = match[2]
78
- @type = match[1]
82
+ @type = :enum
83
+ elsif match = @args[:data][:Type].match(/^(.+)\((\d+)\)/)
84
+ @maxlength = match[2].to_i
85
+ @type = match[1].to_sym
79
86
  end
87
+
88
+ raise "Still not type from: '#{@args[:data][:Type]}'." if @type.to_s.strip.length <= 0
80
89
  end
81
90
 
82
91
  return @type
83
92
  end
84
93
 
94
+ #Return true if the columns allows null. Otherwise false.
85
95
  def null?
86
96
  return false if @args[:data][:Null] == "NO"
87
97
  return true
88
98
  end
89
99
 
100
+ #Returns the maxlength.
90
101
  def maxlength
91
- self.type
102
+ self.type if !@maxlength
92
103
  return @maxlength if @maxlength
93
104
  return false
94
105
  end
95
106
 
107
+ #Returns the default value for the column.
96
108
  def default
97
109
  return false if (self.type == "datetime" or self.type == "date") and @args[:data][:Default].to_s.strip.length <= 0
98
110
  return false if (self.type == "int" or self.type == "bigint") and @args[:data][:Default].to_s.strip.length <= 0
@@ -100,24 +112,30 @@ class KnjDB_mysql::Columns::Column
100
112
  return @args[:data][:Default]
101
113
  end
102
114
 
115
+ #Returns true if the column is the primary key. Otherwise false.
103
116
  def primarykey?
104
- return false if @args[:data][:pk].to_i == 0
105
- return true
117
+ return true if @args[:data][:Key] == "PRI"
118
+ return false
106
119
  end
107
120
 
121
+ #Returns true if the column is auto-increasing. Otherwise false.
108
122
  def autoincr?
109
123
  return true if @args[:data][:Extra].index("auto_increment") != nil
110
124
  return false
111
125
  end
112
126
 
127
+ #Returns the comment for the column.
113
128
  def comment
114
129
  return @args[:data][:Comment]
115
130
  end
116
131
 
132
+ #Drops the column from the table.
117
133
  def drop
118
134
  @args[:db].query("ALTER TABLE `#{@args[:table_name]}` DROP COLUMN `#{self.name}`")
135
+ return nil
119
136
  end
120
137
 
138
+ #Changes the column properties by the given hash.
121
139
  def change(data)
122
140
  col_escaped = "#{@args[:db].enc_col}#{@args[:db].esc_col(self.name)}#{@args[:db].enc_col}"
123
141
  table_escape = "#{@args[:db].enc_table}#{@args[:db].esc_table(self.table.name)}#{@args[:db].enc_table}"