2021-07-01 07:24:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-04-04 03:48:04 +00:00
|
|
|
class PostgresqlQueryService
|
2021-05-14 07:09:38 +00:00
|
|
|
include DatasourceUtils
|
2021-04-29 06:41:23 +00:00
|
|
|
attr_accessor :data_query, :data_source, :options, :source_options, :current_user
|
|
|
|
|
|
2021-05-22 11:29:27 +00:00
|
|
|
def initialize(data_query, data_source, options, source_options, current_user)
|
2021-04-29 06:41:23 +00:00
|
|
|
@data_query = data_query
|
2021-05-22 11:29:27 +00:00
|
|
|
@data_source = data_source
|
2021-04-29 06:41:23 +00:00
|
|
|
@options = options
|
|
|
|
|
@source_options = source_options
|
|
|
|
|
@current_user = current_user
|
|
|
|
|
end
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
def self.connection(options)
|
2021-04-30 05:04:10 +00:00
|
|
|
PG.connect(
|
2021-07-01 07:24:35 +00:00
|
|
|
dbname: options.dig("database", "value"),
|
|
|
|
|
user: options.dig("username", "value"),
|
|
|
|
|
password: options.dig("password", "value"),
|
|
|
|
|
host: options.dig("host", "value"),
|
|
|
|
|
port: options.dig("port", "value"),
|
2021-04-30 05:04:10 +00:00
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
def process
|
2021-05-23 17:11:27 +00:00
|
|
|
error = false
|
2021-05-14 07:09:38 +00:00
|
|
|
|
2021-05-23 17:11:27 +00:00
|
|
|
begin
|
|
|
|
|
connection = get_cached_connection(data_source)
|
|
|
|
|
connection = create_connection unless connection
|
|
|
|
|
|
|
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
query_text = ""
|
|
|
|
|
query_text = if options["mode"] === "gui"
|
2021-05-23 17:11:27 +00:00
|
|
|
send("generate_#{options['operation']}_query", options)
|
|
|
|
|
else
|
2021-07-01 07:24:35 +00:00
|
|
|
options["query"]
|
2021-05-23 17:11:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
result = connection.exec(query_text)
|
|
|
|
|
|
|
|
|
|
rescue StandardError => e
|
2021-06-01 07:57:13 +00:00
|
|
|
if connection&.status === PG::Constants::CONNECTION_BAD
|
|
|
|
|
connection&.finish
|
|
|
|
|
reset_connection(data_source)
|
|
|
|
|
end
|
2021-05-23 17:30:26 +00:00
|
|
|
|
2021-05-23 17:11:27 +00:00
|
|
|
puts e
|
2021-07-01 07:24:35 +00:00
|
|
|
error = { message: e.message }
|
2021-05-23 17:11:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if error
|
2021-07-01 07:24:35 +00:00
|
|
|
{ status: "error", code: 500, message: error[:message] }
|
2021-05-23 17:11:27 +00:00
|
|
|
else
|
2021-07-01 07:24:35 +00:00
|
|
|
{ status: "success", data: result.to_a }
|
2021-05-23 17:11:27 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
end
|
2021-04-24 12:57:15 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
private
|
2021-04-24 12:57:15 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
def generate_bulk_update_pkey_query(options)
|
2021-07-01 07:24:35 +00:00
|
|
|
query_text = ""
|
2021-04-04 03:48:04 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
table_name = options["table"]
|
|
|
|
|
primary_key = options["primary_key_column"]
|
|
|
|
|
records = options["records"]
|
2021-04-20 04:56:17 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
records.each do |record|
|
|
|
|
|
query_text = "#{query_text} UPDATE #{table_name} SET"
|
2021-04-24 12:57:15 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
record.each do |field, value|
|
|
|
|
|
query_text = " #{query_text} #{field} = '#{value}', "
|
|
|
|
|
end
|
2021-04-24 12:57:15 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
query_text = query_text.rstrip.chop
|
|
|
|
|
query_text = "#{query_text} WHERE #{primary_key} = #{record[primary_key]};"
|
|
|
|
|
end
|
2021-04-24 12:57:15 +00:00
|
|
|
|
2021-04-29 06:41:23 +00:00
|
|
|
query_text
|
|
|
|
|
end
|
2021-05-14 07:09:38 +00:00
|
|
|
|
2021-07-01 07:24:35 +00:00
|
|
|
def create_connection
|
2021-05-14 07:09:38 +00:00
|
|
|
connection = PG.connect(
|
2021-07-01 07:24:35 +00:00
|
|
|
dbname: source_options["database"],
|
|
|
|
|
user: source_options["username"],
|
|
|
|
|
password: source_options["password"],
|
|
|
|
|
host: source_options["host"],
|
|
|
|
|
port: source_options["port"]
|
2021-05-14 07:09:38 +00:00
|
|
|
)
|
2021-06-09 12:40:28 +00:00
|
|
|
|
|
|
|
|
connection.type_map_for_results = PG::BasicTypeMapForResults.new connection
|
2021-07-01 07:24:35 +00:00
|
|
|
|
2021-05-14 07:09:38 +00:00
|
|
|
cache_connection(data_source, connection)
|
|
|
|
|
|
|
|
|
|
connection
|
|
|
|
|
end
|
2021-04-04 03:48:04 +00:00
|
|
|
end
|