Skip to content

Commit

Permalink
Added support for Views and Synonyms
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.code.sf.net/p/jailer/code/trunk@1146 3dd849cd-670e-4645-a7cd-dd197c8d0e81
  • Loading branch information
rwisser committed Jan 25, 2017
1 parent b0f9455 commit e3411d8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
2 changes: 1 addition & 1 deletion releasenotes-dbeauty.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2.9.2
2.9.1
- Added support for Views and Synonyms. (https://sourceforge.net/p/jailer/feature-requests/36/)

2.9
Expand Down
113 changes: 92 additions & 21 deletions src/main/net/sf/jailer/Jailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.xml.sax.helpers.AttributesImpl;

import net.sf.jailer.database.DBMS;
import net.sf.jailer.database.DMLTransformer;
import net.sf.jailer.database.DeletionTransformer;
Expand All @@ -59,6 +63,7 @@
import net.sf.jailer.datamodel.Cardinality;
import net.sf.jailer.datamodel.Column;
import net.sf.jailer.datamodel.DataModel;
import net.sf.jailer.datamodel.Filter;
import net.sf.jailer.datamodel.ParameterHandler;
import net.sf.jailer.datamodel.RowIdSupport;
import net.sf.jailer.datamodel.Table;
Expand Down Expand Up @@ -88,10 +93,6 @@
import net.sf.jailer.xml.XmlExportTransformer;
import net.sf.jailer.xml.XmlUtil;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.xml.sax.helpers.AttributesImpl;

/**
* Tool for database subsetting, schema browsing, and rendering. It exports
* consistent, referentially intact row-sets from relational databases. It
Expand Down Expand Up @@ -447,20 +448,20 @@ public void run() throws Exception {
}
});
}
if (jc != null && association.isInsertSourceBeforeDestination()) {
done.add(association);
jobs.add(new JobManager.Job() {
public void run() throws Exception {
_log.info("find dependencies " + datamodel.getDisplayName(association.destination) + " -> "
+ datamodel.getDisplayName(table) + " on " + jc);
String fromAlias, toAlias;
fromAlias = association.reversed ? "B" : "A";
toAlias = association.reversed ? "A" : "B";
entityGraph.addDependencies(association.destination, toAlias, table, fromAlias, jc, aggregationId, dependencyId,
association.reversed);
}
});
}
// if (jc != null && association.isInsertSourceBeforeDestination()) {
// done.add(association);
// jobs.add(new JobManager.Job() {
// public void run() throws Exception {
// _log.info("find dependencies " + datamodel.getDisplayName(association.destination) + " -> "
// + datamodel.getDisplayName(table) + " on " + jc);
// String fromAlias, toAlias;
// fromAlias = association.reversed ? "B" : "A";
// toAlias = association.reversed ? "A" : "B";
// entityGraph.addDependencies(association.destination, toAlias, table, fromAlias, jc, aggregationId, dependencyId,
// association.reversed);
// }
// });
// }
}
}
}
Expand Down Expand Up @@ -676,9 +677,48 @@ public void writeEntities(final String sqlScriptFile, final ScriptType scriptTyp
}
} else {
rest = writeIndependentEntities(result, dependentTables, entityGraph);
if (rest > 0) {
// TODO
}
result.append("-- sync" + System.getProperty("line.separator"));
// if (rest > 0) {
// EntityGraph egCopy = entityGraph.copy(EntityGraph.createUniqueGraphID(), entityGraph.getSession());
// egCopy.setTransformerFactory(entityGraph.getTransformerFactory());
//
// _log.info(rest + " entities in cycle. Involved tables: " + PrintUtil.tableSetAsString(dependentTables));
// Map<Table, Set<Column>> nullableForeignKeys = findAndRemoveNullableForeignKeys(dependentTables, entityGraph, scriptType != ScriptType.DELETE);
// _log.info("nullable foreign keys: " + nullableForeignKeys.values());
//
// if (scriptType != ScriptType.DELETE) {
// List<Runnable> resetFilters = new ArrayList<Runnable>();
// for (Map.Entry<Table, Set<Column>> entry: nullableForeignKeys.entrySet()) {
// for (final Column column: entry.getValue()) {
// final Filter filter = column.getFilter();
// resetFilters.add(new Runnable() {
// @Override
// public void run() {
// column.setFilter(filter);
// }
// });
// column.setFilter(new Filter("null", false, null));
// }
// }
// rest = writeIndependentEntities(result, dependentTables, entityGraph);
//
// for (Runnable runnable: resetFilters) {
// runnable.run();
// }
//
// result.append("-- sync" + System.getProperty("line.separator"));
// for (Map.Entry<Table, Set<Column>> entry: nullableForeignKeys.entrySet()) {
// egCopy.readEntities(entry.getKey(), false);
// }
//
// } else {
//
// // TODO
// }
//
// egCopy.delete();
// result.append("-- sync" + System.getProperty("line.separator"));
// }
}
if (rest > 0) {
break;
Expand Down Expand Up @@ -755,6 +795,37 @@ public void writeEntities(final String sqlScriptFile, final ScriptType scriptTyp
_log.info("file '" + sqlScriptFile + "' written.");
}

private Map<Table, Set<Column>> findAndRemoveNullableForeignKeys(Set<Table> tables, EntityGraph theEntityGraph, boolean fkIsInSource) throws SQLException {
Map<Table, Set<Column>> result = new HashMap<Table, Set<Column>>();

for (Table table: tables) {
for (Association association: table.associations) {
if (association.isInsertDestinationBeforeSource()) {
Map<Column, Column> mapping = association.createSourceToDestinationKeyMapping();
if (!mapping.isEmpty()) {
boolean nullable = true;
Collection<Column> fk = fkIsInSource? mapping.keySet() : mapping.values();
for (Column c: fk) {
if (!c.isNullable) {
nullable = false;
break;
}
}
if (nullable) {
if (!result.containsKey(table)) {
result.put(table, new HashSet<Column>());
}
result.get(table).addAll(fk);
theEntityGraph.removeDependencies(association);
}
}
}
}
}

return result;
}

/**
* Iteratively mark and write out independent entities from a given {@link EntityGraph}
* until no independent entity remains.
Expand Down
17 changes: 16 additions & 1 deletion src/main/net/sf/jailer/entitygraph/EntityGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
import java.util.Map;
import java.util.Set;

import net.sf.jailer.Configuration;
import net.sf.jailer.TransformerFactory;
import net.sf.jailer.database.DBMS;
import net.sf.jailer.database.SQLDialect;
import net.sf.jailer.database.Session;
import net.sf.jailer.database.Session.ResultSetReader;
import net.sf.jailer.datamodel.Association;
import net.sf.jailer.datamodel.DataModel;
import net.sf.jailer.datamodel.PrimaryKey;
import net.sf.jailer.datamodel.Table;
import net.sf.jailer.util.CellContentConverter;

/**
* Persistent graph of entities.
Expand Down Expand Up @@ -403,7 +406,19 @@ public void readCurrentRow(ResultSet resultSet) throws SQLException {
return statistic;
}

public abstract Session getTargetSession();
/**
* Removes all dependencies for a given association.
*
* @param association the asociation
*/
public void removeDependencies(Association association) throws SQLException {
String delete;
delete = "Delete from " + SQLDialect.dmlTableReference(DEPENDENCY, getSession()) +
" Where depend_id=" + association.getId() + " and r_entitygraph=" + graphID;
getSession().executeUpdate(delete);
}

public abstract Session getTargetSession();

public void setDeleteMode(boolean deleteMode) {
inDeleteMode = deleteMode;
Expand Down

0 comments on commit e3411d8

Please sign in to comment.