feat: 医師の部分一致検索に対応
This commit is contained in:
parent
45c6af090c
commit
79d241aa18
@ -4,7 +4,7 @@ class SQLCondition:
|
||||
param: str
|
||||
literal: bool
|
||||
|
||||
def __init__(self, column: str, operator: str, param: str, literal=False, like_wildcard=False) -> None:
|
||||
def __init__(self, column: str, operator: str, param: str, literal=False) -> None:
|
||||
"""
|
||||
Args:
|
||||
column (str): カラム名
|
||||
@ -12,19 +12,15 @@ class SQLCondition:
|
||||
param (str): パラメータ(プレースホルダーかリテラル値か)
|
||||
literal (bool, optional): リテラル値を埋め込むかどうか。
|
||||
画面から渡ってきた値を使うとSQLインジェクションの危険性があるため、固定値で使用すること。
|
||||
like_wildcard (bool, optional): like検索で部分一致検索にするかどうか。
|
||||
"""
|
||||
self.column = column
|
||||
self.operator = operator
|
||||
self.param = param
|
||||
self.literal = literal
|
||||
self.like_wildcard = like_wildcard
|
||||
|
||||
def apply(self):
|
||||
# literalがFalseならプレースホルダー。Trueだったならは固定値。
|
||||
param = f':{self.param}' if self.literal is False else self.param
|
||||
# like_wildcardがTrueならLike検索にする
|
||||
param = f'%{param}%' if self.like_wildcard is False else param
|
||||
return f' {self.column} {self.operator} {param}'
|
||||
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
from src.model.db.ultmarc_doctor import UltmarcDoctorDBModel
|
||||
from src.repositories.base_repository import BaseRepository
|
||||
from src.db import sql_condition as condition
|
||||
from src.db.sql_condition import SQLCondition
|
||||
from src.model.db.ultmarc_doctor import UltmarcDoctorDBModel
|
||||
from src.model.request.ultmarc_doctor import UltmarcDoctorModel
|
||||
from src.repositories.base_repository import BaseRepository
|
||||
from src.util.string_util import is_not_empty
|
||||
@ -35,8 +34,8 @@ class UltmarcDoctorRepository(BaseRepository):
|
||||
WHERE
|
||||
{where_clause}
|
||||
ORDER BY
|
||||
com_dr.dcf_pcf_dr_cd,
|
||||
com_dr_wrkplace.dcf_dsf_inst_cd,
|
||||
com_dr.dcf_pcf_dr_cd,
|
||||
com_dr_wrkplace.dcf_dsf_inst_cd,
|
||||
com_dr_wrkplace.blng_sec_cd,
|
||||
com_dr_trt_course.trt_course_cd
|
||||
\
|
||||
@ -45,9 +44,11 @@ class UltmarcDoctorRepository(BaseRepository):
|
||||
def fetch_many(self, parameter: UltmarcDoctorModel) -> list[UltmarcDoctorDBModel]:
|
||||
try:
|
||||
self._database.connect()
|
||||
where_clause = self.__build_condition(parameter)
|
||||
# 文字列の検索を部分一致にするため、モデルをコピー。以降はこのコピーを使用する。
|
||||
clone_parameter = UltmarcDoctorModel(**parameter.dict())
|
||||
where_clause = self.__build_condition(clone_parameter)
|
||||
query = self.FETCH_SQL.format(where_clause=where_clause)
|
||||
result = self._database.execute_select(query, parameter.dict())
|
||||
result = self._database.execute_select(query, clone_parameter.dict())
|
||||
models = [UltmarcDoctorDBModel(**r) for r in result]
|
||||
|
||||
return models
|
||||
@ -64,31 +65,36 @@ class UltmarcDoctorRepository(BaseRepository):
|
||||
# 医師コード
|
||||
if is_not_empty(parameter.dcf_pcf_dr_cd):
|
||||
# 必ず部分一致で検索
|
||||
where_clauses.append(SQLCondition('com_dr.dcf_pcf_dr_cd', condition.LIKE,
|
||||
'dcf_pcf_dr_cd', like_wildcard=True))
|
||||
parameter.dcf_pcf_dr_cd = f'%{parameter.dcf_pcf_dr_cd}%'
|
||||
where_clauses.append(SQLCondition('com_dr.dcf_pcf_dr_cd', condition.LIKE, 'dcf_pcf_dr_cd'))
|
||||
# 氏名(漢字)
|
||||
if is_not_empty(parameter.dr_name):
|
||||
# 必ず部分一致で検索
|
||||
where_clauses.append(SQLCondition('dr_name', condition.LIKE, 'dr_name', like_wildcard=True))
|
||||
parameter.dr_name = f'%{parameter.dr_name}%'
|
||||
where_clauses.append(SQLCondition('dr_name', condition.LIKE, 'dr_name'))
|
||||
# 氏名(かな・カナ)
|
||||
if is_not_empty(parameter.dr_name_kana):
|
||||
# 必ず部分一致で検索
|
||||
where_clauses.append(SQLCondition('dr_name_kana', condition.LIKE, 'dr_name_kana', like_wildcard=True))
|
||||
parameter.dr_name_kana = f'%{parameter.dr_name_kana}%'
|
||||
where_clauses.append(SQLCondition('dr_name_kana', condition.LIKE, 'dr_name_kana'))
|
||||
# 勤務先コード
|
||||
if is_not_empty(parameter.dcf_dsf_inst_cd):
|
||||
# 必ず部分一致で検索
|
||||
parameter.dcf_dsf_inst_cd = f'%{parameter.dcf_dsf_inst_cd}%'
|
||||
where_clauses.append(SQLCondition('com_inst.dcf_dsf_inst_cd',
|
||||
condition.LIKE, 'dcf_dsf_inst_cd', like_wildcard=True))
|
||||
condition.LIKE, 'dcf_dsf_inst_cd'))
|
||||
# 勤務先名(漢字)
|
||||
if is_not_empty(parameter.form_inst_name_kanji):
|
||||
# 必ず部分一致で検索
|
||||
parameter.form_inst_name_kanji = f'%{parameter.form_inst_name_kanji}%'
|
||||
where_clauses.append(SQLCondition('form_inst_name_kanji', condition.LIKE,
|
||||
'form_inst_name_kanji', like_wildcard=True))
|
||||
'form_inst_name_kanji'))
|
||||
# 勤務先名(かな・カナ)
|
||||
if is_not_empty(parameter.form_inst_name_kana):
|
||||
# 必ず部分一致で検索
|
||||
parameter.form_inst_name_kana = f'%{parameter.form_inst_name_kana}%'
|
||||
where_clauses.append(SQLCondition('form_inst_name_kana', condition.LIKE,
|
||||
'form_inst_name_kana', like_wildcard=True))
|
||||
'form_inst_name_kana'))
|
||||
|
||||
# 勤務先都道府県
|
||||
if is_not_empty(parameter.prefc_cd):
|
||||
@ -97,23 +103,27 @@ class UltmarcDoctorRepository(BaseRepository):
|
||||
# 所属部科(漢字)
|
||||
if is_not_empty(parameter.blng_sec_name):
|
||||
# 必ず部分一致で検索
|
||||
parameter.blng_sec_name = f'%{parameter.blng_sec_name}%'
|
||||
where_clauses.append(SQLCondition('com_blng_sec.blng_sec_name',
|
||||
condition.LIKE, 'blng_sec_name', like_wildcard=True))
|
||||
condition.LIKE, 'blng_sec_name'))
|
||||
|
||||
# 診療科目(漢字)
|
||||
if is_not_empty(parameter.trt_course_name):
|
||||
# 必ず部分一致で検索
|
||||
where_clauses.append(SQLCondition('trt_course_name', condition.LIKE, 'trt_course_name', like_wildcard=True))
|
||||
parameter.trt_course_name = f'%{parameter.trt_course_name}%'
|
||||
where_clauses.append(SQLCondition('trt_course_name', condition.LIKE, 'trt_course_name'))
|
||||
|
||||
# 出身大学(漢字)
|
||||
if is_not_empty(parameter.alma):
|
||||
# 必ず部分一致で検索
|
||||
where_clauses.append(SQLCondition('alma', condition.LIKE, 'alma', like_wildcard=True))
|
||||
parameter.alma = f'%{parameter.alma}%'
|
||||
where_clauses.append(SQLCondition('alma', condition.LIKE, 'alma'))
|
||||
|
||||
# 卒年
|
||||
if is_not_empty(parameter.grad_y):
|
||||
# 必ず部分一致で検索
|
||||
where_clauses.append(SQLCondition('grad_y', condition.LIKE, 'grad_y', like_wildcard=True))
|
||||
parameter.grad_y = f'%{parameter.grad_y}%'
|
||||
where_clauses.append(SQLCondition('grad_y', condition.LIKE, 'grad_y'))
|
||||
|
||||
where_clauses_str = ' AND '.join([condition.apply() for condition in where_clauses])
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user