|
@@ -1,20 +1,156 @@
|
|
|
from game import CURRENCY_NAME
|
|
|
+from util import debug
|
|
|
|
|
|
|
|
|
def setup(cursor):
|
|
|
print('Database setup...')
|
|
|
|
|
|
- replace = False
|
|
|
+ replace = False and debug
|
|
|
|
|
|
if replace:
|
|
|
- print(' - Dropping old tables...')
|
|
|
- cursor.execute("DROP TABLE IF EXISTS users")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS ownables")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS ownership")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS sessions")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS orders")
|
|
|
- cursor.execute("DROP TABLE IF EXISTS transactions")
|
|
|
+ drop_database(cursor)
|
|
|
|
|
|
+ tables(cursor)
|
|
|
+
|
|
|
+ integrity_checks(cursor)
|
|
|
+
|
|
|
+ if replace: # TODO also seed new databases
|
|
|
+ seed(cursor)
|
|
|
+
|
|
|
+
|
|
|
+def drop_database(cursor):
|
|
|
+ print(' - Dropping old tables...')
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS users")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS ownables")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS ownership")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS sessions")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS orders")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS transactions")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS keys")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS news")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS owned_amount_not_negative_after_insert")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS owned_amount_not_negative_after_update")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS order_limit_not_negative_after_insert")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS order_limit_not_negative_after_update")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS order_amount_positive_after_insert")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS order_amount_positive_after_update")
|
|
|
+
|
|
|
+
|
|
|
+def seed(cursor):
|
|
|
+ print(' - Seeding initial data...')
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT INTO ownables
|
|
|
+ (name)
|
|
|
+ VALUES (?)
|
|
|
+ ''', (CURRENCY_NAME,))
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT INTO users
|
|
|
+ (username,password)
|
|
|
+ VALUES ('bank','')
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT INTO ownership
|
|
|
+ (user_id, ownable_id)
|
|
|
+ VALUES ((SELECT rowid FROM users WHERE username = 'bank'),
|
|
|
+ (SELECT rowid FROM ownables WHERE name = ?))
|
|
|
+ ''', (CURRENCY_NAME,))
|
|
|
+
|
|
|
+
|
|
|
+def integrity_checks(cursor):
|
|
|
+ print(' - Integrity checks...')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_insert
|
|
|
+ AFTER INSERT
|
|
|
+ ON ownership
|
|
|
+ WHEN NEW.amount < 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_update
|
|
|
+ AFTER UPDATE
|
|
|
+ ON ownership
|
|
|
+ WHEN NEW.amount < 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS amount_positive_after_insert
|
|
|
+ AFTER INSERT
|
|
|
+ ON transactions
|
|
|
+ WHEN NEW.amount <= 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not perform empty transactions.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS amount_positive_after_update
|
|
|
+ AFTER UPDATE
|
|
|
+ ON transactions
|
|
|
+ WHEN NEW.amount <= 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not perform empty transactions.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_insert
|
|
|
+ AFTER INSERT
|
|
|
+ ON orders
|
|
|
+ WHEN NEW."limit" IS NOT NULL AND NEW."limit" < 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_update
|
|
|
+ AFTER UPDATE
|
|
|
+ ON orders
|
|
|
+ WHEN NEW."limit" IS NOT NULL AND NEW."limit" < 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS order_amount_positive_after_insert
|
|
|
+ AFTER INSERT
|
|
|
+ ON orders
|
|
|
+ WHEN NEW.ordered_amount <= 0 OR NEW.executed_amount < 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS order_amount_positive_after_update
|
|
|
+ AFTER UPDATE
|
|
|
+ ON orders
|
|
|
+ WHEN NEW.ordered_amount <= 0 OR NEW.executed_amount < 0
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS not_more_executed_than_ordered_after_insert
|
|
|
+ AFTER INSERT
|
|
|
+ ON orders
|
|
|
+ WHEN NEW.ordered_amount < NEW.executed_amount
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not execute more than ordered.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS not_more_executed_than_ordered_after_update
|
|
|
+ AFTER UPDATE
|
|
|
+ ON orders
|
|
|
+ WHEN NEW.ordered_amount < NEW.executed_amount
|
|
|
+ BEGIN
|
|
|
+ SELECT RAISE(ROLLBACK, 'Can not execute more than ordered.');
|
|
|
+ END
|
|
|
+ ''')
|
|
|
+
|
|
|
+
|
|
|
+def tables(cursor):
|
|
|
print(' - Creating tables...')
|
|
|
cursor.execute('''
|
|
|
CREATE TABLE IF NOT EXISTS users(
|
|
@@ -48,16 +184,17 @@ def setup(cursor):
|
|
|
buy BOOLEAN NOT NULL,
|
|
|
"limit" CURRENCY,
|
|
|
stop_loss BOOLEAN,
|
|
|
- ordered_amount CURRENCY,
|
|
|
- executed_amount CURRENCY DEFAULT 0,
|
|
|
+ ordered_amount CURRENCY NOT NULL,
|
|
|
+ executed_amount CURRENCY DEFAULT 0 NOT NULL,
|
|
|
FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
|
|
|
)
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TABLE IF NOT EXISTS transactions(
|
|
|
- dt DATETIME NOT NULL,
|
|
|
+ dt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
price CURRENCY NOT NULL,
|
|
|
ownable_id INTEGER NOT NULL,
|
|
|
+ amount CURRENCY NOT NULL,
|
|
|
FOREIGN KEY (ownable_id) REFERENCES ownable(rowid)
|
|
|
)
|
|
|
''')
|
|
@@ -74,78 +211,3 @@ def setup(cursor):
|
|
|
title VARCHAR(50) NOT NULL
|
|
|
)
|
|
|
''')
|
|
|
-
|
|
|
- print(' - Integrity checks...')
|
|
|
- cursor.execute('''
|
|
|
- CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON ownership
|
|
|
- WHEN NEW.amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
|
|
|
- END
|
|
|
- ''')
|
|
|
- cursor.execute('''
|
|
|
- CREATE TRIGGER IF NOT EXISTS owned_amount_not_negative_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON ownership
|
|
|
- WHEN NEW.amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not own an amount less than 0.');
|
|
|
- END
|
|
|
- ''')
|
|
|
- cursor.execute('''
|
|
|
- CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
- WHEN "limit" IS NOT NULL AND "limit" < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
|
|
|
- END
|
|
|
- ''')
|
|
|
- cursor.execute('''
|
|
|
- CREATE TRIGGER IF NOT EXISTS order_limit_not_negative_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
- WHEN "limit" IS NOT NULL AND "limit" < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not set a limit less than 0.');
|
|
|
- END
|
|
|
- ''')
|
|
|
- cursor.execute('''
|
|
|
- CREATE TRIGGER IF NOT EXISTS order_amount_not_negative_after_insert
|
|
|
- AFTER INSERT
|
|
|
- ON orders
|
|
|
- WHEN ordered_amount <= 0 OR executed_amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
|
|
|
- END
|
|
|
- ''')
|
|
|
- cursor.execute('''
|
|
|
- CREATE TRIGGER IF NOT EXISTS order_amount_not_negative_after_update
|
|
|
- AFTER UPDATE
|
|
|
- ON orders
|
|
|
- WHEN ordered_amount <= 0 OR executed_amount < 0
|
|
|
- BEGIN
|
|
|
- SELECT RAISE(ROLLBACK, 'Can not order 0 or less.');
|
|
|
- END
|
|
|
- ''')
|
|
|
-
|
|
|
- if replace: # TODO also seed new databases
|
|
|
- print(' - Seeding initial data...')
|
|
|
- cursor.execute('''
|
|
|
- INSERT INTO ownables
|
|
|
- (name)
|
|
|
- VALUES (?)
|
|
|
- ''', (CURRENCY_NAME,))
|
|
|
- cursor.execute('''
|
|
|
- INSERT INTO users
|
|
|
- (username,password)
|
|
|
- VALUES ('bank','')
|
|
|
- ''')
|
|
|
- cursor.execute('''
|
|
|
- INSERT INTO ownership
|
|
|
- (user_id, ownable_id)
|
|
|
- VALUES ((SELECT rowid FROM users WHERE username = 'bank'),
|
|
|
- (SELECT rowid FROM ownables WHERE name = ?))
|
|
|
- ''', (CURRENCY_NAME,))
|