Skip to content

Commit

Permalink
Merge branch '3.8' of https://github.com/JumpMind/symmetric-ds into 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Jul 19, 2016
2 parents a8f859c + 5800903 commit 62e882c
Show file tree
Hide file tree
Showing 22 changed files with 1,354 additions and 74 deletions.
118 changes: 118 additions & 0 deletions symmetric-db/src/main/java/org/jumpmind/db/model/Trigger.java
@@ -0,0 +1,118 @@
package org.jumpmind.db.model;

import java.util.HashMap;
import java.util.Map;

public class Trigger {

public enum TriggerType { INSERT, UPDATE, DELETE };

String triggerName;

String catalogName;

String schemaName;

String tableName;

String source;

TriggerType triggerType;

boolean enabled;

Map<String, Object> metaData = new HashMap<String, Object>();

public Trigger(String name, String catalogName, String schemaName, String tableName, TriggerType triggerType) {
this(name, catalogName, schemaName, tableName, triggerType, true);
}

public Trigger(String name, String catalogName, String schemaName, String tableName, TriggerType triggerType, boolean enabled) {
this.triggerName = name;
this.catalogName = catalogName;
this.schemaName = schemaName;
this.tableName = tableName;
this.triggerType = triggerType;
this.enabled = enabled;
}

public Trigger() {
}

public String getName() {
return triggerName;
}

public void setName(String name) {
this.triggerName = name;
}

public String getCatalogName() {
return catalogName;
}

public void setCatalogName(String catalogName) {
this.catalogName = catalogName;
}

public String getSchemaName() {
return schemaName;
}

public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}

public String getTableName() {
return tableName;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public Map<String, Object> getMetaData() {
return metaData;
}

public void setMetaData(Map<String, Object> metaData) {
this.metaData = metaData;
}


public TriggerType getTriggerType() {
return triggerType;
}

public void setTriggerType(TriggerType triggerType) {
this.triggerType = triggerType;
}

public String getFullyQualifiedName() {
return getFullyQualifiedName(catalogName, schemaName, tableName, triggerName);
}

public static String getFullyQualifiedName(String catalog, String schema, String tableName, String triggerName) {
String fullName = "";
if (catalog != null) fullName += catalog+".";
if (schema != null) fullName += schema+".";
fullName += tableName+"."+triggerName;
return fullName;
}
}
Expand Up @@ -25,6 +25,7 @@

import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.Trigger;

public interface IDdlReader {

Expand All @@ -42,4 +43,8 @@ public interface IDdlReader {

public List<String> getColumnNames(String catalog, String schema, String tableName);

public List<Trigger> getTriggers(String catalog, String schema, String tableName);

public Trigger getTriggerFor(Table table, String name);

}
Expand Up @@ -21,6 +21,7 @@
package org.jumpmind.db.platform.sqlite;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -35,12 +36,16 @@
import org.jumpmind.db.model.NonUniqueIndex;
import org.jumpmind.db.model.Reference;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.Trigger;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.model.UniqueIndex;
import org.jumpmind.db.model.Trigger.TriggerType;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.IDdlReader;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.Row;
import org.jumpmind.db.sql.SqlConstants;
import org.jumpmind.db.sql.SqlException;
import org.jumpmind.db.sql.mapper.RowMapper;

public class SqliteDdlReader implements IDdlReader {
Expand Down Expand Up @@ -233,7 +238,45 @@ public IndexColumn mapRow(Row row) {
return column;
}
}

public Trigger getTriggerFor(Table table, String triggerName) {
Trigger trigger = null;
List<Trigger> triggers = getTriggers(table.getCatalog(), table.getSchema(), table.getName());
for (Trigger t : triggers) {
if (t.getName().equals(triggerName)) {
trigger = t;
break;
}
}
return trigger;
}


public List<Trigger> getTriggers(final String catalog, final String schema,
final String tableName) throws SqlException {

List<Trigger> triggers = new ArrayList<Trigger>();

String sql = "SELECT "
+ "name AS trigger_name, "
+ "tbl_name AS table_name, "
+ "rootpage, "
+ "sql, "
+ "type AS object_type "
+ "FROM sqlite_master "
+ "WHERE table_name=? AND object_type='trigger';";
triggers = platform.getSqlTemplate().query(sql, new ISqlRowMapper<Trigger>() {
public Trigger mapRow(Row row) {
Trigger trigger = new Trigger();
trigger.setName(row.getString("trigger_name"));
trigger.setTableName(row.getString("table_name"));
trigger.setEnabled(true);
trigger.setSource(row.getString("sql"));
row.remove("sql");
trigger.setMetaData(row);
return trigger;
}
}, tableName.toLowerCase());

return triggers;
}
}
Expand Up @@ -52,6 +52,7 @@
import org.jumpmind.db.model.PlatformColumn;
import org.jumpmind.db.model.Reference;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.Trigger;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.model.UniqueIndex;
import org.jumpmind.db.sql.IConnectionCallback;
Expand Down Expand Up @@ -130,6 +131,12 @@ public AbstractJdbcDdlReader(IDatabasePlatform platform) {
_columnsForFK = initColumnsForFK();
_columnsForIndex = initColumnsForIndex();
}

@Override
public List<Trigger> getTriggers(String catalog, String schema,
String tableName) {
return Collections.emptyList();
}

/*
* Returns the platform that this model reader belongs to.
Expand Down Expand Up @@ -1405,8 +1412,9 @@ public List<String> execute(Connection connection) throws SQLException {

public List<String> getTableNames(final String catalog, final String schema,
final String[] tableTypes) {
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
return sqlTemplate.execute(new IConnectionCallback<List<String>>() {
long startTime = System.nanoTime();
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
List<String> list = sqlTemplate.execute(new IConnectionCallback<List<String>>() {
public List<String> execute(Connection connection) throws SQLException {
ArrayList<String> list = new ArrayList<String>();
DatabaseMetaData meta = connection.getMetaData();
Expand All @@ -1423,6 +1431,8 @@ public List<String> execute(Connection connection) throws SQLException {
}
}
});
log.debug("Elapsed time (old): "+(System.nanoTime()-startTime));
return list;
}

public List<String> getColumnNames(final String catalog, final String schema, final String tableName) {
Expand All @@ -1445,5 +1455,21 @@ public List<String> execute(Connection connection) throws SQLException {
}
});
}

public List<String> getListOfTriggers() {
return new ArrayList<String>();
}

public Trigger getTriggerFor(Table table, String triggerName) {
Trigger trigger = null;
List<Trigger> triggers = getTriggers(table.getCatalog(), table.getSchema(), table.getName());
for (Trigger t : triggers) {
if (t.getName().equals(triggerName)) {
trigger = t;
break;
}
}
return trigger;
}

}
Expand Up @@ -30,6 +30,7 @@
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -39,10 +40,16 @@
import org.jumpmind.db.model.IIndex;
import org.jumpmind.db.model.Reference;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.model.Trigger;
import org.jumpmind.db.model.TypeMap;
import org.jumpmind.db.model.Trigger.TriggerType;
import org.jumpmind.db.platform.AbstractJdbcDdlReader;
import org.jumpmind.db.platform.DatabaseMetaDataWrapper;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.JdbcSqlTemplate;
import org.jumpmind.db.sql.Row;
import org.jumpmind.db.sql.SqlException;

/*
* Reads a database model from a Sybase database.
Expand Down Expand Up @@ -249,4 +256,79 @@ protected boolean isInternalPrimaryKeyIndex(Connection connection,
stmt.close();
}
}

@Override
public List<Trigger> getTriggers(final String catalog, final String schema,
final String tableName) throws SqlException {

List<Trigger> triggers = new ArrayList<Trigger>();

log.debug("Reading triggers for: " + tableName);
JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform
.getSqlTemplate();

String sql = "SELECT "
+ "trig.name AS trigger_name, "
+ "trig.id AS trigger_id, "
+ "tab.name AS table_name, "
+ "tab.id AS table_id, "
+ "db.name AS catalog, "
+ "trig.crdate AS created_on, "
+ "tab.deltrig AS table_delete_trigger_id, "
+ "tab.instrig AS table_insert_trigger_id, "
+ "tab.updtrig AS table_update_trigger_id "
+ "FROM sysobjects AS trig "
+ "INNER JOIN sysobjects AS tab "
+ "ON trig.id = tab.deltrig "
+ "OR trig.id = tab.instrig "
+ "OR trig.id = tab.updtrig "
+ "INNER JOIN master.dbo.sysdatabases AS db "
+ "ON db.dbid = db_id() "
+ "WHERE tab.name = ? AND db.name = ? ";
triggers = sqlTemplate.query(sql, new ISqlRowMapper<Trigger>() {
public Trigger mapRow(Row row) {
Trigger trigger = new Trigger();
trigger.setName(row.getString("trigger_name"));
trigger.setTableName(row.getString("table_name"));
trigger.setCatalogName(row.getString("catalog"));
trigger.setEnabled(true);
trigger.setSource("");
if (row.getString("table_insert_trigger_id")
.equals(row.getString("trigger_id"))) {
trigger.setTriggerType(TriggerType.INSERT);
row.put("trigger_type", "insert");
} else if (row.getString("table_delete_trigger_id")
.equals(row.getString("trigger_id"))) {
trigger.setTriggerType(TriggerType.DELETE);
row.put("trigger_type", "delete");
} else if (row.getString("table_update_trigger_id")
.equals(row.getString("trigger_id"))) {
trigger.setTriggerType(TriggerType.UPDATE);
row.put("trigger_type", "update");
}
row.remove("table_insert_trigger_id");
row.remove("table_delete_trigger_id");
row.remove("table_update_trigger_id");
trigger.setMetaData(row);
return trigger;
}
}, tableName, catalog);


for (final Trigger trigger : triggers) {
int id = (Integer) trigger.getMetaData().get("trigger_id");
String sourceSql = "SELECT text "
+ "FROM syscomments "
+ "WHERE id = ? "
+ "ORDER BY colid ";
sqlTemplate.query(sourceSql, new ISqlRowMapper<Trigger>() {
public Trigger mapRow(Row row) {
trigger.setSource(trigger.getSource()+"\n"+row.getString("text"));
return trigger;
}
}, id);
}

return triggers;
}
}

0 comments on commit 62e882c

Please sign in to comment.