Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
geomultisens
gms-aux
Commits
5936d8f0
Commit
5936d8f0
authored
Nov 10, 2017
by
Daniel Eggert
Browse files
tool to copy entries from a gmsdb table to another
parent
ea36a18a
Changes
1
Hide whitespace changes
Inline
Side-by-side
gms-metadatacrawler/src/main/java/de/potsdam/gfz/gms/metadatacrawler/CopyTableEntries.java
0 → 100644
View file @
5936d8f0
/**
*
*/
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
=
{
9497323
l
,
14721561
l
,
27091075
l
,
5507417
l
,
27944820
l
,
27846486
l
,
9497323
l
,
27091075
l
,
27944820
l
};
final
Long
[]
jobids
=
{
26186263
l
,
26186262
l
,
26186267
l
,
26186196
l
,
26186268
l
,
26186272
l
,
26186273
l
};
// 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
();
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment