|
@@ -28,6 +28,8 @@ def drop_triggers(cursor):
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS order_amount_positive_after_update")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS not_more_executed_than_ordered_after_insert")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS not_more_executed_than_ordered_after_update")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS not_more_ordered_than_available_after_insert")
|
|
|
+ cursor.execute("DROP TRIGGER IF EXISTS not_more_ordered_than_available_after_update")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS expiry_dt_in_future_after_insert")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS expiry_dt_in_future_after_update")
|
|
|
cursor.execute("DROP TRIGGER IF EXISTS stop_loss_requires_limit_after_insert")
|
|
@@ -56,11 +58,13 @@ def drop_triggers(cursor):
|
|
|
|
|
|
def seed(cursor):
|
|
|
print(' - Seeding initial data...')
|
|
|
+ # ₭ollar
|
|
|
cursor.execute('''
|
|
|
INSERT INTO ownables
|
|
|
(name)
|
|
|
VALUES (?)
|
|
|
''', (CURRENCY_NAME,))
|
|
|
+ # The bank/external investors
|
|
|
cursor.execute('''
|
|
|
INSERT INTO users
|
|
|
(username,password)
|
|
@@ -73,6 +77,28 @@ def seed(cursor):
|
|
|
(SELECT rowid FROM ownables WHERE name = ?))
|
|
|
''', (CURRENCY_NAME,))
|
|
|
|
|
|
+ # bank owns all the money that is not owned by players, 1000 * num_used_key - player_money
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT INTO users
|
|
|
+ (user_id, ownable_id, amount)
|
|
|
+ VALUES ((SELECT rowid FROM users WHERE username = 'bank'),
|
|
|
+ (SELECT rowid FROM ownables WHERE name = ?),
|
|
|
+ 1000 * (SELECT COUNT(used_by_user_id) FROM keys) - (SELECT SUM(amount)
|
|
|
+ FROM ownership
|
|
|
+ WHERE ownable_id = (SELECT rowid FROM ownables WHERE name = ?)))
|
|
|
+ ''', (CURRENCY_NAME,))
|
|
|
+
|
|
|
+ # bank owns some stuff (₭ollar is be dealt with separately)
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT OR IGNORE INTO ownership
|
|
|
+ (user_id, ownable_id, amount)
|
|
|
+ SELECT (SELECT rowid FROM users WHERE username = 'bank'),
|
|
|
+ ownable.rowid,
|
|
|
+ (SELECT SUM(amount) FROM ownership WHERE ownable_id = ownables.rowid)
|
|
|
+ FROM ownables WHERE
|
|
|
+ name <> ?
|
|
|
+ ''', (CURRENCY_NAME,))
|
|
|
+
|
|
|
|
|
|
def create_triggers(cursor):
|
|
|
print(' - Creating triggers...')
|
|
@@ -136,6 +162,38 @@ def create_triggers(cursor):
|
|
|
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_ordered_than_available_after_insert
|
|
|
+ AFTER INSERT ON orders
|
|
|
+ WHEN 0 <=
|
|
|
+ -- sell_ordered_amount
|
|
|
+ (SELECT COALESCE(SUM(orders.ordered_amount - orders.executed_amount),0)
|
|
|
+ FROM orders, ownership
|
|
|
+ WHERE ownership.rowid = orders.ownership_id
|
|
|
+ AND ownership.rowid = NEW.ownership_id
|
|
|
+ AND NOT orders.buy)
|
|
|
+ - -- owned_amount
|
|
|
+ (SELECT COALESCE(amount, 0)
|
|
|
+ FROM ownership
|
|
|
+ WHERE ownership.rowid = NEW.ownership_id)
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not order more than you own available.'); END
|
|
|
+ ''') # TODO test these triggers
|
|
|
+ cursor.execute('''
|
|
|
+ CREATE TRIGGER IF NOT EXISTS not_more_ordered_than_available_after_update
|
|
|
+ AFTER UPDATE ON orders
|
|
|
+ WHEN 0 <=
|
|
|
+ -- sell_ordered_amount
|
|
|
+ (SELECT COALESCE(SUM(orders.ordered_amount - orders.executed_amount),0)
|
|
|
+ FROM orders, ownership
|
|
|
+ WHERE ownership.rowid = orders.ownership_id
|
|
|
+ AND ownership.rowid = NEW.ownership_id
|
|
|
+ AND NOT orders.buy)
|
|
|
+ - -- owned_amount
|
|
|
+ (SELECT COALESCE(amount, 0)
|
|
|
+ FROM ownership
|
|
|
+ WHERE ownership.rowid = NEW.ownership_id)
|
|
|
+ BEGIN SELECT RAISE(ROLLBACK, 'Can not order more than you own available.'); END
|
|
|
+ ''') # TODO test these triggers
|
|
|
cursor.execute('''
|
|
|
CREATE TRIGGER IF NOT EXISTS expiry_dt_in_future_after_insert
|
|
|
AFTER INSERT ON orders
|