Class: DBI::DBD::Jdbc::Statement

Inherits:
BaseStatement
  • Object
show all
Includes:
TypeConversions
Defined in:
lib/dbd/jdbc/statement.rb

Overview

Models the DBI::BaseStatement API to create DBI::StatementHandle objects.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeConversions

#date_to_jdbcdate, #jdbc_to_dbi_sqltype, #jdbcdate_to_date, #jdbctime_to_time, #jdbctimestamp_to_timestamp, #time_to_jdbctime, #timestamp_to_jdbctimestamp

Constructor Details

#initialize(statement, nulltype, allow_scroll = false) ⇒ Statement

Returns a new instance of Statement.



36
37
38
39
40
41
42
# File 'lib/dbd/jdbc/statement.rb', line 36

def initialize(statement, nulltype, allow_scroll = false)
  @statement = statement
  @nulltype = nulltype
  @allow_scroll = allow_scroll
  @rows = nil
  @data = []
end

Class Method Details

.from_java_statement(java_statement, type_coercion = false, null_type = java.sql.Types::VARCHAR) ⇒ Object

Raises:

  • (DBI::DatabaseError)


178
179
180
181
182
# File 'lib/dbd/jdbc/statement.rb', line 178

def self.from_java_statement(java_statement, type_coercion = false, null_type = java.sql.Types::VARCHAR)
  raise DBI::DatabaseError.new("Only java.sql.PreparedStatement instances accepted") unless java_statement.kind_of?(java.sql.PreparedStatement)

  DBI::StatementHandle.new(Statement.new(java_statement, null_type), true, true, type_coercion)
end

Instance Method Details

#bind_param(param, value, attribs) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dbd/jdbc/statement.rb', line 44

def bind_param(param, value, attribs)
  raise InterfaceError.new("Statement.bind_param only supports numeric placeholder numbers") unless param.is_a?(Fixnum)
  if value.nil?
    @statement.setNull(param, @nulltype)
  elsif value.is_a?(String)
    #
    # The syntax below appears to be the best way to ensure that
    # RubyStrings get converted to Java Strings correctly if it
    # contains UTF-8.
    #
    # java.lang.String.new() will assume the system default
    # encoding when converting the RubyString bytes ....
    #
    @statement.setString(param, java.lang.String.new(value))
  elsif value.is_a?(Fixnum)
    #no reason not to coerce it to a long?
    @statement.setLong(param, value)
  elsif value.is_a?(Float)
    #despite DBD spec saying Float->SQL Float, using Double gives
    #better precision and passes tests that setFloat does not.
    @statement.setDouble(param, value)
  elsif value.is_a?(::DateTime) || value.is_a?(DBI::Timestamp)
    @statement.setTimestamp(param, timestamp_to_jdbctimestamp(value))
  elsif value.is_a?(::Date) || value.is_a?(DBI::Date)
    @statement.setDate(param, date_to_jdbcdate(value))
  elsif value.is_a?(::Time) || value.is_a?(DBI::Time)
    @statement.setTime(param, time_to_jdbctime(value))
  else
    @statement.setObject(param, value)
  end
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#column_infoObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dbd/jdbc/statement.rb', line 153

def column_info
  info = Array.new
  return info unless @rs
   = @rs.()
  (1...getColumnCount()).each do |columnNumber|
    type_name, dbi_type = jdbc_to_dbi_sqltype(.getColumnType(columnNumber))
    info << {
      "name" => .getColumnName(columnNumber),
      "sql_type" => type_name,
      "type_name" => .getColumnTypeName(columnNumber),
      "precision" => .getPrecision(columnNumber),
      "scale" => .getScale(columnNumber),
      "nullable" => (.isNullable(columnNumber) == 1)
    }
    info[-1]["dbi_type"] = dbi_type if dbi_type
  end
  return info
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#executeObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dbd/jdbc/statement.rb', line 78

def execute
  if @statement.execute()
    @rs = @statement.getResultSet
    @rows = nil
    @data.clear
  else
    @rs = nil
    @rows = @statement.getUpdateCount
  end
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#fetchObject



99
100
101
102
103
104
105
106
107
# File 'lib/dbd/jdbc/statement.rb', line 99

def fetch
  if (@rs && @rs.next())
    return fill_data
  else
    return nil
  end
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#fetch_scroll(direction, offset) ⇒ Object

Unless “allow_scroll” was set on this connection this will default to the DBI::BaseStatement#fetch_scroll implementation.

See DBI::BaseStatement#fetch_scroll. These additional constants are supported when “allow_scroll” is set on the connection.

  • DBI::SQL_FETCH_PRIOR: Fetch the row previous to the current one.

  • DBI::SQL_FETCH_FIRST: Fetch the first row.

  • DBI::SQL_FETCH_ABSOLUTE: Fetch the row at the offset provided.

  • DBI::SQL_FETCH_RELATIVE: Fetch the row at the current point + offset.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dbd/jdbc/statement.rb', line 130

def fetch_scroll(direction, offset)
  return super(direction, offset) unless @allow_scroll

  case direction
  when DBI::SQL_FETCH_NEXT
    fill_data if @rs && @rs.next()
  when DBI::SQL_FETCH_PRIOR
    fill_data if @rs && @rs.previous()
  when DBI::SQL_FETCH_FIRST
    fill_data if @rs && @rs.first()
  when DBI::SQL_FETCH_LAST
    fill_data if @rs && @rs.last()
  when DBI::SQL_FETCH_ABSOLUTE
    fill_data if @rs && @rs.absolute(offset)
  when DBI::SQL_FETCH_RELATIVE
    fill_data if @rs && @rs.relative(offset)
  else
    raise DBI::NotSupportedError
  end
rescue NativeException => error
  raise DBI::NotSupportedError.new(error.message)
end

#fill_dataObject



109
110
111
112
113
114
115
116
# File 'lib/dbd/jdbc/statement.rb', line 109

def fill_data
  @data.clear
   = @rs.()
  (1...getColumnCount()).each do |columnNumber|
    @data << get_value(columnNumber, @rs, )
  end
  return @data
end

#finishObject



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

def finish
  @statement.close()
  @rs = nil
  @rows = nil
rescue NativeException => error
  raise DBI::DatabaseError.new(error.message)
end

#rowsObject



174
175
176
# File 'lib/dbd/jdbc/statement.rb', line 174

def rows
  return @rows
end