def get_all_projects(self): """ Select all the projects from the database. :return: All the databases in the projects table. """ result = None # Make connection try: sql = rti_sql(self.sql_conn_string) except Exception as e: print("Unable to connect to the database") return result # Get all projects try: result = sql.query('SELECT * FROM projects;') except Exception as e: print("Unable to run query", e) return result # Close connection sql.close() return result
def add_prj_sql(self, prj_name, prj_file_path): """ Add the given project name to the projects table. :param prj_name: Project name :return: TRUE = Project added. FALSE = Project already exists and could not add. """ # Check if the project exist project_exist = self.check_project_exist(prj_name) if project_exist == 0: # Add project to database dt = datetime.now() sql = rti_sql(self.sql_conn_string) query = 'INSERT INTO projects (name, path, created, modified) VALUES (%s,%s,%s,%s) RETURNING ID;' sql.cursor.execute(query, (prj_name, prj_file_path, dt, dt)) prj_idx = sql.cursor.fetchone()[0] sql.conn.commit() print(prj_idx) sql.close() return prj_idx elif project_exist > 0: # Send a warning and make them give a new name return -1
def check_project_exist(self, prj_name): """ Check if the given project name exist in the projects table. :param prj_name: Project Name. :return: TRUE = Project exists. """ idx = -1 # Make connection try: sql = rti_sql(self.sql_conn_string) except Exception as e: print("Unable to connect to the database") sql.close() return -1 # Check if the project exists try: result = sql.query('SELECT id FROM projects WHERE name = \'{0}\' LIMIT 1;'.format(prj_name)) # Check for a result if not result: idx = 0 # No project found else: idx = result[0][0] # Index found except Exception as e: print("Unable to run query", e) sql.close() return -2 # Close connection sql.close() return idx
def begin_batch(self, prj_name): # Make connection try: self.batch_sql = rti_sql(self.sql_conn_string) except Exception as e: print("Unable to connect to the database") # Get the index for the given project name self.batch_prj_id = self.batch_sql.query('SELECT id FROM projects WHERE name=\'{0}\''.format(prj_name)) print("Project ID: " + str(self.batch_prj_id))