Commit 5936d8f0 authored by Daniel Eggert's avatar Daniel Eggert
Browse files

tool to copy entries from a gmsdb table to another

parent ea36a18a
/**
*
*/
package de.potsdam.gfz.gms.metadatacrawler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import org.postgis.PGgeometry;
import de.potsdam.gfz.gms.database.SceneDatabase;
/**
* @author Daniel Eggert (daniel.eggert@gfz-potsdam.de)
*
*/
public class CopyTableEntries {
private static final String host = "localhost";
private static final int port = 5433;
private static final String url = "jdbc:postgresql://" + host + ":" + port + "/geomultisens";
private static final String user = "gmsdb";
private static final String password = "gmsdb";
// source db
private static final SceneDatabase gfzDB = SceneDatabase.getInstance();
// destination db
private static Connection con = null;
private static void connectToDestinationDB() throws Exception {
// establish connection to second database (hu)
// load the driver
Class.forName("org.postgresql.Driver");
// // establish connection
con = DriverManager.getConnection(url, user, password);
// register postgis geometry object
((org.postgresql.PGConnection) con).addDataType("geometry", PGgeometry.class);
((org.postgresql.PGConnection) con).addDataType("geography", PGgeometry.class);
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
connectToDestinationDB();
@SuppressWarnings("unused")
final Long[] sceneids = { 9497323l, 14721561l, 27091075l, 5507417l, 27944820l, 27846486l, 9497323l, 27091075l, 27944820l };
final Long[] jobids = { 26186263l, 26186262l, 26186267l, 26186196l, 26186268l, 26186272l, 26186273l };
// copyEntries("scenes", sceneids);
// copyEntries("jobs", jobids);
for (int datasetid : Arrays.asList(249, 250, 251, 252)) {
updateDatasetFields((short) datasetid);
}
}
private static void copyEntries(String tablename, Long[] ids) throws Exception {
// determine number of columns
PreparedStatement numColPst = gfzDB.prepareCustomStatement("select count(*) from information_schema.columns where table_name=?;");
numColPst.setString(1, tablename);
ResultSet rs = numColPst.executeQuery();
int numParam = 0;
if (rs.next()) {
numParam = rs.getInt(1);
}
rs.close();
if (numParam <= 0) {
System.err.println("Unable to identify number of columns for table: " + tablename);
}
// get entries
PreparedStatement selectPst = gfzDB.prepareCustomStatement("select * from " + tablename + " where id = ANY (?);");
selectPst.setArray(1, con.createArrayOf("bigint", ids));
ResultSet selectRS = selectPst.executeQuery();
ResultSetMetaData metaRS = selectRS.getMetaData();
// insert entries into new db
String insertStatement = "INSERT INTO " + tablename + " VALUES (";
for (int i = 1; i <= numParam; ++i) {
// our custom data types (enums) need cast
String type = metaRS.getColumnTypeName(i);
switch (type) {
case "job_status":
case "job_mode":
case "proc_level":
insertStatement += "?::" + type;
break;
default:
insertStatement += "?";
}
if (i < numParam)
insertStatement += ", ";
}
insertStatement += ");";
PreparedStatement insertPst = con.prepareStatement(insertStatement);
while (selectRS.next()) {
for (int param = 1; param <= numParam; ++param) {
insertPst.setObject(param, selectRS.getObject(param));
}
insertPst.executeUpdate();
}
}
private static void updateDatasetFields(short datasetid) throws SQLException {
// update dataset count
PreparedStatement pst = con.prepareStatement("SELECT count(id), min(acquisitiondate), max(acquisitiondate) from scenes where datasetid = ?;");
pst.setShort(1, datasetid);
ResultSet rs = pst.executeQuery();
long totalScenes = 0;
java.sql.Timestamp maxDate = null;
java.sql.Timestamp minDate = null;
if (rs.next()) {
totalScenes = rs.getLong(1);
minDate = rs.getTimestamp(2);
maxDate = rs.getTimestamp(3);
PreparedStatement dsUpPst = con.prepareStatement("UPDATE datasets SET totalscenes = ?, startdate = ?, enddate = ? where id = ?;");
dsUpPst.setLong(1, totalScenes);
dsUpPst.setTimestamp(2, minDate);
dsUpPst.setTimestamp(3, maxDate);
dsUpPst.setShort(4, datasetid);
dsUpPst.executeUpdate();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment