ToolJet/app/services/postgresql_query_service.rb

98 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class PostgresqlQueryService
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
def self.connection(options)
PG.connect(
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"),
)
end
2021-04-29 06:41:23 +00:00
def process
error = false
begin
connection = get_cached_connection(data_source)
connection = create_connection unless connection
query_text = ""
query_text = if options["mode"] === "gui"
send("generate_#{options['operation']}_query", options)
else
options["query"]
end
result = connection.exec(query_text)
rescue StandardError => e
if connection&.status === PG::Constants::CONNECTION_BAD
connection&.finish
reset_connection(data_source)
end
puts e
error = { message: e.message }
end
if error
{ status: "error", code: 500, message: error[:message] }
else
{ status: "success", data: result.to_a }
end
2021-04-29 06:41:23 +00:00
end
2021-04-29 06:41:23 +00:00
private
2021-04-29 06:41:23 +00:00
def generate_bulk_update_pkey_query(options)
query_text = ""
table_name = options["table"]
primary_key = options["primary_key_column"]
records = options["records"]
2021-04-29 06:41:23 +00:00
records.each do |record|
query_text = "#{query_text} UPDATE #{table_name} SET"
2021-04-29 06:41:23 +00:00
record.each do |field, value|
query_text = " #{query_text} #{field} = '#{value}', "
end
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-29 06:41:23 +00:00
query_text
end
def create_connection
connection = PG.connect(
dbname: source_options["database"],
user: source_options["username"],
password: source_options["password"],
host: source_options["host"],
port: source_options["port"]
)
connection.type_map_for_results = PG::BasicTypeMapForResults.new connection
cache_connection(data_source, connection)
connection
end
end