Bladeren bron

begin work on börse

Eren Yilmaz 6 jaren geleden
bovenliggende
commit
f28896ac2b
1 gewijzigde bestanden met toevoegingen van 26 en 13 verwijderingen
  1. 26 13
      model.py

+ 26 - 13
model.py

@@ -59,14 +59,15 @@ def setup():
     replace = False
 
     if replace:
-        print('  Dropping old tables...')
+        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")
 
-    print('  Creating tables...')
+    print(' - Creating tables...')
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS users(
                     username VARCHAR(10) UNIQUE NOT NULL, 
@@ -75,15 +76,15 @@ def setup():
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS ownables(
                     name VARCHAR(10) UNIQUE NOT NULL, 
-                    total_amount INTEGER NOT NULL)
+                    total_amount CURRENCY NOT NULL)
                 ''')
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS ownership(
                     user_id INTEGER NOT NULL,
-                    stock_id INTEGER NOT NULL,
-                    amount INTEGER NOT NULL DEFAULT 0,
+                    ownable_id INTEGER NOT NULL,
+                    amount CURRENCY NOT NULL DEFAULT 0,
                     FOREIGN KEY (user_id) REFERENCES users(rowid),
-                    FOREIGN KEY (stock_id) REFERENCES ownables(rowid),
+                    FOREIGN KEY (ownable_id) REFERENCES ownables(rowid),
                     UNIQUE (user_id, stock_id)
                 )
                 ''')
@@ -95,15 +96,27 @@ def setup():
                 )
                 ''')
     cursor.execute('''
-                CREATE TABLE IF NOT EXISTS sessions(
-                    user_id INTEGER NOT NULL,
-                    session_id STRING NOT NULL,
-                    FOREIGN KEY (user_id) REFERENCES users(rowid)
+                CREATE TABLE IF NOT EXISTS orders(
+                    ownership_id INTEGER NOT NULL,
+                    buy BOOLEAN NOT NULL,
+                    limit CURRENCY,
+                    stop_loss BOOLEAN,
+                    ordered_amount CURRENCY,
+                    executed_amount CURRENCY,
+                    FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
+                )
+                ''')
+    cursor.execute('''
+                CREATE TABLE IF NOT EXISTS transactions(
+                    dt DATETIME NOT NULL,
+                    price CURRENCY NOT NULL,
+                    ownable_id INTEGER NOT NULL,
+                    FOREIGN KEY (ownable_id) REFERENCES ownable(rowid)
                 )
                 ''')
 
-    if replace:
-        print('  Adding initial data...')
+    if replace:  # TODO also seed new databases
+        print(' - Seeding initial data...')
         cursor.execute('''
                     INSERT INTO users
                     (username, password)
@@ -128,7 +141,7 @@ def setup():
         kollar_id = cursor.fetchone()[0]
         cursor.execute('''
                     INSERT INTO ownership
-                    (user_id, stock_id, amount)
+                    (user_id, ownable_id, amount)
                     VALUES (?, ?, ?)
                     ''', (bank_id, kollar_id, money_amount))