Skip to content

Commit

Permalink
First pass at Symmetric Logger with internationalization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanes committed Aug 15, 2009
1 parent 029974e commit 69afb57
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 60 deletions.
Expand Up @@ -27,6 +27,8 @@
import org.apache.commons.lang.StringUtils;
import org.hsqldb.Token;
import org.hsqldb.types.Binary;
import org.jumpmind.symmetric.common.logging.Log;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.model.Data;
import org.jumpmind.symmetric.model.DataEventType;

Expand Down Expand Up @@ -93,9 +95,7 @@ private void init(int type, String triggerName, String tableName) {
if (initialize(getDataEventType(type), tableName)) {
buildDataSelectSql();
buildTransactionIdSql();
if (logger.isDebugEnabled()) {
logger.debug("TriggerInitializing", triggerName, triggerType);
}
logger.debug("TriggerInitializing", triggerName, triggerType);
initialized = true;
}
}
Expand Down
Expand Up @@ -29,11 +29,11 @@

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.Table;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.logging.Log;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
Expand Down Expand Up @@ -116,10 +116,10 @@ public boolean isAutoRegister() {

};
}

@Override
public void removeTrigger(StringBuilder sqlBuffer, final String catalogName, String schemaName, final String triggerName,
String tableName, TriggerHistory oldHistory) {
public void removeTrigger(StringBuilder sqlBuffer, final String catalogName, String schemaName,
final String triggerName, String tableName, TriggerHistory oldHistory) {
schemaName = schemaName == null ? "" : (schemaName + ".");
final String sql = "drop trigger " + schemaName + triggerName;
logSql(sql, sqlBuffer);
Expand All @@ -134,24 +134,23 @@ public Object doInConnection(Connection con) throws SQLException, DataAccessExce
}
stmt = con.createStatement();
stmt.execute(sql);
}
catch (Exception e) {
logger.warn("Could not drop trigger " + triggerName);
}
finally {
} catch (Exception e) {
logger.warn("TriggerDropError", triggerName);
} finally {
if (catalogName != null) {
con.setCatalog(previousCatalog);
}
try {
stmt.close();
} catch (Exception e) { }
} catch (Exception e) {
}
}
return Boolean.FALSE;
}
});
}
}

@Override
protected String switchCatalogForTriggerInstall(String catalog, Connection c) throws SQLException {
if (catalog != null) {
Expand All @@ -161,7 +160,7 @@ protected String switchCatalogForTriggerInstall(String catalog, Connection c) th
} else {
return null;
}
}
}

@Override
public void prepareTableForDataLoad(Table table) {
Expand All @@ -183,18 +182,20 @@ public BinaryEncoding getBinaryEncoding() {
}

@Override
protected boolean doesTriggerExistOnPlatform(final String catalogName, String schema, String tableName, final String triggerName) {
protected boolean doesTriggerExistOnPlatform(final String catalogName, String schema, String tableName,
final String triggerName) {
return (Boolean) jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection con) throws SQLException, DataAccessException {
String previousCatalog = con.getCatalog();
PreparedStatement stmt = con.prepareStatement("select count(*) from sysobjects where type = 'TR' AND name = ?");
PreparedStatement stmt = con
.prepareStatement("select count(*) from sysobjects where type = 'TR' AND name = ?");
try {
if (catalogName != null) {
con.setCatalog(catalogName);
}
stmt.setString(1, triggerName);
ResultSet rs = stmt.executeQuery();
if (rs.next()){
if (rs.next()) {
int count = rs.getInt(1);
return count > 0;
}
Expand All @@ -213,8 +214,8 @@ public void disableSyncTriggers(String nodeId) {
if (nodeId == null) {
nodeId = "";
}
jdbcTemplate.update("DECLARE @CI VarBinary(128);" + "SET @CI=cast ('1" + nodeId
+ "' as varbinary(128));" + "SET context_info @CI;");
jdbcTemplate.update("DECLARE @CI VarBinary(128);" + "SET @CI=cast ('1" + nodeId + "' as varbinary(128));"
+ "SET context_info @CI;");
}

public void enableSyncTriggers() {
Expand Down
Expand Up @@ -24,11 +24,11 @@
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ddlutils.model.Table;
import org.jumpmind.symmetric.Version;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.logging.Log;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
Expand All @@ -42,25 +42,24 @@ public class MySqlDbDialect extends AbstractDbDialect implements IDbDialect {
static final String TRANSACTION_ID_FUNCTION_NAME = "fn_transaction_id";

static final String SYNC_TRIGGERS_DISABLED_USER_VARIABLE = "@sync_triggers_disabled";

static final String SYNC_TRIGGERS_DISABLED_NODE_VARIABLE = "@sync_node_disabled";

private boolean supportsTransactionId = false;

@Override
protected void initForSpecificDialect() {
int[] versions = Version.parseVersion(getProductVersion());
if (getMajorVersion() == 5
&& (getMinorVersion() == 0 || (getMinorVersion() == 1 && versions[2] < 23))) {
logger.info("Enabling transaction ID support");
if (getMajorVersion() == 5 && (getMinorVersion() == 0 || (getMinorVersion() == 1 && versions[2] < 23))) {
logger.info("TransactionIDSupportEnabling");
supportsTransactionId = true;
}
}

@Override
public Table findTable(String catalogName, String schemaName, String tblName)
throws Exception {
catalogName = StringUtils.isBlank(catalogName) ? StringUtils.isBlank(schemaName) ? getDefaultCatalog() :schemaName : catalogName;
public Table findTable(String catalogName, String schemaName, String tblName) throws Exception {
catalogName = StringUtils.isBlank(catalogName) ? StringUtils.isBlank(schemaName) ? getDefaultCatalog()
: schemaName : catalogName;
return super.findTable(catalogName, schemaName, tblName);
}

Expand All @@ -69,10 +68,10 @@ protected void createRequiredFunctions() {
String[] functions = sqlTemplate.getFunctionsToInstall();
for (String funcKey : functions) {
String funcName = tablePrefix + "_" + funcKey;
if (! funcName.equals("fn_transaction_id") || supportsTransactionId) {
if (!funcName.equals("fn_transaction_id") || supportsTransactionId) {
if (jdbcTemplate.queryForInt(sqlTemplate.getFunctionInstalledSql(funcKey, defaultSchema)) == 0) {
jdbcTemplate.update(sqlTemplate.getFunctionSql(funcKey, funcName, defaultSchema));
logger.info("Just installed " + funcName);
logger.info("FunctionInstalled", funcName);
}
}
}
Expand All @@ -98,7 +97,7 @@ public void removeTrigger(StringBuilder sqlBuffer, String catalogName, String sc
try {
jdbcTemplate.update(sql);
} catch (Exception e) {
logger.warn("Trigger does not exist");
logger.warn("TriggerDoesNotExist");
}
}
}
Expand Down Expand Up @@ -185,10 +184,9 @@ public int getStreamingResultsFetchSize() {
public BinaryEncoding getBinaryEncoding() {
return BinaryEncoding.HEX;
}

@Override
public String getIdentifierQuoteString()
{
public String getIdentifierQuoteString() {
return "`";
}

Expand Down
Expand Up @@ -24,9 +24,9 @@
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ddlutils.model.Table;
import org.jumpmind.symmetric.common.logging.Log;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
Expand Down Expand Up @@ -71,9 +71,9 @@ public void initTrigger(StringBuilder sqlBuffer, DataEventType dml, Trigger trig
if (ex.getSQLException().getErrorCode() == 4095) {
try {
// a trigger of the same name must already exist on a table
logger.warn("A trigger already exists for that name. Details are as follows: "
+ jdbcTemplate.queryForMap("select * from user_triggers where trigger_name like upper(?)",
new Object[] { hist.getTriggerNameForDmlType(dml) }));
logger.warn("TriggerAlreadyExists", jdbcTemplate.queryForMap(
"select * from user_triggers where trigger_name like upper(?)", new Object[] { hist
.getTriggerNameForDmlType(dml) }));
} catch (DataAccessException e) {
}
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public boolean storesUpperCaseNamesInCatalog() {
public void purge() {
jdbcTemplate.update("purge recyclebin");
}

protected String getSymmetricPackageName() {
return tablePrefix + "_pkg";
}
Expand Down
Expand Up @@ -24,9 +24,9 @@
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.logging.Log;
import org.jumpmind.symmetric.common.logging.LogFactory;
import org.jumpmind.symmetric.db.AbstractDbDialect;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
Expand All @@ -42,7 +42,7 @@ public class PostgreSqlDbDialect extends AbstractDbDialect implements IDbDialect
static final String TRANSACTION_ID_EXPRESSION = "txid_current()";

static final String SYNC_TRIGGERS_DISABLED_VARIABLE = "symmetric.triggers_disabled";

static final String SYNC_NODE_DISABLED_VARIABLE = "symmetric.node_disabled";

private boolean supportsTransactionId = false;
Expand All @@ -52,17 +52,17 @@ public class PostgreSqlDbDialect extends AbstractDbDialect implements IDbDialect
@Override
protected void initForSpecificDialect() {
if (getMajorVersion() >= 8 && getMinorVersion() >= 3) {
logger.info("Enabling transaction ID support");
logger.info("TransactionIDSupportEnabling");
supportsTransactionId = true;
transactionIdExpression = TRANSACTION_ID_EXPRESSION;
}
try {
enableSyncTriggers();
} catch (Exception e) {
logger.error("Please add \"custom_variable_classes = 'symmetric'\" to your postgresql.conf file");
throw new RuntimeException("Missing custom variable class 'symmetric'", e);
logger.error("PostgreSqlCustomVariableMissing");
throw new RuntimeException("PostgreSqlCustomVariableMissing", e);
}

}

@SuppressWarnings("unchecked")
Expand All @@ -71,18 +71,16 @@ protected Integer overrideJdbcTypeForColumn(Map values) {
String typeName = (String) values.get("TYPE_NAME");
if (typeName != null && typeName.equalsIgnoreCase("ABSTIME")) {
return Types.TIMESTAMP;
}
else {
} else {
return super.overrideJdbcTypeForColumn(values);
}
}

@Override
protected boolean doesTriggerExistOnPlatform(String catalogName, String schema, String tableName, String triggerName) {
return jdbcTemplate.queryForInt(
"select count(*) from information_schema.triggers where trigger_name = ? " +
"and event_object_table = ? and trigger_schema = ?",
new Object[] { triggerName.toLowerCase(), tableName.toLowerCase(), schema == null ? defaultSchema : schema }) > 0;
return jdbcTemplate.queryForInt("select count(*) from information_schema.triggers where trigger_name = ? "
+ "and event_object_table = ? and trigger_schema = ?", new Object[] { triggerName.toLowerCase(),
tableName.toLowerCase(), schema == null ? defaultSchema : schema }) > 0;
}

@Override
Expand All @@ -98,7 +96,7 @@ public void removeTrigger(StringBuilder sqlBuffer, String catalogName, String sc
jdbcTemplate.update(dropSql);
jdbcTemplate.update(dropFunction);
} catch (Exception e) {
logger.warn("Trigger does not exist");
logger.warn("TriggerDoesNotExist");
}
}
}
Expand All @@ -114,8 +112,9 @@ public void disableSyncTriggers(String nodeId) {
public void enableSyncTriggers() {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus transactionstatus) {
if (! transactionstatus.isRollbackOnly()) {
jdbcTemplate.queryForList("select set_config('" + SYNC_TRIGGERS_DISABLED_VARIABLE + "', '', false)");
if (!transactionstatus.isRollbackOnly()) {
jdbcTemplate
.queryForList("select set_config('" + SYNC_TRIGGERS_DISABLED_VARIABLE + "', '', false)");
jdbcTemplate.queryForList("select set_config('" + SYNC_NODE_DISABLED_VARIABLE + "', '', false)");
}
}
Expand Down
2 changes: 1 addition & 1 deletion symmetric/src/main/resources/logMessages_en_US.properties
Expand Up @@ -8,13 +8,13 @@ EnvironmentVariablesCreating=Creating environment variables %s and %s
FirebirdSymUdfMissing=Please install the sym_udf.so/dll to your {firebird_home}/UDF folder
FirebirdSymEscapeMissing=Function SYM_ESCAPE is not installed
FunctionInstalled=Just installed %s
MySqlTransactionIDSupportEnabling=Enabling transaction ID support
PostgreSqlCustomVariableMissing=Please add "custom_variable_classes = 'symmetric'" to your postgresql.conf file
PostTriggerCreateFailed=Failed to create post trigger: %s
Sql=SQL: %s
TableDropped=Just dropped table %s_CONFIG
TablesCreating=About to create SymmetricDS tables
TablesCreatingSkipped=No need to create SymmetricDS tables. They already exist.
TransactionIDSupportEnabling=Enabling transaction ID support
TriggerCreateFailed=Failed to create trigger: %s
TriggerCreating=Creating %s trigger for %s %s
TriggerDropped=Just dropped trigger %s
Expand Down

0 comments on commit 69afb57

Please sign in to comment.