Class: SimpleOracleJDBC::Sql

Inherits:
Object
  • Object
show all
Includes:
Binding, ResultSet
Defined in:
lib/simple_oracle_jdbc/sql.rb

Constant Summary

Constants included from Binding

Binding::RUBY_TO_JDBC_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#initializeSql

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_setJDBC Result Set

Returns the raw JDBC result set after the statement is executed

Returns:

  • (JDBC Result Set)

    Returns the raw JDBC result set after the statement is executed



# File 'lib/simple_oracle_jdbc/sql.rb', line 5

#sqlString

Returns the original SQL string used to create the object

Returns:

  • (String)

    Returns the original SQL string used to create the object



# File 'lib/simple_oracle_jdbc/sql.rb', line 5

#statementJDBC Prepared Statement

Returns the raw JDBC prepared statement

Returns:

  • (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

#closeObject

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_statementObject

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