Class: Aqueduct::Wrappers::Mssql2008
- Inherits:
-
Object
- Object
- Aqueduct::Wrappers::Mssql2008
- Includes:
- Aqueduct::Wrapper
- Defined in:
- lib/aqueduct/wrappers/mssql2008.rb
Instance Method Summary collapse
- #column_values(table, column) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #count(query_concepts, conditions, tables, join_conditions, concept_to_count) ⇒ Object
- #disconnect ⇒ Object
- #get_all_values_for_column(table, column) ⇒ Object
- #get_table_metadata ⇒ Object
- #query(sql_statement) ⇒ Object
- #sql_codes ⇒ Object
- #table_columns(table) ⇒ Object
- #tables ⇒ Object
Instance Method Details
#column_values(table, column) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 159 def column_values(table, column) error = '' result = [] begin require 'dbi' db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) column_found = db_connection.columns(table).collect{|c| c[:name]}.include?(column) if column_found results = db_connection.execute("SELECT [#{column}] as 'column', count(*) FROM #{table} GROUP BY [#{column}];") results.each do |row| if row['column'].class != String and row['column'].respond_to?('round') and row['column'].round == row['column'] result << row['column'].round else result << row['column'] end end end rescue => e error = "Error: #{e.inspect}" ensure db_connection.disconnect if db_connection end { result: result, error: error } end |
#connect ⇒ Object
13 14 15 16 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 13 def connect require 'dbi' @db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) end |
#connected? ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 32 def connected? result = false error = '' begin require 'dbi' db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) rescue => e error = "Connection Error: #{e.inspect}" # { }"#{e.errno}: #{e.error}" ensure result = true if db_connection db_connection.disconnect if db_connection end { result: result, error: error } end |
#count(query_concepts, conditions, tables, join_conditions, concept_to_count) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 185 def count(query_concepts, conditions, tables, join_conditions, concept_to_count) result = 0 error = '' sql_conditions = '' begin t = Time.now if tables.size > 0 sql_conditions = "SELECT count(#{concept_to_count ? 'DISTINCT ' + concept_to_count : '*'}) as record_count FROM #{tables.join(', ')} WHERE #{join_conditions.join(' and ')}#{' and ' unless join_conditions.blank?}#{conditions}" Rails.logger.info sql_conditions require 'dbi' db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) if db_connection results = db_connection.execute(sql_conditions) results.each do |row| result = row['record_count'] end end else error = "Database [#{@source.name}] Error: No tables for concepts. Database not fully mapped." end rescue => e error = "Database [#{@source.name}] Error: #{e}" ensure db_connection.disconnect if db_connection end { result: result, error: error, sql_conditions: sql_conditions } end |
#disconnect ⇒ Object
18 19 20 21 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 18 def disconnect @db_connection.disconnect if @db_connection true end |
#get_all_values_for_column(table, column) ⇒ Object
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/aqueduct/wrappers/mssql2008.rb', line 124 def get_all_values_for_column(table, column) values = [] error = '' begin require 'dbi' db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) if db_connection column_found = db_connection.columns(table).collect{|c| c[:name]}.include?(column) if not column_found result += " <i>#{column}</i> does not exist in <i>#{@source.database}.#{table}</i>" else results = db_connection.execute("SELECT [#{column}] FROM #{table};") results.each do |row| value = row.first if value.class != String and value.respond_to?('round') and value.round == value values << value.round else values << value end end end end rescue => e error = "Get All Values For Column Error: #{e}" ensure if db_connection db_connection.disconnect else error += " unable to connect to <i>#{@source.name}</i>" end end { values: values, error: error } end |
#get_table_metadata ⇒ Object
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 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 47 def result = {} error = '' begin require 'dbi' db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) if db_connection tables = [] results = db_connection.execute('select * from sys.Tables') results.each do |row| tables << row[0] end results = db_connection.execute('select * from sys.Views') results.each do |row| tables << row[0] end tables.sort{|table_a, table_b| table_a.downcase <=> table_b.downcase}.each do |my_table| results = db_connection.columns(my_table) columns = [] results.each do |row| columns << {column: row[:name], datatype: row[:type_name]} end result[my_table] = columns.sort{|a,b| a[:column].downcase <=> b[:column].downcase} end end rescue => e error = e.inspect ensure db_connection.disconnect if db_connection end { result: result, error: error } end |
#query(sql_statement) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 23 def query(sql_statement) results = [] results = @db_connection.execute(sql_statement) if @db_connection temp_array = [] results.each {|row| temp_array << row.to_a} results = temp_array [results, results.size] end |
#sql_codes ⇒ Object
9 10 11 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 9 def sql_codes { text: 'VARCHAR(MAX)', numeric: 'NUMERIC', open: '[', close: ']' } end |
#table_columns(table) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 106 def table_columns(table) columns = [] error = '' begin require 'dbi' db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) if db_connection results = db_connection.columns(table) results.each { |row| columns << { column: row[:name], datatype: row[:type_name] } } end rescue => e error = "Error retrieving column information. Please make sure that this database is configured correctly. #{e.inspect}" ensure db_connection.disconnect if db_connection end { columns: columns, error: error } end |
#tables ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/aqueduct/wrappers/mssql2008.rb', line 82 def tables tables = [] error = '' begin require 'dbi' db_connection = DBI.connect("DBI:ODBC:#{@source.database}", @source.username, @source.password) if db_connection results = db_connection.execute('select * from sys.Tables') results.each do |row| tables << row[0] end results = db_connection.execute('select * from sys.Views') results.each do |row| tables << row[0] end end rescue => e error = e.inspect ensure db_connection.disconnect if db_connection end { result: tables, error: error } end |