Commit 583d0902 authored by Porter Libby's avatar Porter Libby
Browse files

format query strings way better

parent 8461ec19
......@@ -23,7 +23,10 @@ const pool = new Pool({
})
const getTrips = (request, response) => {
pool.query("SELECT tripName, tripID from fieldday_trip;", (error, results) => {
var query = "SELECT tripName, tripID from fieldday_trip;";
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -31,7 +34,10 @@ const getTrips = (request, response) => {
})
}
const getSites = (request, response) => {
pool.query('SELECT siteName, siteID from fieldday_site where tripid='+ (request.query.id) + ';', (error, results) => {
var query = 'SELECT siteName, siteID from fieldday_site where tripid='+ (request.query.id) + ';';
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -39,7 +45,11 @@ const getSites = (request, response) => {
})
}
const getSectors = (request, response) => {
pool.query('SELECT sectorname, sectorid from fieldday_sector where tripid='+ (request.query.tripid) + ' and siteid=' + (request.query.siteid) + ';', (error, results) => {
var query = 'SELECT sectorname, sectorid from fieldday_sector where tripid='+ (request.query.tripid)
+ ' and siteid=' + (request.query.siteid) + ';'
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -47,7 +57,12 @@ const getSectors = (request, response) => {
})
}
const getSpots = (request, response) => {
pool.query('SELECT spotid from fieldday_spot where tripid='+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid +';', (error, results) => {
var query = 'SELECT spotid from fieldday_spot where tripid='+ (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid +';';
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -55,7 +70,14 @@ const getSpots = (request, response) => {
})
}
const getReadingsPlatforms = (request, response) => {
pool.query('SELECT DISTINCT platformid, (SELECT DISTINCT platformname FROM fieldday_platform where platformid=fieldday_streaming.platformid) FROM fieldday_streaming where tripid='+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid + ';', (error, results) => {
var query = 'SELECT DISTINCT platformid, '
+'(SELECT DISTINCT platformname FROM fieldday_platform where platformid=fieldday_streaming.platformid)'
+' FROM fieldday_streaming where tripid='+ (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid + ';';
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -63,7 +85,13 @@ const getReadingsPlatforms = (request, response) => {
})
}
const getReadingsDates = (request, response) => {
pool.query('SELECT DISTINCT recordtime FROM fieldday_streaming where tripid='+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid + " and platformid='" + request.query.platformid + "';", (error, results) => {
var query = 'SELECT DISTINCT recordtime FROM fieldday_streaming where tripid='+ (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid
+ " and platformid='" + request.query.platformid + "';";
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -71,7 +99,13 @@ const getReadingsDates = (request, response) => {
})
}
const getReadings = (request, response) => {
var query = "SELECT tripid,siteid,sectorid,spotid,platformid,(SELECT sensortype FROM fieldday_sensor where sensorid=fieldday_reading.sensorid),recordtime,latitude,longitude,elevation,accuracy,satellites,quality,value,value_2,value_3,value_4,value_5,value_6 from fieldday_reading where tripid="+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid + ' and ';
var query = "SELECT tripid,siteid,sectorid,spotid,platformid,"
+"(SELECT sensortype FROM fieldday_sensor where sensorid=fieldday_reading.sensorid)"
+",recordtime,latitude,longitude,elevation,accuracy,satellites,quality,value,value_2"
+",value_3,value_4,value_5,value_6 from fieldday_reading where tripid="+ (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid + ' and ';
if (request.query.spotids.length > 1){
query += "(";
}
......@@ -86,7 +120,7 @@ const getReadings = (request, response) => {
}
query += ';';
console.info("Database:" + query);
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
......@@ -96,7 +130,14 @@ const getReadings = (request, response) => {
})
}
const getStreamingsHosts = (request, response) => {
pool.query('SELECT DISTINCT hostid, (SELECT DISTINCT hostname FROM fieldday_host_temp where hostid=fieldday_streaming.hostid) FROM fieldday_streaming where tripid='+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid + ';', (error, results) => {
var query = 'SELECT DISTINCT hostid, '
+'(SELECT DISTINCT hostname FROM fieldday_host_temp where hostid=fieldday_streaming.hostid)'
+' FROM fieldday_streaming where tripid='+ (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid + ';';
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -104,9 +145,15 @@ const getStreamingsHosts = (request, response) => {
})
}
const getStreamingsPlatforms = (request, response) => {
console.log('SELECT DISTINCT platformid, (SELECT DISTINCT platformname FROM fieldday_platform where platformid=fieldday_streaming.platformid) FROM fieldday_streaming where tripid='+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid +' and hostid='+ request.query.hostid + ';');
var query = 'SELECT DISTINCT platformid, '
+'(SELECT DISTINCT platformname FROM fieldday_platform where platformid=fieldday_streaming.platformid)'
+' FROM fieldday_streaming where tripid='+ (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid
+" and hostid='"+ request.query.hostid + "';";
serverOut(query);
pool.query('SELECT DISTINCT platformid, (SELECT DISTINCT platformname FROM fieldday_platform where platformid=fieldday_streaming.platformid) FROM fieldday_streaming where tripid='+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid +" and hostid='"+ request.query.hostid + "';", (error, results) => {
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -114,7 +161,13 @@ const getStreamingsPlatforms = (request, response) => {
})
}
const getStreamingsDates = (request, response) => {
pool.query('SELECT DISTINCT recordtime FROM fieldday_streaming where tripid='+ (request.query.tripid) +' and siteid='+ (request.query.siteid) +' and sectorid='+ request.query.sectorid + " and platformid='" + request.query.platformid + "';", (error, results) => {
var query = 'SELECT DISTINCT recordtime FROM fieldday_streaming where tripid='+ (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid
+ " and platformid='" + request.query.platformid + "';";
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
......@@ -122,20 +175,27 @@ const getStreamingsDates = (request, response) => {
})
}
const getStreamings = (request, response) => {
pool.query('SELECT tripid,siteid,sectorid,hostid,platformid,sensorid,(SELECT sensortype FROM fieldday_sensor where sensorid=fieldday_streaming.sensorid),(SELECT sensorunits FROM fieldday_sensor where sensorid=fieldday_streaming.sensorid),recordtime,longitude,latitude,quality,elevation,accuracy,satellites,value_1,value_2,value_3,value_4,value_5,value_6'
var query = 'SELECT tripid,siteid,sectorid,hostid,platformid,sensorid,'
+'(SELECT sensortype FROM fieldday_sensor where sensorid=fieldday_streaming.sensorid)'
+',(SELECT sensorunits FROM fieldday_sensor where sensorid=fieldday_streaming.sensorid)'
+',recordtime,longitude,latitude,quality,elevation,accuracy,satellites,value_1,value_2,value_3,value_4,value_5,value_6'
+' from fieldday_streaming where tripid=' + (request.query.tripid)
+' and siteid='+ (request.query.siteid)
+' and sectorid='+ request.query.sectorid
+' and platformid='+ "'"+ request.query.platformid + "'"
+" and substr(recordtime::text, 0, 11) like '" + request.query.date + "'"
+ ';', (error, results) => {
+" and substr(recordtime::text, 0, 11) like '" + request.query.date + "'" + ';';
serverOut(query);
pool.query(query, (error, results) => {
if (error) {
throw error
}
response.status(200).json(results.rows)
})
}
function serverOut(querytext){
console.info("DATABASE: " + querytext + "\n");
}
function checkConnection(){
console.log('Database: starting connection...');
pool.query("SELECT * FROM fieldday_trip;", (error, results) => {
......@@ -144,7 +204,7 @@ function checkConnection(){
console.log('Halting...');
process.exit(1);
}else{
console.log('Database: connection successful!');
console.log('Database: connection successful!\n');
}
})
}
......
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