newdwh2021/ecs/jskult-webapp/src/db/sql_condition.py
shimoda.m@nds-tyo.co.jp 65acc3ce09 style: format適用
2023-05-12 13:39:14 +09:00

37 lines
1.1 KiB
Python

class SQLCondition:
column: str
operator: str
param: str
literal: bool
def __init__(self, column: str, operator: str, param: str, literal=False) -> None:
"""
Args:
column (str): カラム名
operator (str): 比較演算子
param (str): パラメータ(プレースホルダーかリテラル値か)
literal (bool, optional): リテラル値を埋め込むかどうか。
画面から渡ってきた値を使うとSQLインジェクションの危険性があるため、固定値で使用すること。
"""
self.column = column
self.operator = operator
self.param = param
self.literal = literal
def apply(self):
# literalがFalseならプレースホルダー。Trueだったならは固定値。
param = f':{self.param}' if self.literal is False else self.param
return f' {self.column} {self.operator} {param}'
# 定数
EQ = '='
NE = '<>'
GT = '>'
LT = '<'
GE = '>='
LE = '<='
LIKE = 'LIKE'
IS = 'IS'
IS_NOT = 'IS NOT'