Skip to content
Snippets Groups Projects
Commit 0c1c9c49 authored by Simon Künzel's avatar Simon Künzel
Browse files

Request insertion auto increment value explicitly

parent b448ee30
No related branches found
No related tags found
No related merge requests found
......@@ -40,7 +40,8 @@ def suggest_chapter(lectureid):
course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
id = modify(
'INSERT INTO chapters (lecture_id, time, text, time_created, time_updated, created_by, submitted_by) VALUES (?, ?, ?, ?, ?, ?, ?)',
lectureid, chapter_start, text, datetime.now(), datetime.now(), session.get('user', {'dbid':None})['dbid'], submitter
lectureid, chapter_start, text, datetime.now(), datetime.now(), session.get('user', {'dbid':None})['dbid'], submitter,
get_id=True
)
chapter = query('SELECT * FROM chapters WHERE id = ?', id)[0]
if not ismod():
......
......@@ -100,8 +100,8 @@ def query(operation, *params, delim="sep", nlfix=True):
try:
cur = get_dbcursor()
cur.execute(operation, params)
except mysql.connector.errors.InternalError as e:
if e.msg == 'Deadlock found when trying to get lock; try restarting transaction':
except Exception as e:
if str(e) == 'Deadlock found when trying to get lock; try restarting transaction':
tries += 1
retry = True
else:
......@@ -109,8 +109,8 @@ def query(operation, *params, delim="sep", nlfix=True):
rows = []
try:
rows = cur.fetchall()
except mysql.connector.errors.InterfaceError as e:
if e.msg == 'No result set to fetch from.':
except Exception as e:
if str(e) == 'No result set to fetch from.' or str(e) == "the last operation didn't produce a result":
# no problem, we were just at the end of the result set
pass
else:
......@@ -129,10 +129,12 @@ def query(operation, *params, delim="sep", nlfix=True):
ptr[name] = col
return res
def modify(operation, *params):
def modify(operation, *params, get_id=False):
operation, params = fix_query(operation, params)
cur = get_dbcursor()
cur.execute(operation, params)
if not get_id:
return None
return cur.lastrowid
@app.teardown_request
......
......@@ -227,8 +227,10 @@ def create(table):
assert column not in defaults
columns.append('"'+column+'"')
values.append(val)
assert editable_tables[table]['idcolumn'] == 'id'
id = modify('INSERT INTO %s (%s) VALUES (%s)'%(editable_tables[table]['table'],
','.join(columns), ','.join(['?']*len(values))), *values)
','.join(columns), ','.join(['?']*len(values))), *values,
get_id=True)
if table == 'courses':
set_responsible(id, session['user']['dbid'], 1)
if 'ref' in request.values:
......
......@@ -50,7 +50,8 @@ def schedule_job(jobtype, data=None, priority=0, queue="default"):
if not data:
data = {}
return modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)',
jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now())
jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now(),
get_id=True)
def cancel_job(job_id):
query('UPDATE jobs SET state = \'deleted\' WHERE id = ? AND state = \'ready\'', job_id)
......
......@@ -61,7 +61,8 @@ def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1, dura
(lecture_id, visible, path, video_format, title, comment, internal, file_modified, time_created, time_updated, created_by, hash, file_size, duration, source)
VALUES
(?, ?, ?, ?, '', '', '', ?, ?, ?, ?, ?, ?, ?, ?)''',
lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration, sourceid)
lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration, sourceid,
get_id=True)
if not sourceid:
query('INSERT INTO sortlog (lecture_id,video_id,path,"when") VALUES (?,?,?,?)', lectureid, video_id, dbfilepath, datetime.now())
schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment