-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaucet_db_operations.py
89 lines (64 loc) · 2.49 KB
/
faucet_db_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sqlite3
import logging
from config import Config
config = Config()
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=str(config.LOG_LEVEL))
log = logging.getLogger(__name__)
log.setLevel(config.LOG_LEVEL_INT)
class Database(object):
def __init__(self):
self.__db_connection = sqlite3.connect(config.DATABASE_PATH)
self.__db_cursor = self.__db_connection.cursor()
def create_table(self):
self.__db_cursor.execute('''
CREATE TABLE IF NOT EXISTS Payee(
email text,
amount integer,
date text
)''')
log.info("Database initialized successfully!!!")
self.__db_connection.commit()
def addPayment(self, email, amount, date):
try:
sqlite_insert_with_param = """INSERT INTO Payee
(email, amount, date)
VALUES (?, ?,?);"""
data_tuple = (email, amount, date)
self.__db_cursor.execute(sqlite_insert_with_param, data_tuple)
self.__db_connection.commit()
log.info("Payment added to database")
except sqlite3.Error as error:
print("Failed to add payment", error)
def getTotalAmountOfUser(self, email):
self.__db_cursor.execute("SELECT SUM(amount) FROM Payee WHERE email=?", (email,))
sum = self.__db_cursor.fetchone()
if sum[0] is not None:
amount = sum[0]
log.debug(f'Total Payments send to User {email}: {amount}')
else:
log.debug(f'Until now no sats send to user {email}')
amount = 0
return amount
def getTotalPayedSats(self):
self.__db_cursor.execute("SELECT SUM(amount) FROM Payee")
sum = self.__db_cursor.fetchone()
if sum[0] is not None:
amount = sum[0]
log.debug(f'Total Sats send: {amount}')
else:
log.debug(f'Until now no sats send to user')
amount = 0
return amount
def getNumberOfUsers(self):
self.__db_cursor.execute("SELECT COUNT(DISTINCT email) FROM Payee")
sum = self.__db_cursor.fetchone()
if sum[0] is not None:
users = sum[0]
log.debug(f'Total Payments send: {users}')
else:
log.debug(f'Until now no payemens send to user')
users =0
return users
def __del__(self):
self.__db_cursor.close()
self.__db_connection.close()