diff options
Diffstat (limited to 'opendc/util/database.py')
| -rw-r--r-- | opendc/util/database.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/opendc/util/database.py b/opendc/util/database.py index 32aa947c..e4c257d5 100644 --- a/opendc/util/database.py +++ b/opendc/util/database.py @@ -1,7 +1,6 @@ -from datetime import datetime import json -import sqlite3 import sys +from datetime import datetime from mysql.connector.pooling import MySQLConnectionPool @@ -12,10 +11,12 @@ with open(sys.argv[1]) as file: DATETIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S' CONNECTION_POOL = None + def init_connection_pool(user, password, database, host, port): global CONNECTION_POOL - CONNECTION_POOL = MySQLConnectionPool(pool_name = "opendcpool", pool_size = 5, - user=user, password=password, database=database, host=host, port=port) + CONNECTION_POOL = MySQLConnectionPool(pool_name="opendcpool", pool_size=5, + user=user, password=password, database=database, host=host, port=port) + def execute(statement, t): """Open a database connection and execute the statement.""" @@ -23,7 +24,7 @@ def execute(statement, t): # Connect to the database connection = CONNECTION_POOL.get_connection() cursor = connection.cursor() - + # Execute the statement cursor.execute(statement, t) @@ -38,6 +39,7 @@ def execute(statement, t): # Return the id return row_id + def fetchone(statement, t=None): """Open a database connection and return the first row matched by the SELECT statement.""" @@ -58,6 +60,7 @@ def fetchone(statement, t=None): connection.close() return value + def fetchall(statement, t=None): """Open a database connection and return all rows matched by the SELECT statement.""" @@ -66,7 +69,7 @@ def fetchall(statement, t=None): cursor = connection.cursor() # Execute the SELECT statement - + if t is not None: cursor.execute(statement, t) else: @@ -78,11 +81,13 @@ def fetchall(statement, t=None): connection.close() return values + def datetime_to_string(datetime_to_convert): """Return a database-compatible string representation of the given datetime object.""" return datetime_to_convert.strftime(DATETIME_STRING_FORMAT) + def string_to_datetime(string_to_convert): """Return a datetime corresponding to the given string representation.""" |
