Class: DBI::SQL::BasicQuote::Coerce

Inherits:
Object
  • Object
show all
Defined in:
lib/dbi/sql.rb

Overview

by Masatoshi SEKI

Instance Method Summary collapse

Instance Method Details

#as_bool(str) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/dbi/sql.rb', line 41

def as_bool(str)
  if str == "t" or str == "1"
    true
  elsif str == "f" or str == "0"
    false
  else
    nil
  end
end

#as_date(str) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/dbi/sql.rb', line 76

def as_date(str)
  return nil if str.nil?
  ary = ParseDate.parsedate(str)
  DBI::Date.new(*ary[0,3])
rescue
  nil
end

#as_float(str) ⇒ Object



32
33
34
35
# File 'lib/dbi/sql.rb', line 32

def as_float(str)
  return nil if str.nil?
  str.to_f
end

#as_int(str) ⇒ Object



27
28
29
30
# File 'lib/dbi/sql.rb', line 27

def as_int(str)
  return nil if str.nil?
  if str == "" then nil else str.to_i end 
end

#as_str(str) ⇒ Object



37
38
39
# File 'lib/dbi/sql.rb', line 37

def as_str(str)
  str
end

#as_time(str) ⇒ Object



51
52
53
54
55
# File 'lib/dbi/sql.rb', line 51

def as_time(str)
  return nil if str.nil? or str.empty?
  t = ParseDate.parsedate(str)
  DBI::Time.new(*t[3,3])
end

#as_timestamp(str) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dbi/sql.rb', line 58

def as_timestamp(str)
  return nil if str.nil? or str.empty?
  ary = ParseDate.parsedate(str)
  begin 
    time = ::Time.gm(*(ary[0,6])) 
  rescue ArgumentError => ae 
    # don't fault stupid values that MySQL nevertheless stores 
    return nil 
  end
  if ary[6] =~ /^((\+|\-)\d+)(:\d+)?$/
    diff = $1.to_i * 3600  # seconds per hour 
    time -= diff
    time.localtime
  end 
  DBI::Timestamp.new(time)
end

#coerce(sym, str) ⇒ Object



85
86
87
# File 'lib/dbi/sql.rb', line 85

def coerce(sym, str)
  self.send(sym, str)
end