summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/queryhandlerpracro.cc81
1 files changed, 63 insertions, 18 deletions
diff --git a/server/src/queryhandlerpracro.cc b/server/src/queryhandlerpracro.cc
index e26de0c..ae2f0f6 100644
--- a/server/src/queryhandlerpracro.cc
+++ b/server/src/queryhandlerpracro.cc
@@ -30,6 +30,8 @@
#include <stdlib.h>
+#include "configuration.h"
+
QueryHandlerPracro::QueryHandlerPracro(Database &_db, std::string cpr)
: db(_db)
{
@@ -39,43 +41,86 @@ QueryHandlerPracro::QueryHandlerPracro(Database &_db, std::string cpr)
QueryResult QueryHandlerPracro::exec(Query &query)
{
QueryResult result;
+ result.timestamp = 0;
+ result.source = "";
std::string field = query.attributes["class"];
Fieldnames fields;
fields.push_back(field);
- std::string ttl = query.attributes["ttl"];
- time_t oldest = time(NULL) - atol(ttl.c_str());
+ time_t oldest;
+ if(query.attributes.find("ttl") != query.attributes.end()) {
+ std::string ttl = query.attributes["ttl"];
+ oldest = time(NULL) - atol(ttl.c_str());
+ } else {
+ oldest = time(NULL) - Conf::db_max_ttl;
+ }
Values values = db.getValues(cpr, fields, oldest);
- std::string value = values[field].value;
- time_t timestamp = values[field].timestamp;
+ if(values.find(field) != values.end()) {
+ std::string value = values[field].value;
+ time_t timestamp = values[field].timestamp;
- result.timestamp = timestamp;
- result.values[field] = value;
- result.source = "pracrodb";
+ result.timestamp = timestamp;
+ result.values[field] = value;
+ result.source = "pracrodb";
- PRACRO_DEBUG(queryhandler,"%s => %s (%lu)\n", field.c_str(), value.c_str(), timestamp);
+ PRACRO_DEBUG(queryhandler,"%s => %s (%lu)\n", field.c_str(), value.c_str(), timestamp);
+ }
return result;
}
#ifdef TEST_QUERYHANDLERPRACRO
-#include "configuration.h"
+#include <time.h>
+
+#define PATIENTID "1234567890"
int main()
{
- Database db("testdb", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, "");
-
- QueryHandlerPracro qh(db, "2003791613");
-
- Query q1;
- q1.attributes["device_id"] = "lensmeter";
- q1.attributes["device_type"] = "lensmeter";
- QueryResult res = qh.exec(q1);
- res.print();
+ time_t now = time(NULL);
+ Database db("testdb", "", "", "", "", "");
+ Macro macro;
+ Fields fields;
+ QueryHandlerPracro qh(db, PATIENTID);
+
+ Query query;
+ QueryResult res;
+
+ fields["myfield"] = "myval";
+ db.commitTransaction("testuser", PATIENTID, macro, fields, now - Conf::db_max_ttl - 1);
+
+ query.attributes["class"] = "myfield";
+ res = qh.exec(query);
+ if(res.timestamp != 0) return 1;
+ if(res.source != "") return 1;
+ if(res.values.size() != 0) return 1;
+
+ fields["myfield"] = "myval";
+ db.commitTransaction("testuser", PATIENTID, macro, fields, now - 100);
+
+ query.attributes["class"] = "myfield";
+ query.attributes["ttl"] = "99";
+ res = qh.exec(query);
+ if(res.timestamp != 0) return 1;
+ if(res.source != "") return 1;
+ if(res.values.size() != 0) return 1;
+
+ query.attributes["class"] = "nosuchfield";
+ query.attributes["ttl"] = "10000";
+ res = qh.exec(query);
+ if(res.timestamp != 0) return 1;
+ if(res.source != "") return 1;
+ if(res.values.size() != 0) return 1;
+
+ query.attributes["class"] = "myfield";
+ query.attributes["ttl"] = "100";
+ res = qh.exec(query);
+ if(res.timestamp != now - 100) return 1;
+ if(res.source != "pracrodb") return 1;
+ if(res.values["myfield"] != "myval") return 1;
return 0;
}