Module: ArJdbc::Teradata
- Defined in:
- lib/arjdbc/teradata/adapter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #adapter_name ⇒ Object
-
#add_limit_offset!(sql, options) ⇒ Object
SQL SERVER CODE STARTS HERE.
- #determine_order_clause(sql) ⇒ Object
- #determine_primary_key(table_name) ⇒ Object
- #get_table_name(sql) ⇒ Object
- #modify_types(tp) ⇒ Object
- #replace_limit_offset!(sql, limit, offset, order) ⇒ Object
Class Method Details
.jdbc_connection_class ⇒ Object
12 13 14 |
# File 'lib/arjdbc/teradata/adapter.rb', line 12 def self.jdbc_connection_class ::ActiveRecord::ConnectionAdapters::TeradataJdbcConnection end |
Instance Method Details
#adapter_name ⇒ Object
8 9 10 |
# File 'lib/arjdbc/teradata/adapter.rb', line 8 def adapter_name 'Teradata' end |
#add_limit_offset!(sql, options) ⇒ Object
SQL SERVER CODE STARTS HERE
23 24 25 26 27 28 29 |
# File 'lib/arjdbc/teradata/adapter.rb', line 23 def add_limit_offset!(sql, ) if [:limit] order = "ORDER BY #{[:order] || determine_order_clause(sql)}" sql.sub!(/ ORDER BY.*$/i, '') replace_limit_offset!(sql, [:limit], [:offset], order) end end |
#determine_order_clause(sql) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/arjdbc/teradata/adapter.rb', line 63 def determine_order_clause(sql) return $1 if sql =~ /ORDER BY (.*)$/ "1" #table_name = get_table_name(sql) #"#{table_name}.#{determine_primary_key(table_name)}" end |
#determine_primary_key(table_name) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/arjdbc/teradata/adapter.rb', line 70 def determine_primary_key(table_name) primary_key = columns(table_name).detect { |column| column.primary || column.identity } return primary_key.name if primary_key # Look for an id column. Return it, without changing case, to cover dbs with a case-sensitive collation. columns(table_name).each { |column| return column.name if column.name =~ /^id$/i } # Give up and provide something which is going to crash almost certainly columns(table_name)[0].name end |
#get_table_name(sql) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/arjdbc/teradata/adapter.rb', line 53 def get_table_name(sql) if sql =~ /^\s*insert\s+into\s+([^\(\s,]+)\s*|^\s*update\s+([^\(\s,]+)\s*/i $1 elsif sql =~ /\bfrom\s+([^\(\s,]+)\s*/i $1 else nil end end |
#modify_types(tp) ⇒ Object
16 17 18 19 |
# File 'lib/arjdbc/teradata/adapter.rb', line 16 def modify_types(tp) tp[:integer][:limit] = nil tp end |
#replace_limit_offset!(sql, limit, offset, order) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/arjdbc/teradata/adapter.rb', line 31 def replace_limit_offset!(sql, limit, offset, order) if limit offset ||= 0 start_row = offset + 1 end_row = offset + limit.to_i find_select = /\b(SELECT(?:\s+DISTINCT)?)\b(.*)/im whole, select, rest_of_query = find_select.match(sql).to_a rest_of_query.strip! if rest_of_query[0] == "1" rest_of_query[0] = "*" end if rest_of_query.chars.first == "*" from_table = get_table_name(rest_of_query) rest_of_query = from_table + '.' + rest_of_query end new_sql = "#{select} t.* FROM (SELECT ROW_NUMBER() OVER(#{order}) AS _row_num, #{rest_of_query}" new_sql << ") AS t WHERE t._row_num BETWEEN #{start_row.to_s} AND #{end_row.to_s}" sql.replace(new_sql) end sql end |