Module: Spider::Model::Storage::Db::Connectors::ODBC
- Defined in:
- lib/spiderfw/model/storage/db/connectors/odbc.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #commit ⇒ Object
- #describe_table(table) ⇒ Object
- #execute(sql, *bind_vars) ⇒ Object
- #execute_statement(stmt, *bind_vars) ⇒ Object
- #in_transaction? ⇒ Boolean
-
#list_tables ⇒ Object
Methods to get information from the db #.
- #parse_url(url) ⇒ Object
- #prepare(sql) ⇒ Object
- #release ⇒ Object
- #rollback ⇒ Object
- #start_transaction ⇒ Object
- #table_exists?(table) ⇒ Boolean
- #value_to_mapper(type, value) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
7 8 9 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 7 def self.included(klass) klass.extend(ClassMethods) end |
Instance Method Details
#commit ⇒ Object
61 62 63 64 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 61 def commit @conn.commit if @conn release end |
#describe_table(table) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 145 def describe_table(table) columns = {} connection do |c| fields = c.columns(table) fields.each_hash do |f| name = f['COLUMN_NAME'] type = f['TYPE_NAME'] length = f['LENGTH']; scale = nil precision = f['PRECISION'] col = { :type => type, :length => length, :precision => precision, :scale => scale } col = parse_db_column(col) columns[name] = col end fields.drop pks = c.primary_keys(table) pks.each_hash do |pk| columns[pk['COLUMN_NAME']][:primary_key] = true end pks.drop indexes = {} inds = c.indexes(table) inds.each_hash do |ind| name = ind['INDEX_QUALIFIER'] next unless name indexes[name] = ind['COLUMN_NAME'] end inds.drop end return {:columns => columns} end |
#execute(sql, *bind_vars) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 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 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 71 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 @last_executed = [sql, bind_vars] if (Spider.conf.get('storage.db.replace_debug_vars')) cnt = -1 debug("odbc executing: "+sql.gsub('?'){ debug_vars[cnt+=1] }) else debug_vars_str = debug_vars ? debug_vars.join(', ') : '' debug("odbc executing:\n#{sql}\n[#{debug_vars_str}]") end @stmt = connection.prepare(sql) res = @stmt.execute(*bind_vars) have_result = (@stmt.ncols != 0) if (have_result) result = [] while (h = res.fetch_hash) if block_given? yield h else result << h end end end @stmt.drop if (@last_query_type == :insert) res = conn.run("SELECT @@IDENTITY AS NewID") @last_insert_id = res.fetch[0] end @last_query_type = nil if (have_result) unless block_given? result.extend(StorageResult) return result end else return res end rescue => exc release raise exc ensure @stmt.drop if @stmt release if @conn && !in_transaction? end end |
#execute_statement(stmt, *bind_vars) ⇒ Object
124 125 126 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 124 def execute_statement(stmt, *bind_vars) stmt.execute(*bind_vars) end |
#in_transaction? ⇒ Boolean
57 58 59 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 57 def in_transaction? return @in_transaction ? true : false end |
#list_tables ⇒ Object
Methods to get information from the db #
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 133 def list_tables connection do |c| res = c.tables tables = [] res.each do |a| tables << a[2] end res.drop return tables end end |
#parse_url(url) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 39 def parse_url(url) # adapter://<username:password>@<dsn> if (url =~ /(.+):\/\/(?:(.+):(.+)@)?(.+)/) @adapter = $1 @user = $2 @pass = $3 @dsn = $4 else raise ArgumentError, "ODBC url '#{url}' is invalid" end @connection_params = [@dsn, @user, @pass] end |
#prepare(sql) ⇒ Object
120 121 122 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 120 def prepare(sql) return @stmt = connection.prepare(sql) end |
#release ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 29 def release begin @conn.autocommit = true if @conn super rescue self.class.remove_connection(@conn, @connection_params) @conn = nil end end |
#rollback ⇒ Object
66 67 68 69 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 66 def rollback @conn.rollback release end |
#start_transaction ⇒ Object
52 53 54 55 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 52 def start_transaction connection.autocommit = false @in_transaction = true end |
#table_exists?(table) ⇒ Boolean
199 200 201 202 203 204 205 206 207 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 199 def table_exists?(table) connection do |c| cols = c.columns(table) res = cols.fetch ? true : false cols.drop end Spider.logger.debug("TABLE EXISTS #{table}") if res return res end |
#value_to_mapper(type, value) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/spiderfw/model/storage/db/connectors/odbc.rb', line 183 def value_to_mapper(type, value) return unless value case type.name when 'DateTime' begin return ::ODBC.to_time(value).to_datetime rescue ArgumentError => exc @@time_offset ||= DateTime.now.offset return DateTime.civil(value.year, value.month, value.day, value.hour, value.minute, value.second, @@time_offset) end when 'Date' return ::ODBC.to_date(value) end return super(type, value) end |