2019-09-20 13:19:04 +02:00
|
|
|
"""
|
2019-11-21 11:07:22 +01:00
|
|
|
Sql queries for bitmessagekivy
|
2019-09-20 13:19:04 +02:00
|
|
|
"""
|
|
|
|
from helper_sql import sqlQuery
|
2018-08-07 08:14:14 +02:00
|
|
|
|
|
|
|
|
2019-12-04 16:45:45 +01:00
|
|
|
def search_sql(xAddress="toaddress", account=None, folder="inbox", where=None, what=None, unreadOnly=False, start_indx=0, end_indx=20):
|
2019-09-20 13:19:04 +02:00
|
|
|
"""Method helping for searching mails"""
|
|
|
|
# pylint: disable=too-many-arguments, too-many-branches
|
2018-08-07 08:14:14 +02:00
|
|
|
if what is not None and what != "":
|
|
|
|
what = "%" + what + "%"
|
|
|
|
else:
|
|
|
|
what = None
|
|
|
|
|
2019-08-02 11:11:33 +02:00
|
|
|
if folder == "sent" or folder == "draft":
|
2019-11-21 11:07:22 +01:00
|
|
|
sqlStatementBase = (
|
|
|
|
'''SELECT toaddress, fromaddress, subject, message, status, ackdata,'''
|
|
|
|
''' lastactiontime FROM sent ''')
|
2019-08-07 17:39:46 +02:00
|
|
|
elif folder == "addressbook":
|
|
|
|
sqlStatementBase = '''SELECT label, address From addressbook '''
|
2018-08-09 12:20:20 +02:00
|
|
|
else:
|
2019-11-21 11:07:22 +01:00
|
|
|
sqlStatementBase = (
|
|
|
|
'''SELECT folder, msgid, toaddress, message, fromaddress, subject,'''
|
|
|
|
''' received, read FROM inbox ''')
|
2019-08-07 17:39:46 +02:00
|
|
|
|
2018-08-07 08:14:14 +02:00
|
|
|
sqlStatementParts = []
|
|
|
|
sqlArguments = []
|
|
|
|
if account is not None:
|
|
|
|
if xAddress == 'both':
|
|
|
|
sqlStatementParts.append("(fromaddress = ? OR toaddress = ?)")
|
|
|
|
sqlArguments.append(account)
|
|
|
|
sqlArguments.append(account)
|
|
|
|
else:
|
|
|
|
sqlStatementParts.append(xAddress + " = ? ")
|
|
|
|
sqlArguments.append(account)
|
2019-09-20 13:19:04 +02:00
|
|
|
if folder != "addressbook":
|
2019-08-07 17:39:46 +02:00
|
|
|
if folder is not None:
|
|
|
|
if folder == "new":
|
|
|
|
folder = "inbox"
|
|
|
|
unreadOnly = True
|
|
|
|
sqlStatementParts.append("folder = ? ")
|
|
|
|
sqlArguments.append(folder)
|
|
|
|
else:
|
|
|
|
sqlStatementParts.append("folder != ?")
|
|
|
|
sqlArguments.append("trash")
|
2018-08-07 08:14:14 +02:00
|
|
|
if what is not None:
|
2019-08-02 11:11:33 +02:00
|
|
|
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)
|
|
|
|
sqlArguments.append(what)
|
|
|
|
sqlStatementParts.append(filter_col)
|
2018-08-07 08:14:14 +02:00
|
|
|
if unreadOnly:
|
|
|
|
sqlStatementParts.append("read = 0")
|
2019-09-20 13:19:04 +02:00
|
|
|
if sqlStatementParts:
|
2018-08-07 08:14:14 +02:00
|
|
|
sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts)
|
2019-11-08 13:08:26 +01:00
|
|
|
if folder == "sent" or folder == "draft":
|
2019-12-04 16:45:45 +01:00
|
|
|
sqlStatementBase += " ORDER BY lastactiontime DESC limit {0}, {1}".format(start_indx, end_indx)
|
2019-09-20 18:19:28 +02:00
|
|
|
elif folder == "inbox":
|
2019-12-04 16:45:45 +01:00
|
|
|
sqlStatementBase += " ORDER BY received DESC limit {0}, {1}".format(start_indx, end_indx)
|
|
|
|
elif folder == "addressbook":
|
|
|
|
sqlStatementBase += " limit {0}, {1}".format(start_indx, end_indx)
|
2019-09-20 13:19:04 +02:00
|
|
|
return sqlQuery(sqlStatementBase, sqlArguments)
|