فهرست منبع

added initial implementation of the database class and the first exceptions regarding the database.

Helmut Pozimski 10 سال پیش
والد
کامیت
17057e95a6
2فایلهای تغییر یافته به همراه89 افزوده شده و 0 حذف شده
  1. 57 0
      jwmudlib/database.py
  2. 32 0
      jwmudlib/jwmu_exceptions.py

+ 57 - 0
jwmudlib/database.py

@@ -0,0 +1,57 @@
+# This file is part of jwmud, written by Helmut Pozimski in 2014.
+#
+#       jwmud is free software: you can redistribute it and/or modify
+#       it under the terms of the GNU General Public License as published by
+#       the Free Software Foundation, version 2 of the License.
+#
+#       jwmud is distributed in the hope that it will be useful,
+#       but WITHOUT ANY WARRANTY; without even the implied warranty of
+#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#       GNU General Public License for more details.
+#
+#       You should have received a copy of the GNU General Public License
+#       along with jwmud.  If not, see <http://www.gnu.org/licenses/>.
+
+# -*- coding: utf8 -*-
+
+import sqlite3
+
+from jwmudlib import jwmu_exceptions
+
+
+class db:
+    def __init__(self, db_path):
+        """Constructor, establishes the initial connection to the database."""
+        try:
+            self.__connection = sqlite3.connect(db_path)
+        except sqlite3.OperationalError:
+            raise jwmu_exceptions.DataBaseAccessFailed()
+        else:
+            self.__cursor = self.__connection.cursor()
+
+    def __del__(self):
+        """Destructor, closes the connection to the database"""
+        self.__connection.close()
+
+    def __ExecuteQuery(self, query, arguments=None):
+        """Executes any query and returns the result"""
+        if arguments is None:
+            query_result = self.__cursor.execute(query)
+        else:
+            query_result = self.__cursor.execute(query, arguments)
+        return query_result
+
+    def __ExecuteStatement(self, statement, arguments=None):
+        """Executes any statements that perform a write access to the
+        database
+
+        """
+        try:
+            if arguments is None:
+                self.__cursor.execute(statement)
+            else:
+                self.__cursor.execute(statement, arguments)
+        except sqlite3.OperationalError:
+            raise jwmu_exceptions.DataBaseWriteFailed()
+        else:
+            self.__connection.commit()

+ 32 - 0
jwmudlib/jwmu_exceptions.py

@@ -0,0 +1,32 @@
+# This file is part of jwmud, written by Helmut Pozimski in 2014.
+#
+#       jwmud is free software: you can redistribute it and/or modify
+#       it under the terms of the GNU General Public License as published by
+#       the Free Software Foundation, version 2 of the License.
+#
+#       jwmud is distributed in the hope that it will be useful,
+#       but WITHOUT ANY WARRANTY; without even the implied warranty of
+#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#       GNU General Public License for more details.
+#
+#       You should have received a copy of the GNU General Public License
+#       along with jwmud.  If not, see <http://www.gnu.org/licenses/>.
+
+# -*- coding: utf8 -*-
+
+
+class DataBaseAccessFailed(Exception):
+    def __init__(self):
+        self.__message = "Accessing the database failed!"
+
+    def __str__(self):
+        return self.__message
+
+
+class DataBaseWriteFailed(Exception):
+    def __init__(self):
+        self.__message = "Writing to the database failed, requested action " \
+                         "aborted !"
+
+    def __str__(self):
+        return self.__message