Class: ActiveRecord::ConnectionAdapters::TeradataAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::TeradataAdapter
- Defined in:
- lib/active_record/connection_adapters/teradata_adapter.rb
Defined Under Namespace
Classes: BindSubstitution
Instance Method Summary collapse
-
#active? ⇒ Boolean
CONNECTION MANAGEMENT ====================================.
- #adapter_name ⇒ Object
- #begin_db_transaction ⇒ Object
-
#build_sql_type(column) ⇒ Object
construct the sql type from the teradata column info.
-
#columns(table_name) ⇒ Object
Returns an array of
Columnobjects for the table specified bytable_name. - #commit_db_transaction ⇒ Object
-
#disconnect! ⇒ Object
Disconnects from the database if already connected.
-
#exec_query(sql, name = 'SQL', binds = []) ⇒ Object
Executes
sqlstatement in the context of this connection usingbindsas the bind substitutes. -
#execute(sql, name = nil) ⇒ Object
Executes the SQL statement in the context of this connection.
- #execute_update(sql, name = nil) ⇒ Object
- #get_field_type(type) ⇒ Object
-
#initialize(logger, logon_string, config) ⇒ TeradataAdapter
constructor
A new instance of TeradataAdapter.
-
#new_column(field, default, type, null) ⇒ Object
Overridden by the adapters to instantiate their specific Column type.
- #primary_key(table_name) ⇒ Object
- #reconnect! ⇒ Object (also: #reset!)
- #rollback_db_transaction ⇒ Object
-
#select(sql, name = nil, binds = []) ⇒ Object
Returns an ActiveRecord::Result instance.
-
#select_rows(sql, name = nil) ⇒ Object
Returns an array of arrays containing the field values.
-
#tables(name = nil) ⇒ Object
DATABASE STATEMENTS ======================================.
Constructor Details
#initialize(logger, logon_string, config) ⇒ TeradataAdapter
Returns a new instance of TeradataAdapter.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 49 def initialize(logger, logon_string, config) @logon_string = logon_string @charset = config[:charset] @database = config[:database] @table_name_prefix = config[:table_name_prefix] @config = config connect super @connection, logger @visitor = unprepared_visitor configure_connection end |
Instance Method Details
#active? ⇒ Boolean
CONNECTION MANAGEMENT ====================================
77 78 79 80 81 82 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 77 def active? @connection.execute_query('SELECT 1') true rescue false end |
#adapter_name ⇒ Object
61 62 63 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 61 def adapter_name 'teradata' end |
#begin_db_transaction ⇒ Object
158 159 160 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 158 def begin_db_transaction execute_update "BEGIN TRANSACTION" end |
#build_sql_type(column) ⇒ Object
construct the sql type from the teradata column info
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 195 def build_sql_type(column) field_type = get_field_type(column['ColumnType']) case field_type when "varchar" "#{field_type}(#{column['ColumnLength']})" when "decimal" "#{field_type}(#{column['DecimalTotalDigits']},#{column['DecimalFractionalDigits']})" else field_type end end |
#columns(table_name) ⇒ Object
Returns an array of Column objects for the table specified by table_name.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 171 def columns(table_name)#:nodoc: sql = "SELECT * FROM DBC.COLUMNS WHERE TABLENAME='#{table_name}'" sql << " AND DATABASENAME='#{@database}'" if @database rs = execute(sql) rs.entries.collect do |record| new_column( record['ColumnName'].strip, record['DefaultValue'], build_sql_type(record), record['Nullable'].strip == "Y" ) end end |
#commit_db_transaction ⇒ Object
162 163 164 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 162 def commit_db_transaction execute_update "END TRANSACTION" end |
#disconnect! ⇒ Object
Disconnects from the database if already connected. Otherwise, this method does nothing.
94 95 96 97 98 99 100 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 94 def disconnect! super unless @connection.nil? @connection.close @connection = nil end end |
#exec_query(sql, name = 'SQL', binds = []) ⇒ Object
Executes sql statement in the context of this connection using binds as the bind substitutes. name is logged along with the executed sql statement.
129 130 131 132 133 134 135 136 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 129 def exec_query(sql, name = 'SQL', binds = []) result = execute(sql, name) if result && result.count > 0 ActiveRecord::Result.new(result.entries[0].keys, result.entries.collect{|r|r.collect{|v|v}}) else ActiveRecord::Result.new([],[]) end end |
#execute(sql, name = nil) ⇒ Object
Executes the SQL statement in the context of this connection.
118 119 120 121 122 123 124 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 118 def execute(sql, name = nil) if name == :skip_logging @connection.query(sql) else log(sql, name) { @connection.query(sql) } end end |
#execute_update(sql, name = nil) ⇒ Object
154 155 156 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 154 def execute_update(sql, name = nil) log(sql, name) { @connection.execute_update(sql) } end |
#get_field_type(type) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 207 def get_field_type(type) case type.strip when "I", "I1" "int" when "CV" "varchar" when "DA" "date" when "D" "decimal" when "TS" "datetime" else raise "Column type #{type} not supported" end end |
#new_column(field, default, type, null) ⇒ Object
Overridden by the adapters to instantiate their specific Column type.
190 191 192 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 190 def new_column(field, default, type, null) # :nodoc: Column.new(field, default, type, null) end |
#primary_key(table_name) ⇒ Object
185 186 187 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 185 def primary_key(table_name) columns(table_name).select{|col| col.primary == true}[0].name rescue 'id' end |
#reconnect! ⇒ Object Also known as: reset!
84 85 86 87 88 89 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 84 def reconnect! super disconnect! connect configure_connection end |
#rollback_db_transaction ⇒ Object
166 167 168 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 166 def rollback_db_transaction execute_update "ROLLBACK" end |
#select(sql, name = nil, binds = []) ⇒ Object
Returns an ActiveRecord::Result instance.
139 140 141 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 139 def select(sql, name = nil, binds = []) exec_query(sql, name) end |
#select_rows(sql, name = nil) ⇒ Object
Returns an array of arrays containing the field values. Order is the same as that returned by columns.
145 146 147 148 149 150 151 152 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 145 def select_rows(sql, name = nil) result = execute(sql, name) if result && result.count > 0 result.entries.collect{|r|r.collect{|v|v}} else [] end end |
#tables(name = nil) ⇒ Object
DATABASE STATEMENTS ======================================
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_record/connection_adapters/teradata_adapter.rb', line 104 def tables(name = nil) sql = "SELECT TABLENAME FROM DBC.TABLES " clauses = [] clauses << "DATABASENAME = '#{@database}'" if @database clauses << "TABLENAME = '#{name}'" if name clauses << "TABLENAME LIKE '#{@table_name_prefix}%'" if @table_name_prefix unless clauses.empty? sql << " WHERE " + clauses.join(' AND ') end rs = execute(sql) rs.entries.collect {|record| record['TableName'].strip} end |