Module: DatabaseHelper

Defined in:
lib/db/db_helper.rb

Defined Under Namespace

Classes: DatabaseError, QuillQueryResults

Constant Summary collapse

SUPPORTED_DATABASES =

Add others as they’re implemented

['clickhouse'].freeze

Class Method Summary collapse

Class Method Details

.connect_and_run_query(database_type, connection_string, sql) ⇒ Object



58
59
60
61
62
# File 'lib/db/db_helper.rb', line 58

def self.connect_and_run_query(database_type, connection_string, sql)
  with_connection(database_type, connection_string) do |connection|
    run_query_by_database(database_type, connection, sql)
  end
end

.connect_to_database(database_type, config) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/db/db_helper.rb', line 28

def self.connect_to_database(database_type, config)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.connect_to_clickhouse(config)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.disconnect_from_database(database_type, database) ⇒ Object



64
65
66
67
68
69
# File 'lib/db/db_helper.rb', line 64

def self.disconnect_from_database(database_type, database)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.disconnect_from_clickhouse(database)
  end
end

.get_column_info_by_schema_by_database(database_type, connection, schema_name, tables) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/db/db_helper.rb', line 107

def self.get_column_info_by_schema_by_database(database_type, connection, schema_name, tables)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.get_schema_column_info_clickhouse(connection, schema_name, tables)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_columns_by_table_by_database(database_type, connection, schema_name, table_name) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/db/db_helper.rb', line 89

def self.get_columns_by_table_by_database(database_type, connection, schema_name, table_name)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.get_columns_by_table_clickhouse(connection, schema_name, table_name)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_database_credentials(database_type, connection_string) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/db/db_helper.rb', line 19

def self.get_database_credentials(database_type, connection_string)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.format_clickhouse_config(connection_string)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_foreign_keys_by_database(database_type, connection, schema_name, table_name, primary_key) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/db/db_helper.rb', line 98

def self.get_foreign_keys_by_database(database_type, connection, schema_name, table_name, primary_key)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.get_foreign_keys_clickhouse(connection, schema_name, table_name, primary_key)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_schemas_by_database(database_type, connection) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/db/db_helper.rb', line 71

def self.get_schemas_by_database(database_type, connection)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.get_schemas_clickhouse(connection)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_tables_by_schema_by_database(database_type, connection, schema_name) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/db/db_helper.rb', line 80

def self.get_tables_by_schema_by_database(database_type, connection, schema_name)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.get_tables_by_schema_clickhouse(connection, schema_name)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.run_query_by_database(database_type, connection, sql) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/db/db_helper.rb', line 49

def self.run_query_by_database(database_type, connection, sql)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.run_query_clickhouse(sql, connection)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.with_connection(database_type, connection_string) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/db/db_helper.rb', line 37

def self.with_connection(database_type, connection_string)
  config = get_database_credentials(database_type, connection_string)
  connection = connect_to_database(database_type, config)
  begin
    yield(connection)
  rescue StandardError => e
    { success: false, message: e.message || e.error || e.to_s }
  ensure
    disconnect_from_database(database_type, connection)
  end
end