ok it sets value to null, but it doesn't remove it from insert query, so on sql server identity it doesn't work.
public class IdentityListener extends DefaultExecuteListener {
private static final long serialVersionUID = 1L;
public static final String PREPEND_IDENTITY_INSERT="prepend-identity-insert";
public static final String IDENTITY_TABLE_LIST="identityTableList";
/**
* Check if PREPREND mode is enable, if is an insert, and if is an identity table
* If all conditions match enable IDENTITY_INSERT for this transaction
* @param ctx
*/
private void enableIdentityKeySetting(ExecuteContext ctx){
if (!ctx.configuration().family().equals(SQLDialect.SQLSERVER))
return;
Query q=ctx.query();
if (ctx.configuration().data(PREPEND_IDENTITY_INSERT) != null
&& (boolean)ctx.configuration().data(PREPEND_IDENTITY_INSERT)
&& q instanceof Insert) {
@SuppressWarnings("unchecked")
List<String> identityTableList=(List<String>)ctx.configuration().data(IDENTITY_TABLE_LIST);
String fulltableName=new TableNameParser(ctx.sql()).tables().iterator().next();
Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(fulltableName);
while (m.find()) {
fulltableName= m.group(m.groupCount());
}
final String tableName=fulltableName;
// .replace("dbo","").replaceAll("\\[", "").replaceAll("\\]","").replaceAll("\\.", "")
if (identityTableList.stream().map(String::toLowerCase).anyMatch(x->x.equals(tableName))){
ctx.sql("SET IDENTITY_INSERT " + tableName + " ON " + ctx.sql());
}
}
}
@Override
public void renderEnd(ExecuteContext ctx) {
enableIdentityKeySetting(ctx);
}
}