ToolJet/app/services/mssql_query_service.rb
Deepti Kakade b10d777f63
Fixes Rubocop issues in tests (#359)
* Updated project set up guide for Mac, added node version and Webpack install steps.

* Worked on PR comment i.e Can we change this line to Install Node.js ( version: v14.9.0 )

* Fixed "Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping."

* Resolved rubocop comments i.e "Style/FrozenStringLiteralComment: Missing frozen string literal comment." in test folder

Co-authored-by: Deepti Kakade <deepti@saeloun.com>
Co-authored-by: Deepti Kakade <“deepti@saeloun.com”>
2021-07-01 12:54:35 +05:30

61 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class MssqlQueryService
include DatasourceUtils
attr_accessor :data_query, :data_source, :options, :source_options, :current_user
def initialize(data_query, data_source, options, source_options, current_user)
@data_query = data_query
@data_source = data_source
@options = options
@source_options = source_options
@current_user = current_user
end
def self.connection(options)
TinyTds::Client.new(
database: options.dig("database", "value"),
username: options.dig("username", "value"),
password: options.dig("password", "value"),
host: options.dig("host", "value"),
port: options.dig("port", "value"),
azure: ActiveModel::Type::Boolean.new.cast(
options.dig("azure", "value")
) || false
)
end
def process
connection = get_cached_connection(data_source)
connection ||= create_connection
query_text = options["query"]
results = connection.execute(query_text)
{ status: "success", data: results.to_a }
rescue StandardError => e
if connection&.active?
connection&.close
reset_connection(data_source)
end
error = { message: e.message, code: 400 }
end
private
def create_connection
connection = TinyTds::Client.new(
database: source_options["database"],
username: source_options["username"],
password: source_options["password"],
host: source_options["host"],
port: source_options["port"],
azure: ActiveModel::Type::Boolean.new.cast(
source_options["azure"]
) || false
)
cache_connection(data_source, connection)
connection
end
end