Module: DatabaseHelper

Defined in:
lib/db/db_helper.rb

Defined Under Namespace

Classes: DatabaseError, QuillQueryResults

Constant Summary collapse

SUPPORTED_DATABASES =
['clickhouse', 'postgresql', 'databricks'].freeze

Class Method Summary collapse

Class Method Details

.connect_and_run_query(database_type, connection_string, sql) ⇒ Object



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

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



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/db/db_helper.rb', line 33

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

.disconnect_from_database(database_type, database) ⇒ Object



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

def self.disconnect_from_database(database_type, database)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.disconnect_from_clickhouse(database)
  when 'postgresql'
    PostgresHelper.disconnect_from_postgres(database)
  when 'databricks'
    DatabricksHelper.disconnect_from_databricks(database)
  end
end

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



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/db/db_helper.rb', line 140

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)
  when 'postgresql'
    PostgresHelper.get_schema_column_info_postgres(connection, schema_name, tables)
  when 'databricks'
    DatabricksHelper.get_schema_column_info_databricks(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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/db/db_helper.rb', line 114

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)
  when 'postgresql'
    PostgresHelper.get_columns_by_table_postgres(connection, schema_name, table_name)
  when 'databricks'
    DatabricksHelper.get_columns_by_table_databricks(connection, schema_name, table_name)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_database_credentials(database_type, connection_string) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/db/db_helper.rb', line 20

def self.get_database_credentials(database_type, connection_string)
  case database_type.downcase
  when 'clickhouse'
    ClickHouseHelper.format_clickhouse_config(connection_string)
  when 'postgresql'
    PostgresHelper.format_postgres_config(connection_string)
  when 'databricks'
    DatabricksHelper.format_databricks_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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/db/db_helper.rb', line 127

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)
  when 'postgresql'
    PostgresHelper.get_foreign_keys_postgres(connection, schema_name, table_name, primary_key)
  when 'databricks'
    DatabricksHelper.get_foreign_keys_databricks(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



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

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

.get_tables_by_schema_by_database(database_type, connection, schema_name) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/db/db_helper.rb', line 101

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)
  when 'postgresql'
    PostgresHelper.get_tables_by_schema_postgres(connection, schema_name)
  when 'databricks'
    DatabricksHelper.get_tables_by_schema_databricks(connection, schema_name)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.run_query_by_database(database_type, connection, sql) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/db/db_helper.rb', line 58

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

.with_connection(database_type, connection_string) ⇒ Object



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

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