From 30a210b6b0b95dd07d6ba9e865126a9153df0094 Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Wed, 17 Aug 2016 16:17:41 +0200
Subject: [PATCH] Added sqlite support and made query compatible

---
 server.py | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/server.py b/server.py
index 5fac9fc..153082c 100755
--- a/server.py
+++ b/server.py
@@ -1,16 +1,35 @@
 #!/bin/python
 from flask import Flask, render_template, g
 import mysql.connector
+import sqlite3
 import config
 
 app = Flask(__name__)
 
+# Row wrapper for sqlite
+def dict_factory(cursor, row):
+	d = {}
+	for idx, col in enumerate(cursor.description):
+		if type(row[idx]) == str:
+			d[col[0].split('.')[-1]] = row[idx].replace('\\n','\n')
+		else:
+			d[col[0].split('.')[-1]] = row[idx]
+	return d
+
 def query(operation, *params):
-	if 'db' not in g or not g.db.is_connected():
-		g.db = mysql.connector.connect(user=config.db_user, password=config.db_passwd, host=config.db_host, database=config.db_db)
-	cur = g.db.cursor(dictionary=True)
-	cur.execute(operation, params)
-	return cur.fetchall()
+	if config.db_engine == 'mysql':
+		if 'db' not in g or not g.db.is_connected():
+			g.db = mysql.connector.connect(user=config.db_user, password=config.db_passwd, host=config.db_host, database=config.db_db)
+		cur = g.db.cursor(dictionary=True)
+		cur.execute(operation.replace('?', '%s'), params)
+		return cur.fetchall()
+	else:
+		if 'db' not in g or not g.db.is_connected():
+			g.db = sqlite3.connect(config.db_file)
+			g.db.row_factory = dict_factory
+		cur = g.db.cursor()
+		cur.execute(operation, params)
+		return cur.fetchall()
 
 @app.route('/')
 def index():
@@ -19,8 +38,7 @@ def index():
 				FROM lectures
 				LEFT JOIN videos ON (videos.lecture_id = lectures.id)
 				LEFT JOIN courses on (courses.id = lectures.course_id)
-				WHERE (videos.time_updated >= SUBDATE(NOW(), 14))
-					AND (%s OR (courses.visible AND courses.listed AND lectures.visible AND videos.visible))
+				WHERE (? OR (courses.visible AND courses.listed AND lectures.visible AND videos.visible))
 				GROUP BY videos.lecture_id
 				ORDER BY lastvidtime DESC
 				LIMIT 5
-- 
GitLab