264 lines
12 KiB
Plaintext
264 lines
12 KiB
Plaintext
<%@ jet
|
|
imports="org.talend.designer.codegen.config.CodeGeneratorArgument
|
|
org.talend.core.model.process.INode
|
|
org.talend.core.model.process.ElementParameterParser
|
|
org.talend.core.model.metadata.IMetadataTable
|
|
org.talend.core.model.process.IConnection
|
|
org.talend.core.model.metadata.MappingTypeRetriever
|
|
org.talend.core.model.metadata.MetadataTalendType
|
|
java.util.List
|
|
java.util.ArrayList
|
|
java.util.Map
|
|
java.util.HashMap
|
|
"
|
|
skeleton="../templates/db_output_bulk.skeleton"
|
|
%>
|
|
<%@ include file="@{org.talend.designer.components.localprovider}/components/templates/Log4j/DBLogUtil.javajet"%>
|
|
<%
|
|
CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
|
|
INode node = (INode)codeGenArgument.getArgument();
|
|
|
|
String cid = node.getUniqueName();
|
|
|
|
dbLog = new DBLogUtil(node);
|
|
|
|
List<Map<String, String>> addCols =
|
|
(List<Map<String,String>>)ElementParameterParser.getObjectValue(
|
|
node,"__ADD_COLS__" );
|
|
|
|
boolean useFieldOptions = ("true").equals(ElementParameterParser.getValue(node, "__USE_FIELD_OPTIONS__"));
|
|
|
|
List<Map<String, String>> fieldOptions = (List<Map<String,String>>)ElementParameterParser.getObjectValue(node, "__FIELD_OPTIONS__");
|
|
|
|
String dbname= ElementParameterParser.getValue(node, "__DBNAME__");
|
|
|
|
String useExistingConn = ElementParameterParser.getValue(node,"__USE_EXISTING_CONNECTION__");
|
|
|
|
String tableName = ElementParameterParser.getValue(node,"__TABLE__");
|
|
|
|
String dbmsId = ElementParameterParser.getValue(node,"__MAPPING__");
|
|
|
|
String dataAction = ElementParameterParser.getValue(node,"__DATA_ACTION__");
|
|
|
|
String tableAction = ElementParameterParser.getValue(node,"__TABLE_ACTION__");
|
|
|
|
String commitEvery = ElementParameterParser.getValue(node, "__COMMIT_EVERY__");
|
|
|
|
String javaDbDriver = "org.sqlite.JDBC";
|
|
|
|
boolean isEnableDebug = ("true").equals(ElementParameterParser.getValue(node,"__ENABLE_DEBUG_MODE__"));
|
|
boolean isParallelize ="true".equalsIgnoreCase(ElementParameterParser.getValue(node, "__PARALLELIZE__"));
|
|
|
|
boolean useBatch = "true".equals(ElementParameterParser.getValue(node, "__USE_BATCH__"));
|
|
String batchSize = ElementParameterParser.getValue(node, "__BATCH_SIZE__");
|
|
|
|
String rejectConnName = null;
|
|
List<? extends IConnection> rejectConns = node.getOutgoingConnections("REJECT");
|
|
if(rejectConns != null && rejectConns.size() > 0) {
|
|
IConnection rejectConn = rejectConns.get(0);
|
|
rejectConnName = rejectConn.getName();
|
|
}
|
|
|
|
useBatch = (rejectConnName == null)
|
|
&& (
|
|
("INSERT").equals(dataAction) || ("UPDATE").equals(dataAction) || ("DELETE").equals(dataAction)
|
|
)
|
|
&& useBatch;
|
|
%>
|
|
|
|
int nb_line_<%=cid%> = 0;
|
|
int nb_line_update_<%=cid%> = 0;
|
|
int nb_line_inserted_<%=cid%> = 0;
|
|
int nb_line_deleted_<%=cid%> = 0;
|
|
int nb_line_rejected_<%=cid%> = 0;
|
|
|
|
int deletedCount_<%=cid%>=0;
|
|
int updatedCount_<%=cid%>=0;
|
|
int insertedCount_<%=cid%>=0;
|
|
int rowsToCommitCount_<%=cid%>=0;
|
|
|
|
String tableName_<%=cid%> = <%=tableName%>;
|
|
boolean whetherReject_<%=cid%> = false;
|
|
|
|
<%
|
|
if (useBatch) {
|
|
%>
|
|
int batchSize_<%=cid%> = <%=batchSize%>;
|
|
int batchSizeCounter_<%=cid%>=0;
|
|
int tmp_batchUpdateCount_<%=cid%> = 0;
|
|
<%
|
|
}
|
|
%>
|
|
|
|
<%
|
|
Manager manager = getManager(dbmsId, cid, node);
|
|
|
|
List<IMetadataColumn> columnList = getColumnList(node);
|
|
List<Column> stmtStructure = null;
|
|
if(columnList != null && columnList.size() > 0) {
|
|
stmtStructure = manager.createColumnList(columnList, useFieldOptions, fieldOptions, addCols);
|
|
}
|
|
%>
|
|
|
|
<%@ include file="@{org.talend.designer.components.localprovider}/components/templates/DB/Output/CheckKeysForUpdateAndDelete.javajet"%>
|
|
|
|
java.sql.Connection conn_<%=cid%> = null;
|
|
<%
|
|
if(("true").equals(useExistingConn)) {
|
|
String connection = ElementParameterParser.getValue(node,"__CONNECTION__");
|
|
String conn = "conn_" + connection;
|
|
%>
|
|
conn_<%=cid%> = (java.sql.Connection)globalMap.get("<%=conn%>");
|
|
<%dbLog.conn().useExistConn("conn_"+cid+".getMetaData().getURL()", null);%>
|
|
<%
|
|
} else {
|
|
%>
|
|
<%dbLog.conn().logJDBCDriver(dbLog.str(javaDbDriver));%>
|
|
java.lang.Class.forName("<%=javaDbDriver %>");
|
|
String url_<%=cid %> = "jdbc:sqlite:"+ "/" + <%=dbname%>;
|
|
<%dbLog.conn().connTry(dbLog.var("url"), null);%>
|
|
conn_<%=cid%> = java.sql.DriverManager.getConnection(url_<%=cid %>);
|
|
<%dbLog.conn().connDone(dbLog.var("url"));%>
|
|
resourceMap.put("conn_<%=cid%>", conn_<%=cid%>);
|
|
<%
|
|
}
|
|
if(!("true").equals(useExistingConn)) {
|
|
if(!("").equals(commitEvery) && !("0").equals(commitEvery)) {
|
|
%>
|
|
conn_<%=cid%>.setAutoCommit(false);
|
|
int commitEvery_<%=cid%> = <%=commitEvery%>;
|
|
int commitCounter_<%=cid%> = 0;
|
|
<%
|
|
}
|
|
}
|
|
dbLog.commit().logAutoCommit("conn_"+cid+".getAutoCommit()");
|
|
%>
|
|
|
|
<%
|
|
if(columnList != null && columnList.size() > 0) {
|
|
if(!isParallelize && !("NONE").equals(tableAction)) {
|
|
if(("DROP_CREATE").equals(tableAction)) {
|
|
%>
|
|
try (java.sql.Statement stmtDrop_<%=cid%> = conn_<%=cid%>.createStatement();
|
|
java.sql.Statement stmtCreate_<%=cid%> = conn_<%=cid%>.createStatement()) {
|
|
<%dbLog.table().dropTry(dbLog.var("tableName"));%>
|
|
stmtDrop_<%=cid%>.execute("<%=manager.getDropTableSQL()%>");
|
|
<%dbLog.table().dropDone(dbLog.var("tableName"));%>
|
|
<%dbLog.table().createTry(dbLog.var("tableName"));%>
|
|
stmtCreate_<%=cid%>.execute("<%=manager.getCreateTableSQL(stmtStructure)%>)");
|
|
<%dbLog.table().createDone(dbLog.var("tableName"));%>
|
|
}
|
|
<%
|
|
} else if(("CREATE").equals(tableAction)) {
|
|
%>
|
|
try (java.sql.Statement stmtCreate_<%=cid%> = conn_<%=cid%>.createStatement()) {
|
|
<%dbLog.table().createTry(dbLog.var("tableName"));%>
|
|
stmtCreate_<%=cid%>.execute("<%=manager.getCreateTableSQL(stmtStructure)%>)");
|
|
<%dbLog.table().createDone(dbLog.var("tableName"));%>
|
|
}
|
|
<%
|
|
} else if(("CREATE_IF_NOT_EXISTS").equals(tableAction) || ("DROP_IF_EXISTS_AND_CREATE").equals(tableAction)) {
|
|
%>
|
|
java.sql.DatabaseMetaData dbMetaData_<%=cid%> = conn_<%=cid%>.getMetaData();
|
|
boolean whetherExist_<%=cid%> = false;
|
|
try (java.sql.ResultSet rsTable_<%=cid%> = dbMetaData_<%=cid%>.getTables(null, null, null, new String[]{"TABLE"})) {
|
|
while(rsTable_<%=cid%>.next()) {
|
|
String table_<%=cid%> = rsTable_<%=cid%>.getString("TABLE_NAME");
|
|
if(table_<%=cid%>.equalsIgnoreCase(<%=tableName%>)) {
|
|
whetherExist_<%=cid%> = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
<%
|
|
if(("CREATE_IF_NOT_EXISTS").equals(tableAction)) {
|
|
%>
|
|
if(!whetherExist_<%=cid%>) {
|
|
try (java.sql.Statement stmtCreate_<%=cid%> = conn_<%=cid%>.createStatement()) {
|
|
<%dbLog.table().createTry(dbLog.var("tableName"));%>
|
|
stmtCreate_<%=cid%>.execute("<%=manager.getCreateTableSQL(stmtStructure)%>)");
|
|
<%dbLog.table().createDone(dbLog.var("tableName"));%>
|
|
}
|
|
}
|
|
<%
|
|
} else {
|
|
%>
|
|
if(whetherExist_<%=cid%>) {
|
|
try (java.sql.Statement stmtDrop_<%=cid%> = conn_<%=cid%>.createStatement()) {
|
|
<%dbLog.table().dropTry(dbLog.var("tableName"));%>
|
|
stmtDrop_<%=cid%>.execute("<%=manager.getDropTableSQL()%>");
|
|
<%dbLog.table().dropDone(dbLog.var("tableName"));%>
|
|
}
|
|
}
|
|
try (java.sql.Statement stmtCreate_<%=cid%> = conn_<%=cid%>.createStatement()) {
|
|
<%dbLog.table().createTry(dbLog.var("tableName"));%>
|
|
stmtCreate_<%=cid%>.execute("<%=manager.getCreateTableSQL(stmtStructure)%>)");
|
|
<%dbLog.table().createDone(dbLog.var("tableName"));%>
|
|
}
|
|
<%
|
|
}
|
|
} else if(("CLEAR").equals(tableAction)) {
|
|
%>
|
|
try (java.sql.Statement stmtClear_<%=cid%> = conn_<%=cid%>.createStatement()) {
|
|
<%dbLog.table().clearTry(dbLog.var("tableName"));%>
|
|
deletedCount_<%=cid%> = deletedCount_<%=cid%> + stmtClear_<%=cid%>.executeUpdate("<%=manager.getDeleteTableSQL()%>");
|
|
<%dbLog.table().clearDone(dbLog.var("tableName"));%>
|
|
}
|
|
<%
|
|
}
|
|
}
|
|
Map<String, StringBuilder> actionSQLMap = manager.createProcessSQL(stmtStructure);
|
|
StringBuilder insertColName = actionSQLMap.get(INSERT_COLUMN_NAME);
|
|
StringBuilder insertValueStmt = actionSQLMap.get(INSERT_VALUE_STMT);
|
|
StringBuilder updateSetStmt = actionSQLMap.get(UPDATE_SET_STMT);
|
|
StringBuilder updateWhereStmt = actionSQLMap.get(UPDATE_WHERE_STMT);
|
|
StringBuilder deleteWhereStmt = actionSQLMap.get(DELETE_WHERE_STMT);
|
|
|
|
if(("INSERT").equals(dataAction)) {
|
|
%>
|
|
String insert_<%=cid%> = "INSERT INTO \"" + <%=tableName%> + "\" (<%=insertColName.toString()%>) VALUES (<%=insertValueStmt.toString()%>)";
|
|
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
|
|
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
|
|
<%
|
|
} else if (("UPDATE").equals(dataAction)) {
|
|
%>
|
|
String update_<%=cid%> = "UPDATE \"" + <%=tableName%> + "\" SET <%=updateSetStmt.toString()%> WHERE <%=updateWhereStmt.toString()%>";
|
|
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement(update_<%=cid%>);
|
|
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
|
|
<%
|
|
} else if (("INSERT_OR_UPDATE").equals(dataAction)) {
|
|
%>
|
|
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement("SELECT COUNT(1) FROM \"" + <%=tableName%> + "\" WHERE <%=updateWhereStmt.toString()%>");
|
|
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
|
|
String insert_<%=cid%> = "INSERT INTO \"" + <%=tableName%> + "\" (<%=insertColName.toString()%>) VALUES (<%=insertValueStmt.toString()%>)";
|
|
java.sql.PreparedStatement pstmtInsert_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
|
|
resourceMap.put("pstmtInsert_<%=cid %>", pstmtInsert_<%=cid %>);
|
|
String update_<%=cid%> = "UPDATE \"" + <%=tableName%> + "\" SET <%=updateSetStmt.toString()%> WHERE <%=updateWhereStmt.toString()%>";
|
|
java.sql.PreparedStatement pstmtUpdate_<%=cid %> = conn_<%=cid%>.prepareStatement(update_<%=cid%>);
|
|
resourceMap.put("pstmtUpdate_<%=cid %>", pstmtUpdate_<%=cid %>);
|
|
<%
|
|
} else if (("UPDATE_OR_INSERT").equals(dataAction)) {
|
|
%>
|
|
String update_<%=cid%> = "UPDATE \"" + <%=tableName%> + "\" SET <%=updateSetStmt.toString()%> WHERE <%=updateWhereStmt.toString()%>";
|
|
java.sql.PreparedStatement pstmtUpdate_<%=cid %> = conn_<%=cid%>.prepareStatement(update_<%=cid%>);
|
|
resourceMap.put("pstmtUpdate_<%=cid %>", pstmtUpdate_<%=cid %>);
|
|
String insert_<%=cid%> = "INSERT INTO \"" + <%=tableName%> + "\" (<%=insertColName.toString()%>) VALUES (<%=insertValueStmt.toString()%>)";
|
|
java.sql.PreparedStatement pstmtInsert_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
|
|
resourceMap.put("pstmtInsert_<%=cid %>", pstmtInsert_<%=cid %>);
|
|
<%
|
|
} else if (("DELETE").equals(dataAction)) {
|
|
%>
|
|
String delete_<%=cid%> = "DELETE FROM \"" + <%=tableName%> + "\" WHERE <%=deleteWhereStmt.toString()%>";
|
|
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement(delete_<%=cid%>);
|
|
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
|
|
<%
|
|
}
|
|
if(isEnableDebug) {
|
|
%>
|
|
StringBuffer query_<%=cid%> = null;
|
|
<%@ include file="../templates/DB/Output/splitSQLForAllDBInBegin.javajet" %>
|
|
<%
|
|
}
|
|
}
|
|
%>
|