예제 #1
0
    def run_via_cursor(self, sql_query):
        if self.conn:
            try:
                cursor = self.conn.cursor()
                cursor.execute(sql_query)

                for row in cursor:
                    Verbose.print_ln(row)

                return cursor
            except Exception as err:
                Verbose.print_ln(
                    "Was not able to execute the query via the cursor")
                Verbose.print_ln("Error:", str(err))
                quit()
        else:
            Verbose.print_ln("Something wrong happened, not connected!")
예제 #2
0
    def connect(self):
        try:
            conn = pyodbc.connect(self.connection_string)
            Verbose.print_ln('Connected to the database successfully!')
        except Exception as err:
            conn = None
            Verbose.print_ln(
                f'\nFailed to connect to the database! \nError: {str(err)}')
            Verbose.print_ln(self.connection_string)
            quit()

        return conn
예제 #3
0
 def create(self, sql_query):
     if self.conn:
         try:
             cursor = self.conn.cursor()
             cursor.execute(sql_query)
             self.conn.commit()
         except Exception as err:
             Verbose.print_ln('Was not able to create view', sql_query)
             Verbose.print_ln("Error:", str(err))
             quit()
     else:
         Verbose.print_ln("Something wrong happened, not connected!")
예제 #4
0
    def run(self, sql_query):
        result = None

        if self.conn:
            try:
                result = pd.read_sql(sql_query, self.conn)
            except Exception as err:
                Verbose.print_ln("Was not able to run the query", sql_query)
                Verbose.print_ln("Error:", str(err))
                quit()
        else:
            Verbose.print_ln("Something wrong happened, not connected!")

        return result
예제 #5
0
    def main(self):
        pkl_df = None
        db_updated = False
        first_run = False

        db_df = self.db.run('SELECT * FROM [SurveyStructure]')

        try:
            pkl_df = pd.read_pickle('result.pkl')
            db_updated = pkl_df.shape != db_df.shape or not pkl_df.equals(db_df)
        except FileNotFoundError:
            first_run = pkl_df is None
        except Exception as err:
            Verbose.print_ln("Something went bad \nError: ", err)

        if first_run or db_updated:
            try:
                db_df.to_pickle('result.pkl')
            except Exception as err:
                Verbose.print_ln("Was not able to create new pkl file, Error:", err)
            self.create_or_update_view()
            Verbose.print_ln("update the view and the pkl")
        else:
            Verbose.print_ln('No changes, database table status remained same.')
예제 #6
0
 def create_or_update_view(self):
     procedure = Procedure(self.db)
     final_query = ' CREATE OR ALTER VIEW vw_AllSurveyData AS ' + procedure.get_all_survey_data()
     self.db.create(final_query)
     Verbose.print_ln("View updated.")
예제 #7
0
 def __del__(self):
     Verbose.print_ln('Script executed.')
예제 #8
0
        except Exception as err:
            Verbose.print_ln("Something went bad \nError: ", err)

        if first_run or db_updated:
            try:
                db_df.to_pickle('result.pkl')
            except Exception as err:
                Verbose.print_ln("Was not able to create new pkl file, Error:", err)
            self.create_or_update_view()
            Verbose.print_ln("update the view and the pkl")
        else:
            Verbose.print_ln('No changes, database table status remained same.')

    """ Documentation: create_or_update_view
       Description:
           a method used to create new view "vw_AllSurveyDataPython" and store all survey data we got from procedure object.
    """
    def create_or_update_view(self):
        procedure = Procedure(self.db)
        final_query = ' CREATE OR ALTER VIEW vw_AllSurveyData AS ' + procedure.get_all_survey_data()
        self.db.create(final_query)
        Verbose.print_ln("View updated.")


if __name__ == '__main__':
    args = Argument.get()
    Verbose.show_messages(args.verbose)

    starter = Starter()
    starter.main()