SQL Export Code Commit
This commit is contained in:
parent
e0f7690262
commit
3834c27c8f
@ -36,6 +36,8 @@ package com.google.refine.exporters;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.refine.exporters.sql.SqlExporter;
|
||||
|
||||
abstract public class ExporterRegistry {
|
||||
static final private Map<String, Exporter> s_formatToExporter = new HashMap<String, Exporter>();
|
||||
|
||||
@ -52,6 +54,8 @@ abstract public class ExporterRegistry {
|
||||
s_formatToExporter.put("html", new HtmlTableExporter());
|
||||
|
||||
s_formatToExporter.put("template", new TemplatingExporter());
|
||||
|
||||
s_formatToExporter.put("sql", new SqlExporter());
|
||||
}
|
||||
|
||||
static public void registerExporter(String format, Exporter exporter) {
|
||||
|
130
main/src/com/google/refine/exporters/sql/SqlCreateBuilder.java
Executable file
130
main/src/com/google/refine/exporters/sql/SqlCreateBuilder.java
Executable file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Tony Opara
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of Google nor the names of its contributors may be used to
|
||||
* endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.google.refine.exporters.sql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.refine.util.JSONUtilities;
|
||||
|
||||
public class SqlCreateBuilder {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger("SqlCreateBuilder");
|
||||
|
||||
private String table;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private List<String> columns;
|
||||
|
||||
private JSONObject options;
|
||||
|
||||
public SqlCreateBuilder(String table, List<String> columns, JSONObject options) {
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public String getCreateSQL() {
|
||||
StringBuffer createSB = new StringBuffer();
|
||||
|
||||
JSONArray columnOptionArray = options == null ? null : JSONUtilities.getArray(options, "columns");
|
||||
|
||||
int count = columnOptionArray.length();
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
JSONObject columnOptions = JSONUtilities.getObjectElement(columnOptionArray, i);
|
||||
if (columnOptions != null) {
|
||||
String name = JSONUtilities.getString(columnOptions, "name", null);
|
||||
String type = JSONUtilities.getString(columnOptions, "type", "VARCHAR");
|
||||
String size = JSONUtilities.getString(columnOptions, "size", "");
|
||||
if (name != null) {
|
||||
createSB.append(name + " ");
|
||||
|
||||
if (type.equals("VARCHAR")) {
|
||||
if (size.isEmpty()) {
|
||||
size = "255";
|
||||
}
|
||||
createSB.append(type + "(" + size + ")");
|
||||
|
||||
} else if (type.equals("CHAR")) {
|
||||
if (size.isEmpty()) {
|
||||
size = "10";
|
||||
}
|
||||
createSB.append(type + "(" + size + ")");
|
||||
|
||||
} else if (type.equals("INT") || type.equals("INTEGER")) {
|
||||
if (size.isEmpty()) {
|
||||
createSB.append(type);
|
||||
} else {
|
||||
createSB.append(type + "(" + size + ")");
|
||||
}
|
||||
|
||||
} else if (type.equals("NUMERIC")) {
|
||||
if (size.isEmpty()) {
|
||||
createSB.append(type);
|
||||
} else {
|
||||
createSB.append(type + "(" + size + ")");
|
||||
}
|
||||
} else {
|
||||
createSB.append(type);
|
||||
}
|
||||
|
||||
if (i < count - 1) {
|
||||
createSB.append(",");
|
||||
}
|
||||
createSB.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StringBuffer sql = new StringBuffer();
|
||||
|
||||
boolean includeDrop = JSONUtilities.getBoolean(options, "includeDropStatement", false);
|
||||
if (includeDrop) {
|
||||
sql.append("DROP TABLE " + table + ";\n");
|
||||
}
|
||||
|
||||
sql.append("CREATE TABLE ").append(table);
|
||||
sql.append(" (").append("\n");
|
||||
sql.append(createSB.toString());
|
||||
sql.append(")").append(";" + "\n");
|
||||
|
||||
String createSQL = sql.toString();
|
||||
if(logger.isDebugEnabled()){
|
||||
logger.debug("Create SQL Generated Successfully...{}", createSQL);
|
||||
}
|
||||
return createSQL;
|
||||
}
|
||||
|
||||
}
|
61
main/src/com/google/refine/exporters/sql/SqlData.java
Executable file
61
main/src/com/google/refine/exporters/sql/SqlData.java
Executable file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Tony Opara
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of Google nor the names of its contributors may be used to
|
||||
* endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.google.refine.exporters.sql;
|
||||
|
||||
|
||||
public class SqlData {
|
||||
|
||||
final public String columnName;
|
||||
final public Object value;
|
||||
final public String text;
|
||||
|
||||
public SqlData(String columnName, Object value, String text) {
|
||||
this.columnName = columnName;
|
||||
this.value = value;
|
||||
this.text = text;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String getColumnName() {
|
||||
return columnName;
|
||||
}
|
||||
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
}
|
159
main/src/com/google/refine/exporters/sql/SqlExporter.java
Executable file
159
main/src/com/google/refine/exporters/sql/SqlExporter.java
Executable file
@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Tony Opara
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of Google nor the names of its contributors may be used to
|
||||
* endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.google.refine.exporters.sql;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.refine.ProjectManager;
|
||||
import com.google.refine.browsing.Engine;
|
||||
import com.google.refine.exporters.CustomizableTabularExporterUtilities;
|
||||
import com.google.refine.exporters.TabularSerializer;
|
||||
import com.google.refine.exporters.WriterExporter;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.util.JSONUtilities;
|
||||
|
||||
public class SqlExporter implements WriterExporter {
|
||||
|
||||
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");
|
||||
|
||||
private List<String> columnNames = new ArrayList<String>();
|
||||
private List<ArrayList<SqlData>> sqlDataList = new ArrayList<ArrayList<SqlData>>();
|
||||
private JSONObject sqlOptions;
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "text/plain";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void export(final Project project, Properties params, Engine engine, final Writer writer)
|
||||
throws IOException {
|
||||
|
||||
TabularSerializer serializer = new TabularSerializer() {
|
||||
|
||||
@Override
|
||||
public void startFile(JSONObject options) {
|
||||
sqlOptions = options;
|
||||
//logger.info("setting options::{}", sqlOptions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endFile() {
|
||||
try {
|
||||
if (columnNames.isEmpty()) {
|
||||
writer.write(NO_COL_SELECTED_ERROR);
|
||||
logger.error("No Columns Selected!!");
|
||||
return;
|
||||
}
|
||||
if (sqlOptions == null) {
|
||||
writer.write(NO_OPTIONS_PRESENT_ERROR);
|
||||
logger.error("No Options Selected!!");
|
||||
return;
|
||||
}
|
||||
String tableName = ProjectManager.singleton.getProjectMetadata(project.id).getName();
|
||||
|
||||
Object tableNameManual = sqlOptions.get("tableName");
|
||||
|
||||
if (tableNameManual != null && !tableNameManual.toString().isEmpty()) {
|
||||
tableName = tableNameManual.toString();
|
||||
}
|
||||
|
||||
SqlCreateBuilder createBuilder = new SqlCreateBuilder(tableName, columnNames, sqlOptions);
|
||||
SqlInsertBuilder insertBuilder = new SqlInsertBuilder(tableName, columnNames, sqlDataList,
|
||||
sqlOptions);
|
||||
|
||||
final boolean includeStructure = sqlOptions == null ? true
|
||||
: JSONUtilities.getBoolean(sqlOptions, "includeStructure", true);
|
||||
|
||||
final boolean includeContent = sqlOptions == null ? true
|
||||
: JSONUtilities.getBoolean(sqlOptions, "includeContent", true);
|
||||
|
||||
if (includeStructure) {
|
||||
String sqlCreateStr = createBuilder.getCreateSQL();
|
||||
writer.write(sqlCreateStr);
|
||||
|
||||
}
|
||||
|
||||
if (includeContent) {
|
||||
String sqlInsertStr = insertBuilder.getInsertSQL();
|
||||
writer.write(sqlInsertStr);
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("sqlOptions::{}", sqlOptions);
|
||||
}
|
||||
|
||||
columnNames = new ArrayList<String>();
|
||||
sqlDataList = new ArrayList<ArrayList<SqlData>>();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRow(List<CellData> cells, boolean isHeader) {
|
||||
|
||||
if (isHeader) {
|
||||
for (CellData cellData : cells) {
|
||||
columnNames.add(cellData.text);
|
||||
}
|
||||
|
||||
} else {
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
sqlDataList.add(values);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
CustomizableTabularExporterUtilities.exportRows(project, engine, params, serializer);
|
||||
}
|
||||
}
|
151
main/src/com/google/refine/exporters/sql/SqlInsertBuilder.java
Executable file
151
main/src/com/google/refine/exporters/sql/SqlInsertBuilder.java
Executable file
@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Tony Opara
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of Google nor the names of its contributors may be used to
|
||||
* endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.google.refine.exporters.sql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.refine.util.JSONUtilities;
|
||||
|
||||
public class SqlInsertBuilder {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger("SQLInsertBuilder");
|
||||
|
||||
private String table;
|
||||
|
||||
private List<String> columns;
|
||||
|
||||
private List<ArrayList<SqlData>> sqlDataList;
|
||||
|
||||
private JSONObject options;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param table
|
||||
* @param columns
|
||||
* @param rows
|
||||
* @param options
|
||||
*/
|
||||
public SqlInsertBuilder(String table, List<String> columns, List<ArrayList<SqlData>> rows, JSONObject options) {
|
||||
this.table = table;
|
||||
this.columns = columns;
|
||||
this.sqlDataList = rows;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Insert Sql
|
||||
* @return
|
||||
*/
|
||||
public String getInsertSQL() {
|
||||
// logger.info("options values::::{}", options);
|
||||
JSONArray columnOptionArray = options == null ? null :
|
||||
JSONUtilities.getArray(options, "columns");
|
||||
//logger.info("columnOptionArray::::{}", columnOptionArray);
|
||||
|
||||
Map<String, JSONObject> colOptionsMap = new HashMap<String, JSONObject>();
|
||||
if(columnOptionArray != null) {
|
||||
columnOptionArray.forEach(c -> {
|
||||
JSONObject json = (JSONObject)c;
|
||||
colOptionsMap.put("" + json.get("name"), json);
|
||||
});
|
||||
}
|
||||
|
||||
String colNamesWithSep = columns.stream()
|
||||
.collect(Collectors.joining(","));
|
||||
StringBuffer values = new StringBuffer();
|
||||
|
||||
int idx = 0;
|
||||
for(ArrayList<SqlData> sqlCellData : sqlDataList) {
|
||||
StringBuilder rowValue = new StringBuilder();
|
||||
//logger.info(" row.size:{}", row.size());
|
||||
for(SqlData val : sqlCellData) {
|
||||
|
||||
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 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);
|
||||
|
||||
values.append("( ");
|
||||
values.append(rowValString);
|
||||
values.append(" )");
|
||||
if(idx < sqlDataList.size()) {
|
||||
values.append(",");
|
||||
}
|
||||
values.append("\n");
|
||||
// logger.info("running values:{}", values.toString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
String valuesString = values.toString();
|
||||
valuesString = valuesString.substring(0, valuesString.length() - 1);
|
||||
|
||||
|
||||
StringBuffer sql = new StringBuffer();
|
||||
|
||||
sql.append("INSERT INTO ").append(table);
|
||||
sql.append(" (");
|
||||
sql.append(colNamesWithSep);
|
||||
sql.append(") VALUES ").append("\n");
|
||||
sql.append(valuesString);
|
||||
|
||||
String sqlString = sql.toString();
|
||||
if(logger.isDebugEnabled()) {
|
||||
logger.debug("Insert Statement Generated Successfully...{}", sqlString);
|
||||
}
|
||||
return sqlString;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,304 @@
|
||||
/*
|
||||
|
||||
Copyright 2018, Tony Opara.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
package com.google.refine.tests.exporters.sql;
|
||||
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.ProjectManager;
|
||||
import com.google.refine.browsing.Engine;
|
||||
import com.google.refine.exporters.sql.SqlCreateBuilder;
|
||||
import com.google.refine.exporters.sql.SqlExporter;
|
||||
import com.google.refine.exporters.sql.SqlInsertBuilder;
|
||||
import com.google.refine.model.Cell;
|
||||
import com.google.refine.model.Column;
|
||||
import com.google.refine.model.ModelException;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.model.medadata.ProjectMetadata;
|
||||
import com.google.refine.tests.ProjectManagerStub;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
|
||||
|
||||
public class SqlExporterTests extends RefineTest {
|
||||
|
||||
private static final String TEST_PROJECT_NAME = "SQL_EXPORTER_TEST_PROJECT";
|
||||
|
||||
@Override
|
||||
@BeforeTest
|
||||
public void init() {
|
||||
logger = LoggerFactory.getLogger(this.getClass());
|
||||
}
|
||||
|
||||
//dependencies
|
||||
StringWriter writer;
|
||||
ProjectMetadata projectMetadata;
|
||||
Project project;
|
||||
Engine engine;
|
||||
Properties options;
|
||||
SqlCreateBuilder sqlCreateBuilder;
|
||||
SqlInsertBuilder sqlInsertBuilder;
|
||||
|
||||
//System Under Test
|
||||
SqlExporter SUT;
|
||||
|
||||
@BeforeMethod
|
||||
public void SetUp(){
|
||||
SUT = new SqlExporter();
|
||||
writer = new StringWriter();
|
||||
ProjectManager.singleton = new ProjectManagerStub();
|
||||
projectMetadata = new ProjectMetadata();
|
||||
project = new Project();
|
||||
projectMetadata.setName(TEST_PROJECT_NAME);
|
||||
ProjectManager.singleton.registerProject(project, projectMetadata);
|
||||
engine = new Engine(project);
|
||||
options = mock(Properties.class);
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void TearDown(){
|
||||
SUT = null;
|
||||
writer = null;
|
||||
ProjectManager.singleton.deleteProject(project.id);
|
||||
project = null;
|
||||
projectMetadata = null;
|
||||
engine = null;
|
||||
options = null;
|
||||
sqlCreateBuilder = null;
|
||||
sqlInsertBuilder = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportSimpleSql(){
|
||||
createGrid(2, 2);
|
||||
String tableName = "sql_table_test";
|
||||
String optionsString = createOptionsFromProject(tableName, null,null).toString();
|
||||
when(options.getProperty("options")).thenReturn(optionsString);
|
||||
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
String result = writer.toString();
|
||||
// logger.info("result = \n" + result);
|
||||
Assert.assertNotNull(result);
|
||||
assertNotEquals(writer.toString(), SqlExporter.NO_OPTIONS_PRESENT_ERROR);
|
||||
boolean checkResult = result.contains("CREATE TABLE " + tableName);
|
||||
//logger.info("checkResult1 =" + checkResult);
|
||||
checkResult = result.contains("INSERT INTO " + tableName);
|
||||
// logger.info("checkResult2 =" + checkResult);
|
||||
Assert.assertEquals(checkResult, true);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportSqlNoSchema(){
|
||||
createGrid(2, 2);
|
||||
String tableName = "sql_table_test";
|
||||
JSONObject optionsJson = createOptionsFromProject(tableName, null,null);
|
||||
optionsJson.put("includeStructure", false);
|
||||
when(options.getProperty("options")).thenReturn(optionsJson.toString());
|
||||
// logger.info("Options = " + optionsJson.toString());
|
||||
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
String result = writer.toString();
|
||||
//logger.info("result = \n" + result);
|
||||
Assert.assertNotNull(result);
|
||||
assertNotEquals(writer.toString(), SqlExporter.NO_OPTIONS_PRESENT_ERROR);
|
||||
boolean checkResult = result.contains("CREATE TABLE " + tableName);
|
||||
Assert.assertEquals(checkResult, false);
|
||||
|
||||
checkResult = result.contains("INSERT INTO " + tableName);
|
||||
Assert.assertEquals(checkResult, true);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportSqlNoContent(){
|
||||
createGrid(2, 2);
|
||||
String tableName = "sql_table_test";
|
||||
JSONObject optionsJson = createOptionsFromProject(tableName, null, null);
|
||||
optionsJson.put("includeContent", false);
|
||||
when(options.getProperty("options")).thenReturn(optionsJson.toString());
|
||||
//logger.info("Options = " + optionsJson.toString());
|
||||
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
String result = writer.toString();
|
||||
// logger.info("result = \n" + result);
|
||||
Assert.assertNotNull(result);
|
||||
assertNotEquals(writer.toString(), SqlExporter.NO_OPTIONS_PRESENT_ERROR);
|
||||
boolean checkResult = result.contains("CREATE TABLE " + tableName);
|
||||
Assert.assertEquals(checkResult, true);
|
||||
|
||||
checkResult = result.contains("INSERT INTO " + tableName);
|
||||
Assert.assertEquals(checkResult, false);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExportSqlIncludeSchemaWithDropStmt(){
|
||||
createGrid(2, 2);
|
||||
String tableName = "sql_table_test";
|
||||
JSONObject optionsJson = createOptionsFromProject(tableName, null, null);
|
||||
optionsJson.put("includeStructure", true);
|
||||
optionsJson.put("includeDropStatement", true);
|
||||
|
||||
when(options.getProperty("options")).thenReturn(optionsJson.toString());
|
||||
//logger.info("Options = " + optionsJson.toString());
|
||||
|
||||
try {
|
||||
SUT.export(project, options, engine, writer);
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
String result = writer.toString();
|
||||
|
||||
Assert.assertNotNull(result);
|
||||
assertNotEquals(writer.toString(), SqlExporter.NO_OPTIONS_PRESENT_ERROR);
|
||||
assertNotEquals(writer.toString(), SqlExporter.NO_COL_SELECTED_ERROR);
|
||||
|
||||
boolean checkResult = result.contains("CREATE TABLE " + tableName);
|
||||
Assert.assertEquals(checkResult, true);
|
||||
|
||||
checkResult = result.contains("INSERT INTO " + tableName );
|
||||
Assert.assertEquals(checkResult, true);
|
||||
|
||||
checkResult = result.contains("DROP TABLE " + tableName + ";");
|
||||
Assert.assertEquals(checkResult, true);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetCreateSql(){
|
||||
createGrid(3,3);
|
||||
String tableName = "sql_table_test";
|
||||
String type = "CHAR";
|
||||
String size = "2";
|
||||
JSONObject optionsJson = createOptionsFromProject(tableName, type, size);
|
||||
// logger.info("Options:: = " + optionsJson.toString());
|
||||
List<String> columns = project.columnModel.columns.stream().map(col -> col.getName()).collect(Collectors.toList());
|
||||
|
||||
sqlCreateBuilder = new SqlCreateBuilder(tableName, columns, optionsJson);
|
||||
String createSql = sqlCreateBuilder.getCreateSQL();
|
||||
//logger.info("createSql = \n" + createSql);
|
||||
Assert.assertNotNull(createSql);
|
||||
boolean result = createSql.contains(type + "(" + size + ")");
|
||||
Assert.assertEquals(result, true);
|
||||
|
||||
}
|
||||
|
||||
//helper methods
|
||||
protected void createColumns(int noOfColumns){
|
||||
for(int i = 0; i < noOfColumns; i++){
|
||||
try {
|
||||
project.columnModel.addColumn(i, new Column(i, "column" + i), true);
|
||||
} catch (ModelException e1) {
|
||||
Assert.fail("Could not create column");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void createGrid(int noOfRows, int noOfColumns){
|
||||
createColumns(noOfColumns);
|
||||
|
||||
for(int i = 0; i < noOfRows; i++){
|
||||
Row row = new Row(noOfColumns);
|
||||
for(int j = 0; j < noOfColumns; j++){
|
||||
row.cells.add(new Cell("row" + i + "cell" + j, null));
|
||||
}
|
||||
project.rows.add(row);
|
||||
}
|
||||
}
|
||||
|
||||
protected JSONObject createOptionsFromProject(String tableName, String type, String size) {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
JSONArray columns = new JSONArray();
|
||||
json.put("columns", columns);
|
||||
json.put("tableName", tableName);
|
||||
|
||||
List<Column> cols = project.columnModel.columns;
|
||||
|
||||
cols.forEach(c -> {
|
||||
//logger.info("Column Name = " + c.getName());
|
||||
JSONObject columnModel = new JSONObject();
|
||||
columnModel.put("name", c.getName());
|
||||
columnModel.put("type", "VARCHAR");
|
||||
columnModel.put("size", "100");
|
||||
if(type != null) {
|
||||
columnModel.put("type", type);
|
||||
}
|
||||
if(size != null) {
|
||||
// logger.info(" Size = " + size);
|
||||
columnModel.put("size", size);
|
||||
}
|
||||
|
||||
columns.put(columnModel);
|
||||
|
||||
});
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -467,6 +467,7 @@ function init() {
|
||||
"scripts/dialogs/templating-exporter-dialog.js",
|
||||
"scripts/dialogs/column-reordering-dialog.js",
|
||||
"scripts/dialogs/custom-tabular-exporter-dialog.js",
|
||||
"scripts/dialogs/sql-exporter-dialog.js",
|
||||
"scripts/dialogs/expression-column-dialog.js",
|
||||
"scripts/project/edit-general-metadata-dialog.js",
|
||||
"scripts/dialogs/http-headers-dialog.js",
|
||||
@ -506,7 +507,7 @@ function init() {
|
||||
"styles/dialogs/scatterplot-dialog.less",
|
||||
"styles/dialogs/column-reordering-dialog.less",
|
||||
"styles/dialogs/custom-tabular-exporter-dialog.less",
|
||||
|
||||
"styles/dialogs/sql-exporter-dialog.less",
|
||||
"styles/reconciliation/recon-dialog.less",
|
||||
"styles/reconciliation/standard-service-panel.less",
|
||||
"styles/reconciliation/extend-data-preview-dialog.less",
|
||||
|
@ -237,7 +237,7 @@
|
||||
"help": "Help",
|
||||
"opt-code-applied": "Option code successfully applied.",
|
||||
"error-apply-code": "Error applying option code",
|
||||
"custom-tab-exp": "Custom Tabular Exporter",
|
||||
"custom-tab-exp": "SQL Exporter",
|
||||
"content": "Content",
|
||||
"download": "Download",
|
||||
"upload": "Upload",
|
||||
@ -273,7 +273,13 @@
|
||||
"char-enc": "Character encoding",
|
||||
"line-sep": "Line separator",
|
||||
"upload-to": "Upload to",
|
||||
"json-text": "The following JSON text encodes the options you have set in the other tabs. You can copy it out and save it for later, and paste it back in and click Apply to re-use the same options."
|
||||
"json-text": "The following JSON text encodes the options you have set in the other tabs. You can copy it out and save it for later, and paste it back in and click Apply to re-use the same options.",
|
||||
"columnType": "SQL Type",
|
||||
"for-include-structure-checkbox": "Include Schema",
|
||||
"for-include-drop-statement-checkbox": "Include Drop Statement",
|
||||
"for-include-content-checkbox": "Include Content",
|
||||
"tableNameLabel": "Table Name:"
|
||||
|
||||
},
|
||||
"core-facets": {
|
||||
"remove-facet": "Remove this facet",
|
||||
@ -344,6 +350,7 @@
|
||||
"triple-loader": "Triple loader",
|
||||
"mqlwrite": "MQLWrite",
|
||||
"custom-tabular": "Custom tabular exporter...",
|
||||
"sql-export": "SQL Exporter...",
|
||||
"templating": "Templating...",
|
||||
"warning-align": "You haven't done any schema alignment yet,\n so there is no triple to export.\n\n Use the Freebase > Edit Schema Alignment Skeleton...\n command to align your data with Freebase schemas first.",
|
||||
"json-invalid": "The JSON you pasted is invalid",
|
||||
|
110
main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.html
Executable file
110
main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.html
Executable file
@ -0,0 +1,110 @@
|
||||
<div class="dialog-frame" style="width: 800px;">
|
||||
<div class="dialog-header" bind="dialogHeader"></div>
|
||||
<div class="dialog-body" bind="dialogBody">
|
||||
<div id="sql-exporter-tabs" class="refine-tabs">
|
||||
<ul>
|
||||
<li><a href="#sql-exporter-tabs-content" bind="or_dialog_content"></a></li>
|
||||
<li><a href="#sql-exporter-tabs-download" bind="or_dialog_download"></a></li>
|
||||
</ul>
|
||||
|
||||
<div id="sql-exporter-tabs-content" class="sql-exporter-tabs-content-main">
|
||||
<div class="grid-layout grid-layout-for-ui layout-normal layout-full"><table>
|
||||
<!-- <tr>
|
||||
<td bind="or_dialog_selAndOrd"></td>
|
||||
<td><span bind="or_dialog_optFor"></span><span bind="columnNameSpan" class="sql-exporter-selected-column"></span></td>
|
||||
</tr> -->
|
||||
<tr>
|
||||
<td width="100%">
|
||||
|
||||
<!-- <div bind="columnList" class="sql-exporter-columns"></div> -->
|
||||
<div class="grid-layout layout-tighter layout-full">
|
||||
<div class="sql-exporter-columns">
|
||||
<table bind = "columnListTable">
|
||||
<thead class="header">
|
||||
<tr>
|
||||
<th>Field Name</th>
|
||||
<th>SQL Type</th>
|
||||
<th>Size</th>
|
||||
</tr>
|
||||
<!--
|
||||
<tr>
|
||||
<th><input type="checkbox" bind="allRowsToggleCheckbox" id="allRowsToggleCheckboxId" /></th>
|
||||
|
||||
</tr>
|
||||
-->
|
||||
</thead>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<button class="button" bind="selectAllButton"></button>
|
||||
<button class="button" bind="deselectAllButton"></button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2"><div class="grid-layout layout-tighter layout-full"><table>
|
||||
<tr>
|
||||
|
||||
<td width="1%"><input type="checkbox" bind="outputEmptyRowsCheckbox" id="$sql-output-empty-rows" /></td>
|
||||
<td width="25%"><label for="$sql-output-empty-rows" bind="or_dialog_outEmptyRow"></label></td>
|
||||
<td width="25%"><div bind="sqlTypeApplyAllDiv" id="sqlTypeApplyAllDivId"></div></td>
|
||||
</tr>
|
||||
</table></div></td>
|
||||
</tr>
|
||||
</table></div></div>
|
||||
|
||||
|
||||
<div id="sql-exporter-tabs-download" style="display: none;">
|
||||
|
||||
<div>
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="tableNameTextBox" bind="tableNameLabel"></label></td>
|
||||
<td width="90%"><input type="text" bind="tableNameTextBox" id="tableNameTextBoxId" size="50%"/></td>
|
||||
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="grid-layout grid-layout-for-text layout-tightest"><table>
|
||||
|
||||
<tr>
|
||||
<td width="1%"><input type="checkbox" bind="includeStructureCheckbox" id="includeStructureCheckboxId" checked/></td>
|
||||
<td><label for="includeStructureCheckboxId" bind="includeStructureLabel"></label></td>
|
||||
</tr>
|
||||
<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="includeContentCheckbox" id="includeContentCheckboxId" checked/></td>
|
||||
<td><label for="includeContentCheckboxId" bind="includeContentLabel"></label></td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td colspan="2"><div class="grid-layout grid-layout-for-text layout-tightest layout-full"><table><tr>
|
||||
<td width="100%"> </td>
|
||||
<td width="1%"><button class="button" bind="downloadPreviewButton"></button></td>
|
||||
<td width="1%"><button class="button button-primary" bind="downloadButton"></button></td>
|
||||
</tr></table></div></td>
|
||||
</tr>
|
||||
</table></div></div>
|
||||
|
||||
<div id="sql-exporter-tabs-code" style="display: none;">
|
||||
<textarea class="sql-exporter-code" bind="optionCodeInput"></textarea>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-footer" bind="dialogFooter"><div class="grid-layout layout-tightest layout-full"><table><tr>
|
||||
<td><button class="button" bind="cancelButton"></button></td>
|
||||
</tr></table></div></div>
|
||||
</div>
|
408
main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.js
Executable file
408
main/webapp/modules/core/scripts/dialogs/sql-exporter-dialog.js
Executable file
@ -0,0 +1,408 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Tony Opara
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* - Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* - Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of Google nor the names of its contributors may be used to
|
||||
* endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
function SqlExporterDialog(options) {
|
||||
options = options || {
|
||||
format: 'sql',
|
||||
encoding: 'UTF-8',
|
||||
outputBlankRows: false,
|
||||
columns: null
|
||||
};
|
||||
|
||||
this._columnOptionMap = {};
|
||||
this._createDialog(options);
|
||||
}
|
||||
|
||||
|
||||
SqlExporterDialog.uploadTargets = [];
|
||||
|
||||
SqlExporterDialog.prototype._createDialog = function(options) {
|
||||
var self = this;
|
||||
|
||||
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.or_dialog_outEmptyRow.html($.i18n._('core-dialogs')["out-empty-row"]);
|
||||
|
||||
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"]);
|
||||
|
||||
|
||||
$("#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 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 sel = $('<select>').appendTo('body');
|
||||
$(arr).each(function() {
|
||||
sel.append($("<option>").attr('value',this.val).text(this.text));
|
||||
});
|
||||
sel.attr('id', selectBoxName);
|
||||
sel.attr('rowIndex', i);
|
||||
sel.addClass('typeSelectClass');
|
||||
sel.css({'width':'95%'});
|
||||
|
||||
$(sel).on('change', function() {
|
||||
var rowIndex = this.getAttribute('rowIndex');
|
||||
if (this.value == 'VARCHAR' || this.value == 'CHAR' || this.value == 'NUMERIC' || this.value == 'INT') {
|
||||
$('#sizeInputRow'+ rowIndex).prop("disabled", false);
|
||||
}else{
|
||||
$('#sizeInputRow'+ rowIndex).val("");
|
||||
$('#sizeInputRow'+ rowIndex).prop("disabled", true);
|
||||
}
|
||||
});
|
||||
|
||||
var row = $('<tr>')
|
||||
.addClass("sql-exporter-dialog-row")
|
||||
.attr('id', rowId)
|
||||
.attr("column", name)
|
||||
.attr("rowIndex", i)
|
||||
.appendTo(this._elmts.columnListTable);
|
||||
|
||||
var columnCell = $('<td>')
|
||||
.attr('width', '150px')
|
||||
.appendTo(row);
|
||||
$('<input>')
|
||||
.attr('type', 'checkbox')
|
||||
.attr('checked', 'checked')
|
||||
.addClass("columnNameCheckboxStyle")
|
||||
.appendTo(columnCell);
|
||||
$('<span>')
|
||||
.text(name)
|
||||
.appendTo(columnCell);
|
||||
|
||||
var typeCell = $('<td>')
|
||||
.attr('width', '150px')
|
||||
.appendTo(row);
|
||||
sel.appendTo(typeCell);
|
||||
|
||||
var sizeCell = $('<td>')
|
||||
.attr('width', '20px')
|
||||
.appendTo(row);
|
||||
$('<input>')
|
||||
.attr('type', 'text')
|
||||
.attr('size', '8px')
|
||||
.attr('id', sizeInputName)
|
||||
.addClass("sql-exporter-dialog-input")
|
||||
.appendTo(sizeCell);
|
||||
|
||||
var applyAllCell = $('<td>')
|
||||
.attr('width', '60px')
|
||||
.appendTo(row);
|
||||
$('<input>')
|
||||
.attr('type', 'button')
|
||||
.attr('value', 'Apply All')
|
||||
.attr('id', applyAllBtnName)
|
||||
.attr("rowIndex", i)
|
||||
.appendTo(applyAllCell);
|
||||
|
||||
$('#' + applyAllBtnName).on('click', function() {
|
||||
var rowIndex = this.getAttribute('rowIndex');
|
||||
var typeValue = $("#selectBoxRow" + rowIndex).val();
|
||||
var sizeValue = $("#sizeInputRow" + rowIndex).val();
|
||||
|
||||
$('select.typeSelectClass').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(typeValue);
|
||||
}
|
||||
|
||||
});
|
||||
$('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);
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
this._columnOptionMap[name] = {
|
||||
name: name,
|
||||
type: '',
|
||||
size: ''
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
this._elmts.selectAllButton.click(function() {
|
||||
$("input:checkbox[class=columnNameCheckboxStyle]").each(function () {
|
||||
$(this).attr('checked', true)
|
||||
});
|
||||
self._updateOptionCode();
|
||||
});
|
||||
this._elmts.deselectAllButton.click(function() {
|
||||
$("input:checkbox[class=columnNameCheckboxStyle]").each(function () {
|
||||
$(this).attr('checked', false)
|
||||
});
|
||||
self._updateOptionCode();
|
||||
});
|
||||
|
||||
this._elmts.includeStructureCheckbox.click(function() {
|
||||
var checked = $(this).is(':checked');
|
||||
//alert('checked ' + checked);
|
||||
if(checked == true){
|
||||
$('#includeDropStatementCheckboxId').removeAttr("disabled");
|
||||
}else{
|
||||
$('#includeDropStatementCheckboxId').attr("disabled", true);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
this._elmts.cancelButton.click(function() { self._dismiss(); });
|
||||
this._elmts.downloadButton.click(function() { self._download(); });
|
||||
this._elmts.downloadPreviewButton.click(function(evt) { self._previewDownload(); });
|
||||
|
||||
this._configureUIFromOptionCode(options);
|
||||
this._updateOptionCode();
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._configureUIFromOptionCode = function(options) {
|
||||
|
||||
this._elmts.tableNameTextBox.val(theProject.metadata.name);
|
||||
this._elmts.outputEmptyRowsCheckbox.attr('checked', 'checked');
|
||||
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._dismiss = function() {
|
||||
DialogSystem.dismissUntil(this._level - 1);
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._previewDownload = function() {
|
||||
this._postExport(true);
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._download = function() {
|
||||
var result = this._postExport(false);
|
||||
// alert("result::" + result);
|
||||
if(result == true){
|
||||
this._dismiss();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._postExport = function(preview) {
|
||||
// var exportAllRowsCheckbox = this._elmts.exportAllRowsCheckbox[0].checked;
|
||||
var options = this._getOptionCode();
|
||||
|
||||
if(options.columns == null || options.columns.length == 0){
|
||||
alert("Please select at least one column...");
|
||||
return false;
|
||||
}
|
||||
|
||||
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 = this._prepareSqlExportRowsForm(format, false, "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) {
|
||||
var name = $.trim(theProject.metadata.name.replace(/\W/g, ' ')).replace(/\s+/g, '-');
|
||||
var form = document.createElement("form");
|
||||
$(form)
|
||||
.css("display", "none")
|
||||
.attr("method", "post")
|
||||
.attr("action", "command/core/export-rows/" + name + ((ext) ? ("." + ext) : ""))
|
||||
.attr("target", "refine-export");
|
||||
|
||||
$('<input />')
|
||||
.attr("name", "project")
|
||||
.attr("value", theProject.id)
|
||||
.appendTo(form);
|
||||
$('<input />')
|
||||
.attr("name", "format")
|
||||
.attr("value", format)
|
||||
.appendTo(form);
|
||||
if (includeEngine) {
|
||||
$('<input />')
|
||||
.attr("name", "engine")
|
||||
.attr("value", JSON.stringify(ui.browsingEngine.getJSON()))
|
||||
.appendTo(form);
|
||||
}
|
||||
|
||||
return form;
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._selectColumn = function(columnName) {
|
||||
|
||||
this._elmts.columnNameSpan.text(columnName);
|
||||
var columnOptions = this._columnOptionMap[columnName];
|
||||
alert("in _selectColumn:column type::" + columnOptions.type);
|
||||
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._updateCurrentColumnOptions = function() {
|
||||
// var selectedColumnName = this._elmts.columnList.find('.sql-exporter-dialog-column.selected').attr('column');
|
||||
// //alert("_updateCurrentColumnOptions::" + selectedColumnName);
|
||||
// var columnOptions = this._columnOptionMap[selectedColumnName];
|
||||
// columnOptions.type= this._elmts.columnOptionPane.find('input[name="sql-exporter-type"]:checked').val();
|
||||
|
||||
};
|
||||
|
||||
SqlExporterDialog.prototype._updateOptionCode = function() {
|
||||
this._elmts.optionCodeInput.val(JSON.stringify(this._getOptionCode(), null, 2));
|
||||
};
|
||||
|
||||
|
||||
SqlExporterDialog.prototype._getOptionCode = function() {
|
||||
var options = {
|
||||
//format: this._dialog.find('input[name="sql-exporter-download-format"]:checked').val()
|
||||
};
|
||||
var unescapeJavascriptString = function(s) {
|
||||
try {
|
||||
return JSON.parse('"' + s + '"');
|
||||
} catch (e) {
|
||||
// We're not handling the case where the user doesn't escape double quotation marks.
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
options.format = 'sql';
|
||||
options.separator = ';';
|
||||
options.encoding = 'UTF-8';
|
||||
|
||||
options.outputBlankRows = this._elmts.outputEmptyRowsCheckbox[0].checked;
|
||||
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.columns = [];
|
||||
|
||||
var self = this;
|
||||
this._elmts.columnListTable.find('.sql-exporter-dialog-row').each(function() {
|
||||
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'){
|
||||
typeSize = $('#sizeInputRow' + rowIndex).val();
|
||||
// alert("typeSize::" + typeSize);
|
||||
}
|
||||
|
||||
var fullColumnOptions = self._columnOptionMap[name];
|
||||
var columnOptions = {
|
||||
name: name,
|
||||
type: selectedValue,
|
||||
size: typeSize
|
||||
|
||||
};
|
||||
|
||||
// alert('checked type ' + columnIndex + ' =' + check_value);
|
||||
|
||||
options.columns.push(columnOptions);
|
||||
}
|
||||
});
|
||||
//alert('options:' + options);
|
||||
return options;
|
||||
};
|
||||
|
@ -97,6 +97,11 @@ ExporterManager.MenuItems = [
|
||||
"label": $.i18n._('core-project')["custom-tabular"],
|
||||
"click": function() { new CustomTabularExporterDialog(); }
|
||||
},
|
||||
{
|
||||
"id" : "core/export-sql",
|
||||
"label": $.i18n._('core-project')["sql-export"],
|
||||
"click": function() { new SqlExporterDialog(); }
|
||||
},
|
||||
{
|
||||
"id" : "core/export-templating",
|
||||
"label": $.i18n._('core-project')["templating"],
|
||||
@ -142,6 +147,7 @@ ExporterManager.handlers.exportRows = function(format, ext) {
|
||||
|
||||
ExporterManager.prepareExportRowsForm = function(format, includeEngine, ext) {
|
||||
var name = $.trim(theProject.metadata.name.replace(/\W/g, ' ')).replace(/\s+/g, '-');
|
||||
//alert("name:" + name);
|
||||
var form = document.createElement("form");
|
||||
$(form)
|
||||
.css("display", "none")
|
||||
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
|
||||
Copyright 2010, Google Inc.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
@import-less url("../theme.less");
|
||||
|
||||
#sql-exporter-tabs .ui-tabs-panel {
|
||||
padding: @padding_looser;
|
||||
}
|
||||
|
||||
.sql-exporter-tabs-content-main {
|
||||
|
||||
}
|
||||
.sql-exporter-columns, .sql-exporter-column-options {
|
||||
/*border: 1px solid @chrome_primary;*/
|
||||
height: 8em;
|
||||
padding: @padding_loose;
|
||||
}
|
||||
.sql-exporter-columns {
|
||||
min-height: 200px;
|
||||
overflow: auto;
|
||||
}
|
||||
.sql-exporter-selected-column {
|
||||
font-weight: bold;
|
||||
border: 1px solid @chrome_primary;
|
||||
background: @chrome_primary;
|
||||
padding: @padding_tighter;
|
||||
.rounded_corners();
|
||||
}
|
||||
|
||||
.sql-exporter-dialog-column {
|
||||
/* border: 1px solid @chrome_primary;*/
|
||||
background: @fill_secondary;
|
||||
padding: @padding_tighter;
|
||||
margin: @padding_tight;
|
||||
cursor: move;
|
||||
.rounded_corners();
|
||||
}
|
||||
.sql-exporter-dialog-column.selected {
|
||||
background: @chrome_primary;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
textarea.sql-exporter-code {
|
||||
width: 100%;
|
||||
border: 1px solid @chrome_primary;
|
||||
height: 25em;
|
||||
}
|
||||
.sql-exporter-dialog-row {
|
||||
margin-top: 2px;
|
||||
padding-top: 2px;
|
||||
margin-bottom: 2px;
|
||||
margin-top: 2px;
|
||||
}
|
||||
tr.sql-exporter-dialog-row td {
|
||||
border:1px dashed lightblue;
|
||||
margin: 2px;
|
||||
}
|
||||
.sql-exporter-dialog-input {
|
||||
margin:2px;
|
||||
border: solid 2px lightblue;
|
||||
}
|
||||
.typeSelectClass {
|
||||
margin-left:2px;
|
||||
}
|
Loading…
Reference in New Issue
Block a user