Skip to content
Snippets Groups Projects
Commit df3119f7 authored by Robert Butora's avatar Robert Butora
Browse files

cleans up DbPSearch queries by unique inputRegion and dbRegion calls

parent 09bc6463
No related branches found
No related tags found
No related merge requests found
...@@ -26,81 +26,25 @@ public class DbPSearch ...@@ -26,81 +26,25 @@ public class DbPSearch
private static final Logger LOGGER = Logger.getLogger(DbPSearch.class.getName()); private static final Logger LOGGER = Logger.getLogger(DbPSearch.class.getName());
private DBConn dbConn; private DBConn dbConn;
//private static final String DB_DRIVER = "org.postgresql.Driver";
DbPSearch(DBConn dbConn) DbPSearch(DBConn dbConn)
{ {
this.dbConn = dbConn; this.dbConn = dbConn;
} }
public String[] queryOverlapingPubdid(Coord coord, SubsurveyId subsurveyId)
{
LOGGER.info("trace");
// String skySystem = coord.skySystem;
String shape = coord.pos.shape;
double lon;
double lat;
double radius;
double dlon;
double dlat;
String inputRegion = null;
if(shape.equals("CIRCLE")) public String[] queryOverlapingPubdid(Coord coord, SubsurveyId subsurveyId)
{
lon = coord.pos.circle.lon;
lat = coord.pos.circle.lat;
radius = coord.pos.circle.radius;
inputRegion = "scircle '<(" + Double.toString(lon) + "d," + Double.toString(lat) + "d),"
+ Double.toString(radius) + "d>'";
}
else if (shape.equals("RECT"))
{
lon = (coord.pos.range.lon1 + coord.pos.range.lon2)/2.0;
lat = (coord.pos.range.lat1 + coord.pos.range.lat2)/2.0;
dlon = (coord.pos.range.lon2 - coord.pos.range.lon1)/2.0;
dlat = (coord.pos.range.lat2 - coord.pos.range.lat1)/2.0;
/* South-West and North-East corners of a box */
String sw_lon = Double.toString(lon - dlon/2.0);
String sw_lat = Double.toString(lat - dlat/2.0);
String ne_lon = Double.toString(lon + dlon/2.0);
String ne_lat = Double.toString(lat + dlat/2.0);
inputRegion = "sbox '( ("+ sw_lon + "d, " + sw_lat + "d), (" + ne_lon +"d, " + ne_lat + "d) )'";
}
else if (shape.equals("POLYGON"))
{
// FIXME redefine Polygon as point-array:
assert(coord.pos.polygon.lon.length == coord.pos.polygon.lat.length);
// Polygon has at least 3 points
inputRegion = "spoly '( (" + coord.pos.polygon.lon[0] + "d, " + coord.pos.polygon.lat[0] + "d),";
for(int ii=1; ii < coord.pos.polygon.lon.length; ii++)
{
inputRegion += ", (" + coord.pos.polygon.lon[ii] + "d, " + coord.pos.polygon.lat[ii] + "d)";
}
inputRegion += " )'";
}
else
{ {
throw new IllegalArgumentException("Coord::shape was: " + shape + " but valid is CIRCLE or RECT"); LOGGER.info("trace");
}
boolean vel_valid = (coord.band != null) && (coord.band.system != Band.System.NONE);//coord.vel_valid; String inputRegion = toPgSphereSqlTypeString(coord.pos);
String dbRegion = toRegionColumnName(coord.pos.system);
String theQuery; String theQuery = "SELECT obs_publisher_did FROM obscore WHERE ("+inputRegion+" && " + dbRegion + ")";
if(coord.pos.system == Pos.System.GALACTIC)
{
theQuery ="SELECT obs_publisher_did FROM obscore WHERE ("+inputRegion+" && polygon_region_galactic)";
}
else
{
theQuery ="SELECT obs_publisher_did FROM obscore WHERE (" + inputRegion + " && polygon_region)";
}
boolean vel_valid = (coord.band != null) && (coord.band.system != Band.System.NONE);
if(vel_valid) if(vel_valid)
{ {
String vel_no_overlap String vel_no_overlap
...@@ -115,7 +59,7 @@ public class DbPSearch ...@@ -115,7 +59,7 @@ public class DbPSearch
if(subsurveyId != null) if(subsurveyId != null)
{ {
/* FIXME replace this implementation with exact string match once survey_id is defined / added to obs_core */ /* FIXME replace this implementation with exact string match once survey_id is defined in obs_core */
String addSS = ""; String addSS = "";
if((subsurveyId.surveyName != null) && (subsurveyId.surveyName.length() > 0)) if((subsurveyId.surveyName != null) && (subsurveyId.surveyName.length() > 0))
...@@ -141,20 +85,19 @@ public class DbPSearch ...@@ -141,20 +85,19 @@ public class DbPSearch
List<String> pubdidList = new ArrayList<>(); List<String> pubdidList = new ArrayList<>();
LOGGER.info("Connecting to: " + dbConn.uri() + " with optional user/pwd: " + dbConn.userName() +" / "+ dbConn.password() ); LOGGER.info("Connecting to: " + dbConn.uri()
+ " with optional user/pwd: " + dbConn.userName() +" / "+ dbConn.password() );
try( try(
Connection conn = DriverManager.getConnection(dbConn.uri(), dbConn.userName(), dbConn.password()); Connection conn = DriverManager.getConnection(dbConn.uri(), dbConn.userName(), dbConn.password());
Statement st = conn.createStatement(); Statement st = conn.createStatement();
ResultSet res = st.executeQuery(theQuery);) ResultSet res = st.executeQuery(theQuery);)
{ {
while (res.next()) while (res.next())
{ {
String pubdid_str = res.getString("obs_publisher_did"); String pubdid_str = res.getString("obs_publisher_did");
pubdidList.add(pubdid_str); pubdidList.add(pubdid_str);
} }
} }
catch (SQLException se) catch (SQLException se)
{ {
...@@ -172,127 +115,19 @@ public class DbPSearch ...@@ -172,127 +115,19 @@ public class DbPSearch
public FormatResponseFilter.ObsCore[] queryOutputData(String[] pubdidArr, Coord coord/*, SubsurveyId subsurveyId*/) public FormatResponseFilter.ObsCore[] queryOutputData(String[] pubdidArr, Coord coord)
{ {
LOGGER.info(""); LOGGER.info("");
String skySystem = coord.pos.system.toString();
String shape = coord.pos.shape;
double lon;
double lat;
double radius;
double dlon;
double dlat;
String inputRegion = null;
if(shape.equals("CIRCLE"))
{
lon = coord.pos.circle.lon;
lat = coord.pos.circle.lat;
radius = coord.pos.circle.radius;
inputRegion = "scircle '<(" + Double.toString(lon) + "d," + Double.toString(lat) + "d),"
+ Double.toString(radius) + "d>'";
}
else if (shape.equals("RECT"))
{
lon = (coord.pos.range.lon1 + coord.pos.range.lon2)/2.0;
lat = (coord.pos.range.lat1 + coord.pos.range.lat2)/2.0;
dlon = (coord.pos.range.lon2 - coord.pos.range.lon1)/2.0;
dlat = (coord.pos.range.lat2 - coord.pos.range.lat1)/2.0;
/* South-West and North-East corners of a box */
String sw_lon = Double.toString(lon - dlon/2.0);
String sw_lat = Double.toString(lat - dlat/2.0);
String ne_lon = Double.toString(lon + dlon/2.0);
String ne_lat = Double.toString(lat + dlat/2.0);
inputRegion = "sbox '( ("+ sw_lon + "d, " + sw_lat + "d), (" + ne_lon +"d, " + ne_lat + "d) )'";
}
else
{ // FIXME how to deal with Polygon ? -> use enclose-RECT -> RANGE
/* lon = coord.lon;
lat = coord.lat;
radius = coord.radius;
dlon = coord.dlon;
dlat = coord.dlat;*/
throw new IllegalArgumentException("Coord::shape was: " + shape + " but valid is CIRCLE or RECT");
}
boolean vel_valid = (coord.band != null) && (coord.band.system != Band.System.NONE);//coord.vel_valid;
// String vel_type = coord.vel_type;
// double vel_low = coord.band.wavelength[0];//vel_low;
// double vel_up = coord.band.wavelength[1];//vel_up;
/*
double lon = coord.lon;
double lat = coord.lat;
double radius = coord.radius;
double dlon = coord.dlon;
double dlat = coord.dlat;
boolean vel_valid = coord.vel_valid;
String vel_type = coord.vel_type;
double vel_low = coord.vel_low;
double vel_up = coord.vel_up;
String inputRegion = null;
if(coord.shape.equals("CIRCLE"))
{
inputRegion = "scircle '<(" + Double.toString(lon) + "d," + Double.toString(lat) + "d),"
+ Double.toString(radius) + "d>'";
}
else if( coord.shape.equals("RECT") )
{
/*Vert vert[] = toVertices(lon, lat, dlon, dlat);
inputRegion = "spoly '{"
+ "(" + Double.toString(vert[0].lon) + "d," + Double.toString(vert[0].lat) + "d),"
+ "(" + Double.toString(vert[1].lon) + "d," + Double.toString(vert[1].lat) + "d),"
+ "(" + Double.toString(vert[2].lon) + "d," + Double.toString(vert[2].lat) + "d),"
+ "(" + Double.toString(vert[3].lon) + "d," + Double.toString(vert[3].lat) + "d)"
+ "}'";
*/
/* South-West and North-East corners of a box * /
String sw_lon = Double.toString(lon - dlon/2.0);
String sw_lat = Double.toString(lat - dlat/2.0);
String ne_lon = Double.toString(lon + dlon/2.0);
String ne_lat = Double.toString(lat + dlat/2.0);
inputRegion = "sbox '( ("+ sw_lon + "d, " + sw_lat + "d), (" + ne_lon +"d, " + ne_lat + "d) )'";
}
else
{
throw new IllegalArgumentException("Coord::shape was: " + coord.shape + " but valid is CIRCLE or RECT");
}
*/
String commaSepPubdids = String.join("\',\'", pubdidArr); String commaSepPubdids = String.join("\',\'", pubdidArr);
String inputRegion = toPgSphereSqlTypeString(coord.pos);
String dbRegion = toRegionColumnName(coord.pos.system);
//String theQuery ="SELECT dataproduct_type,obs_publisher_did,obs_collection,polygon_region_galactic,access_url,em_min,em_max," String theQuery ="SELECT *,"
String theQuery; + inputRegion + " <@ " + dbRegion + " AS inputInsideDb, "
if(coord.pos.system == Pos.System.GALACTIC) + inputRegion + " @> " + dbRegion + " AS dbInsideInput FROM obscore WHERE (obs_publisher_did IN (\'"
{ +commaSepPubdids+"\'))";
theQuery ="SELECT *,"
+ inputRegion + " <@ polygon_region_galactic AS inputInsideDb, "
+ inputRegion + " @> polygon_region_galactic AS dbInsideInput FROM obscore WHERE (obs_publisher_did IN (\'"+commaSepPubdids+"\'))";
}
else
{
theQuery ="SELECT *,"
+ inputRegion + " <@ polygon_region AS inputInsideDb, "
+ inputRegion + " @> polygon_region AS dbInsideInput FROM obscore WHERE (obs_publisher_did IN (\'"+commaSepPubdids+"\'))";
}
theQuery += " ORDER BY obs_collection"; theQuery += " ORDER BY obs_collection";
...@@ -300,14 +135,13 @@ public class DbPSearch ...@@ -300,14 +135,13 @@ public class DbPSearch
List<FormatResponseFilter.ObsCore> obsCoreList = new ArrayList<>(); List<FormatResponseFilter.ObsCore> obsCoreList = new ArrayList<>();
LOGGER.info("Connecting to: " + dbConn.uri() + " with optional user/pwd: " + dbConn.userName() +" / "+ dbConn.password() ); LOGGER.info("Connecting to: " + dbConn.uri()
+ " with optional user/pwd: " + dbConn.userName() +" / "+ dbConn.password() );
try( try(
Connection conn = DriverManager.getConnection(dbConn.uri(), dbConn.userName(), dbConn.password()); Connection conn = DriverManager.getConnection(dbConn.uri(), dbConn.userName(), dbConn.password());
Statement st = conn.createStatement(); Statement st = conn.createStatement();
ResultSet res = st.executeQuery(theQuery);) ResultSet res = st.executeQuery(theQuery);)
{ {
//ResultSet res = doQuery(theQuery);
while (res.next()) while (res.next())
{ {
FormatResponseFilter.ObsCore obsCore = new FormatResponseFilter.ObsCore(); FormatResponseFilter.ObsCore obsCore = new FormatResponseFilter.ObsCore();
...@@ -371,100 +205,76 @@ public class DbPSearch ...@@ -371,100 +205,76 @@ public class DbPSearch
return cubes; return cubes;
} }
/*
public static void loadDriver()// throws ClassNotFoundException
{
/* https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html :
* Any JDBC 4.0 drivers that are found in your class path are automatically loaded.
* (However, you must manually load any drivers prior to JDBC 4.0 with the method
* Class.forName.)
* /
/* OR
* DriverManager.registerDriver(new org.postgresql.Driver());
* LOGGER.info(getClasspathString());
* LOGGER.info(getRegisteredDriverList());
*i /
try
{
// Class.forName(DB_DRIVER);
}
catch (ClassNotFoundException e)
{
LOGGER.info("DB driver "+ DB_DRIVER +" not found: " + e.getMessage());
e.printStackTrace();
}
}
*/
/*
private ResultSet doQuery(String theQuery) throws SQLException, ClassNotFoundException
{
/* https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html :
* Any JDBC 4.0 drivers that are found in your class path are automatically loaded.
* (However, you must manually load any drivers prior to JDBC 4.0 with the method
* Class.forName.)
* /
// Class.forName(DB_DRIVER);
/* OR
* DriverManager.registerDriver(new org.postgresql.Driver());
* LOGGER.info(getClasspathString());
* LOGGER.info(getRegisteredDriverList());
* /
LOGGER.info("Connecting to: " + dbConn.uri() + " with optional user/pwd: " + dbConn.userName() +" / "+ dbConn.password() );
Connection conn = DriverManager.getConnection(dbConn.uri(), dbConn.userName(), dbConn.password()); private String toPgSphereSqlTypeString(Pos pos)
{
double lon;
double lat;
double radius;
double dlon;
double dlat;
Statement st = conn.createStatement(); String inputRegion = null;
ResultSet res = st.executeQuery(theQuery); String shape = pos.shape;
return res; if(shape.equals("CIRCLE"))
{
lon = pos.circle.lon;
lat = pos.circle.lat;
radius = pos.circle.radius;
inputRegion = "scircle '<(" + Double.toString(lon) + "d," + Double.toString(lat) + "d),"
+ Double.toString(radius) + "d>'";
} }
*/ else if (shape.equals("RECT"))
/* public Subsurvey[] getSurveyTable()
{ {
List<Subsurvey> survList = new ArrayList<Subsurvey>(); lon = (pos.range.lon1 + pos.range.lon2)/2.0;
lat = (pos.range.lat1 + pos.range.lat2)/2.0;
String theQuery = "SELECT name,species,transition,rest_frequency,restf_fits_unit,velocity_fits_unit,description FROM surveys"; dlon = (pos.range.lon2 - pos.range.lon1)/2.0;
dlat = (pos.range.lat2 - pos.range.lat1)/2.0;
/* South-West and North-East corners of a box */
String sw_lon = Double.toString(lon - dlon/2.0);
String sw_lat = Double.toString(lat - dlat/2.0);
String ne_lon = Double.toString(lon + dlon/2.0);
String ne_lat = Double.toString(lat + dlat/2.0);
LOGGER.info("Connecting to: " + dbConn.uri() + " with optional user/pwd: " + dbConn.userName() +" / "+ dbConn.password() ); inputRegion = "sbox '( ("+ sw_lon + "d, " + sw_lat + "d), (" + ne_lon +"d, " + ne_lat + "d) )'";
try( }
Connection conn = DriverManager.getConnection(dbConn.uri(), dbConn.userName(), dbConn.password()); else if (shape.equals("POLYGON"))
Statement st = conn.createStatement();
ResultSet res = st.executeQuery(theQuery);)
{ {
//ResultSet res = doQuery(theQuery); // FIXME redefine Polygon as point-array:
assert(pos.polygon.lon.length == pos.polygon.lat.length);
if(res == null) // Polygon has at least 3 points
inputRegion = "spoly '( (" + pos.polygon.lon[0] + "d, " + pos.polygon.lat[0] + "d),";
for(int ii=1; ii < pos.polygon.lon.length; ii++)
{ {
LOGGER.info("Query yielded no resuls: " + theQuery); inputRegion += ", (" + pos.polygon.lon[ii] + "d, " + pos.polygon.lat[ii] + "d)";
}
inputRegion += " )'";
} }
else else
{ {
while (res.next()) throw new IllegalArgumentException("Coord::shape was: " + shape + " but valid is CIRCLE or RECT");
{
Subsurvey surv = new Subsurvey();
surv.rf = res.getDouble("rest_frequency");
surv.surveyname = res.getString("name");
surv.species = res.getString("species");
surv.transition = res.getString("transition");
surv.rf_unit = res.getString("restf_fits_unit");
surv.vel_unit = res.getString("velocity_fits_unit");
surv.description = res.getString("description");
survList.add(surv);
}
} }
return inputRegion;
} }
catch (SQLException se)
private String toRegionColumnName(Pos.System system)
{ {
logSqlExInfo(se); String dbRegion;
se.printStackTrace(); switch(system)
{
case GALACTIC:
dbRegion = "polygon_region_galactic";
break;
default:
dbRegion = "polygon_region";
} }
return survList.toArray(new Subsurvey[0]); return dbRegion;
} }
*/
...@@ -483,36 +293,5 @@ public class DbPSearch ...@@ -483,36 +293,5 @@ public class DbPSearch
private String getClasspathString()
{
StringBuffer classpath = new StringBuffer("getClasspathString:\r\n");
ClassLoader applicationClassLoader = this.getClass().getClassLoader();
if (applicationClassLoader == null) {
applicationClassLoader = ClassLoader.getSystemClassLoader();
}
URL[] urls = ((URLClassLoader)applicationClassLoader).getURLs();
for(int i=0; i < urls.length; i++) {
classpath.append(urls[i].getFile()).append("\r\n");
}
return classpath.toString();
}
/* Returns the list of JDBC Drivers loaded by the caller's class loader */
private String getRegisteredDriverList()
{
StringBuffer drvList = new StringBuffer("getRegisteredDriverList:\r\n");
for (Enumeration e = DriverManager.getDrivers();
e.hasMoreElements(); )
{
Driver d = (Driver) e.nextElement();
String driverClass = d.getClass().getName();
drvList.append(driverClass).append("\r\n");
}
return drvList.toString();
} }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment