Implement exportNode() method to export a node into a different
(relational) database than its default one. (The database must contain a compatible table.)
This commit is contained in:
parent
01016e6236
commit
61274e4350
1 changed files with 181 additions and 155 deletions
|
@ -433,11 +433,11 @@ public final class NodeManager {
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a new node in the embedded database or a relational database table, depending
|
* Insert a new node in the embedded database or a relational database table,
|
||||||
* on its db mapping.
|
* depending on its db mapping.
|
||||||
*/
|
*/
|
||||||
public void insertNode(IDatabase db, ITransaction txn, Node node)
|
public void insertNode(IDatabase db, ITransaction txn, Node node)
|
||||||
throws Exception {
|
throws IOException, SQLException, ClassNotFoundException {
|
||||||
// Transactor tx = (Transactor) Thread.currentThread ();
|
// Transactor tx = (Transactor) Thread.currentThread ();
|
||||||
// tx.timer.beginEvent ("insertNode "+node);
|
// tx.timer.beginEvent ("insertNode "+node);
|
||||||
DbMapping dbm = node.getDbMapping();
|
DbMapping dbm = node.getDbMapping();
|
||||||
|
@ -445,6 +445,43 @@ public final class NodeManager {
|
||||||
if ((dbm == null) || !dbm.isRelational()) {
|
if ((dbm == null) || !dbm.isRelational()) {
|
||||||
db.saveNode(txn, node.getID(), node);
|
db.saveNode(txn, node.getID(), node);
|
||||||
} else {
|
} else {
|
||||||
|
insertRelationalNode(node, dbm, dbm.getConnection());
|
||||||
|
dbm.notifyDataChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
// tx.timer.endEvent ("insertNode "+node);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert a node into a different (relational) database than its default one.
|
||||||
|
*/
|
||||||
|
public void exportNode(Node node, DbSource dbs)
|
||||||
|
throws IOException, SQLException, ClassNotFoundException {
|
||||||
|
if (node == null) {
|
||||||
|
throw new IllegalArgumentException("Node can't be null in exportNode");
|
||||||
|
}
|
||||||
|
|
||||||
|
DbMapping dbm = node.getDbMapping();
|
||||||
|
|
||||||
|
if (dbs == null) {
|
||||||
|
throw new IllegalArgumentException("DbSource can't be null in exportNode");
|
||||||
|
} else if ((dbm == null) || !dbm.isRelational()) {
|
||||||
|
throw new SQLException("Can't export into non-relational database");
|
||||||
|
} else {
|
||||||
|
insertRelationalNode(node, dbm, dbs.getConnection());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert a node into a relational database.
|
||||||
|
*/
|
||||||
|
protected void insertRelationalNode(Node node, DbMapping dbm, Connection con)
|
||||||
|
throws ClassNotFoundException, SQLException {
|
||||||
|
|
||||||
|
if (con == null) {
|
||||||
|
throw new NullPointerException("Error inserting relational node: Connection is null");
|
||||||
|
}
|
||||||
|
|
||||||
// app.logEvent ("inserting relational node: "+node.getID ());
|
// app.logEvent ("inserting relational node: "+node.getID ());
|
||||||
DbColumn[] columns = dbm.getColumns();
|
DbColumn[] columns = dbm.getColumns();
|
||||||
|
|
||||||
|
@ -468,7 +505,7 @@ public final class NodeManager {
|
||||||
b1.append(b2.toString());
|
b1.append(b2.toString());
|
||||||
b1.append(" )");
|
b1.append(" )");
|
||||||
|
|
||||||
Connection con = dbm.getConnection();
|
// Connection con = dbm.getConnection();
|
||||||
PreparedStatement stmt = con.prepareStatement(b1.toString());
|
PreparedStatement stmt = con.prepareStatement(b1.toString());
|
||||||
|
|
||||||
if (logSql) {
|
if (logSql) {
|
||||||
|
@ -581,22 +618,14 @@ public final class NodeManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
} catch (Exception x) {
|
|
||||||
x.printStackTrace();
|
|
||||||
throw x;
|
|
||||||
} finally {
|
} finally {
|
||||||
if (stmt != null) {
|
if (stmt != null) {
|
||||||
try {
|
try {
|
||||||
stmt.close();
|
stmt.close();
|
||||||
} catch (Exception ignore) {
|
} catch (Exception ignore) {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dbm.notifyDataChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
// tx.timer.endEvent ("insertNode "+node);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -604,7 +633,7 @@ public final class NodeManager {
|
||||||
* on its database mapping.
|
* on its database mapping.
|
||||||
*/
|
*/
|
||||||
public void updateNode(IDatabase db, ITransaction txn, Node node)
|
public void updateNode(IDatabase db, ITransaction txn, Node node)
|
||||||
throws Exception {
|
throws IOException, SQLException, ClassNotFoundException {
|
||||||
// Transactor tx = (Transactor) Thread.currentThread ();
|
// Transactor tx = (Transactor) Thread.currentThread ();
|
||||||
// tx.timer.beginEvent ("updateNode "+node);
|
// tx.timer.beginEvent ("updateNode "+node);
|
||||||
DbMapping dbm = node.getDbMapping();
|
DbMapping dbm = node.getDbMapping();
|
||||||
|
@ -771,9 +800,6 @@ public final class NodeManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt.executeUpdate();
|
stmt.executeUpdate();
|
||||||
} catch (Exception x) {
|
|
||||||
x.printStackTrace();
|
|
||||||
throw x;
|
|
||||||
} finally {
|
} finally {
|
||||||
if (stmt != null) {
|
if (stmt != null) {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Reference in a new issue