| Class | DBI::BaseStatement |
| In: |
lib/dbi/base_classes/statement.rb
|
| Parent: | Base |
StatementHandles are used to encapsulate the process of managing a statement (DDL or DML) and its parameters, sending it to the database, and gathering any results from the execution of that statement.
As with the other `Base` classes, the terms "DBD Required" and "DBD Optional" are defined in DBI::BaseDatabase.
| raise_error | [RW] |
# File lib/dbi/base_classes/statement.rb, line 14
14: def initialize(attr=nil)
15: @attr = attr || {}
16: end
Get statement attributes.
# File lib/dbi/base_classes/statement.rb, line 159
159: def [](attr)
160: @attr ||= { }
161: @attr[attr]
162: end
Set statement attributes. DBD Optional.
# File lib/dbi/base_classes/statement.rb, line 167
167: def []=(attr, value)
168: raise NotSupportedError
169: end
Bind a parameter to the statement. DBD Required.
The parameter number is numeric and indexes starting at 1. This corresponds to the question marks (?) in the statement from the left-most part of the statement moving forward.
The value may be any ruby type. How these are handled is DBD-dependent, but the vast majority of DBDs will convert these to string inside the query.
# File lib/dbi/base_classes/statement.rb, line 29
29: def bind_param(param, value, attribs)
30: raise NotImplementedError
31: end
Take a list of bind variables and bind them successively using bind_param.
# File lib/dbi/base_classes/statement.rb, line 74
74: def bind_params(*bindvars)
75: bindvars.each_with_index {|val,i| bind_param(i+1, val, nil) }
76: self
77: end
Cancel any result cursors. DBD Optional, but intentionally does not raise any exception as it‘s used internally to maintain consistency.
# File lib/dbi/base_classes/statement.rb, line 83
83: def cancel
84: end
returns result-set column information as array of hashs, where each hash represents one column. See BaseDatabase#columns. DBD Required.
# File lib/dbi/base_classes/statement.rb, line 63
63: def column_info
64: raise NotImplementedError
65: end
Execute the statement with the known binds. DBD Required.
# File lib/dbi/base_classes/statement.rb, line 36
36: def execute
37: raise NotImplementedError
38: end
Fetch all available rows. Result is Array of DBI::Row.
# File lib/dbi/base_classes/statement.rb, line 141
141: def fetch_all
142: rows = []
143: loop do
144: row = fetch
145: break if row.nil?
146: rows << row.dup
147: end
148:
149: if rows.empty?
150: nil
151: else
152: rows
153: end
154: end
fetch_scroll is provided with a direction and offset and works similar to how seek() is used on files.
The constants available for direction are as follows:
Other constants can be used, but if this method is not supplied by the driver, they will result in a raise of DBI::NotSupportedError.
# File lib/dbi/base_classes/statement.rb, line 100
100: def fetch_scroll(direction, offset)
101: case direction
102: when SQL_FETCH_NEXT
103: return fetch
104: when SQL_FETCH_LAST
105: last_row = nil
106: while (row=fetch) != nil
107: last_row = row
108: end
109: return last_row
110: when SQL_FETCH_RELATIVE
111: raise NotSupportedError if offset <= 0
112: row = nil
113: offset.times { row = fetch; break if row.nil? }
114: return row
115: else
116: raise NotSupportedError
117: end
118: end