From 4dd7add65ed2648e7c139df3138e489f8dbd8e43 Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 3 Sep 2008 15:07:50 +0000 Subject: The dbwidget now work generally, controlled by the xml. --- client/formatparser.cc | 79 ++++++++++++++++++++++++++++++++++++++++++++++ client/formatparser.h | 2 ++ client/widgets/dbwidget.cc | 49 +++++++++++++++++----------- client/widgets/dbwidget.h | 5 +++ 4 files changed, 116 insertions(+), 19 deletions(-) diff --git a/client/formatparser.cc b/client/formatparser.cc index 260a2bf..233a694 100644 --- a/client/formatparser.cc +++ b/client/formatparser.cc @@ -26,6 +26,8 @@ */ #include "formatparser.h" +#include +#include #include QString format_parser(QString format, QVector< Widget *> widgets) @@ -98,3 +100,80 @@ QString format_parser(QString format, QVector< Widget *> widgets) return resume; } + + +QString format_parser(QString format, QSqlQuery &query) +{ + QString resume; + QString var; + + QChar *p = format.data(); + QChar *theend = p + format.size(); + while(p < theend) { + switch((*p).toLatin1()) { + case '$': + p++; + switch((*p).toLatin1()) { + case '$': + resume.append(*p); + break; + + case '{': + p++; + var = ""; + // Parser + while(p < theend && *p != '}') { + var.append(*p); + p++; + } + { + int fieldNo = query.record().indexOf(var); + resume += query.value(fieldNo).toString(); + + /* + QVector< Widget* >::iterator i = widgets.begin(); + while (i != widgets.end()) { + Widget* w = *i; + if(w->getName() == var) resume += w->getValue(); + i++; + } + */ + } + break; + + default: + resume.append(*p); + break; + } + p++; + break; + + case '\\': + p++; + switch((*p).toLatin1()) { + case 't': + resume.append('\t'); + break; + case 'n': + resume.append('\n'); + break; + case 'r': + resume.append('\r'); + break; + case '\\': + default: + resume.append(*p); + break; + } + p++; + break; + + default: + resume.append(QChar(*p)); + p++; + break; + } + } + + return resume; +} diff --git a/client/formatparser.h b/client/formatparser.h index 8612a0e..5d900b0 100644 --- a/client/formatparser.h +++ b/client/formatparser.h @@ -30,7 +30,9 @@ #include #include #include "widgets/widget.h" +#include QString format_parser(QString format, QVector< Widget *> widgets); +QString format_parser(QString format, QSqlQuery &query); #endif/*__PRACRO_FORMATPARSER_H__*/ 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 #include #include - +#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(); } } diff --git a/client/widgets/dbwidget.h b/client/widgets/dbwidget.h index 0438ca5..98499b9 100644 --- a/client/widgets/dbwidget.h +++ b/client/widgets/dbwidget.h @@ -59,6 +59,11 @@ protected: private: QSqlDatabase db; + + QString select; + QString from; + QString where; + QString format; }; #endif/*__PRACRO_DBWIDGET_H__*/ -- cgit v1.2.3