Commit a33d2d8d authored by Ismail Moumni's avatar Ismail Moumni
Browse files

Adding db_job_check and db_insert

parent f066f144
Pipeline #894 passed with stage
in 13 seconds
import mysql.connector
from get_docker_secret import get_docker_secret
import os
import random
# Exception thrown when no password has been defined to connect to the database
class NoPasswordDefineError(Exception):
......@@ -26,6 +27,7 @@ def get_password():
return password
return __rdos_password__
# Connect to the local MYSQL database on lipn-rdos
def database_connection():
mydb = mysql.connector.connect(
......@@ -100,6 +102,7 @@ def getParameters(generators: dict):
# Returns a dictionary containing all database generators existing in database
# The result returned contains every generators in database with parameters and default values for each field
def get_generators():
database = database_connection()
generators = {}
......@@ -112,19 +115,41 @@ def get_generators():
generators[result[0]] = [result]
# Insert in database a query send from client and returns insert status
def db_insert(query: dict):
if(query is not None):
database = database_connection()
if(db_job_check(query) != 0):
query['id'] = str(random.getrandbits(64))
try:
print(query)
values = query.values()
print(values)
cols = query.keys()
print(cols)
sql_new_job_row = "INSERT INTO jobs (%s) VALUES ('%s');" % (", ".join(cols), "','".join(values))
print(sql_new_job_row)
mycursor = database.cursor(buffered=False)
mycursor = database.cursor(buffered=True)
mycursor.execute(sql_new_job_row)
database.commit()
print("Record inserted", mycursor.rowcount)
database.commit()
mycursor.close()
except mysql.connector.Error as err:
print("MYSQL Error :{}".format(err))
def db_job_check(query: dict):
if (query is not None):
db = database_connection()
try:
ret = 0
val = query['id']
sql_new_job_row = "SELECT * FROM jobs WHERE ID=('%s') LIMIT 1;" % (val)
mycursor = db.cursor(buffered=True)
mycursor.execute(sql_new_job_row)
db.commit()
ret = mycursor.rowcount
mycursor.close()
return ret
except mysql.connector.Error as err:
print("MYSQL Error :{}".format(err))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment