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
-
#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 ⇒ Sql
constructor
Creates a new instance of this class.
-
#prepare(connection, 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
Constructor Details
#initialize ⇒ 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 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 33 def initialize @auto_statement_close = true end |
Instance Attribute Details
#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.
47 48 49 50 51 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 47 def self.execute(connection, sql, *binds) sql_object = self.new sql_object.prepare(connection, 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.
39 40 41 42 43 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 39 def self.prepare(connection, sql) sql_object = self.new sql_object.disable_auto_statement_close sql_object.prepare(connection,sql) end |
Instance Method Details
#close ⇒ Object
Closes both the prepared SQL statement stored in @statement and any result set stored in @result_set
86 87 88 89 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 86 def close close_result_set close_statement end |
#close_statement ⇒ Object
Closes the JDBC statement stored in @statement
92 93 94 95 96 97 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 92 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
107 108 109 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 107 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 67 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(connection, 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.
57 58 59 60 61 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 57 def prepare(connection, sql) @sql = sql @statement = connection.prepare_statement(@sql) self end |