Skip to content

Commit

Permalink
0003146: Wildcards for schema and table on Oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Jun 8, 2017
1 parent cece499 commit a7bf44e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Expand Up @@ -48,7 +48,7 @@ public class OracleSymmetricDialect extends AbstractSymmetricDialect implements

static final String ORACLE_OBJECT_TYPE = "FUNCTION";

static final String SQL_SELECT_TRIGGERS = "from ALL_TRIGGERS where owner in (SELECT sys_context('USERENV', 'CURRENT_SCHEMA') FROM dual) and trigger_name like upper(?) and table_name like upper(?)";
static final String SQL_SELECT_TRIGGERS = "from ALL_TRIGGERS where owner = sys_context('USERENV', 'CURRENT_SCHEMA') and trigger_name = upper(?) and table_name = upper(?) and table_owner = upper(?)";

static final String SQL_SELECT_TRANSACTIONS = "select min(start_time) from gv$transaction where status = 'ACTIVE'";

Expand Down Expand Up @@ -81,7 +81,7 @@ protected void buildSqlReplacementTokens() {
protected boolean doesTriggerExistOnPlatform(String catalog, String schema, String tableName,
String triggerName) {
return platform.getSqlTemplate().queryForInt("select count(*) " + SQL_SELECT_TRIGGERS,
new Object[] { triggerName, tableName }) > 0;
new Object[] { triggerName, tableName, schema }) > 0;
}

@Override
Expand All @@ -105,7 +105,7 @@ public void createTrigger(StringBuilder sqlBuffer, DataEventType dml, Trigger tr
platform.getSqlTemplate().queryForMap(
"select * " + SQL_SELECT_TRIGGERS,
new Object[] { history.getTriggerNameForDmlType(dml),
history.getSourceTableName() }));
history.getSourceTableName(), history.getSourceSchemaName() }));
} catch (SqlException e) {
}
}
Expand Down
Expand Up @@ -725,20 +725,29 @@ protected boolean matches(String match, String target, boolean ignoreCase) {

public boolean matches(Table table, String defaultCatalog, String defaultSchema,
boolean ignoreCase) {
boolean schemaAndCatalogMatch = (StringUtils.equals(sourceCatalogName, table.getCatalog()) || (StringUtils
.isBlank(sourceCatalogName) && StringUtils.equals(defaultCatalog,
table.getCatalog())))
&& (StringUtils.equals(sourceSchemaName, table.getSchema()) || (StringUtils
.isBlank(sourceSchemaName) && StringUtils.equals(defaultSchema,
table.getSchema())));
boolean catalogMatch = false;
if (isSourceCatalogNameWildCarded()) {
catalogMatch = matches(sourceCatalogName, table.getCatalog(), ignoreCase);
} else {
catalogMatch = (StringUtils.equals(sourceCatalogName, table.getCatalog()) ||
(StringUtils.isBlank(sourceCatalogName) && StringUtils.equals(defaultCatalog, table.getCatalog())));
}

boolean schemaMatch = false;
if (isSourceSchemaNameWildCarded()) {
schemaMatch = matches(sourceSchemaName, table.getSchema(), ignoreCase);
} else {
schemaMatch = (StringUtils.equals(sourceSchemaName, table.getSchema()) ||
(StringUtils.isBlank(sourceSchemaName) && StringUtils.equals(defaultSchema, table.getSchema())));
}

boolean tableMatches = ignoreCase ? table.getName().equalsIgnoreCase(sourceTableName)
: table.getName().equals(sourceTableName);

if (!tableMatches && isSourceTableNameWildCarded()) {
tableMatches = matches(sourceTableName, table.getName(), ignoreCase);
}
return schemaAndCatalogMatch && tableMatches;
return catalogMatch && schemaMatch && tableMatches;
}

public boolean matches(Trigger trigger) {
Expand Down
Expand Up @@ -316,11 +316,13 @@ public Map<Long, TriggerHistory> getHistoryRecords() {
return retMap;
}

protected boolean isTriggerNameInUse(List<TriggerHistory> activeTriggerHistories, String triggerId, String triggerName) {
protected boolean isTriggerNameInUse(List<TriggerHistory> activeTriggerHistories, Trigger trigger, String triggerName,
TriggerHistory oldhist) {
synchronized (activeTriggerHistories) {
for (TriggerHistory triggerHistory : activeTriggerHistories) {
if (!triggerHistory.getTriggerId().equals(triggerId) && (
(triggerHistory.getNameForDeleteTrigger() != null && triggerHistory.getNameForDeleteTrigger().equals(triggerName)) ||
if ((!triggerHistory.getTriggerId().equals(trigger.getTriggerId()) ||
((trigger.isSourceCatalogNameWildCarded() || trigger.isSourceSchemaNameWildCarded()) && (oldhist == null || triggerHistory.getTriggerHistoryId() != oldhist.getTriggerHistoryId()))) &&
((triggerHistory.getNameForDeleteTrigger() != null && triggerHistory.getNameForDeleteTrigger().equals(triggerName)) ||
(triggerHistory.getNameForInsertTrigger() != null && triggerHistory.getNameForInsertTrigger().equals(triggerName)) ||
(triggerHistory.getNameForUpdateTrigger() != null && triggerHistory.getNameForUpdateTrigger().equals(triggerName)))) {
return true;
Expand Down Expand Up @@ -1737,17 +1739,17 @@ protected TriggerHistory rebuildTriggerIfNecessary(StringBuilder sqlBuffer,

if (trigger.isSyncOnInsert()) {
newTriggerHist.setNameForInsertTrigger(getTriggerName(DataEventType.INSERT,
maxTriggerNameLength, trigger, table, activeTriggerHistories).toUpperCase());
maxTriggerNameLength, trigger, table, activeTriggerHistories, oldhist).toUpperCase());
}

if (trigger.isSyncOnUpdate()) {
newTriggerHist.setNameForUpdateTrigger(getTriggerName(DataEventType.UPDATE,
maxTriggerNameLength, trigger, table, activeTriggerHistories).toUpperCase());
maxTriggerNameLength, trigger, table, activeTriggerHistories, oldhist).toUpperCase());
}

if (trigger.isSyncOnDelete()) {
newTriggerHist.setNameForDeleteTrigger(getTriggerName(DataEventType.DELETE,
maxTriggerNameLength, trigger, table, activeTriggerHistories).toUpperCase());
maxTriggerNameLength, trigger, table, activeTriggerHistories, oldhist).toUpperCase());
}

String oldTriggerName = null;
Expand Down Expand Up @@ -1831,7 +1833,7 @@ protected static String replaceCharsToShortenName(String triggerName) {
}

protected String getTriggerName(DataEventType dml, int maxTriggerNameLength, Trigger trigger,
Table table, List<TriggerHistory> activeTriggerHistories) {
Table table, List<TriggerHistory> activeTriggerHistories, TriggerHistory oldhist) {

String triggerName = null;
switch (dml) {
Expand Down Expand Up @@ -1887,7 +1889,7 @@ protected String getTriggerName(DataEventType dml, int maxTriggerNameLength, Tri
}

int duplicateCount = 0;
while (isTriggerNameInUse(activeTriggerHistories, trigger.getTriggerId(), triggerName)) {
while (isTriggerNameInUse(activeTriggerHistories, trigger, triggerName, oldhist)) {
duplicateCount++;
String duplicateSuffix = Integer.toString(duplicateCount);
if (triggerName.length() + duplicateSuffix.length() > maxTriggerNameLength) {
Expand Down

0 comments on commit a7bf44e

Please sign in to comment.