def get_user(*, username: str = None, email: str = None, id: int = None, is_blocked: bool = None):
SQL = "SELECT * FROM USERS {} LIMIT %s OFFSET %s"
more = []
if username:
more.append("WHERE username = '%s'" % username)
if email:
if any([username]):
more.append("& email = '%s'" % email)
else:
more.append("WHERE email = '%s'" % email)
if id:
if any([username, email]):
more.append("& id = '%s'" % id)
else:
more.append("WHERE id = '%s'" % id)
...
cursor.execute(SQL.format(" ".join(more)))
def get_user(*, username: str = None, email: str = None, id: int = None, is_blocked: bool = None):
SQL = "SELECT * FROM USERS {} LIMIT %s OFFSET %s WHERE 1=1 "
more = []
if username:
more.append("AND username = '%s'" % username)
if email:
if any([username]):
more.append("AND email = '%s'" % email)
if id:
if any([username, email]):
more.append("AND id = '%s'" % id)
... AND (column = {parameter} OR {parameter} IS NULL) ..
... AND (column = {parameter} OR FALSE) ..
==> ... AND (column = {parameter}) ..
... AND (column = {parameter} OR TRUE) ..
==> ... AND (TRUE) ..
.SQL = "SELECT * FROM USERS WHERE 1=1 {} LIMIT %s OFFSET %s"
AND {условие}
where = []
if username:
where.append("username = '%s'" % username)
if email:
where.append("email = '%s'" % email)
if id:
where.append("id = '%s'" % id)
...
whereString = "WHERE " + " AND ".join(where);
where = []
where.append("1 = 1")
if username:
...