Module: Spider::Model::Storage::Db::Connectors::OCI8
- Defined in:
- lib/spiderfw/model/storage/db/connectors/oci8.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #do_commit ⇒ Object
- #do_describe_table(conn, table) ⇒ Object
- #do_rollback ⇒ Object
- #do_start_transaction ⇒ Object
- #execute(sql, *bind_vars) ⇒ Object
- #execute_statement(stmt, *bind_vars) ⇒ Object
- #in_transaction? ⇒ Boolean
- #prepare(sql) ⇒ Object
- #prepare_value(type, value) ⇒ Object
- #release ⇒ Object
- #table_exists?(table) ⇒ Boolean
Class Method Details
.included(klass) ⇒ Object
11 12 13 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 11 def self.included(klass) klass.extend(ClassMethods) end |
Instance Method Details
#do_commit ⇒ Object
59 60 61 62 63 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 59 def do_commit return release unless transactions_enabled? curr[:conn].commit if curr[:conn] release end |
#do_describe_table(conn, table) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 182 def do_describe_table(conn, table) columns = [] t = conn.describe_table(table) t.columns.each do |c| col = { :name => c.name, :type => c.data_type.to_s.upcase, :length => c.data_size, :precision => c.precision, :scale => c.scale, :null => c.nullable? } col.delete(:length) if (col[:precision]) columns << col end columns end |
#do_rollback ⇒ Object
65 66 67 68 69 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 65 def do_rollback return release unless transactions_enabled? curr[:conn].rollback release end |
#do_start_transaction ⇒ Object
49 50 51 52 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 49 def do_start_transaction return unless transactions_enabled? connection.autocommit = false end |
#execute(sql, *bind_vars) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 84 def execute(sql, *bind_vars) begin if (bind_vars && bind_vars.length > 0) debug_vars = bind_vars.map{|var| var = var.to_s; var && var.length > 50 ? var[0..50]+"...(#{var.length-50} chars more)" : var} end curr[:last_executed] = [sql, bind_vars] if Spider.conf.get('storage.db.replace_debug_vars') debug("oci8 #{connection} executing: "+sql.gsub(/:(\d+)/){ i = $1.to_i v = bind_vars[i-1] dv = debug_vars[i-1] v.is_a?(String) ? "'#{dv}'" : dv }) else debug_vars_str = debug_vars ? debug_vars.join(', ') : '' debug("oci8 #{connection} executing:\n#{sql}\n[#{debug_vars_str}]") end query_start cursor = connection.parse(sql) return cursor if (!cursor || cursor.is_a?(Fixnum)) bind_vars.each_index do |i| var = bind_vars[i] if (var.is_a?(Oracle::OracleNilValue)) cursor.bind_param(i+1, nil, var.type, 0) else cursor.bind_param(i+1, var) end end res = cursor.exec have_result = (cursor.type == ::OCI8::STMT_SELECT) # @cursor = connection.exec(sql, *bind_vars) if (have_result) result = [] while (h = cursor.fetch_hash) h.each do |key, val| if val.respond_to?(:read) h[key] = val.read end end if block_given? yield h else result << h end end end if (have_result) unless block_given? result.extend(StorageResult) curr[:last_result] = result return result end else return res end cursor.close rescue => exc curr[:conn].break if curr[:conn] rollback! if in_transaction? #curr[:conn].logoff release if (exc. =~ /ORA-00001/) raise Spider::Model::Storage::DuplicateKey else raise exc end ensure query_finished cursor.close if cursor release if curr[:conn] && !in_transaction? post_execute end end |
#execute_statement(stmt, *bind_vars) ⇒ Object
165 166 167 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 165 def execute_statement(stmt, *bind_vars) stmt.exec(bind_vars) end |
#in_transaction? ⇒ Boolean
54 55 56 57 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 54 def in_transaction? return false unless transactions_enabled? return curr[:conn] && !curr[:conn].autocommit? end |
#prepare(sql) ⇒ Object
160 161 162 163 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 160 def prepare(sql) debug("oci8 preparing: #{sql}") return connection.parse(sql) end |
#prepare_value(type, value) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 71 def prepare_value(type, value) value = super type = type.primary_keys[0].type if type < Spider::Model::BaseModel type = Fixnum if type == Spider::DataTypes::PK return Oracle::OracleNilValue.new(Spider::Model.ruby_type(type)) if (value == nil) case type.name when 'Spider::DataTypes::Binary' return OCI8::BLOB.new(curr[:conn], value) end return value end |
#release ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 39 def release begin curr[:conn].autocommit = true if curr[:conn] super rescue self.class.remove_connection(curr[:conn], @connection_params) curr[:conn] = nil end end |
#table_exists?(table) ⇒ Boolean
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/spiderfw/model/storage/db/connectors/oci8.rb', line 170 def table_exists?(table) begin connection do |c| c.describe_table(table) end Spider.logger.debug("TABLE EXISTS #{table}") return true rescue OCIError return false end end |