def _on_save_results_button_pressed(self, event): event.Skip() user_query = self._TCTRL_query.GetValue().strip().strip(';') if user_query == '': return pat = None curr_pat = gmPerson.gmCurrentPatient() if curr_pat.connected: pat = curr_pat.ID success, hint, cols, rows = gmDataMining.run_report_query( query=user_query, limit=None, pk_identity=pat) if not success: return if len(rows) == 0: return dlg = wx.FileDialog( parent=self, message=_("Save SQL report query results as CSV in..."), defaultDir=gmTools.gmPaths().user_work_dir, defaultFile='gm-query_results.csv', wildcard='%s (*.csv)|*.csv|%s (*)|*' % (_("CSV files"), _("all files")), style=wx.FD_SAVE) choice = dlg.ShowModal() csv_name = dlg.GetPath() dlg.DestroyLater() if choice != wx.ID_OK: return csv_file = io.open(csv_name, mode='wt', encoding='utf8') csv_file.write( '#-------------------------------------------------------------------------------------\n' ) csv_file.write('# GNUmed SQL report results\n') csv_file.write('#\n') csv_file.write('# Report: "%s"\n' % self._PRW_report_name.GetValue().strip()) csv_file.write('#\n') csv_file.write('# SQL:\n') for line in user_query.split('\n'): csv_file.write('# %s\n' % line) csv_file.write('#\n') csv_file.write('# ID of active patient: %s\n' % pat) csv_file.write('#\n') csv_file.write('# hits found: %s\n' % len(rows)) csv_file.write( '#-------------------------------------------------------------------------------------\n' ) csv_writer = csv.writer(csv_file) csv_writer.writerow(cols) for row in rows: csv_writer.writerow(row) csv_file.close()
def _on_save_results_button_pressed(self, event): event.Skip() user_query = self._TCTRL_query.GetValue().strip().strip(';') if user_query == '': return pat = None curr_pat = gmPerson.gmCurrentPatient() if curr_pat.connected: pat = curr_pat.ID success, hint, cols, rows = gmDataMining.run_report_query ( query = user_query, limit = None, pk_identity = pat ) if not success: return if len(rows) == 0: return dlg = wx.FileDialog ( parent = self, message = _("Save SQL report query results as CSV in..."), defaultDir = os.path.abspath(os.path.expanduser(os.path.join('~', 'gnumed'))), defaultFile = 'gm-query_results.csv', wildcard = '%s (*.csv)|*.csv|%s (*)|*' % (_("CSV files"), _("all files")), style = wx.FD_SAVE ) choice = dlg.ShowModal() csv_name = dlg.GetPath() dlg.DestroyLater() if choice != wx.ID_OK: return csv_file = io.open(csv_name, mode = 'wt', encoding = 'utf8') csv_file.write('#-------------------------------------------------------------------------------------\n') csv_file.write('# GNUmed SQL report results\n') csv_file.write('#\n') csv_file.write('# Report: "%s"\n' % self._PRW_report_name.GetValue().strip()) csv_file.write('#\n') csv_file.write('# SQL:\n') for line in user_query.split('\n'): csv_file.write('# %s\n' % line) csv_file.write('#\n') csv_file.write('# ID of active patient: %s\n' % pat) csv_file.write('#\n') csv_file.write('# hits found: %s\n' % len(rows)) csv_file.write('#-------------------------------------------------------------------------------------\n') csv_writer = csv.writer(csv_file) csv_writer.writerow(cols) for row in rows: csv_writer.writerow(row) csv_file.close()
def _on_run_button_pressed(self, evt): self._BTN_visualize.Enable(False) self._BTN_waiting_list.Enable(False) self._BTN_save_results.Enable(False) user_query = self._TCTRL_query.GetValue().strip().strip(';') if user_query == '': return True limit = 1001 pat = None curr_pat = gmPerson.gmCurrentPatient() if curr_pat.connected: pat = curr_pat.ID success, hint, cols, rows = gmDataMining.run_report_query ( query = user_query, limit = limit, pk_identity = pat ) self._LCTRL_result.set_columns() if len(rows) == 0: self._LCTRL_result.set_columns([_('Results')]) self._LCTRL_result.set_string_items([[_('Report returned no data.')]]) self._LCTRL_result.set_column_widths() gmDispatcher.send('statustext', msg = _('No data returned for this report.'), beep = True) return True gmDispatcher.send(signal = 'statustext', msg = _('Found %s results.') % len(rows)) if len(rows) == 1001: gmGuiHelpers.gm_show_info ( aMessage = _( 'This query returned at least %s results.\n' '\n' 'GNUmed will only show the first %s rows.\n' '\n' 'You may want to narrow down the WHERE conditions\n' 'or use LIMIT and OFFSET to batchwise go through\n' 'all the matching rows.' ) % (limit, limit-1), aTitle = _('Report Generator') ) rows = rows[:-1] # make it true :-) self._LCTRL_result.set_columns(cols) for row in rows: try: label = str(gmTools.coalesce(row[0], '')).replace('\n', '<LF>').replace('\r', '<CR>') except UnicodeDecodeError: label = _('not str()able') if len(label) > 150: label = label[:150] + gmTools.u_ellipsis row_num = self._LCTRL_result.InsertItem(sys.maxsize, label = label) for col_idx in range(1, len(row)): try: label = str(gmTools.coalesce(row[col_idx], '')).replace('\n', '<LF>').replace('\r', '<CR>')[:250] except UnicodeDecodeError: label = _('not str()able') if len(label) > 150: label = label[:150] + gmTools.u_ellipsis self._LCTRL_result.SetItem ( index = row_num, column = col_idx, label = label ) # must be called explicitely, because string items are set above without calling set_string_items self._LCTRL_result._invalidate_sorting_metadata() self._LCTRL_result.set_column_widths() self._LCTRL_result.set_data(data = rows) self.query_results = rows self._BTN_visualize.Enable(True) self._BTN_waiting_list.Enable(True) self._BTN_save_results.Enable(True) return success
def _on_run_button_pressed(self, evt): self._BTN_visualize.Enable(False) self._BTN_waiting_list.Enable(False) self._BTN_save_results.Enable(False) user_query = self._TCTRL_query.GetValue().strip().strip(';') if user_query == '': return True limit = 1001 pat = None curr_pat = gmPerson.gmCurrentPatient() if curr_pat.connected: pat = curr_pat.ID success, hint, cols, rows = gmDataMining.run_report_query( query=user_query, limit=limit, pk_identity=pat) self._LCTRL_result.set_columns() if len(rows) == 0: self._LCTRL_result.set_columns([_('Results')]) self._LCTRL_result.set_string_items( [[_('Report returned no data.')]]) self._LCTRL_result.set_column_widths() gmDispatcher.send('statustext', msg=_('No data returned for this report.'), beep=True) return True gmDispatcher.send(signal='statustext', msg=_('Found %s results.') % len(rows)) if len(rows) == 1001: gmGuiHelpers.gm_show_info( aMessage=_('This query returned at least %s results.\n' '\n' 'GNUmed will only show the first %s rows.\n' '\n' 'You may want to narrow down the WHERE conditions\n' 'or use LIMIT and OFFSET to batchwise go through\n' 'all the matching rows.') % (limit, limit - 1), aTitle=_('Report Generator')) rows = rows[:-1] # make it true :-) self._LCTRL_result.set_columns(cols) for row in rows: try: label = str(gmTools.coalesce(row[0], '')).replace( '\n', '<LF>').replace('\r', '<CR>') except UnicodeDecodeError: label = _('not str()able') if len(label) > 150: label = label[:150] + gmTools.u_ellipsis row_num = self._LCTRL_result.InsertItem(sys.maxsize, label=label) for col_idx in range(1, len(row)): try: label = str(gmTools.coalesce(row[col_idx], '')).replace( '\n', '<LF>').replace('\r', '<CR>')[:250] except UnicodeDecodeError: label = _('not str()able') if len(label) > 150: label = label[:150] + gmTools.u_ellipsis self._LCTRL_result.SetItem(index=row_num, column=col_idx, label=label) # must be called explicitely, because string items are set above without calling set_string_items self._LCTRL_result._invalidate_sorting_metadata() self._LCTRL_result.set_column_widths() self._LCTRL_result.set_data(data=rows) self.query_results = rows self._BTN_visualize.Enable(True) self._BTN_waiting_list.Enable(True) self._BTN_save_results.Enable(True) return success