Skip to content

Commit

Permalink
- power import/export additional fix for csv import, add info if
Browse files Browse the repository at this point in the history
import/export finished and if there were some problems during import
 
 review by [revid: 8007]
  • Loading branch information
Marcin Szalowicz committed Feb 12, 2015
1 parent 63663fb commit 50e2ad6
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions plugins/wb.sqlide/sqlide_power_import_export.py
Expand Up @@ -84,6 +84,7 @@ def __init__(self, module):
self.exception = None
self.module = module
self.finished_callback = None
self.run_result = True

def call_finished_callback(self):
if self.is_running:
Expand All @@ -106,7 +107,7 @@ def _run(self):
self.is_running = True

try:
self.module.start(self.stop)
self.run_result = self.module.start(self.stop)
except Exception, e:
import traceback
log_error("WorkerThread caught exception: %s" % traceback.format_exc())
Expand Down Expand Up @@ -248,9 +249,9 @@ def start_import(self):
def start(self, event):
self._thread_event = event
if self._is_import:
self.start_import()
return self.start_import()
else:
self.start_export()
return self.start_export()

class Utf8Reader:
def __init__(self, f, enc):
Expand Down Expand Up @@ -356,15 +357,19 @@ def start_export(self):
ok = rset.nextRow()
else:
self._editor.executeManagementCommand(query, 1)

return True

def start_import(self):
if not self._last_analyze:
return
return False

if self._new_table:
if not self.prepare_new_table():
return
return False

result = True

with open(self._filepath, 'rb') as csvfile:
dest_col_order = list(set([i['dest_col'] for i in self._mapping if i['active']]))
query = """PREPARE stmt FROM 'INSERT INTO %s (%s) VALUES(%s)'""" % (self._table_w_prefix, ",".join(dest_col_order), ",".join(["?" for i in dest_col_order]))
Expand All @@ -387,8 +392,9 @@ def start_import(self):
continue

for i, col in enumerate(col_order):
if col_order[col] not in row:
if col_order[col] >= len(row):
log_error("Can't find col: %s in row: %s" % (col_order[col], row))
result = False
break
val = row[col_order[col]]
if col_type[col] == 'double':
Expand All @@ -409,6 +415,8 @@ def start_import(self):
log_error("Import failed: %s" % e)
self._editor.executeManagementCommand("DEALLOCATE PREPARE stmt", 1)

return result

def analyze_file(self):
with open(self._filepath, 'rb') as csvfile:
if self.dialect is None:
Expand Down Expand Up @@ -511,14 +519,17 @@ def start_export(self):
jsonfile.flush()
jsonfile.write(']')

return True

def start_import(self):
if not self._last_analyze:
return
return False

if self._new_table:
if not self.prepare_new_table():
return
return False

result = True
with open(self._filepath, 'rb') as jsonfile:
import json
data = json.load(jsonfile)
Expand All @@ -538,6 +549,7 @@ def start_import(self):
for i, col in enumerate(col_order):
if col_order[col] not in row:
log_error("Can't find col: %s in row: %s" % (col_order[col], row))
result = False
break
val = row[col_order[col]]
if col_type[col] == 'double':
Expand All @@ -555,6 +567,8 @@ def start_import(self):
except Exception, e:
log_error("Import failed: %s" % e)
self._editor.executeManagementCommand("DEALLOCATE PREPARE stmt", 1)

return result

def analyze_file(self):
import json
Expand Down Expand Up @@ -962,7 +976,10 @@ def export_finished(self, success):
mforms.Utilities.show_message("Table Data Export", "Export cancelled by user", "Ok", "","")
else:
if success:
mforms.Utilities.show_message("Table Data Export", "Export finished succesfully", "Ok", "","")
if self.export_thread.run_result:
mforms.Utilities.show_message("Table Data Export", "Export finished succesfully", "Ok", "","")
else:
mforms.Utilities.show_message("Table Data Export", "Export finished but there was some problems, please analyze log file for details.", "Ok", "","")
self.export_thread = None # we need to set it to None because can_close check this property to show confirmation dialog
self.close()
else:
Expand Down Expand Up @@ -1418,7 +1435,10 @@ def import_finished(self, success):
mforms.Utilities.show_message("Table Data Export", "Import cancelled by user", "Ok", "","")
else:
if success:
mforms.Utilities.show_message("Table Data Import", "Import finished succesfully", "Ok", "","")
if self.import_thread.run_result:
mforms.Utilities.show_message("Table Data Import", "Import finished succesfully", "Ok", "","")
else:
mforms.Utilities.show_message("Table Data Import", "Import finished, but there was some problems, please analyze log file for details.", "Ok", "","")
self.import_thread = None # we need to set it to None because can_close check this property to show confirmation dialog
self.close()
else:
Expand Down

0 comments on commit 50e2ad6

Please sign in to comment.