Explorar el Código

implemented the necessary code for the Populate function to create the database tables and insert the days of the week.

Helmut Pozimski hace 10 años
padre
commit
c7ff552dd4
Se han modificado 1 ficheros con 44 adiciones y 1 borrados
  1. 44 1
      jwmudlib/database.py

+ 44 - 1
jwmudlib/database.py

@@ -54,4 +54,47 @@ class db:
         except sqlite3.OperationalError:
         except sqlite3.OperationalError:
             raise jwmu_exceptions.DataBaseWriteFailed()
             raise jwmu_exceptions.DataBaseWriteFailed()
         else:
         else:
-            self.__connection.commit()
+            self.__connection.commit()
+
+    def Populate(self):
+        """Creates the initial database structure with all the necessary
+        tables.
+
+        """
+        create_weekdays = """CREATE TABLE weekdays(
+        day_id INTEGER PRIMARY KEY NOT NULL,
+        alarm_time TEXT,
+        name TEXT
+        );"""
+        create_categories = """CREATE TABLE categories(
+        category_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
+        name TEXT NOT NULL,
+        alarm_time TEXT
+        );"""
+        create_alarmdays = """CREATE TABLE alarmdays(
+        date TEXT NOT NULL,
+        alarm_time TEXT,
+        category INTEGER DEFAULT NULL,
+        FOREIGN KEY(category) REFERENCES category(category_id),
+        PRIMARY KEY(date, alarm_time)
+        );"""
+        # create the empty tables
+        self.__ExecuteStatement(create_weekdays)
+        self.__ExecuteStatement(create_categories)
+        self.__ExecuteStatement(create_alarmdays)
+        # fill the weekdays table with the existing 7 days of the week,
+        # these are always hardcoded
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (1, 'undefined', 'Monday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (2, 'undefined', 'Tuesday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (3, 'undefined', 'Wednesday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (4, 'undefined', 'Thursday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (5, 'undefined', 'Friday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (6, 'undefined', 'Saturday')")
+        self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
+                                "name) VALUES (7, 'undefined', 'Sunday')")