48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import os
|
|
import sqlite3
|
|
|
|
SQLITE_DB = os.environ.get('SQLITE_DB', 'db.sqlite3')
|
|
|
|
TABLE_REPEATS = '''
|
|
CREATE TABLE IF NOT EXISTS repeats (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
probability FLOAT NOT NULL,
|
|
orders TEXT NOT NULL,
|
|
count INTEGER DEFAULT 0
|
|
);
|
|
'''
|
|
|
|
class Database:
|
|
def __init__(self):
|
|
self.conn = sqlite3.connect(SQLITE_DB, detect_types=sqlite3.PARSE_DECLTYPES)
|
|
self.conn.row_factory = sqlite3.Row
|
|
self.table_init(TABLE_REPEATS)
|
|
|
|
def table_init(self, table_sql):
|
|
c = self.conn.cursor()
|
|
c.execute(table_sql)
|
|
self.conn.commit()
|
|
|
|
def repeat_get(self):
|
|
c = self.conn.cursor()
|
|
sql = 'SELECT id, probability, orders, count FROM repeats LIMIT 1'
|
|
c.execute(sql)
|
|
return c.fetchone()
|
|
|
|
def repeat_increment(self):
|
|
c = self.conn.cursor()
|
|
sql = 'UPDATE repeats SET count = count + 1'
|
|
c.execute(sql)
|
|
self.conn.commit()
|
|
|
|
def repeat_put(self, probability, orders):
|
|
c = self.conn.cursor()
|
|
sql = 'INSERT INTO repeats (probability, orders) VALUES (?, ?);'
|
|
c.execute(sql, [probability, orders])
|
|
self.conn.commit()
|
|
|
|
def repeat_clear(self):
|
|
c = self.conn.cursor()
|
|
sql = 'DELETE FROM repeats'
|
|
c.execute(sql)
|
|
self.conn.commit()
|