diff options
author | Lukas Fleischer <lfleischer@archlinux.org> | 2016-08-03 20:28:22 +0200 |
---|---|---|
committer | Lukas Fleischer <lfleischer@archlinux.org> | 2016-08-05 12:05:22 +0200 |
commit | baf8a220ab923371cf19c742d2a7805e2276a037 (patch) | |
tree | dced61148242d7d715b235cff5a23d6c773caa86 | |
parent | f2a6bd207d5c3400e304c53f0b6eafb4bc5b7ece (diff) | |
download | aur-baf8a220ab923371cf19c742d2a7805e2276a037.tar.gz aur-baf8a220ab923371cf19c742d2a7805e2276a037.tar.xz |
git-interface: Support SQLite as database backend
In addition to MySQL, add support for SQLite to the database abstraction
layer. Also, add a new configuration option to select the DBMS.
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
-rw-r--r-- | conf/config.proto | 2 | ||||
-rw-r--r-- | git-interface/db.py | 42 |
2 files changed, 30 insertions, 14 deletions
diff --git a/conf/config.proto b/conf/config.proto index 543c3ca1..c56141c2 100644 --- a/conf/config.proto +++ b/conf/config.proto @@ -1,5 +1,5 @@ [database] -dsn_prefix = mysql +backend = mysql host = localhost socket = /var/run/mysqld/mysqld.sock name = AUR diff --git a/git-interface/db.py b/git-interface/db.py index c4c7d31c..060689b1 100644 --- a/git-interface/db.py +++ b/git-interface/db.py @@ -1,27 +1,43 @@ import mysql.connector +import sqlite3 import config class Connection: _conn = None + _paramstyle = None def __init__(self): - aur_db_host = config.get('database', 'host') - aur_db_name = config.get('database', 'name') - aur_db_user = config.get('database', 'user') - aur_db_pass = config.get('database', 'password') - aur_db_socket = config.get('database', 'socket') - - self._conn = mysql.connector.connect(host=aur_db_host, - user=aur_db_user, - passwd=aur_db_pass, - db=aur_db_name, - unix_socket=aur_db_socket, - buffered=True) + aur_db_backend = config.get('database', 'backend') + + if aur_db_backend == 'mysql': + aur_db_host = config.get('database', 'host') + aur_db_name = config.get('database', 'name') + aur_db_user = config.get('database', 'user') + aur_db_pass = config.get('database', 'password') + aur_db_socket = config.get('database', 'socket') + self._conn = mysql.connector.connect(host=aur_db_host, + user=aur_db_user, + passwd=aur_db_pass, + db=aur_db_name, + unix_socket=aur_db_socket, + buffered=True) + self._paramstyle = mysql.connector.paramstyle + elif aur_db_backend == 'sqlite': + aur_db_name = config.get('database', 'name') + self._conn = sqlite3.connect(aur_db_name) + self._paramstyle = sqlite3.paramstyle + else: + raise ValueError('unsupported database backend') def execute(self, query, params=()): - query = query.replace('%', '%%').replace('?', '%s') + if self._paramstyle == 'format': + query = query.replace('%', '%%').replace('?', '%s') + elif self._paramstyle == 'qmark': + pass + else: + raise ValueError('unsupported paramstyle') cur = self._conn.cursor() cur.execute(query, params) |