Module: DBI::DBD::Jdbc::TypeConversions

Included in:
Database, Statement
Defined in:
lib/dbd/jdbc/type_conversion.rb

Overview

© 2009 Chad Johnson <[email protected]>.

(C) 2008 Ola Bini <[email protected]>.
(C) 2006 Kristopher Schmidt <[email protected]>.

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Instance Method Summary collapse

Instance Method Details

#date_to_jdbcdate(dbidate) ⇒ Object



59
60
61
62
63
# File 'lib/dbd/jdbc/type_conversion.rb', line 59

def date_to_jdbcdate(dbidate)
  cal = Calendar.getInstance()
  set_calendar_date_fields(dbidate, cal)
  return java.sql.Date.new(cal.getTime().getTime())
end

#jdbc_to_dbi_sqltype(jdbctype) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dbd/jdbc/type_conversion.rb', line 33

def jdbc_to_dbi_sqltype(jdbctype)
  return case jdbctype
  when Types::BIGINT then [DBI::SQL_BIGINT, nil]
  when Types::BINARY then [DBI::SQL_BINARY, nil]
  when Types::BIT then [DBI::SQL_BIT, DBI::Type::Boolean]
  when Types::CHAR then [DBI::SQL_CHAR, nil]
  when Types::DATE then [DBI::SQL_DATE, DBI::DBD::Jdbc::Type::Timestamp]
  when Types::DECIMAL then [DBI::SQL_DECIMAL, nil]
  when Types::DOUBLE then [DBI::SQL_DOUBLE, nil]
  when Types::FLOAT then [DBI::SQL_FLOAT, nil]
  when Types::INTEGER then [DBI::SQL_INTEGER, nil]
  when Types::LONGVARBINARY then [DBI::SQL_LONGVARBINARY, nil]
  when Types::LONGVARCHAR then [DBI::SQL_LONGVARCHAR, nil]
  when Types::NUMERIC then [DBI::SQL_NUMERIC, nil]
  when Types::REAL then [DBI::SQL_REAL, nil]
  when Types::SMALLINT then [DBI::SQL_SMALLINT, nil]
  when Types::TIME then [DBI::SQL_TIME, DBI::DBD::Jdbc::Type::Timestamp]
  when Types::TIMESTAMP then [DBI::SQL_TIMESTAMP, DBI::DBD::Jdbc::Type::Timestamp]
  when Types::TINYINT then [DBI::SQL_TINYINT, nil]
  when Types::VARBINARY then [DBI::SQL_VARBINARY, nil]
  when Types::VARCHAR then [DBI::SQL_VARCHAR, nil]
  else
    [DBI::SQL_OTHER, nil]
  end
end

#jdbcdate_to_date(jdbcdate) ⇒ Object



79
80
81
82
83
# File 'lib/dbd/jdbc/type_conversion.rb', line 79

def jdbcdate_to_date(jdbcdate)
  return nil if jdbcdate.nil?
  cal = get_calendar(jdbcdate)
  return ::Date.new(cal.get(Calendar::YEAR), cal.get(Calendar::MONTH)+1, cal.get(Calendar::DAY_OF_MONTH))
end

#jdbctime_to_time(jdbctime) ⇒ Object



85
86
87
88
89
# File 'lib/dbd/jdbc/type_conversion.rb', line 85

def jdbctime_to_time(jdbctime)
  return nil if jdbctime.nil?
  cal = get_calendar(jdbctime)
  return ::Time.mktime(cal.get(Calendar::YEAR), cal.get(Calendar::MONTH)+1, cal.get(Calendar::DAY_OF_MONTH), cal.get(Calendar::HOUR_OF_DAY), cal.get(Calendar::MINUTE), cal.get(Calendar::SECOND), cal.get(Calendar::MILLISECOND) * 1000)
end

#jdbctimestamp_to_timestamp(jdbctimestamp) ⇒ Object



91
92
93
94
95
# File 'lib/dbd/jdbc/type_conversion.rb', line 91

def jdbctimestamp_to_timestamp(jdbctimestamp)
  return nil if jdbctimestamp.nil?
  cal = get_calendar(jdbctimestamp)
  return ::DateTime.new(cal.get(Calendar::YEAR), cal.get(Calendar::MONTH)+1, cal.get(Calendar::DAY_OF_MONTH), cal.get(Calendar::HOUR_OF_DAY), cal.get(Calendar::MINUTE), cal.get(Calendar::SECOND))
end

#time_to_jdbctime(dbitime) ⇒ Object



65
66
67
68
69
70
# File 'lib/dbd/jdbc/type_conversion.rb', line 65

def time_to_jdbctime(dbitime)
  cal = Calendar.getInstance()
  set_calendar_date_fields(dbitime, cal)
  set_calendar_time_fields(dbitime, cal)
  return java.sql.Time.new(cal.getTime().getTime())
end

#timestamp_to_jdbctimestamp(dbitimestamp) ⇒ Object



72
73
74
75
76
77
# File 'lib/dbd/jdbc/type_conversion.rb', line 72

def timestamp_to_jdbctimestamp(dbitimestamp)
  cal = Calendar.getInstance()
  set_calendar_date_fields(dbitimestamp, cal)
  set_calendar_time_fields(dbitimestamp, cal)
  return java.sql.Timestamp.new(cal.getTime().getTime())
end