diff --git a/main/src/com/google/refine/commands/exporting/SqlExporterCommand.java b/main/src/com/google/refine/commands/exporting/SqlExporterCommand.java new file mode 100644 index 000000000..4503f5dc1 --- /dev/null +++ b/main/src/com/google/refine/commands/exporting/SqlExporterCommand.java @@ -0,0 +1,88 @@ +package com.google.refine.commands.exporting; + +import java.io.IOException; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.JSONWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.refine.ProjectManager; +import com.google.refine.commands.Command; +import com.google.refine.exporters.sql.SqlDataError; +import com.google.refine.exporters.sql.SqlExporter; +import com.google.refine.model.Project; + + +public class SqlExporterCommand extends Command { + + private static final Logger logger = LoggerFactory.getLogger("SqlExporterCommand"); + + @SuppressWarnings("unchecked") + public Properties getRequestParameters(HttpServletRequest request) { + Properties options = new Properties(); + Enumeration en = request.getParameterNames(); + while (en.hasMoreElements()) { + String name = en.nextElement(); + String value = request.getParameter(name); + options.put(name, value); + + } + return options; + } + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + ProjectManager.singleton.setBusy(true); + try { + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + JSONWriter writer = new JSONWriter(response.getWriter()); + writer.object(); + Project project = getProject(request); + //Engine engine = getEngine(request, project); + Properties params = getRequestParameters(request); + SqlExporter sqlExporter = new SqlExporter(); + List result = sqlExporter.validateSqlData(project, params); + if(result == null || result.isEmpty()) { + writer.key("code"); + writer.value("OK"); + }else { + writer.key("code"); + writer.value("Error"); + + writer.key("SqlDataErrors"); + writer.array(); + result.stream().forEach(err -> { + writer.object(); + + writer.key("errorcode"); + writer.value(err.getErrorCode()); + writer.key("errormessage"); + writer.value(err.getErrorMessage()); + + writer.endObject(); + + }); + writer.endArray(); + + } + + writer.endObject(); + + } catch (Exception e) { + throw new ServletException(e); + } finally { + ProjectManager.singleton.setBusy(false); + } + } + +} diff --git a/main/src/com/google/refine/exporters/sql/SqlCreateBuilder.java b/main/src/com/google/refine/exporters/sql/SqlCreateBuilder.java index 416b26e30..e6450df55 100755 --- a/main/src/com/google/refine/exporters/sql/SqlCreateBuilder.java +++ b/main/src/com/google/refine/exporters/sql/SqlCreateBuilder.java @@ -62,8 +62,9 @@ public class SqlCreateBuilder { StringBuffer createSB = new StringBuffer(); JSONArray columnOptionArray = options == null ? null : JSONUtilities.getArray(options, "columns"); + boolean trimColNames = options == null ? false : JSONUtilities.getBoolean(options, "trimColumnNames", false); + - final boolean trimColNames = options == null ? false : JSONUtilities.getBoolean(options, "trimColumnNames", false); int count = columnOptionArray.length(); @@ -71,39 +72,45 @@ public class SqlCreateBuilder { JSONObject columnOptions = JSONUtilities.getObjectElement(columnOptionArray, i); if (columnOptions != null) { String name = JSONUtilities.getString(columnOptions, "name", null); - String type = JSONUtilities.getString(columnOptions, "type", "VARCHAR"); + String type = JSONUtilities.getString(columnOptions, "type", SqlData.SQL_TYPE_VARCHAR); String size = JSONUtilities.getString(columnOptions, "size", ""); - //logger.info("Before Trim Column Names::" + name); + boolean allowNull = JSONUtilities.getBoolean(columnOptions, "allowNull", true); + String defaultValue = JSONUtilities.getString(columnOptions, "defaultValue", null); + + String allowNullStr = "NULL"; + if(!allowNull) { + allowNullStr = "NOT NULL"; + } + if (name != null) { if(trimColNames) { String trimmedCol = name.replaceAll("\\s", ""); createSB.append( trimmedCol + " "); - //logger.info("After Trim Column Names::" + name); }else{ createSB.append(name + " "); } - if (type.equals("VARCHAR")) { + if (type.equals(SqlData.SQL_TYPE_VARCHAR)) { if (size.isEmpty()) { size = "255"; } createSB.append(type + "(" + size + ")"); - } else if (type.equals("CHAR")) { + } else if (type.equals(SqlData.SQL_TYPE_CHAR)) { if (size.isEmpty()) { size = "10"; } createSB.append(type + "(" + size + ")"); - } else if (type.equals("INT") || type.equals("INTEGER")) { + } else if (type.equals(SqlData.SQL_TYPE_INT) || type.equals(SqlData.SQL_TYPE_INTEGER)) { if (size.isEmpty()) { createSB.append(type); } else { createSB.append(type + "(" + size + ")"); } - } else if (type.equals("NUMERIC")) { + } else if (type.equals(SqlData.SQL_TYPE_NUMERIC)) { if (size.isEmpty()) { createSB.append(type); } else { @@ -112,7 +119,17 @@ public class SqlCreateBuilder { } else { createSB.append(type); } - + + createSB.append(" " + allowNullStr); + if(defaultValue != null && !defaultValue.isEmpty()) { + if(type.equals(SqlData.SQL_TYPE_VARCHAR) || type.equals(SqlData.SQL_TYPE_CHAR) || type.equals(SqlData.SQL_TYPE_TEXT)) { + createSB.append(" DEFAULT " + "'" + defaultValue + "'"); + }else { + createSB.append(" DEFAULT " + defaultValue); + } + + } + if (i < count - 1) { createSB.append(","); } @@ -124,8 +141,14 @@ public class SqlCreateBuilder { StringBuffer sql = new StringBuffer(); boolean includeDrop = JSONUtilities.getBoolean(options, "includeDropStatement", false); + boolean addIfExist = options == null ? false : JSONUtilities.getBoolean(options, "includeIfExistWithDropStatement", true); if (includeDrop) { - sql.append("DROP TABLE " + table + ";\n"); + if(addIfExist) { + sql.append("DROP TABLE IF EXISTS " + table + ";\n"); + }else { + sql.append("DROP TABLE " + table + ";\n"); + } + } sql.append("CREATE TABLE ").append(table); diff --git a/main/src/com/google/refine/exporters/sql/SqlData.java b/main/src/com/google/refine/exporters/sql/SqlData.java index 6e595b733..383c17e8d 100755 --- a/main/src/com/google/refine/exporters/sql/SqlData.java +++ b/main/src/com/google/refine/exporters/sql/SqlData.java @@ -32,9 +32,17 @@ package com.google.refine.exporters.sql; public class SqlData { - final public String columnName; - final public Object value; - final public String text; + private String columnName; + private Object value; + private String text; + public static final String SQL_TYPE_VARCHAR = "VARCHAR"; + public static final String SQL_TYPE_CHAR = "CHAR"; + public static final String SQL_TYPE_TEXT = "TEXT"; + public static final String SQL_TYPE_INTEGER = "INTEGER"; + public static final String SQL_TYPE_INT = "INT"; + public static final String SQL_TYPE_NUMERIC = "NUMERIC"; + + public SqlData(String columnName, Object value, String text) { this.columnName = columnName; @@ -58,4 +66,10 @@ public class SqlData { return text; } + + @Override + public String toString() { + return "SqlData [columnName=" + columnName + ", value=" + value + ", text=" + text + "]"; + } + } diff --git a/main/src/com/google/refine/exporters/sql/SqlDataError.java b/main/src/com/google/refine/exporters/sql/SqlDataError.java new file mode 100644 index 000000000..521443d87 --- /dev/null +++ b/main/src/com/google/refine/exporters/sql/SqlDataError.java @@ -0,0 +1,34 @@ +package com.google.refine.exporters.sql; + + +public class SqlDataError { + private int errorCode; + private String errorMessage; + + public SqlDataError(int errorCode, String errorMessage) { + super(); + this.errorCode = errorCode; + this.errorMessage = errorMessage; + } + + + public int getErrorCode() { + return errorCode; + } + + + public void setErrorCode(int errorCode) { + this.errorCode = errorCode; + } + + + public String getErrorMessage() { + return errorMessage; + } + + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} diff --git a/main/src/com/google/refine/exporters/sql/SqlExporter.java b/main/src/com/google/refine/exporters/sql/SqlExporter.java index d0629763d..f538c45d1 100755 --- a/main/src/com/google/refine/exporters/sql/SqlExporter.java +++ b/main/src/com/google/refine/exporters/sql/SqlExporter.java @@ -49,11 +49,14 @@ import com.google.refine.util.JSONUtilities; public class SqlExporter implements WriterExporter { + private static final Logger logger = LoggerFactory.getLogger("SqlExporter"); public static final String NO_COL_SELECTED_ERROR = "****NO COLUMNS SELECTED****"; public static final String NO_OPTIONS_PRESENT_ERROR = "****NO OPTIONS PRESENT****"; - - private final static Logger logger = LoggerFactory.getLogger("SqlExporter"); - + //JSON Property names + public static final String JSON_INCLUDE_STRUCTURE = "includeStructure"; + public static final String JSON_INCLUDE_CONTENT = "includeContent"; + public static final String JSON_TABLE_NAME = "tableName"; + private List columnNames = new ArrayList(); private List> sqlDataList = new ArrayList>(); private JSONObject sqlOptions; @@ -92,7 +95,7 @@ public class SqlExporter implements WriterExporter { } String tableName = ProjectManager.singleton.getProjectMetadata(project.id).getName(); - Object tableNameManual = sqlOptions.get("tableName"); + Object tableNameManual = sqlOptions.get(JSON_TABLE_NAME); if (tableNameManual != null && !tableNameManual.toString().isEmpty()) { tableName = tableNameManual.toString(); @@ -103,15 +106,14 @@ public class SqlExporter implements WriterExporter { sqlOptions); final boolean includeStructure = sqlOptions == null ? true - : JSONUtilities.getBoolean(sqlOptions, "includeStructure", true); + : JSONUtilities.getBoolean(sqlOptions, JSON_INCLUDE_STRUCTURE, true); final boolean includeContent = sqlOptions == null ? true - : JSONUtilities.getBoolean(sqlOptions, "includeContent", true); + : JSONUtilities.getBoolean(sqlOptions, JSON_INCLUDE_CONTENT, true); if (includeStructure) { String sqlCreateStr = createBuilder.getCreateSQL(); writer.write(sqlCreateStr); - } if (includeContent) { @@ -143,9 +145,12 @@ public class SqlExporter implements WriterExporter { ArrayList values = new ArrayList<>(); for (CellData cellData : cells) { - if (cellData != null && cellData.text != null) { - SqlData newSql = new SqlData(cellData.columnName, cellData.value, cellData.text); - values.add(newSql); + if (cellData != null) { + if(cellData.text == null || cellData.text.isEmpty()) { + values.add(new SqlData(cellData.columnName, "", "")); + }else { + values.add(new SqlData(cellData.columnName, cellData.value, cellData.text)); + } } @@ -158,4 +163,11 @@ public class SqlExporter implements WriterExporter { CustomizableTabularExporterUtilities.exportRows(project, engine, params, serializer); } + + public List validateSqlData(Project project, Properties params){ + + logger.info("Param Name:{}, Param Value:{}", project, params); + return null; + + } } diff --git a/main/src/com/google/refine/exporters/sql/SqlInsertBuilder.java b/main/src/com/google/refine/exporters/sql/SqlInsertBuilder.java index 9c60334d9..efd7686b5 100755 --- a/main/src/com/google/refine/exporters/sql/SqlInsertBuilder.java +++ b/main/src/com/google/refine/exporters/sql/SqlInsertBuilder.java @@ -72,55 +72,77 @@ public class SqlInsertBuilder { * Get Insert Sql * @return */ - public String getInsertSQL() { + public String getInsertSQL(){ if(logger.isDebugEnabled()) { logger.debug("Insert SQL with columns: {}", columns); } - JSONArray columnOptionArray = options == null ? null : - JSONUtilities.getArray(options, "columns"); - //logger.info("columnOptionArray::::{}", columnOptionArray); + JSONArray colOptionArray = options == null ? null : JSONUtilities.getArray(options, "columns"); Map colOptionsMap = new HashMap(); - if(columnOptionArray != null) { - columnOptionArray.forEach(c -> { + if(colOptionArray != null) { + colOptionArray.forEach(c -> { JSONObject json = (JSONObject)c; colOptionsMap.put("" + json.get("name"), json); }); } - final boolean trimColNames = options == null ? false : JSONUtilities.getBoolean(options, "trimColumnNames", false); - String colNamesWithSep = null; - if(trimColNames) { - colNamesWithSep = columns.stream().map(col -> col.replaceAll("\\s", "")).collect(Collectors.joining(",")); - }else { - colNamesWithSep = columns.stream().collect(Collectors.joining(",")); - } - + boolean nullValueToEmptyStr = options == null ? false : JSONUtilities.getBoolean(options, "convertNulltoEmptyString", true); + StringBuffer values = new StringBuffer(); int idx = 0; - for(ArrayList sqlCellData : sqlDataList) { + for(ArrayList sqlRow : sqlDataList) { StringBuilder rowValue = new StringBuilder(); - //logger.info(" row.size:{}", row.size()); - for(SqlData val : sqlCellData) { - + + for(SqlData val : sqlRow) { + JSONObject jsonOb = colOptionsMap.get(val.getColumnName()); String type = (String)jsonOb.get("type"); + String defaultValue = (String)jsonOb.get("defaultValue"); + boolean allowNullChkBox = (boolean)jsonOb.get("allowNull"); + + if(type == null) { - type = "VARCHAR"; + type = SqlData.SQL_TYPE_VARCHAR; } - if(type.equals("VARCHAR") || type.equals("CHAR") || type.equals("TEXT")) { + //Character Types + if(type.equals(SqlData.SQL_TYPE_VARCHAR) || type.equals(SqlData.SQL_TYPE_CHAR) || type.equals(SqlData.SQL_TYPE_TEXT)) { + + if(!allowNullChkBox) { + throw new RuntimeException("bad input"); + } + if((val.getText() == null || val.getText().isEmpty()) && nullValueToEmptyStr ) { + // logger.info("Appending empty String:::{}" , val.getText()); + if(defaultValue != null && !defaultValue.isEmpty()) { + rowValue.append("'" + defaultValue + "'"); + }else { + rowValue.append("null"); + } + + }else { + rowValue.append("'" + val.getText() + "'"); + } + + }else {//Numeric Types + + if((val.getText() == null || val.getText().isEmpty()) && nullValueToEmptyStr ) { + // logger.info("Appending empty String others:::{}" , val.getText()); + if(defaultValue != null && !defaultValue.isEmpty()) { + rowValue.append("'" + defaultValue + "'"); + }else { + rowValue.append("null"); + } + }else { + rowValue.append(val.getText()); + } - String value = "'" + val.text + "'"; - rowValue.append(value); - }else { - rowValue.append(val.text); } rowValue.append(","); - //logger.info("jsonObject:{}", jsonOb); - + } + + idx++; String rowValString = rowValue.toString(); rowValString = rowValString.substring(0, rowValString.length() - 1); @@ -135,6 +157,11 @@ public class SqlInsertBuilder { } + boolean trimColNames = options == null ? false : JSONUtilities.getBoolean(options, "trimColumnNames", false); + String colNamesWithSep = columns.stream().map(col -> col.replaceAll("\\s", "")).collect(Collectors.joining(","));; + if(!trimColNames) { + colNamesWithSep = columns.stream().collect(Collectors.joining(",")); + } String valuesString = values.toString(); valuesString = valuesString.substring(0, valuesString.length() - 1); diff --git a/main/webapp/modules/core/MOD-INF/controller.js b/main/webapp/modules/core/MOD-INF/controller.js index a11a5f238..bffff591f 100644 --- a/main/webapp/modules/core/MOD-INF/controller.js +++ b/main/webapp/modules/core/MOD-INF/controller.js @@ -151,6 +151,8 @@ function registerCommands() { RS.registerCommand(module, "authorize", new Packages.com.google.refine.commands.auth.AuthorizeCommand()); RS.registerCommand(module, "deauthorize", new Packages.com.google.refine.commands.auth.DeAuthorizeCommand()); + + RS.registerCommand(module, "preview-sql-export", new Packages.com.google.refine.commands.exporting.SqlExporterCommand()); } function registerOperations() { diff --git a/main/webapp/modules/core/langs/translation-en.json b/main/webapp/modules/core/langs/translation-en.json index b727fe5fa..e95e48275 100644 --- a/main/webapp/modules/core/langs/translation-en.json +++ b/main/webapp/modules/core/langs/translation-en.json @@ -281,7 +281,9 @@ "tableNameLabel": "Table Name:", "sqlExporterTrimColumns": "Trim Column Names", "sqlExporterIgnoreFacets": "Ignore facets and filters and export all rows", - "sqlExporterOutputEmptyRows":"Output empty row (i.e. all cells null)" + "sqlExporterOutputEmptyRows":"Output empty row (i.e. all cells null)", + "for-include-if-exist-drop-stmt-checkbox": "Include 'If Exists' in Drop Statement", + "for-null-cell-value-to-empty-str-label": "Convert Null Value to null in Insert" }, diff --git a/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.html b/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.html index b76756c3b..37c20803f 100755 --- a/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.html +++ b/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.html @@ -25,13 +25,11 @@ Field Name SQL Type Size + + Allow Null + Default - + @@ -75,12 +73,17 @@ + +
+ + + @@ -88,12 +91,24 @@ + + + + + + + + + + + +
diff --git a/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.js b/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.js index ec6ffdea8..4e266004c 100755 --- a/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.js +++ b/main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.js @@ -48,16 +48,12 @@ function SqlExporterDialog(options) { this._dialog = $(DOM.loadHTML("core", "scripts/dialogs/sql-exporter-dialog.html")); this._elmts = DOM.bind(this._dialog); this._level = DialogSystem.showDialog(this._dialog); - - this._elmts.dialogHeader.html($.i18n._('core-dialogs')["custom-tab-exp"]); this._elmts.or_dialog_content.html($.i18n._('core-dialogs')["content"]); this._elmts.or_dialog_download.html($.i18n._('core-dialogs')["download"]); this._elmts.selectAllButton.html($.i18n._('core-buttons')["select-all"]); this._elmts.deselectAllButton.html($.i18n._('core-buttons')["deselect-all"]); - - this._elmts.downloadPreviewButton.html($.i18n._('core-buttons')["preview"]); this._elmts.downloadButton.html($.i18n._('core-buttons')["download"]); @@ -65,44 +61,33 @@ function SqlExporterDialog(options) { this._elmts.cancelButton.html($.i18n._('core-buttons')["cancel"]); // this._elmts.nextButton.html($.i18n._('core-buttons')["next"]); - this._elmts.tableNameLabel.html($.i18n._('core-dialogs')["tableNameLabel"]); this._elmts.includeStructureLabel.html($.i18n._('core-dialogs')["for-include-structure-checkbox"]); this._elmts.includeDropStatementLabel.html($.i18n._('core-dialogs')["for-include-drop-statement-checkbox"]); this._elmts.includeContentLabel.html($.i18n._('core-dialogs')["for-include-content-checkbox"]); + this._elmts.includeIfExistDropStatementLabel.html($.i18n._('core-dialogs')["for-include-if-exist-drop-stmt-checkbox"]); - + this._elmts.nullCellValueToEmptyStringLabel.html($.i18n._('core-dialogs')["for-null-cell-value-to-empty-str-label"]); + this._elmts.sqlExportIgnoreFacetsLabel.html($.i18n._('core-dialogs')["sqlExporterIgnoreFacets"]); this._elmts.sqlExportTrimAllColumnsLabel.html($.i18n._('core-dialogs')["sqlExporterTrimColumns"]); this._elmts.sqlExportOutputEmptyRowsLabel.html($.i18n._('core-dialogs')["sqlExporterOutputEmptyRows"]); - - $("#sql-exporter-tabs-content").css("display", ""); $("#sql-exporter-tabs-download").css("display", ""); $("#sql-exporter-tabs").tabs(); - - - /* - * Populate column list. - */ + for (var i = 0; i < theProject.columnModel.columns.length; i++) { - var column = theProject.columnModel.columns[i]; - var name = column.name; - var rowId = "sql-exporter-dialog-row" + i; - var selectBoxName = 'selectBoxRow' + i; - var sizeInputName = 'sizeInputRow' + i; - var applyAllBtnName = 'applyAllBtn' + i; + var column = theProject.columnModel.columns[i]; + var name = column.name; + var rowId = "sql-exporter-dialog-row" + i; + var selectBoxName = 'selectBoxRow' + i; + var sizeInputName = 'sizeInputRow' + i; + var applyAllBtnName = 'applyAllBtn' + i; -// var arr = [ -// {val : 'VARCHAR', text: 'VARCHAR'}, -// {val : 'TEXT', text: 'TEXT'}, -// {val : 'INT', text: 'INT'}, -// {val : 'NUMERIC', text: 'NUMERIC'}, -// {val : 'CHAR', text: 'CHAR'}, -// {val : 'DATE', text: 'DATE'}, -// {val : 'TIMESTAMP', text: 'TIMESTAMP'} -// ]; + var allowNullChkBoxName = 'allowNullChkBox' + i; + var defaultValueTextBoxName = 'defaultValueTextBox' + i; + var sel = $('') + //create and add Row + var row = $('') .addClass("sql-exporter-dialog-row") .attr('id', rowId) .attr("column", name) .attr("rowIndex", i) .appendTo(this._elmts.columnListTable); + //create and add column var columnCell = $('
') .attr('width', '150px') .appendTo(row); @@ -166,15 +148,40 @@ function SqlExporterDialog(options) { .addClass("sql-exporter-dialog-input") .appendTo(sizeCell); - var applyAllCell = $('') - .attr('width', '60px') - .appendTo(row); + var applyAllCell = $('') + .attr('width', '60px') + .appendTo(row); + $('') + .attr('type', 'button') + .attr('value', 'Apply All') + .attr('id', applyAllBtnName) + .attr("rowIndex", i) + .appendTo(applyAllCell); + + var nullableCell = $('') + .attr('width', '90px') + .addClass("allowNullCellStyle") + .appendTo(row); $('') - .attr('type', 'button') - .attr('value', 'Apply All') - .attr('id', applyAllBtnName) - .attr("rowIndex", i) - .appendTo(applyAllCell); + .attr('type', 'checkbox') + .attr('checked', 'checked') + .attr('id', allowNullChkBoxName) + .attr("rowIndex", i) + .addClass("allowNullCheckboxStyle") + .appendTo(nullableCell); + + var defValueCell = $('') + .attr('width', '30px') + .appendTo(row); + $('') + .attr('type', 'text') + .attr('size', '8px') + .attr('id', defaultValueTextBoxName) + .attr("rowIndex", i) + .addClass("defaultValueTextBoxStyle") + .appendTo(defValueCell); + + $('#' + applyAllBtnName).on('click', function() { var rowIndex = this.getAttribute('rowIndex'); @@ -191,18 +198,29 @@ function SqlExporterDialog(options) { }); $('input.sql-exporter-dialog-input').each(function() { - //alert("Value:" + this.value + " RowIndex:" + rowIndex + " TypeValue:" + typeValue + "" + this.value); var rowId = this.getAttribute('rowIndex'); var id = this.getAttribute('id'); if(rowIndex !== rowId){ $("#" + id).val(sizeValue); } - - + }); }); + $('#' + allowNullChkBoxName).on('click', function() { + var rowIndex = this.getAttribute('rowIndex'); + var id = this.getAttribute('id'); + var checked = $(this).is(':checked');; + + if(checked == false){ + $('#defaultValueTextBox'+ rowIndex).prop("disabled", false); + }else{ + $('#defaultValueTextBox'+ rowIndex).val(""); + $('#defaultValueTextBox'+ rowIndex).prop("disabled", true); + } + }); + this._columnOptionMap[name] = { name: name, type: '', @@ -210,7 +228,27 @@ function SqlExporterDialog(options) { }; } - + + this._elmts.allowNullToggleCheckbox.click(function() { + var checked = $(this).is(':checked'); + if(checked == true){ + $("input:checkbox[class=allowNullCheckboxStyle]").each(function () { + $(this).attr('checked', true); + }); + $("input:text[class=defaultValueTextBoxStyle]").each(function () { + $(this).attr('disabled', true); + }); + + }else{ + $("input:checkbox[class=allowNullCheckboxStyle]").each(function () { + $(this).attr('checked', false); + }); + $("input:text[class=defaultValueTextBoxStyle]").each(function () { + $(this).attr('disabled', false); + }); + } + + }); this._elmts.selectAllButton.click(function() { $("input:checkbox[class=columnNameCheckboxStyle]").each(function () { @@ -230,8 +268,20 @@ function SqlExporterDialog(options) { //alert('checked ' + checked); if(checked == true){ $('#includeDropStatementCheckboxId').removeAttr("disabled"); + $('#includeIfExistDropStatementCheckboxId').removeAttr("disabled"); }else{ $('#includeDropStatementCheckboxId').attr("disabled", true); + $('#includeIfExistDropStatementCheckboxId').attr("disabled", true); + } + }); + + this._elmts.includeContentCheckbox.click(function() { + var checked = $(this).is(':checked'); + if(checked == true){ + $('#nullCellValueToEmptyStringCheckboxId').removeAttr("disabled"); + }else{ + $('#nullCellValueToEmptyStringCheckboxId').attr("disabled", true); + } }); @@ -249,8 +299,13 @@ function SqlExporterDialog(options) { this._elmts.tableNameTextBox.val(theProject.metadata.name); this._elmts.sqlExportOutputEmptyRowsCheckbox.attr('checked', 'checked'); this._elmts.sqlExportTrimAllColumnsCheckbox.attr('checked', 'checked'); + this._elmts.nullCellValueToEmptyStringLabel.attr('checked', 'checked'); - + $("input:text[class=defaultValueTextBoxStyle]").each(function () { + $(this).prop("disabled", true); + }); + + }; SqlExporterDialog.prototype._dismiss = function() { @@ -271,6 +326,7 @@ function SqlExporterDialog(options) { }; SqlExporterDialog.prototype._postExport = function(preview) { + var self = this; var exportAllRowsCheckbox = this._elmts.sqlExportAllRowsCheckbox[0].checked; var options = this._getOptionCode(); @@ -279,42 +335,71 @@ function SqlExporterDialog(options) { return false; } - var format = options.format; - var encoding = options.encoding; + var name = $.trim(theProject.metadata.name.replace(/\W/g, ' ')).replace(/\s+/g, '-'); + var sqlExportInput = {}; + sqlExportInput.name = name; + sqlExportInput.options = JSON.stringify(options); + sqlExportInput.project = theProject.id; - delete options.format; - delete options.encoding; - if (preview) { - options.limit = 10; - } + //validate form @ backend + $.post( + "command/core/preview-sql-export", + sqlExportInput, + function(sqlValidationResult) { + if(sqlValidationResult && sqlValidationResult.code === 'OK'){ + //generate code + + var format = options.format; + var encoding = options.encoding; + + delete options.format; + delete options.encoding; + if (preview) { + options.limit = 10; + } + + // var ext = SqlExporterDialog.formats[format].extension; + var form = self._prepareSqlExportRowsForm(format, !exportAllRowsCheckbox, "sql"); + $('') + .attr("name", "options") + .attr("value", JSON.stringify(options)) + .appendTo(form); + if (encoding) { + $('') + .attr("name", "encoding") + .attr("value", encoding) + .appendTo(form); + } + if (!preview) { + $('') + .attr("name", "contentType") + .attr("value", "application/x-unknown") // force download + .appendTo(form); + } + + // alert("form::" + form); + document.body.appendChild(form); + + window.open("about:blank", "refine-export"); + form.submit(); + + document.body.removeChild(form); + return true; + + }else{ + window.alert("Problem with sql code genration"); + return false; + } + + }, + "json" + ).fail(function( jqXhr, textStatus, errorThrown ){ + alert( textStatus + ':' + errorThrown ); + return false; + }); - // var ext = SqlExporterDialog.formats[format].extension; - var form = this._prepareSqlExportRowsForm(format, !exportAllRowsCheckbox, "sql"); - $('') - .attr("name", "options") - .attr("value", JSON.stringify(options)) - .appendTo(form); - if (encoding) { - $('') - .attr("name", "encoding") - .attr("value", encoding) - .appendTo(form); - } - if (!preview) { - $('') - .attr("name", "contentType") - .attr("value", "application/x-unknown") // force download - .appendTo(form); - } - - // alert("form::" + form); - document.body.appendChild(form); + return true; - window.open("about:blank", "refine-export"); - form.submit(); - - document.body.removeChild(form); - return true; }; SqlExporterDialog.prototype._prepareSqlExportRowsForm = function(format, includeEngine, ext) { @@ -386,8 +471,11 @@ function SqlExporterDialog(options) { options.includeStructure = this._elmts.includeStructureCheckbox[0].checked; options.includeDropStatement = this._elmts.includeDropStatementCheckbox[0].checked; options.includeContent = this._elmts.includeContentCheckbox[0].checked; - options.tableName = $.trim(this._elmts.tableNameTextBox.val().replace(/\W/g, ' ')).replace(/\s+/g, '-'); + options.tableName = $.trim(this._elmts.tableNameTextBox.val().replace(/\W/g, ' ')).replace(/\s+/g, '_'); options.trimColumnNames = this._elmts.sqlExportTrimAllColumnsCheckbox[0].checked; + + options.convertNulltoEmptyString = this._elmts.nullCellValueToEmptyStringCheckbox[0].checked; + options.includeIfExistWithDropStatement = this._elmts.includeIfExistDropStatementCheckbox[0].checked; options.columns = []; @@ -397,21 +485,28 @@ function SqlExporterDialog(options) { if ($(this).find('input[type="checkbox"]')[0].checked) { var name = this.getAttribute('column'); var rowIndex = this.getAttribute('rowIndex'); - // alert("column::"+ name + " rowIndex::" + rowIndex); var selectedValue = $('#selectBoxRow' + rowIndex).val(); - //alert("selectedValue::"+ selectedValue); + var typeSize = 0; - if(selectedValue == 'VARCHAR' || selectedValue == 'CHAR' || selectedValue == 'INT' || selectedValue == 'NUMERIC'){ + if(selectedValue === 'VARCHAR' || selectedValue === 'CHAR' || selectedValue === 'INT' || selectedValue === 'NUMERIC'){ typeSize = $('#sizeInputRow' + rowIndex).val(); // alert("typeSize::" + typeSize); } + var allowNullChkBoxName = $('#allowNullChkBox' + rowIndex).is(':checked'); + var defaultValueTextBoxName = $('#defaultValueTextBox' + rowIndex).val(); + // alert("allowNullChkBoxName::" + allowNullChkBoxName); + // alert("defaultValueTextBoxName::" + defaultValueTextBoxName); + var fullColumnOptions = self._columnOptionMap[name]; var columnOptions = { name: name, type: selectedValue, - size: typeSize + size: typeSize, + allowNull: allowNullChkBoxName, + defaultValue: defaultValueTextBoxName, + nullValueToEmptyStr: options.convertNulltoEmptyString }; diff --git a/main/webapp/modules/core/styles/dialogs/sql-exporter-dialog.less b/main/webapp/modules/core/styles/dialogs/sql-exporter-dialog.less index 4915575be..c090bf1dd 100644 --- a/main/webapp/modules/core/styles/dialogs/sql-exporter-dialog.less +++ b/main/webapp/modules/core/styles/dialogs/sql-exporter-dialog.less @@ -89,6 +89,13 @@ tr.sql-exporter-dialog-row td { margin:2px; border: solid 2px lightblue; } +.defaultValueTextBoxStyle { + margin:2px; +} +.allowNullCellStyle { + text-align: center; + vertical-align: middle; +} .typeSelectClass { margin-left:2px; } \ No newline at end of file