Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/sql/kernel/qsqldatabase.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | namespace { namespace Q_QGS_loader { typedef QFactoryLoader Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized) guard.store(QtGlobalStatic::Destroyed); } }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type ("org.qt-project.Qt.QSqlDriverFactoryInterface", QLatin1String("/sqldrivers")))) : value ("org.qt-project.Qt.QSqlDriverFactoryInterface", QLatin1String("/sqldrivers")) { guard.store(QtGlobalStatic::Initialized); } } holder; return &holder.value; } } } static QGlobalStatic<QFactoryLoader, Q_QGS_loader::innerFunction, Q_QGS_loader::guard> loader; | - | ||||||||||||
6 | - | |||||||||||||
7 | - | |||||||||||||
8 | - | |||||||||||||
9 | - | |||||||||||||
10 | - | |||||||||||||
11 | const | - | ||||||||||||
12 | - | |||||||||||||
13 | char *QSqlDatabase::defaultConnection = const_cast<char *>("qt_sql_default_connection"); | - | ||||||||||||
14 | - | |||||||||||||
15 | typedef QHash<QString, QSqlDriverCreatorBase*> DriverDict; | - | ||||||||||||
16 | - | |||||||||||||
17 | class QConnectionDict: public QHash<QString, QSqlDatabase> | - | ||||||||||||
18 | { | - | ||||||||||||
19 | public: | - | ||||||||||||
20 | inline bool contains_ts(const QString &key) | - | ||||||||||||
21 | { | - | ||||||||||||
22 | QReadLocker locker(&lock); | - | ||||||||||||
23 | return contains(key); | - | ||||||||||||
24 | } | - | ||||||||||||
25 | inline QStringList keys_ts() const | - | ||||||||||||
26 | { | - | ||||||||||||
27 | QReadLocker locker(&lock); | - | ||||||||||||
28 | return keys(); | - | ||||||||||||
29 | } | - | ||||||||||||
30 | - | |||||||||||||
31 | mutable QReadWriteLock lock; | - | ||||||||||||
32 | }; | - | ||||||||||||
33 | namespace { namespace Q_QGS_dbDict { typedef QConnectionDict Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized) guard.store(QtGlobalStatic::Destroyed); } }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type ())) : value () { guard.store(QtGlobalStatic::Initialized); } } holder; return &holder.value; } } } static QGlobalStatic<QConnectionDict, Q_QGS_dbDict::innerFunction, Q_QGS_dbDict::guard> dbDict; | - | ||||||||||||
34 | - | |||||||||||||
35 | class QSqlDatabasePrivate | - | ||||||||||||
36 | { | - | ||||||||||||
37 | public: | - | ||||||||||||
38 | QSqlDatabasePrivate(QSqlDatabase *d, QSqlDriver *dr = 0): | - | ||||||||||||
39 | ref(1), | - | ||||||||||||
40 | q(d), | - | ||||||||||||
41 | driver(dr), | - | ||||||||||||
42 | port(-1) | - | ||||||||||||
43 | { | - | ||||||||||||
44 | precisionPolicy = QSql::LowPrecisionDouble; | - | ||||||||||||
45 | } | - | ||||||||||||
46 | QSqlDatabasePrivate(const QSqlDatabasePrivate &other); | - | ||||||||||||
47 | ~QSqlDatabasePrivate(); | - | ||||||||||||
48 | void init(const QString& type); | - | ||||||||||||
49 | void copy(const QSqlDatabasePrivate *other); | - | ||||||||||||
50 | void disable(); | - | ||||||||||||
51 | - | |||||||||||||
52 | QAtomicInt ref; | - | ||||||||||||
53 | QSqlDatabase *q; | - | ||||||||||||
54 | QSqlDriver* driver; | - | ||||||||||||
55 | QString dbname; | - | ||||||||||||
56 | QString uname; | - | ||||||||||||
57 | QString pword; | - | ||||||||||||
58 | QString hname; | - | ||||||||||||
59 | QString drvName; | - | ||||||||||||
60 | int port; | - | ||||||||||||
61 | QString connOptions; | - | ||||||||||||
62 | QString connName; | - | ||||||||||||
63 | QSql::NumericalPrecisionPolicy precisionPolicy; | - | ||||||||||||
64 | - | |||||||||||||
65 | static QSqlDatabasePrivate *shared_null(); | - | ||||||||||||
66 | static QSqlDatabase database(const QString& name, bool open); | - | ||||||||||||
67 | static void addDatabase(const QSqlDatabase &db, const QString & name); | - | ||||||||||||
68 | static void removeDatabase(const QString& name); | - | ||||||||||||
69 | static void invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn = true); | - | ||||||||||||
70 | static DriverDict &driverDict(); | - | ||||||||||||
71 | static void cleanConnections(); | - | ||||||||||||
72 | }; | - | ||||||||||||
73 | - | |||||||||||||
74 | QSqlDatabasePrivate::QSqlDatabasePrivate(const QSqlDatabasePrivate &other) : ref(1) | - | ||||||||||||
75 | { | - | ||||||||||||
76 | q = other.q; | - | ||||||||||||
77 | dbname = other.dbname; | - | ||||||||||||
78 | uname = other.uname; | - | ||||||||||||
79 | pword = other.pword; | - | ||||||||||||
80 | hname = other.hname; | - | ||||||||||||
81 | drvName = other.drvName; | - | ||||||||||||
82 | port = other.port; | - | ||||||||||||
83 | connOptions = other.connOptions; | - | ||||||||||||
84 | driver = other.driver; | - | ||||||||||||
85 | precisionPolicy = other.precisionPolicy; | - | ||||||||||||
86 | } | - | ||||||||||||
87 | - | |||||||||||||
88 | QSqlDatabasePrivate::~QSqlDatabasePrivate() | - | ||||||||||||
89 | { | - | ||||||||||||
90 | if (driver != shared_null()->driver) | - | ||||||||||||
91 | delete driver; | - | ||||||||||||
92 | } | - | ||||||||||||
93 | - | |||||||||||||
94 | void QSqlDatabasePrivate::cleanConnections() | - | ||||||||||||
95 | { | - | ||||||||||||
96 | QConnectionDict *dict = dbDict(); | - | ||||||||||||
97 | ((!(dict)) ? qt_assert("dict",__FILE__,190194) : qt_noop()); | - | ||||||||||||
98 | QWriteLocker locker(&dict->lock); | - | ||||||||||||
99 | - | |||||||||||||
100 | QConnectionDict::iterator it = dict->begin(); | - | ||||||||||||
101 | while (it != dict->end()) { | - | ||||||||||||
102 | invalidateDb(it.value(), it.key(), false); | - | ||||||||||||
103 | ++it; | - | ||||||||||||
104 | } | - | ||||||||||||
105 | dict->clear(); | - | ||||||||||||
106 | } | - | ||||||||||||
107 | - | |||||||||||||
108 | static bool qDriverDictInit = false; | - | ||||||||||||
109 | static void cleanDriverDict() | - | ||||||||||||
110 | { | - | ||||||||||||
111 | qDeleteAll(QSqlDatabasePrivate::driverDict()); | - | ||||||||||||
112 | QSqlDatabasePrivate::driverDict().clear(); | - | ||||||||||||
113 | QSqlDatabasePrivate::cleanConnections(); | - | ||||||||||||
114 | qDriverDictInit = false; | - | ||||||||||||
115 | } | - | ||||||||||||
116 | - | |||||||||||||
117 | DriverDict &QSqlDatabasePrivate::driverDict() | - | ||||||||||||
118 | { | - | ||||||||||||
119 | static DriverDict dict; | - | ||||||||||||
120 | if (!qDriverDictInit) { | - | ||||||||||||
121 | qDriverDictInit = true; | - | ||||||||||||
122 | qAddPostRoutine(cleanDriverDict); | - | ||||||||||||
123 | } | - | ||||||||||||
124 | return dict; | - | ||||||||||||
125 | } | - | ||||||||||||
126 | - | |||||||||||||
127 | QSqlDatabasePrivate *QSqlDatabasePrivate::shared_null() | - | ||||||||||||
128 | { | - | ||||||||||||
129 | static QSqlNullDriver dr; | - | ||||||||||||
130 | static QSqlDatabasePrivate n(__null, &dr); | - | ||||||||||||
131 | return &n; | - | ||||||||||||
132 | } | - | ||||||||||||
133 | - | |||||||||||||
134 | void QSqlDatabasePrivate::invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn) | - | ||||||||||||
135 | { | - | ||||||||||||
136 | if (db.d->ref.load() != 1 && doWarn) { | - | ||||||||||||
137 | QMessageLogger(__FILE__, 230234, __PRETTY_FUNCTION__).warning("QSqlDatabasePrivate::removeDatabase: connection '%s' is still in use, " | - | ||||||||||||
138 | "all queries will cease to work.", name.toLocal8Bit().constData()); | - | ||||||||||||
139 | db.d->disable(); | - | ||||||||||||
140 | db.d->connName.clear(); | - | ||||||||||||
141 | } | - | ||||||||||||
142 | } | - | ||||||||||||
143 | - | |||||||||||||
144 | void QSqlDatabasePrivate::removeDatabase(const QString &name) | - | ||||||||||||
145 | { | - | ||||||||||||
146 | QConnectionDict *dict = dbDict(); | - | ||||||||||||
147 | ((!(dict)) ? qt_assert("dict",__FILE__,240244) : qt_noop()); | - | ||||||||||||
148 | QWriteLocker locker(&dict->lock); | - | ||||||||||||
149 | - | |||||||||||||
150 | if (!dict->contains(name)) | - | ||||||||||||
151 | return; | - | ||||||||||||
152 | - | |||||||||||||
153 | invalidateDb(dict->take(name), name); | - | ||||||||||||
154 | } | - | ||||||||||||
155 | - | |||||||||||||
156 | void QSqlDatabasePrivate::addDatabase(const QSqlDatabase &db, const QString &name) | - | ||||||||||||
157 | { | - | ||||||||||||
158 | QConnectionDict *dict = dbDict(); | - | ||||||||||||
159 | ((!(dict)) ? qt_assert("dict",__FILE__,252256) : qt_noop()); | - | ||||||||||||
160 | QWriteLocker locker(&dict->lock); | - | ||||||||||||
161 | - | |||||||||||||
162 | if (dict->contains(name)) { | - | ||||||||||||
163 | invalidateDb(dict->take(name), name); | - | ||||||||||||
164 | QMessageLogger(__FILE__, 257261, __PRETTY_FUNCTION__).warning("QSqlDatabasePrivate::addDatabase: duplicate connection name '%s', old " | - | ||||||||||||
165 | "connection removed.", name.toLocal8Bit().data()); | - | ||||||||||||
166 | } | - | ||||||||||||
167 | dict->insert(name, db); | - | ||||||||||||
168 | db.d->connName = name; | - | ||||||||||||
169 | } | - | ||||||||||||
170 | - | |||||||||||||
171 | - | |||||||||||||
172 | - | |||||||||||||
173 | QSqlDatabase QSqlDatabasePrivate::database(const QString& name, bool open) | - | ||||||||||||
174 | { | - | ||||||||||||
175 | const QConnectionDict *dict = dbDict(); | - | ||||||||||||
176 | ((!(dict)) ? qt_assert("dict",__FILE__,269273) : qt_noop()); | - | ||||||||||||
177 | - | |||||||||||||
178 | dict->lock.lockForRead(); | - | ||||||||||||
179 | QSqlDatabase db = dict->value(name); | - | ||||||||||||
180 | dict->lock.unlock(); | - | ||||||||||||
181 | if (db.isValid() && !db.isOpen() && open) { | - | ||||||||||||
182 | if (!db.open()) | - | ||||||||||||
183 | QMessageLogger(__FILE__, 276280, __PRETTY_FUNCTION__).warning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text(); | - | ||||||||||||
184 | - | |||||||||||||
185 | } | - | ||||||||||||
186 | return db; | - | ||||||||||||
187 | } | - | ||||||||||||
188 | - | |||||||||||||
189 | - | |||||||||||||
190 | - | |||||||||||||
191 | - | |||||||||||||
192 | - | |||||||||||||
193 | void QSqlDatabasePrivate::copy(const QSqlDatabasePrivate *other) | - | ||||||||||||
194 | { | - | ||||||||||||
195 | q = other->q; | - | ||||||||||||
196 | dbname = other->dbname; | - | ||||||||||||
197 | uname = other->uname; | - | ||||||||||||
198 | pword = other->pword; | - | ||||||||||||
199 | hname = other->hname; | - | ||||||||||||
200 | drvName = other->drvName; | - | ||||||||||||
201 | port = other->port; | - | ||||||||||||
202 | connOptions = other->connOptions; | - | ||||||||||||
203 | precisionPolicy = other->precisionPolicy; | - | ||||||||||||
204 | } | - | ||||||||||||
205 | - | |||||||||||||
206 | void QSqlDatabasePrivate::disable() | - | ||||||||||||
207 | { | - | ||||||||||||
208 | if (driver != shared_null()->driver) { | - | ||||||||||||
209 | delete driver; | - | ||||||||||||
210 | driver = shared_null()->driver; | - | ||||||||||||
211 | } | - | ||||||||||||
212 | } | - | ||||||||||||
213 | QSqlDatabase QSqlDatabase::addDatabase(const QString &type, const QString &connectionName) | - | ||||||||||||
214 | { | - | ||||||||||||
215 | QSqlDatabase db(type); | - | ||||||||||||
216 | QSqlDatabasePrivate::addDatabase(db, connectionName); | - | ||||||||||||
217 | return db; | - | ||||||||||||
218 | } | - | ||||||||||||
219 | QSqlDatabase QSqlDatabase::database(const QString& connectionName, bool open) | - | ||||||||||||
220 | { | - | ||||||||||||
221 | return QSqlDatabasePrivate::database(connectionName, open); | - | ||||||||||||
222 | } | - | ||||||||||||
223 | void QSqlDatabase::removeDatabase(const QString& connectionName) | - | ||||||||||||
224 | { | - | ||||||||||||
225 | QSqlDatabasePrivate::removeDatabase(connectionName); | - | ||||||||||||
226 | } | - | ||||||||||||
227 | - | |||||||||||||
228 | - | |||||||||||||
229 | - | |||||||||||||
230 | - | |||||||||||||
231 | - | |||||||||||||
232 | - | |||||||||||||
233 | - | |||||||||||||
234 | QStringList QSqlDatabase::drivers() | - | ||||||||||||
235 | { | - | ||||||||||||
236 | QStringList list; | - | ||||||||||||
237 | if (QFactoryLoader *fl = loader()
| 0-26 | ||||||||||||
238 | typedef QMultiMap<int, QString> PluginKeyMap; | - | ||||||||||||
239 | typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; | - | ||||||||||||
240 | - | |||||||||||||
241 | const PluginKeyMap keyMap = fl->keyMap(); | - | ||||||||||||
242 | const PluginKeyMapConstIterator cend = keyMap.constEnd(); | - | ||||||||||||
243 | for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend
| 26-234 | ||||||||||||
244 | if (!list.contains(it.value())
| 0-234 | ||||||||||||
245 | list << it.value(); executed 234 times by 7 tests: list << it.value(); Executed by:
| 234 | ||||||||||||
246 | } executed 26 times by 7 tests: end of block Executed by:
| 26 | ||||||||||||
247 | - | |||||||||||||
248 | DriverDict dict = QSqlDatabasePrivate::driverDict(); | - | ||||||||||||
249 | for (DriverDict::const_iterator i = dict.constBegin(); i != dict.constEnd()
| 1-26 | ||||||||||||
250 | if (!list.contains(i.key())
| 0-1 | ||||||||||||
251 | list << i.key(); executed 1 time by 1 test: list << i.key(); Executed by:
| 1 | ||||||||||||
252 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
253 | - | |||||||||||||
254 | return executed 26 times by 7 tests: list;return list; Executed by:
executed 26 times by 7 tests: return list; Executed by:
| 26 | ||||||||||||
255 | } | - | ||||||||||||
256 | void QSqlDatabase::registerSqlDriver(const QString& name, QSqlDriverCreatorBase *creator) | - | ||||||||||||
257 | { | - | ||||||||||||
258 | delete QSqlDatabasePrivate::driverDict().take(name); | - | ||||||||||||
259 | if (creator) | - | ||||||||||||
260 | QSqlDatabasePrivate::driverDict().insert(name, creator); | - | ||||||||||||
261 | } | - | ||||||||||||
262 | bool QSqlDatabase::contains(const QString& connectionName) | - | ||||||||||||
263 | { | - | ||||||||||||
264 | return dbDict()->contains_ts(connectionName); | - | ||||||||||||
265 | } | - | ||||||||||||
266 | QStringList QSqlDatabase::connectionNames() | - | ||||||||||||
267 | { | - | ||||||||||||
268 | return dbDict()->keys_ts(); | - | ||||||||||||
269 | } | - | ||||||||||||
270 | QSqlDatabase::QSqlDatabase(const QString &type) | - | ||||||||||||
271 | { | - | ||||||||||||
272 | d = new QSqlDatabasePrivate(this); | - | ||||||||||||
273 | d->init(type); | - | ||||||||||||
274 | } | - | ||||||||||||
275 | - | |||||||||||||
276 | - | |||||||||||||
277 | - | |||||||||||||
278 | - | |||||||||||||
279 | - | |||||||||||||
280 | - | |||||||||||||
281 | - | |||||||||||||
282 | QSqlDatabase::QSqlDatabase(QSqlDriver *driver) | - | ||||||||||||
283 | { | - | ||||||||||||
284 | d = new QSqlDatabasePrivate(this, driver); | - | ||||||||||||
285 | } | - | ||||||||||||
286 | - | |||||||||||||
287 | - | |||||||||||||
288 | - | |||||||||||||
289 | - | |||||||||||||
290 | - | |||||||||||||
291 | - | |||||||||||||
292 | QSqlDatabase::QSqlDatabase() | - | ||||||||||||
293 | { | - | ||||||||||||
294 | d = QSqlDatabasePrivate::shared_null(); | - | ||||||||||||
295 | d->ref.ref(); | - | ||||||||||||
296 | } | - | ||||||||||||
297 | - | |||||||||||||
298 | - | |||||||||||||
299 | - | |||||||||||||
300 | - | |||||||||||||
301 | QSqlDatabase::QSqlDatabase(const QSqlDatabase &other) | - | ||||||||||||
302 | { | - | ||||||||||||
303 | d = other.d; | - | ||||||||||||
304 | d->ref.ref(); | - | ||||||||||||
305 | } | - | ||||||||||||
306 | - | |||||||||||||
307 | - | |||||||||||||
308 | - | |||||||||||||
309 | - | |||||||||||||
310 | QSqlDatabase &QSqlDatabase::operator=(const QSqlDatabase &other) | - | ||||||||||||
311 | { | - | ||||||||||||
312 | qAtomicAssign(d, other.d); | - | ||||||||||||
313 | return *this; | - | ||||||||||||
314 | } | - | ||||||||||||
315 | - | |||||||||||||
316 | - | |||||||||||||
317 | - | |||||||||||||
318 | - | |||||||||||||
319 | - | |||||||||||||
320 | - | |||||||||||||
321 | - | |||||||||||||
322 | void QSqlDatabasePrivate::init(const QString &type) | - | ||||||||||||
323 | { | - | ||||||||||||
324 | drvName = type; | - | ||||||||||||
325 | - | |||||||||||||
326 | if (!driver
| 0-36 | ||||||||||||
327 | } executed 36 times by 9 tests: end of block Executed by:
| 36 | ||||||||||||
328 | - | |||||||||||||
329 | if (!driver
| 0-36 | ||||||||||||
330 | DriverDict dict = QSqlDatabasePrivate::driverDict(); | - | ||||||||||||
331 | for (DriverDict::const_iterator it = dict.constBegin(); | - | ||||||||||||
332 | it != dict.constEnd()
| 0-36 | ||||||||||||
333 | if (type == it.key()
| 0-1 | ||||||||||||
334 | driver = ((QSqlDriverCreatorBase*)(*it))->createObject(); | - | ||||||||||||
335 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
336 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
337 | } executed 36 times by 9 tests: end of block Executed by:
| 36 | ||||||||||||
338 | - | |||||||||||||
339 | if (!driver
| 0-35 | ||||||||||||
340 | driver = qLoadPlugin<QSqlDriver, QSqlDriverPlugin>(loader(), type); executed 35 times by 9 tests: driver = qLoadPlugin<QSqlDriver, QSqlDriverPlugin>(loader(), type); Executed by:
| 35 | ||||||||||||
341 | - | |||||||||||||
342 | if (!driver
| 3-33 | ||||||||||||
343 | QMessageLogger(__FILE__, 781, __PRETTY_FUNCTION__).warning("QSqlDatabase: %s driver not loaded", type.toLatin1().data()); | - | ||||||||||||
344 | QMessageLogger(__FILE__, 782, __PRETTY_FUNCTION__).warning("QSqlDatabase: available drivers: %s", | - | ||||||||||||
345 | QSqlDatabase::drivers().join(QLatin1Char(' ')).toLatin1().data()); | - | ||||||||||||
346 | if (QCoreApplication::instance() == 0
| 1-2 | ||||||||||||
347 | QMessageLogger(__FILE__, 785, __PRETTY_FUNCTION__).warning("QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins"); executed 1 time by 1 test: QMessageLogger(__FILE__, 785, __PRETTY_FUNCTION__).warning("QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins"); Executed by:
| 1 | ||||||||||||
348 | driver = shared_null()->driver; | - | ||||||||||||
349 | } executed 3 times by 3 tests: end of block Executed by:
| 3 | ||||||||||||
350 | } executed 36 times by 9 tests: end of block Executed by:
| 36 | ||||||||||||
351 | - | |||||||||||||
352 | - | |||||||||||||
353 | - | |||||||||||||
354 | - | |||||||||||||
355 | - | |||||||||||||
356 | - | |||||||||||||
357 | - | |||||||||||||
358 | QSqlDatabase::~QSqlDatabase() | - | ||||||||||||
359 | { | - | ||||||||||||
360 | if (!d->ref.deref()) { | - | ||||||||||||
361 | close(); | - | ||||||||||||
362 | delete d; | - | ||||||||||||
363 | } | - | ||||||||||||
364 | } | - | ||||||||||||
365 | QSqlQuery QSqlDatabase::exec(const QString & query) const | - | ||||||||||||
366 | { | - | ||||||||||||
367 | QSqlQuery r(d->driver->createResult()); | - | ||||||||||||
368 | if (!query.isEmpty()) { | - | ||||||||||||
369 | r.exec(query); | - | ||||||||||||
370 | d->driver->setLastError(r.lastError()); | - | ||||||||||||
371 | } | - | ||||||||||||
372 | return r; | - | ||||||||||||
373 | } | - | ||||||||||||
374 | bool QSqlDatabase::open() | - | ||||||||||||
375 | { | - | ||||||||||||
376 | return d->driver->open(d->dbname, d->uname, d->pword, d->hname, | - | ||||||||||||
377 | d->port, d->connOptions); | - | ||||||||||||
378 | } | - | ||||||||||||
379 | bool QSqlDatabase::open(const QString& user, const QString& password) | - | ||||||||||||
380 | { | - | ||||||||||||
381 | setUserName(user); | - | ||||||||||||
382 | return d->driver->open(d->dbname, user, password, d->hname, | - | ||||||||||||
383 | d->port, d->connOptions); | - | ||||||||||||
384 | } | - | ||||||||||||
385 | void QSqlDatabase::close() | - | ||||||||||||
386 | { | - | ||||||||||||
387 | d->driver->close(); | - | ||||||||||||
388 | } | - | ||||||||||||
389 | - | |||||||||||||
390 | - | |||||||||||||
391 | - | |||||||||||||
392 | - | |||||||||||||
393 | - | |||||||||||||
394 | - | |||||||||||||
395 | bool QSqlDatabase::isOpen() const | - | ||||||||||||
396 | { | - | ||||||||||||
397 | return d->driver->isOpen(); | - | ||||||||||||
398 | } | - | ||||||||||||
399 | - | |||||||||||||
400 | - | |||||||||||||
401 | - | |||||||||||||
402 | - | |||||||||||||
403 | - | |||||||||||||
404 | - | |||||||||||||
405 | - | |||||||||||||
406 | bool QSqlDatabase::isOpenError() const | - | ||||||||||||
407 | { | - | ||||||||||||
408 | return d->driver->isOpenError(); | - | ||||||||||||
409 | } | - | ||||||||||||
410 | bool QSqlDatabase::transaction() | - | ||||||||||||
411 | { | - | ||||||||||||
412 | if (!d->driver->hasFeature(QSqlDriver::Transactions)) | - | ||||||||||||
413 | return false; | - | ||||||||||||
414 | return d->driver->beginTransaction(); | - | ||||||||||||
415 | } | - | ||||||||||||
416 | bool QSqlDatabase::commit() | - | ||||||||||||
417 | { | - | ||||||||||||
418 | if (!d->driver->hasFeature(QSqlDriver::Transactions)) | - | ||||||||||||
419 | return false; | - | ||||||||||||
420 | return d->driver->commitTransaction(); | - | ||||||||||||
421 | } | - | ||||||||||||
422 | bool QSqlDatabase::rollback() | - | ||||||||||||
423 | { | - | ||||||||||||
424 | if (!d->driver->hasFeature(QSqlDriver::Transactions)) | - | ||||||||||||
425 | return false; | - | ||||||||||||
426 | return d->driver->rollbackTransaction(); | - | ||||||||||||
427 | } | - | ||||||||||||
428 | void QSqlDatabase::setDatabaseName(const QString& name) | - | ||||||||||||
429 | { | - | ||||||||||||
430 | if (isValid()) | - | ||||||||||||
431 | d->dbname = name; | - | ||||||||||||
432 | } | - | ||||||||||||
433 | void QSqlDatabase::setUserName(const QString& name) | - | ||||||||||||
434 | { | - | ||||||||||||
435 | if (isValid()) | - | ||||||||||||
436 | d->uname = name; | - | ||||||||||||
437 | } | - | ||||||||||||
438 | void QSqlDatabase::setPassword(const QString& password) | - | ||||||||||||
439 | { | - | ||||||||||||
440 | if (isValid()) | - | ||||||||||||
441 | d->pword = password; | - | ||||||||||||
442 | } | - | ||||||||||||
443 | void QSqlDatabase::setHostName(const QString& host) | - | ||||||||||||
444 | { | - | ||||||||||||
445 | if (isValid()) | - | ||||||||||||
446 | d->hname = host; | - | ||||||||||||
447 | } | - | ||||||||||||
448 | void QSqlDatabase::setPort(int port) | - | ||||||||||||
449 | { | - | ||||||||||||
450 | if (isValid()) | - | ||||||||||||
451 | d->port = port; | - | ||||||||||||
452 | } | - | ||||||||||||
453 | - | |||||||||||||
454 | - | |||||||||||||
455 | - | |||||||||||||
456 | - | |||||||||||||
457 | - | |||||||||||||
458 | - | |||||||||||||
459 | - | |||||||||||||
460 | QString QSqlDatabase::databaseName() const | - | ||||||||||||
461 | { | - | ||||||||||||
462 | return d->dbname; | - | ||||||||||||
463 | } | - | ||||||||||||
464 | - | |||||||||||||
465 | - | |||||||||||||
466 | - | |||||||||||||
467 | - | |||||||||||||
468 | - | |||||||||||||
469 | - | |||||||||||||
470 | QString QSqlDatabase::userName() const | - | ||||||||||||
471 | { | - | ||||||||||||
472 | return d->uname; | - | ||||||||||||
473 | } | - | ||||||||||||
474 | - | |||||||||||||
475 | - | |||||||||||||
476 | - | |||||||||||||
477 | - | |||||||||||||
478 | - | |||||||||||||
479 | - | |||||||||||||
480 | QString QSqlDatabase::password() const | - | ||||||||||||
481 | { | - | ||||||||||||
482 | return d->pword; | - | ||||||||||||
483 | } | - | ||||||||||||
484 | - | |||||||||||||
485 | - | |||||||||||||
486 | - | |||||||||||||
487 | - | |||||||||||||
488 | - | |||||||||||||
489 | - | |||||||||||||
490 | QString QSqlDatabase::hostName() const | - | ||||||||||||
491 | { | - | ||||||||||||
492 | return d->hname; | - | ||||||||||||
493 | } | - | ||||||||||||
494 | - | |||||||||||||
495 | - | |||||||||||||
496 | - | |||||||||||||
497 | - | |||||||||||||
498 | - | |||||||||||||
499 | - | |||||||||||||
500 | QString QSqlDatabase::driverName() const | - | ||||||||||||
501 | { | - | ||||||||||||
502 | return d->drvName; | - | ||||||||||||
503 | } | - | ||||||||||||
504 | - | |||||||||||||
505 | - | |||||||||||||
506 | - | |||||||||||||
507 | - | |||||||||||||
508 | - | |||||||||||||
509 | - | |||||||||||||
510 | - | |||||||||||||
511 | int QSqlDatabase::port() const | - | ||||||||||||
512 | { | - | ||||||||||||
513 | return d->port; | - | ||||||||||||
514 | } | - | ||||||||||||
515 | QSqlDriver* QSqlDatabase::driver() const | - | ||||||||||||
516 | { | - | ||||||||||||
517 | return d->driver; | - | ||||||||||||
518 | } | - | ||||||||||||
519 | QSqlError QSqlDatabase::lastError() const | - | ||||||||||||
520 | { | - | ||||||||||||
521 | return d->driver->lastError(); | - | ||||||||||||
522 | } | - | ||||||||||||
523 | QStringList QSqlDatabase::tables(QSql::TableType type) const | - | ||||||||||||
524 | { | - | ||||||||||||
525 | return d->driver->tables(type); | - | ||||||||||||
526 | } | - | ||||||||||||
527 | QSqlIndex QSqlDatabase::primaryIndex(const QString& tablename) const | - | ||||||||||||
528 | { | - | ||||||||||||
529 | return d->driver->primaryIndex(tablename); | - | ||||||||||||
530 | } | - | ||||||||||||
531 | QSqlRecord QSqlDatabase::record(const QString& tablename) const | - | ||||||||||||
532 | { | - | ||||||||||||
533 | return d->driver->record(tablename); | - | ||||||||||||
534 | } | - | ||||||||||||
535 | void QSqlDatabase::setConnectOptions(const QString &options) | - | ||||||||||||
536 | { | - | ||||||||||||
537 | if (isValid()) | - | ||||||||||||
538 | d->connOptions = options; | - | ||||||||||||
539 | } | - | ||||||||||||
540 | - | |||||||||||||
541 | - | |||||||||||||
542 | - | |||||||||||||
543 | - | |||||||||||||
544 | - | |||||||||||||
545 | - | |||||||||||||
546 | - | |||||||||||||
547 | QString QSqlDatabase::connectOptions() const | - | ||||||||||||
548 | { | - | ||||||||||||
549 | return d->connOptions; | - | ||||||||||||
550 | } | - | ||||||||||||
551 | bool QSqlDatabase::isDriverAvailable(const QString& name) | - | ||||||||||||
552 | { | - | ||||||||||||
553 | return drivers().contains(name); | - | ||||||||||||
554 | } | - | ||||||||||||
555 | QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName) | - | ||||||||||||
556 | { | - | ||||||||||||
557 | QSqlDatabase db(driver); | - | ||||||||||||
558 | QSqlDatabasePrivate::addDatabase(db, connectionName); | - | ||||||||||||
559 | return db; | - | ||||||||||||
560 | } | - | ||||||||||||
561 | - | |||||||||||||
562 | - | |||||||||||||
563 | - | |||||||||||||
564 | - | |||||||||||||
565 | - | |||||||||||||
566 | - | |||||||||||||
567 | - | |||||||||||||
568 | bool QSqlDatabase::isValid() const | - | ||||||||||||
569 | { | - | ||||||||||||
570 | return d->driver && d->driver != d->shared_null()->driver; | - | ||||||||||||
571 | } | - | ||||||||||||
572 | QSqlDatabase QSqlDatabase::cloneDatabase(const QSqlDatabase &other, const QString &connectionName) | - | ||||||||||||
573 | { | - | ||||||||||||
574 | if (!other.isValid()) | - | ||||||||||||
575 | return QSqlDatabase(); | - | ||||||||||||
576 | - | |||||||||||||
577 | QSqlDatabase db(other.driverName()); | - | ||||||||||||
578 | db.d->copy(other.d); | - | ||||||||||||
579 | QSqlDatabasePrivate::addDatabase(db, connectionName); | - | ||||||||||||
580 | return db; | - | ||||||||||||
581 | } | - | ||||||||||||
582 | QString QSqlDatabase::connectionName() const | - | ||||||||||||
583 | { | - | ||||||||||||
584 | return d->connName; | - | ||||||||||||
585 | } | - | ||||||||||||
586 | void QSqlDatabase::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) | - | ||||||||||||
587 | { | - | ||||||||||||
588 | if(driver()) | - | ||||||||||||
589 | driver()->setNumericalPrecisionPolicy(precisionPolicy); | - | ||||||||||||
590 | d->precisionPolicy = precisionPolicy; | - | ||||||||||||
591 | } | - | ||||||||||||
592 | QSql::NumericalPrecisionPolicy QSqlDatabase::numericalPrecisionPolicy() const | - | ||||||||||||
593 | { | - | ||||||||||||
594 | if(driver()) | - | ||||||||||||
595 | return driver()->numericalPrecisionPolicy(); | - | ||||||||||||
596 | else | - | ||||||||||||
597 | return d->precisionPolicy; | - | ||||||||||||
598 | } | - | ||||||||||||
599 | - | |||||||||||||
600 | - | |||||||||||||
601 | - | |||||||||||||
602 | QDebug operator<<(QDebug dbg, const QSqlDatabase &d) | - | ||||||||||||
603 | { | - | ||||||||||||
604 | QDebugStateSaver saver(dbg); | - | ||||||||||||
605 | dbg.nospace(); | - | ||||||||||||
606 | dbg.noquote(); | - | ||||||||||||
607 | if (!d.isValid()) { | - | ||||||||||||
608 | dbg << "QSqlDatabase(invalid)"; | - | ||||||||||||
609 | return dbg; | - | ||||||||||||
610 | } | - | ||||||||||||
611 | - | |||||||||||||
612 | dbg << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\"" | - | ||||||||||||
613 | << d.databaseName() << "\", host=\"" << d.hostName() << "\", port=" << d.port() | - | ||||||||||||
614 | << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ')'; | - | ||||||||||||
615 | return dbg; | - | ||||||||||||
616 | } | - | ||||||||||||
617 | - | |||||||||||||
618 | - | |||||||||||||
619 | - | |||||||||||||
Switch to Source code | Preprocessed file |