def export_sql_statement(self): if self.validate_query_parameters(): qry = self.make_query_object() sql = qry.sql if qry.status is not None: self.error = qry.status return False else: name = qry.name path = FSO.write_text(sql, name) FSO.open_file(path) return True return False
def export_query_results(self, parent, name, tbl, sql, pw): """ Writes a sql query to a csv. """ start_datetime = timestr() start_time = just_time() logger = logging.getLogger( 'main.sql_exporter.ExportSql.export_query_results') logger.debug('Pulling query ' + name) fso = FSO() fso.make_dir('output') csv_path = 'output\\' + name + '_' + start_datetime + '.csv' con = Connection(table=tbl, password=pw) def result_iter(cursor, chunksize=1000): while True: results = cursor.fetchmany(chunksize) if not results: break for result in results: yield result def call_grandfather(status, done=False): if done == True: finish = just_time() else: finish = '...' parent.parent.callback( name , start_time , finish , status ) call_grandfather(status='Connecting') if con.open(): cursor = con.connection.cursor() call_grandfather(status='Executing query') try: cursor.execute(sql) with open(csv_path, 'w', newline='') as csv_file: call_grandfather(status='Writing csv') writer = csv.writer(csv_file, quoting=csv.QUOTE_ALL) writer.writerow([i[0] for i in cursor.description]) # header for r, row in enumerate(result_iter(cursor, 1000)): if r > 100000: break if r % 1000 == 0: logger.info('Writing row ' + str(r)) writer.writerow(list(row) + ['', '', '', '']) call_grandfather(status='Great Success!', done=True) fso.open_file(csv_path) except Exception as e: err = str(e) logger.error(err) call_grandfather(status=str(e), done=True) finally: con.close() parent.call_dad()
def open_output_folder(self, path='output'): self.logger.info('Opening ' + path + ' folder...') FSO.make_dir(path) FSO.open_file(path)