diff options
Diffstat (limited to 'client/widgets/dbwidget.cc')
-rw-r--r-- | client/widgets/dbwidget.cc | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/client/widgets/dbwidget.cc b/client/widgets/dbwidget.cc index d0f4def..69c868e 100644 --- a/client/widgets/dbwidget.cc +++ b/client/widgets/dbwidget.cc @@ -34,7 +34,7 @@ #include <QSqlQuery> #include <QSqlError> #include <QLineEdit> - +#include "formatparser.h" #include "common.h" DBWidget::DBWidget(QDomNode &node, MacroWindow *macrowindow) @@ -42,11 +42,28 @@ DBWidget::DBWidget(QDomNode &node, MacroWindow *macrowindow) { QDomElement elem = node.toElement(); - db = QSqlDatabase::addDatabase("QPSQL"); - db.setHostName("sensei.j.auh.dk"); - db.setDatabaseName("lms"); - db.setUserName("postgres"); - // db.setPassword(""); + if(!elem.hasAttribute("driver") || + !elem.hasAttribute("server") || + !elem.hasAttribute("user") || + !elem.hasAttribute("database") || + !elem.hasAttribute("select") || + !elem.hasAttribute("from") || + !elem.hasAttribute("where") || + !elem.hasAttribute("format")) { + printf("Error: missing tag!\n"); + } + + select = elem.attribute("select"); + from = elem.attribute("from"); + where = elem.attribute("where"); + format = elem.attribute("format"); + + db = QSqlDatabase::addDatabase(elem.attribute("driver")); + db.setHostName(elem.attribute("server")); + if(elem.hasAttribute("port")) db.setPort(elem.attribute("port").toInt()); + db.setDatabaseName(elem.attribute("database")); + db.setUserName(elem.attribute("user")); + if(elem.hasAttribute("password")) db.setPassword(elem.attribute("password")); bool ok = db.open(); if(!ok) { printf("DB connect failed!\n"); @@ -108,9 +125,7 @@ void DBWidget::setValue(QString value) bool DBWidget::isValid() { - QSqlQuery query = db.exec("SELECT drugname, dosageform_code, strength_text FROM lms01 WHERE" - " LOWER(drugname || ' ' || strength_text || ' (' || dosageform_code || ')' )" - " = '" + currentText().toLower() + "';"); + QSqlQuery query = db.exec("SELECT " + select + " FROM " + from + " WHERE LOWER(" + where + ") = '" + currentText().toLower() + "';"); return query.size() != 0; } @@ -121,7 +136,7 @@ void DBWidget::changed() if(isValid() && luaValidator()) { // valid string - palette.setBrush(QPalette::Base, QBrush(QColor(128, 255, 128))); + palette.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255))); } else { // invalid string palette.setBrush(QPalette::Base, QBrush(QColor(230, 200, 200))); @@ -140,16 +155,11 @@ void DBWidget::update_list(QString prefix) return; } */ - QSqlQuery query = db.exec("SELECT drugname, dosageform_code, strength_text FROM lms01 WHERE" - " LOWER(drugname || ' ' || strength_text || ' (' || dosageform_code || ')' )" - " LIKE '" + prefix.toLower() + "%';"); + QSqlQuery query = db.exec("SELECT " + select + " FROM " + from + " WHERE LOWER(" + where + ") LIKE '" + prefix.toLower() + "%';"); QStringList lst; while(query.next()) { - QString drugname = query.value(0).toString(); - QString dosageform = query.value(1).toString(); - QString strength = query.value(2).toString(); - lst << drugname + " " + strength + " (" + dosageform + ")"; + lst << format_parser(format, query); } lst.sort(); @@ -158,10 +168,11 @@ void DBWidget::update_list(QString prefix) if(mdl->stringList() != lst) { QString val = currentText(); clear(); - if(lst.size() != 0) addItems(lst); - else addItem("Søgningen passede ikke på noget."); + if(lst.size() == 0) lst << "Søgningen passede ikke på noget."; + addItems(lst); setEditText(val); mdl->setStringList(lst); + //showPopup(); } } |