check empty cells

This commit is contained in:
TonyO 2018-04-22 00:39:11 -05:00
parent 000be9a783
commit c7c0d8884a
11 changed files with 461 additions and 142 deletions

View File

@ -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<String> 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<SqlDataError> 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);
}
}
}

View File

@ -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 {
@ -113,6 +120,16 @@ public class SqlCreateBuilder {
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,10 +141,16 @@ 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) {
if(addIfExist) {
sql.append("DROP TABLE IF EXISTS " + table + ";\n");
}else {
sql.append("DROP TABLE " + table + ";\n");
}
}
sql.append("CREATE TABLE ").append(table);
sql.append(" (").append("\n");
sql.append(createSB.toString());

View File

@ -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 + "]";
}
}

View File

@ -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;
}
}

View File

@ -49,10 +49,13 @@ 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<String> columnNames = new ArrayList<String>();
private List<ArrayList<SqlData>> sqlDataList = new ArrayList<ArrayList<SqlData>>();
@ -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<SqlData> 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<SqlDataError> validateSqlData(Project project, Properties params){
logger.info("Param Name:{}, Param Value:{}", project, params);
return null;
}
}

View File

@ -76,51 +76,73 @@ public class SqlInsertBuilder {
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<String, JSONObject> colOptionsMap = new HashMap<String, JSONObject>();
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<SqlData> sqlCellData : sqlDataList) {
for(ArrayList<SqlData> 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");
if(type == null) {
type = "VARCHAR";
}
if(type.equals("VARCHAR") || type.equals("CHAR") || type.equals("TEXT")) {
String defaultValue = (String)jsonOb.get("defaultValue");
boolean allowNullChkBox = (boolean)jsonOb.get("allowNull");
String value = "'" + val.text + "'";
rowValue.append(value);
if(type == null) {
type = SqlData.SQL_TYPE_VARCHAR;
}
//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(val.text);
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());
}
}
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);

View File

@ -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() {

View File

@ -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"
},

View File

@ -25,13 +25,11 @@
<th>Field Name</th>
<th>SQL Type</th>
<th>Size</th>
<th></th>
<th> Allow Null <span style="text-align:center;"><input type="checkbox" bind="allowNullToggleCheckbox" id="allowNullToggleCheckbox" checked /></span></th>
<th> Default</th>
</tr>
<!--
<tr>
<th><input type="checkbox" bind="allRowsToggleCheckbox" id="allRowsToggleCheckboxId" /></th>
</tr>
-->
</thead>
</table>
@ -75,12 +73,17 @@
<td width="90%"><input type="text" bind="tableNameTextBox" id="tableNameTextBoxId" size="50%"/></td>
</tr>
<tr>
</tr>
</table>
</div>
<div class="grid-layout grid-layout-for-text layout-tightest"><table>
<tr>
<td><span style="margin:2px; padding:2px;"></span></td>
</tr>
<tr>
<td width="1%"><input type="checkbox" bind="includeStructureCheckbox" id="includeStructureCheckboxId" checked/></td>
<td><label for="includeStructureCheckboxId" bind="includeStructureLabel"></label></td>
@ -88,12 +91,24 @@
<tr>
<td width="1%"><input type="checkbox" bind="includeDropStatementCheckbox" id="includeDropStatementCheckboxId"/></td>
<td><label for="includeDropStatementCheckboxId" bind="includeDropStatementLabel"></label></td>
</tr>
<tr>
<td width="1%"><input type="checkbox" bind="includeIfExistDropStatementCheckbox" id="includeIfExistDropStatementCheckboxId"/></td>
<td><label for="includeIfExistDropStatementCheckboxId" bind="includeIfExistDropStatementLabel"></label></td>
</tr>
<tr>
<td><span style="margin:2px; padding:2px;"></span></td>
</tr>
<tr>
<td width="1%"><input type="checkbox" bind="includeContentCheckbox" id="includeContentCheckboxId" checked/></td>
<td><label for="includeContentCheckboxId" bind="includeContentLabel"></label></td>
</tr>
<tr>
<td width="1%"><input type="checkbox" bind="nullCellValueToEmptyStringCheckbox" id="nullCellValueToEmptyStringCheckboxId" checked/></td>
<td><label for="nullCellValueToEmptyStringCheckbox" bind="nullCellValueToEmptyStringLabel"></label></td>
</tr>
<tr>
<td colspan="2"><div class="grid-layout grid-layout-for-text layout-tightest layout-full"><table><tr>

View File

@ -48,8 +48,6 @@ 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"]);
@ -57,35 +55,28 @@ function SqlExporterDialog(options) {
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"]);
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;
@ -94,15 +85,9 @@ function SqlExporterDialog(options) {
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 = $('<select>').appendTo('body');
sel.append($("<option>").attr('value','VARCHAR').text('VARCHAR'));
@ -113,10 +98,6 @@ function SqlExporterDialog(options) {
sel.append($("<option>").attr('value','DATE').text('DATE'));
sel.append($("<option>").attr('value','TIMESTAMP').text('TIMESTAMP'));
// $(arr).each(function() {
// sel.append($("<option>").attr('value',this.val).text(this.text));
// });
sel.attr('id', selectBoxName);
sel.attr('rowIndex', i);
sel.addClass('typeSelectClass');
@ -131,7 +112,7 @@ function SqlExporterDialog(options) {
$('#sizeInputRow'+ rowIndex).prop("disabled", true);
}
});
//create and add Row
var row = $('<tr>')
.addClass("sql-exporter-dialog-row")
.attr('id', rowId)
@ -139,6 +120,7 @@ function SqlExporterDialog(options) {
.attr("rowIndex", i)
.appendTo(this._elmts.columnListTable);
//create and add column
var columnCell = $('<td>')
.attr('width', '150px')
.appendTo(row);
@ -176,6 +158,31 @@ function SqlExporterDialog(options) {
.attr("rowIndex", i)
.appendTo(applyAllCell);
var nullableCell = $('<td>')
.attr('width', '90px')
.addClass("allowNullCellStyle")
.appendTo(row);
$('<input>')
.attr('type', 'checkbox')
.attr('checked', 'checked')
.attr('id', allowNullChkBoxName)
.attr("rowIndex", i)
.addClass("allowNullCheckboxStyle")
.appendTo(nullableCell);
var defValueCell = $('<td>')
.attr('width', '30px')
.appendTo(row);
$('<input>')
.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');
var typeValue = $("#selectBoxRow" + rowIndex).val();
@ -191,16 +198,27 @@ 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] = {
@ -211,6 +229,26 @@ 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,6 +299,11 @@ 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);
});
};
@ -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,6 +335,20 @@ function SqlExporterDialog(options) {
return false;
}
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;
//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;
@ -289,7 +359,7 @@ function SqlExporterDialog(options) {
}
// var ext = SqlExporterDialog.formats[format].extension;
var form = this._prepareSqlExportRowsForm(format, !exportAllRowsCheckbox, "sql");
var form = self._prepareSqlExportRowsForm(format, !exportAllRowsCheckbox, "sql");
$('<input />')
.attr("name", "options")
.attr("value", JSON.stringify(options))
@ -315,6 +385,21 @@ function SqlExporterDialog(options) {
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;
});
return true;
};
SqlExporterDialog.prototype._prepareSqlExportRowsForm = function(format, includeEngine, ext) {
@ -386,9 +471,12 @@ 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
};

View File

@ -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;
}