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.
-
#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_refcursor, #bind_string, #bind_time, #bind_value, #retrieve_date, #retrieve_int, #retrieve_number, #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 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 33 def initialize 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.
45 46 47 48 49 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 45 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.
38 39 40 41 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 38 def self.prepare(connection, sql) sql_object = self.new 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
82 83 84 85 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 82 def close close_result_set close_statement end |
#close_statement ⇒ Object
Closes the JDBC statement stored in @statement
88 89 90 91 92 93 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 88 def close_statement if @statement @statement.close @statement = nil end 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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 65 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() close_statement 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.
55 56 57 58 59 |
# File 'lib/simple_oracle_jdbc/sql.rb', line 55 def prepare(connection, sql) @sql = sql @statement = connection.prepare_statement(@sql) self end |