Class: SimpleOracleJDBC::Sql
- Inherits:
-
Object
- Object
- SimpleOracleJDBC::Sql
- Defined in:
- lib/simple_oracle_jdbc/sql.rb
Constant Summary
Constants included from Binding
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#result_set ⇒ JDBC Result Set
Returns the raw JDBC result set after the statement is executed.
-
#sql ⇒ String
Returns the original SQL string used to create the object.
-
#statement ⇒ JDBC Prepared Statement
Returns the raw JDBC prepared statement.
Class Method Summary collapse
-
.execute(connection, sql, *binds) ⇒ Object
Takes a JDBC connection object, an SQL statement and an optional list of bind variables and will prepare and execute the sql statement, returning an SimpleOracle::Sql object.
-
.prepare(connection, sql) ⇒ Object
Takes a JDBC connection object and an SQL statement and returns a SimpleOracleJDBC::Sql object with the prepared statement.
Instance Method Summary collapse
-
#close ⇒ Object
Closes both the prepared SQL statement stored in @statement and any result set stored in @result_set.
-
#close_statement ⇒ Object
Closes the JDBC statement stored in @statement.
-
#disable_auto_statement_close ⇒ Object
If a statement was prepared, it is likely it is going to be reused, so the statement handle should not be closed after execution.
-
#execute(*binds) ⇒ Object
Executes the SQL prepared by the prepare method and binds the optional list of bind varibles.
-
#initialize(connection) ⇒ Sql
constructor
Creates a new instance of this class.
-
#prepare(sql) ⇒ Object
Given a JDBC connection and a SQL string, the sql will be stored in the @sql instance variable and a JDBC prepared statement will be stored in @statement.
Methods included from ResultSet
#all_array, #all_hash, #close_result_set, #each_array, #each_hash, #next_array, #next_hash
Methods included from Binding
#bind_date, #bind_int, #bind_number, #bind_out_parameter, #bind_raw, #bind_refcursor, #bind_string, #bind_time, #bind_value, #retrieve_date, #retrieve_int, #retrieve_number, #retrieve_raw, #retrieve_refcursor, #retrieve_string, #retrieve_time, #retrieve_value
Methods included from TypeMap
#java_date_as_date, #java_date_as_time, #java_integer_as_integer, #java_number_as_float, #java_string_as_string, #oracle_raw_as_string, #ruby_any_date_as_jdbc_date, #ruby_date_as_jdbc_date, #ruby_number_as_jdbc_number, #ruby_raw_string_as_jdbc_raw, #ruby_time_as_jdbc_timestamp
Constructor Details
#initialize(connection) ⇒ Sql
Creates a new instance of this class. Not intended to be used directly. Use the factory class methods prepare or execute instead.
33 34 35 36 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 33 def initialize(connection) @connection = connection @auto_statement_close = true end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
27 28 29 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 27 def connection @connection end |
#result_set ⇒ JDBC Result Set
Returns the raw JDBC result set after the statement is executed
|
|
# File 'lib/simple_oracle_jdbc/sql.rb', line 5
|
#sql ⇒ String
Returns the original SQL string used to create the object
|
|
# File 'lib/simple_oracle_jdbc/sql.rb', line 5
|
#statement ⇒ JDBC Prepared Statement
Returns the raw JDBC prepared statement
|
|
# File 'lib/simple_oracle_jdbc/sql.rb', line 5
|
Class Method Details
.execute(connection, sql, *binds) ⇒ Object
Takes a JDBC connection object, an SQL statement and an optional list of bind variables and will prepare and execute the sql statement, returning an SimpleOracle::Sql object.
48 49 50 51 52 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 48 def self.execute(connection, sql, *binds) sql_object = self.new(connection) sql_object.prepare(sql) sql_object.execute(*binds) end |
.prepare(connection, sql) ⇒ Object
Takes a JDBC connection object and an SQL statement and returns a SimpleOracleJDBC::Sql object with the prepared statement.
40 41 42 43 44 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 40 def self.prepare(connection, sql) sql_object = self.new(connection) sql_object.disable_auto_statement_close sql_object.prepare(sql) end |
Instance Method Details
#close ⇒ Object
Closes both the prepared SQL statement stored in @statement and any result set stored in @result_set
87 88 89 90 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 87 def close close_result_set close_statement end |
#close_statement ⇒ Object
Closes the JDBC statement stored in @statement
93 94 95 96 97 98 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 93 def close_statement if @statement @statement.close @statement = nil end end |
#disable_auto_statement_close ⇒ Object
If a statement was prepared, it is likely it is going to be reused, so the statement handle should not be closed after execution.
If the statement is directly executed, then the prepared handle was never requested and so it probably should be closed.
This method is called by the prepare class/factory method
108 109 110 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 108 def disable_auto_statement_close @auto_statement_close = false end |
#execute(*binds) ⇒ Object
Executes the SQL prepared by the prepare method and binds the optional list of bind varibles.
If the SQL statement does not return data (ie is not a select statement) then @result_set will be set to nil. Otherwise, the resulting JDBC result set will be stored in @result_set
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 68 def execute(*binds) binds.each_with_index do |b, i| bind_value(@statement, b, i+1) end # What about a select that starts with the WITH clause? unless @sql =~ /^\s*select/i @result_set = nil @statement.execute() if @auto_statement_close close_statement end else @result_set = @statement.execute_query() end self end |
#prepare(sql) ⇒ Object
Given a JDBC connection and a SQL string, the sql will be stored in the @sql instance variable and a JDBC prepared statement will be stored in @statement.
This method returns self to allow calls to be chained.
58 59 60 61 62 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 58 def prepare(sql) @sql = sql @statement = @connection.prepare_statement(@sql) self end |