ToolJet/app/services/mysql_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

50 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class MysqlQueryService
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
connection = Mysql2::Client.new(
database: 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
def process
connection = get_cached_connection(data_source)
connection = create_connection unless connection
query_text = options["query"]
results = connection.query(query_text)
{ status: "success", data: results.to_a }
end
private
def create_connection
connection = Mysql2::Client.new(
host: source_options["host"],
username: source_options["username"],
password: source_options["password"],
port: source_options["port"],
database: source_options["database"]
)
cache_connection(data_source, connection)
connection
end
end