我正在尝试创建一个系统,在该系统中,我从与我的类不同的模块中调用一个方法,该模块基于该模块中的字符串执行SQL查询。因此,例如,现在我目前正在WhLoan类中进行如下调用:
WhLoan班
qm_code = WhQueryHelper.sql_query(:qm_code)
调用我的WhQueryHelper模块中的方法,该方法包含根据传递的内容应作为sql语句执行的字符串,在我的情况下为符号。在我的WhQueryHelper模块中如何解决此问题,如下所示:
WhQueryHelper模块
QUALIFIED_MORTGAGE_TYPE_QUERY = "
SELECT
[LoanNum] AS loan_number,
[QM_Code] AS qualified_mortgage_type
FROM
[clg_strategy].[dbo].[QM_Codes] (NOLOCK)
"
def self.sql_query(q_sym)
conn = Apollo::SqlServer.get_connection(CONFIG_PREFIX)
qs = self.map_to_query_string(q_sym)
res = conn.execute(qs).to_a
conn.close if conn && conn.active?
res.empty? ? nil : res
end
def self.map_to_query_string(q_sym)
qs = ""
case q_sym
when :lock_type
qs = LOCK_TYPE_QUERY
when :home_indicators
qs = HOME_INDICATORS_QUERY
when :hpm_indicator
qs = HIGHER_PRICED_MORTGAGE_INDICATOR_QUERY
when :qm_code
qs = QUALIFIED_MORTGAGE_TYPE_QUERY
when :db_eligibility
qs = APPLICANT_INCOME_PERCENT_OF_MEDIAN_QUERY
when :corr_tnw_amount
qs = CORRESPONDENT_TOTAL_NET_WORTH_AMOUNT_QUERY
else
raise "not a valid q_sym passed from #{caller[0]}"
qs
end
end
TLDR 因此,基本上现在,我只是将符号传递给单独模块中的方法,然后在该模块中通过一个case语句运行该符号,以查看该模块与哪个字符串相关,然后我将该字符串作为SQL语句运行。如何改善这个生命周期,以便可以在WhLoan类中创建许多单独的变量,这些变量可以调用WhQueryHelper模块以获取正确的SQL语句返回值?