Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
vollt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sonia Zorba
vollt
Commits
904130f6
Commit
904130f6
authored
11 years ago
by
gmantele
Browse files
Options
Downloads
Patches
Plain Diff
TEST-TAP: Experiment metadata extraction from the database.
parent
65b4a8a0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/tap/metadata/MetadataExtractionTest.java
+139
-0
139 additions, 0 deletions
test/tap/metadata/MetadataExtractionTest.java
with
139 additions
and
0 deletions
test/tap/metadata/MetadataExtractionTest.java
0 → 100644
+
139
−
0
View file @
904130f6
package
tap.metadata
;
/*
* This file is part of TAPLibrary.
*
* TAPLibrary is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TAPLibrary is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TAPLibrary. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2014 - Astronomisches Rechen Institute (ARI)
*/
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
/**
* @author Grégory Mantelet (ARI) - gmantele@ari.uni-heidelberg.de
* @version 1.1 (04/2014)
*/
public
class
MetadataExtractionTest
{
public
static
void
main
(
String
[]
args
)
throws
Throwable
{
MetadataExtractionTest
extractor
=
new
MetadataExtractionTest
();
try
{
extractor
.
connect
();
extractor
.
printTableMetadata
(
"gums"
);
}
finally
{
extractor
.
close
();
}
}
private
Connection
connection
=
null
;
private
Statement
statement
=
null
;
public
void
connect
(){
try
{
Class
.
forName
(
"org.postgresql.Driver"
);
connection
=
DriverManager
.
getConnection
(
"jdbc:postgresql:gmantele"
,
"gmantele"
,
"pwd"
);
statement
=
connection
.
createStatement
();
System
.
out
.
println
(
"[OK] DB connection successfully established !"
);
}
catch
(
ClassNotFoundException
notFoundException
){
notFoundException
.
printStackTrace
();
System
.
err
.
println
(
"[ERROR] Connection error !"
);
}
catch
(
SQLException
sqlException
){
sqlException
.
printStackTrace
();
System
.
err
.
println
(
"[ERROR] Connection error !"
);
}
}
public
ResultSet
query
(
String
requet
){
ResultSet
resultat
=
null
;
try
{
resultat
=
statement
.
executeQuery
(
requet
);
}
catch
(
SQLException
e
){
e
.
printStackTrace
();
System
.
out
.
println
(
"Erreur dans la requête: "
+
requet
);
}
return
resultat
;
}
public
TAPSchema
printTableMetadata
(
final
String
table
){
try
{
DatabaseMetaData
dbMeta
=
connection
.
getMetaData
();
TAPSchema
tapSchema
=
null
;
TAPTable
tapTable
=
null
;
// Extract Table metadata (schema, table, type):
ResultSet
rs
=
dbMeta
.
getTables
(
null
,
null
,
table
,
null
);
rs
.
last
();
if
(
rs
.
getRow
()
==
0
)
System
.
err
.
println
(
"[ERROR] No found table for \""
+
table
+
"\" !"
);
else
if
(
rs
.
getRow
()
>
1
){
rs
.
first
();
System
.
err
.
println
(
"[ERROR] More than one match for \""
+
table
+
"\":"
);
while
(
rs
.
next
())
System
.
err
.
println
(
rs
.
getString
(
2
)
+
"."
+
rs
.
getString
(
3
)
+
" : "
+
rs
.
getString
(
4
));
}
else
{
rs
.
first
();
tapSchema
=
new
TAPSchema
(
rs
.
getString
(
2
));
tapTable
=
new
TAPTable
(
rs
.
getString
(
3
),
rs
.
getString
(
4
));
tapSchema
.
addTable
(
tapTable
);
System
.
out
.
println
(
"[OK] 1 table FOUND ! => "
+
tapTable
+
" : "
+
tapTable
.
getType
());
}
// Extract all columns metadata (type, precision, scale):
rs
=
dbMeta
.
getColumns
(
null
,
tapSchema
.
getDBName
(),
tapTable
.
getDBName
(),
null
);
String
type
;
while
(
rs
.
next
()){
type
=
rs
.
getString
(
6
);
if
(
type
.
endsWith
(
"char"
)
||
type
.
equals
(
"numeric"
)){
type
+=
"("
+
rs
.
getInt
(
7
);
if
(
type
.
startsWith
(
"numeric"
))
type
+=
","
+
rs
.
getInt
(
9
);
type
+=
")"
;
}
System
.
out
.
println
(
" * "
+
rs
.
getString
(
4
)
+
" : "
+
type
);
}
// Extract all indexed columns:
rs
=
dbMeta
.
getIndexInfo
(
null
,
tapSchema
.
getDBName
(),
tapTable
.
getDBName
(),
false
,
true
);
while
(
rs
.
next
()){
System
.
out
.
println
(
" # "
+
rs
.
getString
(
6
)
+
" : "
+
rs
.
getShort
(
7
)
+
" (unique ? "
+
(!
rs
.
getBoolean
(
4
))
+
") -> "
+
rs
.
getString
(
9
)
+
" => "
+
rs
.
getInt
(
11
)
+
" unique values in the index ; "
+
rs
.
getInt
(
12
)
+
" pages"
);
}
return
tapSchema
;
}
catch
(
SQLException
e
){
e
.
printStackTrace
();
return
null
;
}
}
public
void
close
(){
try
{
connection
.
close
();
statement
.
close
();
System
.
out
.
println
(
"[OK] Connection closed !"
);
}
catch
(
SQLException
e
){
e
.
printStackTrace
();
System
.
out
.
println
(
"[ERROR] Connection CAN NOT be closed !"
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment