Files
tdi-studio-se/main/plugins/org.talend.designer.components.localprovider/components/tSQLDWHOutput/tSQLDWHOutput_begin.javajet
Dmytro Sylaiev a9e7d7b09a feat(TDI-44188): Dsylaiev/tdi 39575 active directory for mssql (#4584)
* feat(TDI-39575): Add AD auth support for AzureSynapse

* feat(TDI-39575): Add AD Auth support for MsSQL components

* feat(TDI-39575): Provide AD feature for tCreateTable
2020-05-12 13:28:49 +03:00

522 lines
20 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.metadata.types.JavaTypesManager
org.talend.core.model.metadata.MappingTypeRetriever
org.talend.core.model.metadata.MetadataTalendType
org.talend.core.model.process.IConnection
org.talend.core.model.utils.NodeUtil
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 dbproperties = ElementParameterParser.getValue(node, "__PROPERTIES__");
String dbhost = ElementParameterParser.getValue(node, "__HOST__");
String dbport = ElementParameterParser.getValue(node, "__PORT__");
String dbschema = ElementParameterParser.getValue(node, "__DB_SCHEMA__");
String dbuser= ElementParameterParser.getValue(node, "__USER__");
String table = ElementParameterParser.getValue(node,"__TABLE__");
String dbmsId = ElementParameterParser.getValue(node,"__MAPPING__");
getManager(dbmsId, cid, node);//register the MSSQLManager
String dataAction = ElementParameterParser.getValue(node,"__DATA_ACTION__");
String tableAction = ElementParameterParser.getValue(node,"__TABLE_ACTION__");
boolean isSpecifyIdentityField = ("true").equals(ElementParameterParser.getValue(node,"__SPECIFY_IDENTITY_FIELD__"));
String identityField = ElementParameterParser.getValue(node, "__IDENTITY_FIELD__");
int startValue = Integer.parseInt(ElementParameterParser.getValue(node, "__START_VALUE__"));
int step = Integer.parseInt(ElementParameterParser.getValue(node, "__STEP__"));
String commitEvery = ElementParameterParser.getValue(node, "__COMMIT_EVERY__");
boolean identityInsert= "true".equals(ElementParameterParser.getValue(node, "__IDENTITY_INSERT__"));
boolean useActiveDirectoryAuth = "true".equals(ElementParameterParser.getValue(node, "__ACTIVE_DIR_AUTH__"));
boolean isEnableDebug = ("true").equals(ElementParameterParser.getValue(node,"__ENABLE_DEBUG_MODE__"));
boolean usingStatsLogs=cid.equals("talendLogs_DB") || cid.equals("talendStats_DB") || cid.equals("talendMeter_DB");
boolean useBatchSize = "true".equals(ElementParameterParser.getValue(node,"__USE_BATCH_SIZE__"));
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();
}
boolean useBatch = useBatchSize && (rejectConnName == null)
&& (
("INSERT").equals(dataAction) || ("UPDATE").equals(dataAction) || ("DELETE").equals(dataAction)
);
%>
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 rejectedCount_<%=cid%>=0;
String dbschema_<%=cid%> = null;
String tableName_<%=cid%> = null;
boolean whetherReject_<%=cid%> = false;
java.util.Calendar calendar_<%=cid %> = java.util.Calendar.getInstance();
long year1_<%=cid %> = TalendDate.parseDate("yyyy-MM-dd","0001-01-01").getTime();
long year2_<%=cid %> = TalendDate.parseDate("yyyy-MM-dd","1753-01-01").getTime();
long year10000_<%=cid %> = TalendDate.parseDate("yyyy-MM-dd HH:mm:ss","9999-12-31 24:00:00").getTime();
long date_<%=cid %>;
java.util.Calendar calendar_datetimeoffset_<%=cid%> = java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"));
<%
List<IMetadataColumn> columnList = getColumnList(node);
List<Column> stmtStructure = null;
Manager manager = null;
boolean isDynamic = false;
List<IMetadataTable> metadatas = node.getMetadataList();
if ((metadatas!=null)&&(metadatas.size()>0)) {
IMetadataTable metadata = metadatas.get(0);
isDynamic = metadata.isDynamicSchema();
}
if(columnList != null && columnList.size() > 0) {
boolean isParallelize ="true".equalsIgnoreCase(ElementParameterParser.getValue(node, "__PARALLELIZE__"));
if(isParallelize){
stmtStructure = getManager(dbmsId, cid).createColumnList(columnList, useFieldOptions, fieldOptions, addCols);
}else{
stmtStructure = getManager(dbmsId, cid).createColumnList(columnList, useFieldOptions, fieldOptions, addCols, isSpecifyIdentityField, identityField, startValue, step);
}
isDynamic = isDynamic && !getManager(dbmsId, cid).isDynamicColumnReplaced();
}
boolean isUseParallelize ="true".equalsIgnoreCase(ElementParameterParser.getValue(node, "__PARALLELIZE__"));
boolean isSpecifyIdentityKey = "true".equals(ElementParameterParser.getValue(node, "__SPECIFY_IDENTITY_FIELD__"));
if(isDynamic || isUseParallelize || identityInsert || (!"DROP_CREATE".equals(tableAction) && !"CREATE".equals(tableAction) && !"DROP_IF_EXISTS_AND_CREATE".equals(tableAction))){
isSpecifyIdentityKey = false;
}
String identityKey = ElementParameterParser.getValue(node, "__IDENTITY_FIELD__");
getManager(dbmsId, cid, node).setIsSpecifyIdentityKey(isSpecifyIdentityKey);
%>
<%
if(("UPDATE").equals(dataAction) || ("INSERT_OR_UPDATE").equals(dataAction) || ("UPDATE_OR_INSERT").equals(dataAction)) {
int updateKeyCount = 0;
if(stmtStructure != null) {
for(Column column : stmtStructure) {
if(column.isUpdateKey() && !column.isDynamic()) {
updateKeyCount++;
}
}
%>
int updateKeyCount_<%=cid%> = <%=updateKeyCount%>;
if(updateKeyCount_<%=cid%> < 1) {
throw new RuntimeException("For update, Schema must have a key");
}
<%
}
} else if(("DELETE").equals(dataAction)) {
int deleteKeyCount = 0;
if(stmtStructure != null) {
for(Column column : stmtStructure) {
if(column.isDeleteKey() && !column.isDynamic()) {
deleteKeyCount++;
}
}
%>
int deleteKeyCount_<%=cid%> = <%=deleteKeyCount%>;
if(deleteKeyCount_<%=cid%> < 1) {
throw new RuntimeException("For delete, Schema must have a key");
}
<%
}
} else if (("INSERT_IF_NOT_EXIST").equals(dataAction)){
int insertKeyCount = 0;
if(stmtStructure != null) {
for(Column column : stmtStructure) {
if(column.isUpdateKey() && !column.isDynamic()) {
insertKeyCount++;
}
}
%>
int insertKeyCount_<%=cid%> = <%=insertKeyCount%>;
if(insertKeyCount_<%=cid%> < 1) {
throw new RuntimeException("For insert if not exist, Schema must have a key");
}
<%
}
}
// From "../templates/tMSSql/_tMSSqlConnection.javajet" ******start******
%>
java.sql.Connection conn_<%=cid%> = null;
<%
boolean useExistingConnection = "true".equals(ElementParameterParser.getValue(node,"__USE_EXISTING_CONNECTION__"));
%>
String dbUser_<%=cid %> = null;
<%
if(useExistingConnection) {
String connection = ElementParameterParser.getValue(node,"__CONNECTION__");
String conn = "conn_" + connection;
String schema = "dbschema_" + connection;
%>
dbschema_<%=cid%> = (String)globalMap.get("<%=schema%>");
conn_<%=cid%> = (java.sql.Connection)globalMap.get("<%=conn%>");
<%dbLog.conn().useExistConn("conn_"+cid+".getMetaData().getURL()", "conn_"+cid+".getMetaData().getUserName()");%>
<%
} else {
boolean specify_alias = "true".equals(ElementParameterParser.getValue(node, "__SPECIFY_DATASOURCE_ALIAS__"));
String driver = ElementParameterParser.getValue(node, "__DRIVER__");
if(specify_alias){
String alias = ElementParameterParser.getValue(node, "__DATASOURCE_ALIAS__");
%>
java.util.Map<String, routines.system.TalendDataSource> dataSources_<%=cid%> = (java.util.Map<String, routines.system.TalendDataSource>) globalMap.get(KEY_DB_DATASOURCES);
if (null != dataSources_<%=cid%>) {
String dsAlias_<%=cid%> = <%=(null != alias && !("".equals(alias)))?alias:"\"\""%>;
if (dataSources_<%=cid%>.get(dsAlias_<%=cid%>) == null) {
throw new RuntimeException("No DataSource with alias: " + dsAlias_<%=cid%> + " available!");
}
conn_<%=cid%> = dataSources_<%=cid%>.get(dsAlias_<%=cid%>).getConnection();
} else {
<%
}
%>
dbschema_<%=cid%> = <%=dbschema%>;
<%
if(driver.equals("JTDS")) {
%>
String driverClass_<%=cid%> = "net.sourceforge.jtds.jdbc.Driver";
<%
} else {
%>
String driverClass_<%=cid%> = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
<%
}
%>
<%dbLog.conn().logJDBCDriver(dbLog.var("driverClass"));%>
java.lang.Class.forName(driverClass_<%=cid%>);
String port_<%=cid%> = <%=dbport%>;
String dbname_<%=cid%> = <%=dbname%> ;
<%
if(driver.equals("JTDS")) {
%>
String url_<%=cid %> = "jdbc:jtds:sqlserver://" + <%=dbhost%> ;
<%
} else {
%>
String url_<%=cid %> = "jdbc:sqlserver://" + <%=dbhost%> ;
<%
}
%>
if (!"".equals(port_<%=cid%>)) {
url_<%=cid %> += ":" + <%=dbport%>;
}
if (!"".equals(dbname_<%=cid%>)) {
<%
if(driver.equals("JTDS")) {
%>
url_<%=cid%> += "//" + <%=dbname%>;
<%
} else {
%>
url_<%=cid%> += ";databaseName=" + <%=dbname%>;
<%
}
%>
}
<%
if (useActiveDirectoryAuth) {
%>
url_<%=cid%> += ";encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;Authentication=ActiveDirectoryPassword";
url_<%=cid%> += ";database=" + <%=dbname%>;
<%
}
%>
url_<%=cid%> += ";applicationName=Talend;" + <%=dbproperties%>;
dbUser_<%=cid %> = <%=dbuser%>;
<%
String passwordFieldName = "__PASS__";
%>
<%@ include file="@{org.talend.designer.components.localprovider}/components/templates/password.javajet"%>
String dbPwd_<%=cid %> = decryptedPassword_<%=cid%>;
<%dbLog.conn().connTry(dbLog.var("url"), dbLog.var("dbUser"));%>
conn_<%=cid%> = java.sql.DriverManager.getConnection(url_<%=cid %>,dbUser_<%=cid%>,dbPwd_<%=cid%>);
<%dbLog.conn().connDone(dbLog.var("url"));%>
<%
if(specify_alias){
%>
}
<%
}
}
// From "../templates/tMSSql/_tMSSqlConnection.javajet" ******end******
dbLog.commit().logAutoCommit("conn_"+cid+".getAutoCommit()");
if (useBatch) {
%>
int batchSize_<%=cid%> = <%=batchSize%>;
int batchSizeCounter_<%=cid%>=0;
<%
}
%>
if(dbschema_<%=cid%> == null || dbschema_<%=cid%>.trim().length() == 0) {
tableName_<%=cid%> = <%=table%>;
} else {
tableName_<%=cid%> = dbschema_<%=cid%> + "].[" + <%=table%>;
}
<%
if(columnList != null && columnList.size()>0) {
if(!isDynamic) {
%>
int count_<%=cid%>=0;
<%@ include file="../templates/_tableActionForOutput.javajet"%>
<%
if(!useExistingConnection) {
%>
resourceMap.put("conn_<%=cid%>", conn_<%=cid%>);
<%
if(!("").equals(commitEvery) && !("0").equals(commitEvery)) {
%>
conn_<%=cid%>.setAutoCommit(false);
int commitEvery_<%=cid%> = <%=commitEvery%>;
int commitCounter_<%=cid%> = 0;
<%
}
}
Map<String, StringBuilder> actionSQLMap = getManager(dbmsId, cid).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_<%=cid%> + "] (<%=insertColName.toString()%>) VALUES (<%=insertValueStmt.toString()%>)";
<%
//to fixed: bug8422
if(!usingStatsLogs){%>
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
<%}%>
<%
//to fixed: bug8422
//to fixed: bug18928,in bug 8422 if a statement is created,it will be used for many times,but it's not thread-safty.Now give one statement for each thread.
if(usingStatsLogs){ %>
java.sql.PreparedStatement pstmt_<%=cid %> = null;
String keyPsmt_<%=cid %> = conn_<%=cid%> + "[psmt]" + "[" + <%=table%> + "]";
pstmt_<%=cid %> = SharedDBPreparedStatement.getSharedPreparedStatement(conn_<%=cid%>,insert_<%=cid%>,keyPsmt_<%=cid%>);
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
<%}%>
<%
} else if (("UPDATE").equals(dataAction)) {
%>
String update_<%=cid%> = "UPDATE [" + tableName_<%=cid%> + "] 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_<%=cid%> + "] WHERE <%=updateWhereStmt.toString()%>");
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
String insert_<%=cid%> = "INSERT INTO [" + tableName_<%=cid%> + "] (<%=insertColName.toString()%>) VALUES (<%=insertValueStmt.toString()%>)";
java.sql.PreparedStatement pstmtInsert_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
resourceMap.put("insert_<%=cid %>", insert_<%=cid %>);
String update_<%=cid%> = "UPDATE [" + tableName_<%=cid%> + "] 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_<%=cid%> + "] 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_<%=cid%> + "] (<%=insertColName.toString()%>) VALUES (<%=insertValueStmt.toString()%>)";
java.sql.PreparedStatement pstmtInsert_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
resourceMap.put("insert_<%=cid %>", insert_<%=cid %>);
<%
} else if (("DELETE").equals(dataAction)) {
%>
String delete_<%=cid%> = "DELETE FROM [" + tableName_<%=cid%> + "] WHERE <%=deleteWhereStmt.toString()%>";
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement(delete_<%=cid%>);
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
<%
} else if (("INSERT_IF_NOT_EXIST").equals(dataAction)) {
%>
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement("SELECT COUNT(1) FROM [" + tableName_<%=cid%> + "] WHERE <%=updateWhereStmt.toString()%>");
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
String insert_<%=cid%> = "INSERT INTO [" + tableName_<%=cid%> + "] (<%=insertColName.toString()%>) VALUES (<%=insertValueStmt.toString()%>)";
java.sql.PreparedStatement pstmtInsert_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
resourceMap.put("insert_<%=cid %>", insert_<%=cid %>);
<%
}else if("SINGLE_INSERT".equals(dataAction)){
%>
class BufferLine_<%=cid%> {
<%
for(Column column : stmtStructure) {
if(!column.isReplaced() && !column.isAddCol() && column.isInsertable()) {
if(isSpecifyIdentityKey && (column.getName().equals(identityKey))) {
continue;
}
String typeToGenerate = JavaTypesManager.getTypeToGenerate(column.getColumn().getTalendType(), column.getColumn().isNullable());
%>
<%=typeToGenerate%> <%=column.getName()%>;
<%
}
}
%>
public BufferLine_<%=cid%>(
<%
int count = 0;
for(Column column : stmtStructure) {
if(!column.isReplaced() && !column.isAddCol() && column.isInsertable()) {
if(isSpecifyIdentityKey && (column.getName().equals(identityKey))) {
continue;
}
if(count != 0) {
%>
,
<%
}
String typeToGenerate = JavaTypesManager.getTypeToGenerate(column.getColumn().getTalendType(), column.getColumn().isNullable());
%>
<%=typeToGenerate%> <%=column.getName()%>
<%
count++;
}
}
%>
){
<%
for(Column column : stmtStructure) {
if(!column.isReplaced() && !column.isAddCol() && column.isInsertable()) {
if(isSpecifyIdentityKey && (column.getName().equals(identityKey))) {
continue;
}
%>
this.<%=column.getName()%> = <%=column.getName()%>;
<%
}
}
%>
}
}
java.util.List<BufferLine_<%=cid%>> sInsertColValueList<%=cid%> = new java.util.ArrayList();
BufferLine_<%=cid%> sInsertColValue<%=cid%> = null;
StringBuilder extendInsertValueStmt_<%=cid%> = new StringBuilder();
for(int i=0;i < <%=batchSize%>;i++){
extendInsertValueStmt_<%=cid%>.append("(<%=insertValueStmt.toString()%>)");
if (i!=<%=batchSize%>-1) extendInsertValueStmt_<%=cid%>.append(",");
}
String insert_<%=cid%> = "INSERT INTO "+<%=table%>+" (<%=insertColName.toString()%>) VALUES " + extendInsertValueStmt_<%=cid%>.toString();
java.sql.PreparedStatement pstmt_<%=cid %> = conn_<%=cid%>.prepareStatement(insert_<%=cid%>);
resourceMap.put("pstmt_<%=cid %>", pstmt_<%=cid %>);
String insertColName<%=cid%> = "<%=insertColName.toString()%>";
String insertColValue<%=cid%> = "<%=insertValueStmt.toString()%>";
int rowCount<%=cid%> = 0;
<%}
if(isEnableDebug) {
%>
StringBuffer query_<%=cid%> = null;
<%@ include file="../templates/DB/Output/splitSQLForAllDBInBegin.javajet" %>
<%
}
} //end isDynamic
if (useExistingConnection) {
String connection = ElementParameterParser.getValue(node,"__CONNECTION__");
%>
boolean isShareIdentity_<%=cid%> = globalMap.get("shareIdentitySetting_<%=connection%>") != null && (Boolean)globalMap.get("shareIdentitySetting_<%=connection%>") == true;
<%
}
if(identityInsert ) {
%>
java.sql.Statement stmt_<%=cid %> = conn_<%=cid%>.createStatement();
<%
if (useExistingConnection) {
String connection = ElementParameterParser.getValue(node,"__CONNECTION__");
%>
if ( !isShareIdentity_<%=cid%> ) {
stmt_<%=cid %>.execute("SET IDENTITY_INSERT ["+ tableName_<%=cid%> +"] ON");
}
<%
} else {
%>
stmt_<%=cid %>.execute("SET IDENTITY_INSERT ["+ tableName_<%=cid%> +"] ON");
<%
}
}
}
if(isDynamic) {
%>
java.sql.PreparedStatement pstmt_<%=cid %> =null;
java.sql.PreparedStatement pstmtInsert_<%=cid %> =null;
java.sql.PreparedStatement pstmtUpdate_<%=cid %> =null;
<%if(isEnableDebug) {%>
StringBuffer query_<%=cid%> = null;
<%@ include file="../templates/DB/Output/splitSQLForAllDBInDynamicBegin.javajet" %>
<%
}
}
%>