2022-08-25 15:53:07 +02:00
|
|
|
"""
|
|
|
|
Sql queries for bitmessagekivy
|
|
|
|
"""
|
|
|
|
from pybitmessage.helper_sql import sqlQuery
|
2024-05-27 15:43:01 +02:00
|
|
|
from dbcompat import dbstr
|
2022-08-25 15:53:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
def search_sql(
|
|
|
|
xAddress="toaddress", account=None, folder="inbox", where=None,
|
|
|
|
what=None, unreadOnly=False, start_indx=0, end_indx=20):
|
|
|
|
# pylint: disable=too-many-arguments, too-many-branches
|
|
|
|
"""Method helping for searching mails"""
|
|
|
|
if what is not None and what != "":
|
|
|
|
what = "%" + what + "%"
|
|
|
|
else:
|
|
|
|
what = None
|
|
|
|
if folder in ("sent", "draft"):
|
|
|
|
sqlStatementBase = (
|
|
|
|
'''SELECT toaddress, fromaddress, subject, message, status,'''
|
|
|
|
''' ackdata, senttime FROM sent '''
|
|
|
|
)
|
|
|
|
elif folder == "addressbook":
|
|
|
|
sqlStatementBase = '''SELECT label, address From addressbook '''
|
|
|
|
else:
|
|
|
|
sqlStatementBase = (
|
|
|
|
'''SELECT folder, msgid, toaddress, message, fromaddress,'''
|
|
|
|
''' subject, received, read FROM inbox '''
|
|
|
|
)
|
|
|
|
sqlStatementParts = []
|
|
|
|
sqlArguments = []
|
|
|
|
if account is not None:
|
|
|
|
if xAddress == 'both':
|
|
|
|
sqlStatementParts.append("(fromaddress = ? OR toaddress = ?)")
|
2024-05-27 15:43:01 +02:00
|
|
|
sqlArguments.append(dbstr(account))
|
|
|
|
sqlArguments.append(dbstr(account))
|
2022-08-25 15:53:07 +02:00
|
|
|
else:
|
|
|
|
sqlStatementParts.append(xAddress + " = ? ")
|
2024-05-27 15:43:01 +02:00
|
|
|
sqlArguments.append(dbstr(account))
|
2022-08-25 15:53:07 +02:00
|
|
|
if folder != "addressbook":
|
|
|
|
if folder is not None:
|
|
|
|
if folder == "new":
|
|
|
|
folder = "inbox"
|
|
|
|
unreadOnly = True
|
|
|
|
sqlStatementParts.append("folder = ? ")
|
2024-05-27 15:43:01 +02:00
|
|
|
sqlArguments.append(dbstr(folder))
|
2022-08-25 15:53:07 +02:00
|
|
|
else:
|
|
|
|
sqlStatementParts.append("folder != ?")
|
2024-05-27 15:43:01 +02:00
|
|
|
sqlArguments.append(dbstr("trash"))
|
2022-08-25 15:53:07 +02:00
|
|
|
if what is not None:
|
|
|
|
for colmns in where:
|
|
|
|
if len(where) > 1:
|
|
|
|
if where[0] == colmns:
|
|
|
|
filter_col = "(%s LIKE ?" % (colmns)
|
|
|
|
else:
|
|
|
|
filter_col += " or %s LIKE ? )" % (colmns)
|
|
|
|
else:
|
|
|
|
filter_col = "%s LIKE ?" % (colmns)
|
2024-05-27 15:43:01 +02:00
|
|
|
sqlArguments.append(dbstr(what))
|
2022-08-25 15:53:07 +02:00
|
|
|
sqlStatementParts.append(filter_col)
|
|
|
|
if unreadOnly:
|
|
|
|
sqlStatementParts.append("read = 0")
|
|
|
|
if sqlStatementParts:
|
|
|
|
sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts)
|
|
|
|
if folder in ("sent", "draft"):
|
|
|
|
sqlStatementBase += \
|
|
|
|
"ORDER BY senttime DESC limit {0}, {1}".format(
|
|
|
|
start_indx, end_indx)
|
|
|
|
elif folder == "inbox":
|
|
|
|
sqlStatementBase += \
|
|
|
|
"ORDER BY received DESC limit {0}, {1}".format(
|
|
|
|
start_indx, end_indx)
|
|
|
|
return sqlQuery(sqlStatementBase, sqlArguments)
|