1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* dbwidget.cc
*
* Mon Sep 1 16:23:54 CEST 2008
* Copyright 2008 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Pracro.
*
* Pracro is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Pracro is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "dbwidget.h"
#include <QDomNodeList>
#include <QCompleter>
#include <QRegExpValidator>
#include <QRegExp>
#include <QStringListModel>
#include <QSqlQuery>
#include <QSqlError>
#include <QLineEdit>
#include "formatparser.h"
#include "common.h"
DBWidget::DBWidget(QDomNode &node, MacroWindow *macrowindow)
: QComboBox(), Widget(node, macrowindow)
{
changedByUser = true;
QDomElement elem = node.toElement();
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"));
db.setConnectOptions("connect_timeout=2000");
bool ok = db.open();
if(!ok) {
printf("DB connect failed!\n");
}
setCommonAttributes(this, node);
setInsertPolicy(QComboBox::InsertAlphabetically);
setEditable(true);
// Make empty default selection.
addItem("Skriv noget i søgefeltet.");
setCurrentIndex(-1);
QStringListModel *strlst = new QStringListModel();
QCompleter *completer = new QCompleter(this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setModel(strlst);
setCompleter(completer);
connect(this, SIGNAL(editTextChanged(QString)), this, SLOT(changed()));
connect((QWidget*)lineEdit(), SIGNAL(textEdited(QString)), this, SLOT(update_list(QString)));
changed();
}
DBWidget::~DBWidget()
{
db.close();
db = QSqlDatabase();
}
QString DBWidget::getValue()
{
QString value;
value = currentText();
return value;
}
void DBWidget::setValue(QString value, QString source)
{
changedByUser = false;
if(isUserSource(source)) emit wasChanged();
setEditText(value);
setInitialValue(value);
changedByUser = true;
}
bool DBWidget::isValid()
{
QSqlQuery query = db.exec("SELECT " + select + " FROM " + from + " WHERE LOWER(" + where + ") = '" + currentText().toLower() + "';");
return query.size() != 0;
}
void DBWidget::changed()
{
QPalette palette;
if(isValid() && luaValidator()) {
// valid string
palette.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255)));
} else {
// invalid string
palette.setBrush(QPalette::Base, QBrush(QColor(230, 200, 200)));
}
lineEdit()->setPalette(palette);
if(changedByUser) emit wasChanged();
}
void DBWidget::update_list(QString prefix)
{
/*
if(prefix == "") {
clear();
addItem("Skriv noget i søgefeltet.");
setCurrentIndex(-1);
return;
}
*/
QSqlQuery query = db.exec("SELECT " + select + " FROM " + from + " WHERE LOWER(" + where + ") LIKE '" + prefix.toLower() + "%';");
QStringList lst;
while(query.next()) {
lst << format_parser(format, query);
}
lst.sort();
QStringListModel *mdl = (QStringListModel *)completer()->model();
if(mdl->stringList() != lst) {
QString val = currentText();
clear();
if(lst.size() == 0) lst << "Søgningen passede ikke på noget.";
addItems(lst);
setEditText(val);
mdl->setStringList(lst);
//showPopup();
}
}
void DBWidget::enable()
{
setEnabled(true);
}
void DBWidget::disable()
{
setEnabled(false);
}
bool DBWidget::isDisabled()
{
return isEnabled() == false;
}
void DBWidget::connectFrom(const char *signal,
const QObject *receiver, const char *method)
{
connect(this, signal, receiver, method);
}
void DBWidget::connectTo(const QObject *sender, const char *signal,
const char *method)
{
connect(sender, signal, this, method);
}
bool DBWidget::setKeyboardFocus()
{
setFocus();
return true;
}
void DBWidget::setVisibility(bool visible)
{
setVisible(visible);
}
/*
$ psql -h sensei -d lms -U postgres
===================================================================
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | lms01 | table | postgres
(1 row)
lms=# \d lms01
Table "public.lms01"
Column | Type | Modifiers
-------------------------------+-----------------------+-----------
drugid | character varying(32) |
producttype | character varying(14) |
productsubtype | character varying(14) |
sequencetext | character varying(28) |
specialitynumber | character varying(20) |
drugname | character varying(70) |
dosageform_text | character varying(50) |
dosageform_code | character varying(24) |
further_dos_info | character varying(24) |
strength_text | character varying(50) |
strength_numeric | character varying(30) |
strength_unit | character varying(16) |
marketing_authorisation_owner | character varying(22) |
importer | character varying(22) |
atc | character varying(26) |
route_of_administration | character varying(26) |
traffic_warning | character varying(12) |
substitution | character varying(12) |
substitution_group | character varying(18) |
dose_dispensing | character varying(12) |
deregistration_date | character varying(26) |
quarantine_date | character varying(26) |
*/
|