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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qmlsqlconnection.h"
#include "private/qobject_p.h"
#include <QSqlError>
#include <QSqlDriver>
#include <QDebug>
#include <qml.h>
#include <qmlcontext.h>
QT_BEGIN_NAMESPACE
QML_DEFINE_TYPE(QmlSqlConnection, SqlConnection)
class QmlSqlConnectionPrivate: public QObjectPrivate
{
Q_DECLARE_PUBLIC(QmlSqlConnection)
public:
QmlSqlConnectionPrivate() : port(0) {}
int port;
QString name, databaseName, connectionOptions;
QString hostName, userName, password, driver;
};
/*!
\qmlclass SqlConnection QmlSqlConnection
\brief The SqlConnection element describes a connection to an SQL database.
The SqlConnection element works in a similar way to
QSqlDatabase::addDatabase(). It allows setting the database properties
such that the connection does not need to be set up in C++ code.
It differs from QSqlDatabase::addDatabase() in that it will automatically
open the database.
The database can then either be used from an SqlQuery element using its id
as a bind, or using its name. If the database is set up externally to
Qml the query should connect using its name.
\qml
SqlConnection {
id: myConnection
name: "qmlConnection"
driver: "QSQLITE"
databaseName: "mydb.sqlite"
}
SqlQuery { id: listmodel; connection: myConnection; query: "SELECT * FROM mytable" }
SqlQuery { id: othermodel; connection: "qmlConnection"; query: "SELECT * FROM myothertable" }
\endqml
*/
/*!
\qmlproperty QString SqlConnection::name
\default
Defines the connection's name. The name allows the connection to be
retrieved using the connection property of SqlQuery or when
QSqlDatabase::database()
*/
/*!
\qmlproperty list<string> SqlConnection::tables
Defines the set of tables that exist in the database for the connection.
*/
/*!
\qmlproperty string SqlConnection::databaseName
Defines the connection's database name. This is used when opening the
connection to the database.
*/
/*!
\qmlproperty string SqlConnection::driver
Defines the driver type of the connection. This is used when opening the
connection to the database.
*/
/*!
\qmlproperty string SqlConnection::connectOptions
Defines the options used when connecting to the database. These are used
when opening the connection to the database.
*/
/*!
\qmlproperty string SqlConnection::hostName
Defines the connection's host name. This is used when opening the
connection to the database.
*/
/*!
\qmlproperty int SqlConnection::port
Defines the connection's port number. This is used when opening the
connection to the database.
*/
/*!
\qmlproperty string SqlConnection::userName
Defines the connection's user name. This is used when opening the
connection to the database.
*/
/*!
\qmlproperty string SqlConnection::password
Defines the connection's password. This is used when opening the
connection to the database.
*/
/*!
\qmlproperty string SqlConnection::lastError
Defines the last error, if one occurred, when working with the database.
If the error occurred in conjunction with an SQL query the error will be
defined by SqlQuery::lastError.
*/
/*!
\internal
\class QmlSqlConnection
\brief The QmlSqlConnection class manages a connection to an SQL database.
*/
/*!
Constructs a QmlSqlConnection with the given \a parent.
*/
QmlSqlConnection::QmlSqlConnection(QObject *parent)
: QObject(*(new QmlSqlConnectionPrivate), parent)
{
}
/*!
Destroys the QmlSqlConnection.
*/
QmlSqlConnection::~QmlSqlConnection()
{
QSqlDatabase db = database();
if (db.isOpen())
db.close();
}
/*!
Returns the connection's name.
This is equivalent to QSqlDatabase::connectionName().
\sa setName()
*/
QString QmlSqlConnection::name() const
{
Q_D(const QmlSqlConnection);
return d->name;
}
/*!
Returns the connection's database name.
This is equivalent to QSqlDatabase::databaseName().
\sa setDatabaseName()
*/
QString QmlSqlConnection::databaseName() const
{
Q_D(const QmlSqlConnection);
return d->databaseName;
}
/*!
Returns the connect options string used for this connection.
\sa setConnectOptions()
*/
QString QmlSqlConnection::connectOptions() const
{
Q_D(const QmlSqlConnection);
return d->connectionOptions;
}
/*!
Returns the connection's host name.
\sa setHostName()
*/
QString QmlSqlConnection::hostName() const
{
Q_D(const QmlSqlConnection);
return d->hostName;
}
/*!
Returns the connection's port number. The value is undefined if the port
number has not been set.
\sa setPort()
*/
int QmlSqlConnection::port() const
{
Q_D(const QmlSqlConnection);
return d->port;
}
/*!
Returns the connection's user name.
\sa setUserName()
*/
QString QmlSqlConnection::userName() const
{
Q_D(const QmlSqlConnection);
return d->userName;
}
/*!
Returns the connection's password.
\sa setPassword)()
*/
QString QmlSqlConnection::password() const
{
Q_D(const QmlSqlConnection);
return d->password;
}
/*!
Returns the connection's driver name.
\sa setDriver()
*/
QString QmlSqlConnection::driver() const
{
Q_D(const QmlSqlConnection);
return d->driver;
}
/*!
Returns a list of the database's tables.
*/
QStringList QmlSqlConnection::tables() const
{
return database().tables();
}
/*!
Sets the connection's name to the given \a name.
\sa name()
*/
void QmlSqlConnection::setName(const QString &name)
{
Q_D(QmlSqlConnection);
d->name = name;
}
/*!
Set's the connection's database name to the given \a name.
\sa databaseName()
*/
void QmlSqlConnection::setDatabaseName(const QString &name)
{
Q_D(QmlSqlConnection);
d->databaseName = name;
}
/*!
Sets the connection's options to the given \a options.
\sa connectOptions(), QSqlDatabase::setConnectOptions()
*/
void QmlSqlConnection::setConnectOptions(const QString &options)
{
Q_D(QmlSqlConnection);
d->connectionOptions = options;
}
/*!
Sets the connection's host name to the given \a name.
\sa hostName()
*/
void QmlSqlConnection::setHostName(const QString &name)
{
Q_D(QmlSqlConnection);
d->hostName = name;
}
/*!
Sets the connection's port number to the given \a port.
\sa port()
*/
void QmlSqlConnection::setPort(int port)
{
Q_D(QmlSqlConnection);
d->port = port;
}
/*!
Sets the connection's user name to the given \a name.
\sa userName()
*/
void QmlSqlConnection::setUserName(const QString &name)
{
Q_D(QmlSqlConnection);
d->userName = name;
}
/*!
Sets the connection's password to the given \a password.
\sa password()
*/
void QmlSqlConnection::setPassword(const QString &password)
{
Q_D(QmlSqlConnection);
d->password = password;
}
/*!
Returns information about the last error that occurred on the database.
Failures that occur in conjunction with an individual query are
reported by QmlSqlQuery::lastError()
*/
QString QmlSqlConnection::lastError() const
{
return database().lastError().text();
}
/*!
Sets the connection's driver to the specified driver \a type.
\sa driver(), QSqlDatabase::addDatabase()
*/
void QmlSqlConnection::setDriver(const QString &type)
{
Q_D(QmlSqlConnection);
d->driver = type;
}
/*!
\reimp
*/
void QmlSqlConnection::classComplete()
{
}
/*!
Returns the database object associated with this connection.
If the database is not yet open, it will open the database
passed on the settings specified for the SQL connection.
*/
QSqlDatabase QmlSqlConnection::database() const
{
Q_D(const QmlSqlConnection);
QSqlDatabase db;
if (QSqlDatabase::connectionNames().contains(d->name)) {
db = QSqlDatabase::database(d->name);
} else {
db = QSqlDatabase::addDatabase(
d->driver.isEmpty()
? QLatin1String("QSQLITE")
: d->driver,
d->name.isEmpty()
? QLatin1String(QSqlDatabase::defaultConnection)
: d->name);
}
if (db.isOpen())
return db;
if ((d->driver.isEmpty() || d->driver == QLatin1String("QSQLITE")) &&
qmlContext(this)) {
// SQLITE uses files for databases, hence use relative pathing
// if possible.
QUrl url = qmlContext(this)->resolvedUrl(QUrl(d->databaseName));
if (url.isRelative() || url.scheme() == QLatin1String("file"))
db.setDatabaseName(url.toLocalFile());
else
db.setDatabaseName(d->databaseName);
} else {
db.setDatabaseName(d->databaseName);
}
db.setConnectOptions(d->connectionOptions);
db.setHostName(d->hostName);
db.setPassword(d->password);
db.setPort(d->port);
db.setUserName(d->userName);
if (!db.open())
qWarning() << "Failed to open database" << lastError();
return db;
}
QT_END_NAMESPACE
|