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(); StringBuffer createSB = new StringBuffer();
JSONArray columnOptionArray = options == null ? null : JSONUtilities.getArray(options, "columns"); 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(); int count = columnOptionArray.length();
@ -71,39 +72,45 @@ public class SqlCreateBuilder {
JSONObject columnOptions = JSONUtilities.getObjectElement(columnOptionArray, i); JSONObject columnOptions = JSONUtilities.getObjectElement(columnOptionArray, i);
if (columnOptions != null) { if (columnOptions != null) {
String name = JSONUtilities.getString(columnOptions, "name", 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", ""); 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 (name != null) {
if(trimColNames) { if(trimColNames) {
String trimmedCol = name.replaceAll("\\s", ""); String trimmedCol = name.replaceAll("\\s", "");
createSB.append( trimmedCol + " "); createSB.append( trimmedCol + " ");
//logger.info("After Trim Column Names::" + name);
}else{ }else{
createSB.append(name + " "); createSB.append(name + " ");
} }
if (type.equals("VARCHAR")) { if (type.equals(SqlData.SQL_TYPE_VARCHAR)) {
if (size.isEmpty()) { if (size.isEmpty()) {
size = "255"; size = "255";
} }
createSB.append(type + "(" + size + ")"); createSB.append(type + "(" + size + ")");
} else if (type.equals("CHAR")) { } else if (type.equals(SqlData.SQL_TYPE_CHAR)) {
if (size.isEmpty()) { if (size.isEmpty()) {
size = "10"; size = "10";
} }
createSB.append(type + "(" + size + ")"); 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()) { if (size.isEmpty()) {
createSB.append(type); createSB.append(type);
} else { } else {
createSB.append(type + "(" + size + ")"); createSB.append(type + "(" + size + ")");
} }
} else if (type.equals("NUMERIC")) { } else if (type.equals(SqlData.SQL_TYPE_NUMERIC)) {
if (size.isEmpty()) { if (size.isEmpty()) {
createSB.append(type); createSB.append(type);
} else { } else {
@ -112,7 +119,17 @@ public class SqlCreateBuilder {
} else { } else {
createSB.append(type); 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) { if (i < count - 1) {
createSB.append(","); createSB.append(",");
} }
@ -124,8 +141,14 @@ public class SqlCreateBuilder {
StringBuffer sql = new StringBuffer(); StringBuffer sql = new StringBuffer();
boolean includeDrop = JSONUtilities.getBoolean(options, "includeDropStatement", false); boolean includeDrop = JSONUtilities.getBoolean(options, "includeDropStatement", false);
boolean addIfExist = options == null ? false : JSONUtilities.getBoolean(options, "includeIfExistWithDropStatement", true);
if (includeDrop) { 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); sql.append("CREATE TABLE ").append(table);

View File

@ -32,9 +32,17 @@ package com.google.refine.exporters.sql;
public class SqlData { public class SqlData {
final public String columnName; private String columnName;
final public Object value; private Object value;
final public String text; 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) { public SqlData(String columnName, Object value, String text) {
this.columnName = columnName; this.columnName = columnName;
@ -58,4 +66,10 @@ public class SqlData {
return text; 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,11 +49,14 @@ import com.google.refine.util.JSONUtilities;
public class SqlExporter implements WriterExporter { 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_COL_SELECTED_ERROR = "****NO COLUMNS SELECTED****";
public static final String NO_OPTIONS_PRESENT_ERROR = "****NO OPTIONS PRESENT****"; public static final String NO_OPTIONS_PRESENT_ERROR = "****NO OPTIONS PRESENT****";
//JSON Property names
private final static Logger logger = LoggerFactory.getLogger("SqlExporter"); 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<String> columnNames = new ArrayList<String>();
private List<ArrayList<SqlData>> sqlDataList = new ArrayList<ArrayList<SqlData>>(); private List<ArrayList<SqlData>> sqlDataList = new ArrayList<ArrayList<SqlData>>();
private JSONObject sqlOptions; private JSONObject sqlOptions;
@ -92,7 +95,7 @@ public class SqlExporter implements WriterExporter {
} }
String tableName = ProjectManager.singleton.getProjectMetadata(project.id).getName(); 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()) { if (tableNameManual != null && !tableNameManual.toString().isEmpty()) {
tableName = tableNameManual.toString(); tableName = tableNameManual.toString();
@ -103,15 +106,14 @@ public class SqlExporter implements WriterExporter {
sqlOptions); sqlOptions);
final boolean includeStructure = sqlOptions == null ? true final boolean includeStructure = sqlOptions == null ? true
: JSONUtilities.getBoolean(sqlOptions, "includeStructure", true); : JSONUtilities.getBoolean(sqlOptions, JSON_INCLUDE_STRUCTURE, true);
final boolean includeContent = sqlOptions == null ? true final boolean includeContent = sqlOptions == null ? true
: JSONUtilities.getBoolean(sqlOptions, "includeContent", true); : JSONUtilities.getBoolean(sqlOptions, JSON_INCLUDE_CONTENT, true);
if (includeStructure) { if (includeStructure) {
String sqlCreateStr = createBuilder.getCreateSQL(); String sqlCreateStr = createBuilder.getCreateSQL();
writer.write(sqlCreateStr); writer.write(sqlCreateStr);
} }
if (includeContent) { if (includeContent) {
@ -143,9 +145,12 @@ public class SqlExporter implements WriterExporter {
ArrayList<SqlData> values = new ArrayList<>(); ArrayList<SqlData> values = new ArrayList<>();
for (CellData cellData : cells) { for (CellData cellData : cells) {
if (cellData != null && cellData.text != null) { if (cellData != null) {
SqlData newSql = new SqlData(cellData.columnName, cellData.value, cellData.text); if(cellData.text == null || cellData.text.isEmpty()) {
values.add(newSql); 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); 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

@ -72,55 +72,77 @@ public class SqlInsertBuilder {
* Get Insert Sql * Get Insert Sql
* @return * @return
*/ */
public String getInsertSQL() { public String getInsertSQL(){
if(logger.isDebugEnabled()) { if(logger.isDebugEnabled()) {
logger.debug("Insert SQL with columns: {}", columns); 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>(); Map<String, JSONObject> colOptionsMap = new HashMap<String, JSONObject>();
if(columnOptionArray != null) { if(colOptionArray != null) {
columnOptionArray.forEach(c -> { colOptionArray.forEach(c -> {
JSONObject json = (JSONObject)c; JSONObject json = (JSONObject)c;
colOptionsMap.put("" + json.get("name"), json); 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(); StringBuffer values = new StringBuffer();
int idx = 0; int idx = 0;
for(ArrayList<SqlData> sqlCellData : sqlDataList) { for(ArrayList<SqlData> sqlRow : sqlDataList) {
StringBuilder rowValue = new StringBuilder(); StringBuilder rowValue = new StringBuilder();
//logger.info(" row.size:{}", row.size());
for(SqlData val : sqlCellData) { for(SqlData val : sqlRow) {
JSONObject jsonOb = colOptionsMap.get(val.getColumnName()); JSONObject jsonOb = colOptionsMap.get(val.getColumnName());
String type = (String)jsonOb.get("type"); String type = (String)jsonOb.get("type");
String defaultValue = (String)jsonOb.get("defaultValue");
boolean allowNullChkBox = (boolean)jsonOb.get("allowNull");
if(type == null) { 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(","); rowValue.append(",");
//logger.info("jsonObject:{}", jsonOb);
} }
idx++; idx++;
String rowValString = rowValue.toString(); String rowValString = rowValue.toString();
rowValString = rowValString.substring(0, rowValString.length() - 1); 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(); String valuesString = values.toString();
valuesString = valuesString.substring(0, valuesString.length() - 1); 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, "authorize", new Packages.com.google.refine.commands.auth.AuthorizeCommand());
RS.registerCommand(module, "deauthorize", new Packages.com.google.refine.commands.auth.DeAuthorizeCommand()); 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() { function registerOperations() {

View File

@ -281,7 +281,9 @@
"tableNameLabel": "Table Name:", "tableNameLabel": "Table Name:",
"sqlExporterTrimColumns": "Trim Column Names", "sqlExporterTrimColumns": "Trim Column Names",
"sqlExporterIgnoreFacets": "Ignore facets and filters and export all rows", "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>Field Name</th>
<th>SQL Type</th> <th>SQL Type</th>
<th>Size</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>
<!--
<tr>
<th><input type="checkbox" bind="allRowsToggleCheckbox" id="allRowsToggleCheckboxId" /></th>
</tr>
-->
</thead> </thead>
</table> </table>
@ -75,12 +73,17 @@
<td width="90%"><input type="text" bind="tableNameTextBox" id="tableNameTextBoxId" size="50%"/></td> <td width="90%"><input type="text" bind="tableNameTextBox" id="tableNameTextBoxId" size="50%"/></td>
</tr> </tr>
<tr>
</tr>
</table> </table>
</div> </div>
<div class="grid-layout grid-layout-for-text layout-tightest"><table> <div class="grid-layout grid-layout-for-text layout-tightest"><table>
<tr>
<td><span style="margin:2px; padding:2px;"></span></td>
</tr>
<tr> <tr>
<td width="1%"><input type="checkbox" bind="includeStructureCheckbox" id="includeStructureCheckboxId" checked/></td> <td width="1%"><input type="checkbox" bind="includeStructureCheckbox" id="includeStructureCheckboxId" checked/></td>
<td><label for="includeStructureCheckboxId" bind="includeStructureLabel"></label></td> <td><label for="includeStructureCheckboxId" bind="includeStructureLabel"></label></td>
@ -88,12 +91,24 @@
<tr> <tr>
<td width="1%"><input type="checkbox" bind="includeDropStatementCheckbox" id="includeDropStatementCheckboxId"/></td> <td width="1%"><input type="checkbox" bind="includeDropStatementCheckbox" id="includeDropStatementCheckboxId"/></td>
<td><label for="includeDropStatementCheckboxId" bind="includeDropStatementLabel"></label></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>
<tr> <tr>
<td width="1%"><input type="checkbox" bind="includeContentCheckbox" id="includeContentCheckboxId" checked/></td> <td width="1%"><input type="checkbox" bind="includeContentCheckbox" id="includeContentCheckboxId" checked/></td>
<td><label for="includeContentCheckboxId" bind="includeContentLabel"></label></td> <td><label for="includeContentCheckboxId" bind="includeContentLabel"></label></td>
</tr> </tr>
<tr>
<td width="1%"><input type="checkbox" bind="nullCellValueToEmptyStringCheckbox" id="nullCellValueToEmptyStringCheckboxId" checked/></td>
<td><label for="nullCellValueToEmptyStringCheckbox" bind="nullCellValueToEmptyStringLabel"></label></td>
</tr>
<tr> <tr>
<td colspan="2"><div class="grid-layout grid-layout-for-text layout-tightest layout-full"><table><tr> <td colspan="2"><div class="grid-layout grid-layout-for-text layout-tightest layout-full"><table><tr>

View File

@ -48,16 +48,12 @@ function SqlExporterDialog(options) {
this._dialog = $(DOM.loadHTML("core", "scripts/dialogs/sql-exporter-dialog.html")); this._dialog = $(DOM.loadHTML("core", "scripts/dialogs/sql-exporter-dialog.html"));
this._elmts = DOM.bind(this._dialog); this._elmts = DOM.bind(this._dialog);
this._level = DialogSystem.showDialog(this._dialog); this._level = DialogSystem.showDialog(this._dialog);
this._elmts.dialogHeader.html($.i18n._('core-dialogs')["custom-tab-exp"]); this._elmts.dialogHeader.html($.i18n._('core-dialogs')["custom-tab-exp"]);
this._elmts.or_dialog_content.html($.i18n._('core-dialogs')["content"]); this._elmts.or_dialog_content.html($.i18n._('core-dialogs')["content"]);
this._elmts.or_dialog_download.html($.i18n._('core-dialogs')["download"]); this._elmts.or_dialog_download.html($.i18n._('core-dialogs')["download"]);
this._elmts.selectAllButton.html($.i18n._('core-buttons')["select-all"]); this._elmts.selectAllButton.html($.i18n._('core-buttons')["select-all"]);
this._elmts.deselectAllButton.html($.i18n._('core-buttons')["deselect-all"]); this._elmts.deselectAllButton.html($.i18n._('core-buttons')["deselect-all"]);
this._elmts.downloadPreviewButton.html($.i18n._('core-buttons')["preview"]); this._elmts.downloadPreviewButton.html($.i18n._('core-buttons')["preview"]);
this._elmts.downloadButton.html($.i18n._('core-buttons')["download"]); 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.cancelButton.html($.i18n._('core-buttons')["cancel"]);
// this._elmts.nextButton.html($.i18n._('core-buttons')["next"]); // this._elmts.nextButton.html($.i18n._('core-buttons')["next"]);
this._elmts.tableNameLabel.html($.i18n._('core-dialogs')["tableNameLabel"]); this._elmts.tableNameLabel.html($.i18n._('core-dialogs')["tableNameLabel"]);
this._elmts.includeStructureLabel.html($.i18n._('core-dialogs')["for-include-structure-checkbox"]); 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.includeDropStatementLabel.html($.i18n._('core-dialogs')["for-include-drop-statement-checkbox"]);
this._elmts.includeContentLabel.html($.i18n._('core-dialogs')["for-include-content-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.sqlExportIgnoreFacetsLabel.html($.i18n._('core-dialogs')["sqlExporterIgnoreFacets"]);
this._elmts.sqlExportTrimAllColumnsLabel.html($.i18n._('core-dialogs')["sqlExporterTrimColumns"]); this._elmts.sqlExportTrimAllColumnsLabel.html($.i18n._('core-dialogs')["sqlExporterTrimColumns"]);
this._elmts.sqlExportOutputEmptyRowsLabel.html($.i18n._('core-dialogs')["sqlExporterOutputEmptyRows"]); this._elmts.sqlExportOutputEmptyRowsLabel.html($.i18n._('core-dialogs')["sqlExporterOutputEmptyRows"]);
$("#sql-exporter-tabs-content").css("display", ""); $("#sql-exporter-tabs-content").css("display", "");
$("#sql-exporter-tabs-download").css("display", ""); $("#sql-exporter-tabs-download").css("display", "");
$("#sql-exporter-tabs").tabs(); $("#sql-exporter-tabs").tabs();
/*
* Populate column list.
*/
for (var i = 0; i < theProject.columnModel.columns.length; i++) { for (var i = 0; i < theProject.columnModel.columns.length; i++) {
var column = theProject.columnModel.columns[i]; var column = theProject.columnModel.columns[i];
var name = column.name; var name = column.name;
var rowId = "sql-exporter-dialog-row" + i; var rowId = "sql-exporter-dialog-row" + i;
var selectBoxName = 'selectBoxRow' + i; var selectBoxName = 'selectBoxRow' + i;
var sizeInputName = 'sizeInputRow' + i; var sizeInputName = 'sizeInputRow' + i;
var applyAllBtnName = 'applyAllBtn' + i; var applyAllBtnName = 'applyAllBtn' + i;
// var arr = [ var allowNullChkBoxName = 'allowNullChkBox' + i;
// {val : 'VARCHAR', text: 'VARCHAR'}, var defaultValueTextBoxName = 'defaultValueTextBox' + i;
// {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 sel = $('<select>').appendTo('body'); var sel = $('<select>').appendTo('body');
sel.append($("<option>").attr('value','VARCHAR').text('VARCHAR')); sel.append($("<option>").attr('value','VARCHAR').text('VARCHAR'));
@ -112,11 +97,7 @@ function SqlExporterDialog(options) {
sel.append($("<option>").attr('value','CHAR').text('CHAR')); sel.append($("<option>").attr('value','CHAR').text('CHAR'));
sel.append($("<option>").attr('value','DATE').text('DATE')); sel.append($("<option>").attr('value','DATE').text('DATE'));
sel.append($("<option>").attr('value','TIMESTAMP').text('TIMESTAMP')); 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('id', selectBoxName);
sel.attr('rowIndex', i); sel.attr('rowIndex', i);
sel.addClass('typeSelectClass'); sel.addClass('typeSelectClass');
@ -131,14 +112,15 @@ function SqlExporterDialog(options) {
$('#sizeInputRow'+ rowIndex).prop("disabled", true); $('#sizeInputRow'+ rowIndex).prop("disabled", true);
} }
}); });
//create and add Row
var row = $('<tr>') var row = $('<tr>')
.addClass("sql-exporter-dialog-row") .addClass("sql-exporter-dialog-row")
.attr('id', rowId) .attr('id', rowId)
.attr("column", name) .attr("column", name)
.attr("rowIndex", i) .attr("rowIndex", i)
.appendTo(this._elmts.columnListTable); .appendTo(this._elmts.columnListTable);
//create and add column
var columnCell = $('<td>') var columnCell = $('<td>')
.attr('width', '150px') .attr('width', '150px')
.appendTo(row); .appendTo(row);
@ -166,15 +148,40 @@ function SqlExporterDialog(options) {
.addClass("sql-exporter-dialog-input") .addClass("sql-exporter-dialog-input")
.appendTo(sizeCell); .appendTo(sizeCell);
var applyAllCell = $('<td>') var applyAllCell = $('<td>')
.attr('width', '60px') .attr('width', '60px')
.appendTo(row); .appendTo(row);
$('<input>')
.attr('type', 'button')
.attr('value', 'Apply All')
.attr('id', applyAllBtnName)
.attr("rowIndex", i)
.appendTo(applyAllCell);
var nullableCell = $('<td>')
.attr('width', '90px')
.addClass("allowNullCellStyle")
.appendTo(row);
$('<input>') $('<input>')
.attr('type', 'button') .attr('type', 'checkbox')
.attr('value', 'Apply All') .attr('checked', 'checked')
.attr('id', applyAllBtnName) .attr('id', allowNullChkBoxName)
.attr("rowIndex", i) .attr("rowIndex", i)
.appendTo(applyAllCell); .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() { $('#' + applyAllBtnName).on('click', function() {
var rowIndex = this.getAttribute('rowIndex'); var rowIndex = this.getAttribute('rowIndex');
@ -191,18 +198,29 @@ function SqlExporterDialog(options) {
}); });
$('input.sql-exporter-dialog-input').each(function() { $('input.sql-exporter-dialog-input').each(function() {
//alert("Value:" + this.value + " RowIndex:" + rowIndex + " TypeValue:" + typeValue + "" + this.value);
var rowId = this.getAttribute('rowIndex'); var rowId = this.getAttribute('rowIndex');
var id = this.getAttribute('id'); var id = this.getAttribute('id');
if(rowIndex !== rowId){ if(rowIndex !== rowId){
$("#" + id).val(sizeValue); $("#" + 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] = { this._columnOptionMap[name] = {
name: name, name: name,
type: '', 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() { this._elmts.selectAllButton.click(function() {
$("input:checkbox[class=columnNameCheckboxStyle]").each(function () { $("input:checkbox[class=columnNameCheckboxStyle]").each(function () {
@ -230,8 +268,20 @@ function SqlExporterDialog(options) {
//alert('checked ' + checked); //alert('checked ' + checked);
if(checked == true){ if(checked == true){
$('#includeDropStatementCheckboxId').removeAttr("disabled"); $('#includeDropStatementCheckboxId').removeAttr("disabled");
$('#includeIfExistDropStatementCheckboxId').removeAttr("disabled");
}else{ }else{
$('#includeDropStatementCheckboxId').attr("disabled", true); $('#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.tableNameTextBox.val(theProject.metadata.name);
this._elmts.sqlExportOutputEmptyRowsCheckbox.attr('checked', 'checked'); this._elmts.sqlExportOutputEmptyRowsCheckbox.attr('checked', 'checked');
this._elmts.sqlExportTrimAllColumnsCheckbox.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() { SqlExporterDialog.prototype._dismiss = function() {
@ -271,6 +326,7 @@ function SqlExporterDialog(options) {
}; };
SqlExporterDialog.prototype._postExport = function(preview) { SqlExporterDialog.prototype._postExport = function(preview) {
var self = this;
var exportAllRowsCheckbox = this._elmts.sqlExportAllRowsCheckbox[0].checked; var exportAllRowsCheckbox = this._elmts.sqlExportAllRowsCheckbox[0].checked;
var options = this._getOptionCode(); var options = this._getOptionCode();
@ -279,42 +335,71 @@ function SqlExporterDialog(options) {
return false; return false;
} }
var format = options.format; var name = $.trim(theProject.metadata.name.replace(/\W/g, ' ')).replace(/\s+/g, '-');
var encoding = options.encoding; var sqlExportInput = {};
sqlExportInput.name = name;
sqlExportInput.options = JSON.stringify(options);
sqlExportInput.project = theProject.id;
delete options.format; //validate form @ backend
delete options.encoding; $.post(
if (preview) { "command/core/preview-sql-export",
options.limit = 10; 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");
$('<input />')
.attr("name", "options")
.attr("value", JSON.stringify(options))
.appendTo(form);
if (encoding) {
$('<input />')
.attr("name", "encoding")
.attr("value", encoding)
.appendTo(form);
}
if (!preview) {
$('<input />')
.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; return true;
var form = this._prepareSqlExportRowsForm(format, !exportAllRowsCheckbox, "sql");
$('<input />')
.attr("name", "options")
.attr("value", JSON.stringify(options))
.appendTo(form);
if (encoding) {
$('<input />')
.attr("name", "encoding")
.attr("value", encoding)
.appendTo(form);
}
if (!preview) {
$('<input />')
.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;
}; };
SqlExporterDialog.prototype._prepareSqlExportRowsForm = function(format, includeEngine, ext) { SqlExporterDialog.prototype._prepareSqlExportRowsForm = function(format, includeEngine, ext) {
@ -386,8 +471,11 @@ function SqlExporterDialog(options) {
options.includeStructure = this._elmts.includeStructureCheckbox[0].checked; options.includeStructure = this._elmts.includeStructureCheckbox[0].checked;
options.includeDropStatement = this._elmts.includeDropStatementCheckbox[0].checked; options.includeDropStatement = this._elmts.includeDropStatementCheckbox[0].checked;
options.includeContent = this._elmts.includeContentCheckbox[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.trimColumnNames = this._elmts.sqlExportTrimAllColumnsCheckbox[0].checked;
options.convertNulltoEmptyString = this._elmts.nullCellValueToEmptyStringCheckbox[0].checked;
options.includeIfExistWithDropStatement = this._elmts.includeIfExistDropStatementCheckbox[0].checked;
options.columns = []; options.columns = [];
@ -397,21 +485,28 @@ function SqlExporterDialog(options) {
if ($(this).find('input[type="checkbox"]')[0].checked) { if ($(this).find('input[type="checkbox"]')[0].checked) {
var name = this.getAttribute('column'); var name = this.getAttribute('column');
var rowIndex = this.getAttribute('rowIndex'); var rowIndex = this.getAttribute('rowIndex');
// alert("column::"+ name + " rowIndex::" + rowIndex);
var selectedValue = $('#selectBoxRow' + rowIndex).val(); var selectedValue = $('#selectBoxRow' + rowIndex).val();
//alert("selectedValue::"+ selectedValue);
var typeSize = 0; 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(); typeSize = $('#sizeInputRow' + rowIndex).val();
// alert("typeSize::" + typeSize); // 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 fullColumnOptions = self._columnOptionMap[name];
var columnOptions = { var columnOptions = {
name: name, name: name,
type: selectedValue, 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; margin:2px;
border: solid 2px lightblue; border: solid 2px lightblue;
} }
.defaultValueTextBoxStyle {
margin:2px;
}
.allowNullCellStyle {
text-align: center;
vertical-align: middle;
}
.typeSelectClass { .typeSelectClass {
margin-left:2px; margin-left:2px;
} }