Module: DatabaseHelper

Defined in:
lib/db/db_helper.rb

Defined Under Namespace

Classes: DatabaseError, QuillQueryResults

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.connect_and_run_query(database_type, connection_string, sql) ⇒ Object



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

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/db/db_helper.rb', line 31

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)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.disconnect_from_database(database_type, database) ⇒ Object



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

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)
  end
end

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



124
125
126
127
128
129
130
131
132
133
# File 'lib/db/db_helper.rb', line 124

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)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

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



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

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)
  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
# 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)
  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



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

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)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_schemas_by_database(database_type, connection) ⇒ Object



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

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)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.get_tables_by_schema_by_database(database_type, connection, schema_name) ⇒ Object



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

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)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.run_query_by_database(database_type, connection, sql) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/db/db_helper.rb', line 54

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)
  else
    raise DatabaseError, "Invalid database type: #{database_type}"
  end
end

.with_connection(database_type, connection_string) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/db/db_helper.rb', line 42

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