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.

Methods

[]   []=   bind_param   bind_params   cancel   column_info   execute   fetch   fetch_all   fetch_many   fetch_scroll   finish   new  

Attributes

raise_error  [RW] 

Public Class methods

[Source]

    # File lib/dbi/base_classes/statement.rb, line 14
14:         def initialize(attr=nil)
15:             @attr = attr || {}
16:         end

Public Instance methods

Get statement attributes.

[Source]

     # File lib/dbi/base_classes/statement.rb, line 159
159:         def [](attr)
160:             @attr ||= { }
161:             @attr[attr]
162:         end

Set statement attributes. DBD Optional.

[Source]

     # 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.

[Source]

    # 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.

[Source]

    # 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.

[Source]

    # 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.

[Source]

    # 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.

[Source]

    # File lib/dbi/base_classes/statement.rb, line 36
36:         def execute
37:             raise NotImplementedError
38:         end

Fetch the next row in the result set. DBD Required.

DBI::Row is responsible for formatting the data the DBD provides it.

[Source]

    # File lib/dbi/base_classes/statement.rb, line 54
54:         def fetch
55:             raise NotImplementedError
56:         end

Fetch all available rows. Result is Array of DBI::Row.

[Source]

     # 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 x rows. The result is Array of DBI::Row.

[Source]

     # File lib/dbi/base_classes/statement.rb, line 123
123:         def fetch_many(cnt)
124:             rows = []
125:             cnt.times do
126:                 row = fetch
127:                 break if row.nil?
128:                 rows << row.dup
129:             end
130: 
131:             if rows.empty?
132:                 nil
133:             else
134:                 rows
135:             end
136:         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:

  • SQL_FETCH_NEXT: fetch the next result.
  • SQL_FETCH_LAST: fetch the last result, period.
  • SQL_FETCH_RELATIVE: fetch the result at the offset.

Other constants can be used, but if this method is not supplied by the driver, they will result in a raise of DBI::NotSupportedError.

[Source]

     # 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

Close the statement and any result cursors. DBD Required.

Note:Most implementations will fail miserably if you forget to finish your statement handles.

[Source]

    # File lib/dbi/base_classes/statement.rb, line 45
45:         def finish
46:             raise NotImplementedError
47:         end

[Validate]