Class: OrangeLib::OracleHandler
- Inherits:
-
Object
- Object
- OrangeLib::OracleHandler
- Defined in:
- lib/orange_lib/oracle_handler.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#execute(sql_string) ⇒ Array
Execute a sql statement.
-
#execute_update(sql_string) ⇒ Int
Execute a sql statement for updating.
-
#initialize(user, password, host, port, service_name) ⇒ OracleHandler
constructor
Initialize a connection to cassandra.
Constructor Details
#initialize(user, password, host, port, service_name) ⇒ OracleHandler
Initialize a connection to cassandra
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/orange_lib/oracle_handler.rb', line 18 def initialize(user, password, host, port, service_name) oradriver = OracleDriver.new DriverManager.registerDriver oradriver @user = user @password = password @host = host @port = port @service_name = service_name begin @connection = DriverManager.get_connection("jdbc:oracle:thin:@#{@host}:#{@port.to_i}/#{@service_name}", @user, @password) @connection.auto_commit = false rescue Exception => ex raise "Connection failed! :X url=jdbc:oracle:thin:@#{host}:#{port}/#{service_name}\nException: #{ex}" return end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/orange_lib/oracle_handler.rb', line 9 def connection @connection end |
Instance Method Details
#execute(sql_string) ⇒ Array
Execute a sql statement.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/orange_lib/oracle_handler.rb', line 46 def execute(sql_string) error_msg = nil open_connection if @connection.is_closed begin stmt = @connection.prepare_statement(sql_string) row_set = stmt.execute_query = row_set. column_count = .get_column_count columns = {} for index in 1..column_count do label = .get_column_label(index) columns[label] = {} columns[label][:type] = .get_column_type_name(index).to_s.downcase columns[label][:precision] = .get_precision(index) columns[label][:scale] = .get_scale(index) end materias = [] while row_set.next hash_materia = {} columns.each do |column_name, column_type| if column_type[:type] == 'blob' value = row_set.get_blob(column_name.to_s) else value = row_set.get_string(column_name.to_s) end hash_materia[column_name] = convert_data_type(column_type, value) end materias << hash_materia end materias rescue Exception => ex error_msg = "Oracle query execution failed.\nException: #{ex}" ensure row_set.close unless row_set.nil? stmt.close unless stmt.nil? @connection.close unless (@connection.is_closed || @connection.nil?) raise error_msg unless error_msg.nil? end end |
#execute_update(sql_string) ⇒ Int
Execute a sql statement for updating.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/orange_lib/oracle_handler.rb', line 95 def execute_update(sql_string) error_msg = nil open_connection if @connection.is_closed begin stmt = @connection.prepare_statement(sql_string) number_of_rows = stmt.execute_update @connection.commit number_of_rows rescue Exception => ex @connection.rollback error_msg = "Oracle query execution failed.\nException: #{ex}" ensure stmt.close unless stmt.nil? @connection.close unless (@connection.is_closed || @connection.nil?) raise error_msg unless error_msg.nil? end end |