Commit 3bd34221 authored by Cristiano Urban's avatar Cristiano Urban
Browse files

Added psycopg2 extras + added 'RealDictCursor' + changed some methods accordingly.

parent 8713c40e
Loading
Loading
Loading
Loading
Loading
+40 −9
Original line number Diff line number Diff line
import json
import psycopg2
import psycopg2.extras
import sys

from node import Node
@@ -23,7 +24,7 @@ class DbConnector(object):
                                         database = self.dbname)
        except(Exception, psycopg2.Error) as error :
            sys.exit(f"Error while connecting to PostgreSQL: {error}")
        self.cursor = self.conn.cursor()
        self.cursor = self.conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor)

    def disconnect(self):
        if self.conn:
@@ -32,8 +33,22 @@ class DbConnector(object):
            
    def insertJob(self, jobObj):
        if self.conn:
            self.cursor.execute("INSERT INTO job(job_id, owner_id, job_type, phase, start_time, end_time, job_info, results) VALUES (%s, %s, %s, %s, %s, %s, %s, %s);", 
                                (jobObj.jobId, jobObj.ownerId, jobObj.type, jobObj.phase, jobObj.startTime, jobObj.endTime, json.dumps(jobObj.jobInfo), json.dumps(jobObj.results),))
            self.cursor.execute("""
INSERT INTO job(job_id, owner_id, job_type, phase, start_time, end_time, job_info, results)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (job_id)
DO UPDATE SET 
(owner_id, job_type, phase, start_time, end_time, job_info, results) 
= (EXCLUDED.owner_id, EXCLUDED.job_type, EXCLUDED.phase, EXCLUDED.start_time, EXCLUDED.end_time, EXCLUDED.job_info, EXCLUDED.results);
""", 
                                (jobObj.jobId, 
                                 jobObj.ownerId, 
                                 jobObj.type, 
                                 jobObj.phase, 
                                 jobObj.startTime, 
                                 jobObj.endTime, 
                                 json.dumps(jobObj.jobInfo), 
                                 json.dumps(jobObj.results),))
            self.conn.commit()

    def insertNode(self, node, parentOSPath):
@@ -45,13 +60,22 @@ class DbConnector(object):
            for i in result:
                out.write(f"queryResult: {i}\n")
            #parentLtreePath = self.cursor.fetchone()[0]
            parentLtreePath = result[0]
            parentLtreePath = result[0]["path"]
            out.write(f"parentLtreePath: {parentLtreePath}\n")
            out.write(f"parentPath: {node.parentPath}\n\n")
            out.close()
            #print(f"parentLtreePath: {parentLtreePath}, type: {type(parentLtreePath)}")
            self.cursor.execute("INSERT INTO Node(parent_path, parent_relative_path, name, type, owner_id, creator_id, content_md5) VALUES (%s, %s, %s, %s, %s, %s, %s);", 
                                (parentLtreePath, parentLtreePath, node.name, node.type, node.ownerID, node.creatorID, node.contentMD5,))
            self.cursor.execute("""
INSERT INTO Node(parent_path, parent_relative_path, name, type, owner_id, creator_id, content_md5) 
VALUES (%s, %s, %s, %s, %s, %s, %s);
""", 
                                (parentLtreePath, 
                                 parentLtreePath, 
                                 node.name, 
                                 node.type, 
                                 node.ownerID, 
                                 node.creatorID, 
                                 node.contentMD5,))
            self.conn.commit()

    def selectNode(self):
@@ -60,9 +84,16 @@ class DbConnector(object):
    def getJob(self, jobId):
        if self.conn:
            self.cursor.execute("SELECT * FROM job WHERE job_id = %s;", (jobId,))
            return self.cursor.fetchone()[0]
            out = open("db_connector_log.txt", "a")
            result = self.cursor.fetchall()
            out.write(f"result: {result}\n\n")
            out.close()
            if not result:
                return json.loads('{ "error": "JOB_NOT_FOUND" }')
            else:                
                return result[0]

    def getRapId(self, username):
        if self.conn:
            self.cursor.execute("SELECT rap_id FROM Users WHERE user_name = %s;", (username,))            
            return self.cursor.fetchone()[0]
 No newline at end of file
            return self.cursor.fetchall()[0]["rap_id"]
 No newline at end of file