|
Line 0
Link Here
|
|
|
1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Implementation of SQLite driver classes. |
| 4 |
** |
| 5 |
** Copyright (C) 1992-2003 Trolltech AS. All rights reserved. |
| 6 |
** |
| 7 |
** This file is part of the sql module of the TQt GUI Toolkit. |
| 8 |
** EDITIONS: FREE, ENTERPRISE |
| 9 |
** |
| 10 |
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 |
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
** |
| 13 |
****************************************************************************/ |
| 14 |
|
| 15 |
#include "qsql_sqlite3.h" |
| 16 |
|
| 17 |
#include <ntqdatetime.h> |
| 18 |
#include <ntqvaluevector.h> |
| 19 |
#include <ntqregexp.h> |
| 20 |
#include <ntqfile.h> |
| 21 |
#include <sqlite3.h> |
| 22 |
|
| 23 |
#if (TQT_VERSION-0 < 0x030200) |
| 24 |
# include <ntqvector.h> |
| 25 |
# if !defined Q_WS_WIN32 |
| 26 |
# include <unistd.h> |
| 27 |
# endif |
| 28 |
#else |
| 29 |
# include <ntqptrvector.h> |
| 30 |
# if !defined Q_WS_WIN32 |
| 31 |
# include <unistd.h> |
| 32 |
# endif |
| 33 |
#endif |
| 34 |
|
| 35 |
typedef struct sqlite3_stmt sqlite3_stmt; |
| 36 |
|
| 37 |
#define TQSQLITE3_DRIVER_NAME "TQSQLITE3" |
| 38 |
|
| 39 |
static TQVariant::Type qSqliteType(int tp) |
| 40 |
{ |
| 41 |
switch (tp) { |
| 42 |
case SQLITE_INTEGER: |
| 43 |
return TQVariant::Int; |
| 44 |
case SQLITE_FLOAT: |
| 45 |
return TQVariant::Double; |
| 46 |
case SQLITE_BLOB: |
| 47 |
return TQVariant::ByteArray; |
| 48 |
case SQLITE_TEXT: |
| 49 |
default: |
| 50 |
return TQVariant::String; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static TQSqlError qMakeError(sqlite3 *access, const TQString &descr, TQSqlError::Type type, |
| 55 |
int errorCode = -1) |
| 56 |
{ |
| 57 |
return TQSqlError(descr, |
| 58 |
TQString::fromUtf8(sqlite3_errmsg(access)), |
| 59 |
type, errorCode); |
| 60 |
} |
| 61 |
|
| 62 |
class TQSQLite3DriverPrivate |
| 63 |
{ |
| 64 |
public: |
| 65 |
TQSQLite3DriverPrivate(); |
| 66 |
sqlite3 *access; |
| 67 |
bool utf8; |
| 68 |
}; |
| 69 |
|
| 70 |
TQSQLite3DriverPrivate::TQSQLite3DriverPrivate() : access(0) |
| 71 |
{ |
| 72 |
utf8 = true; |
| 73 |
} |
| 74 |
|
| 75 |
class TQSQLite3ResultPrivate |
| 76 |
{ |
| 77 |
public: |
| 78 |
TQSQLite3ResultPrivate(TQSQLite3Result *res); |
| 79 |
void cleanup(); |
| 80 |
bool fetchNext(TQtSqlCachedResult::RowCache *row); |
| 81 |
bool isSelect(); |
| 82 |
// initializes the recordInfo and the cache |
| 83 |
void initColumns(TQtSqlCachedResult::RowCache **row = 0); |
| 84 |
void finalize(); |
| 85 |
|
| 86 |
TQSQLite3Result* q; |
| 87 |
sqlite3 *access; |
| 88 |
|
| 89 |
sqlite3_stmt *stmt; |
| 90 |
|
| 91 |
uint skippedStatus: 1; // the status of the fetchNext() that's skipped |
| 92 |
TQtSqlCachedResult::RowCache *skipRow; |
| 93 |
|
| 94 |
uint utf8: 1; |
| 95 |
TQSqlRecord rInf; |
| 96 |
}; |
| 97 |
|
| 98 |
static const uint initial_cache_size = 128; |
| 99 |
|
| 100 |
TQSQLite3ResultPrivate::TQSQLite3ResultPrivate(TQSQLite3Result* res) : q(res), access(0), |
| 101 |
stmt(0), skippedStatus(false), skipRow(0), utf8(false) |
| 102 |
{ |
| 103 |
} |
| 104 |
|
| 105 |
void TQSQLite3ResultPrivate::cleanup() |
| 106 |
{ |
| 107 |
finalize(); |
| 108 |
rInf.clear(); |
| 109 |
skippedStatus = false; |
| 110 |
delete skipRow; |
| 111 |
skipRow = 0; |
| 112 |
q->setAt(TQSql::BeforeFirst); |
| 113 |
q->setActive(false); |
| 114 |
q->cleanup(); |
| 115 |
} |
| 116 |
|
| 117 |
void TQSQLite3ResultPrivate::finalize() |
| 118 |
{ |
| 119 |
if (!stmt) |
| 120 |
return; |
| 121 |
|
| 122 |
sqlite3_finalize(stmt); |
| 123 |
stmt = 0; |
| 124 |
} |
| 125 |
|
| 126 |
// called on first fetch |
| 127 |
void TQSQLite3ResultPrivate::initColumns(TQtSqlCachedResult::RowCache** row) |
| 128 |
{ |
| 129 |
rInf.clear(); |
| 130 |
|
| 131 |
int nCols = sqlite3_column_count(stmt); |
| 132 |
if (nCols <= 0) |
| 133 |
return; |
| 134 |
|
| 135 |
q->init(nCols); |
| 136 |
|
| 137 |
for (int i = 0; i < nCols; ++i) { |
| 138 |
TQString colName = TQString::fromUtf8(sqlite3_column_name(stmt, i)); |
| 139 |
|
| 140 |
int dotIdx = colName.findRev('.'); |
| 141 |
rInf.append(TQSqlField(colName.mid(dotIdx == -1 ? 0 : dotIdx + 1), |
| 142 |
qSqliteType(sqlite3_column_type(stmt, i)))); |
| 143 |
} |
| 144 |
|
| 145 |
// skip the first fetch |
| 146 |
if (row && !*row) { |
| 147 |
*row = new TQtSqlCachedResult::RowCache(nCols); |
| 148 |
skipRow = *row; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
bool TQSQLite3ResultPrivate::fetchNext(TQtSqlCachedResult::RowCache* row) |
| 153 |
{ |
| 154 |
int res; |
| 155 |
unsigned int i; |
| 156 |
|
| 157 |
if (skipRow) { |
| 158 |
// already fetched |
| 159 |
if (row) |
| 160 |
*row = *skipRow; |
| 161 |
delete skipRow; |
| 162 |
skipRow = 0; |
| 163 |
return skippedStatus; |
| 164 |
} |
| 165 |
|
| 166 |
if (!stmt) |
| 167 |
return false; |
| 168 |
|
| 169 |
// keep trying while busy, wish I could implement this better. |
| 170 |
while ((res = sqlite3_step(stmt)) == SQLITE_BUSY) { |
| 171 |
// sleep instead requesting result again immidiately. |
| 172 |
#if defined Q_OS_WIN |
| 173 |
Sleep(1000); |
| 174 |
#else |
| 175 |
sleep(1); |
| 176 |
#endif |
| 177 |
} |
| 178 |
|
| 179 |
switch(res) { |
| 180 |
case SQLITE_ROW: |
| 181 |
// check to see if should fill out columns |
| 182 |
if (rInf.isEmpty()) |
| 183 |
// must be first call. |
| 184 |
initColumns(&row); |
| 185 |
if (!row) |
| 186 |
return true; |
| 187 |
for (i = 0; i < rInf.count(); ++i) |
| 188 |
// todo - handle other types |
| 189 |
(*row)[i] = TQString::fromUtf8((char *)(sqlite3_column_text(stmt, i))); |
| 190 |
// values[i + idx] = utf8 ? TQString::fromUtf8(fvals[i]) : TQString::fromAscii(fvals[i]); |
| 191 |
return true; |
| 192 |
case SQLITE_DONE: |
| 193 |
if (rInf.isEmpty()) |
| 194 |
// must be first call. |
| 195 |
initColumns(&row); |
| 196 |
q->setAt(TQSql::AfterLast); |
| 197 |
return false; |
| 198 |
case SQLITE_ERROR: |
| 199 |
case SQLITE_MISUSE: |
| 200 |
default: |
| 201 |
// something wrong, don't get col info, but still return false |
| 202 |
q->setLastError(qMakeError(access, "Unable to fetch row", TQSqlError::Connection, res)); |
| 203 |
finalize(); |
| 204 |
q->setAt(TQSql::AfterLast); |
| 205 |
return false; |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
TQSQLite3Result::TQSQLite3Result(const TQSQLite3Driver* db) |
| 211 |
: TQtSqlCachedResult(db) |
| 212 |
{ |
| 213 |
d = new TQSQLite3ResultPrivate(this); |
| 214 |
d->access = db->d->access; |
| 215 |
} |
| 216 |
|
| 217 |
TQSQLite3Result::~TQSQLite3Result() |
| 218 |
{ |
| 219 |
d->cleanup(); |
| 220 |
delete d; |
| 221 |
} |
| 222 |
|
| 223 |
/* |
| 224 |
Execute \a query. |
| 225 |
*/ |
| 226 |
bool TQSQLite3Result::reset (const TQString &query) |
| 227 |
{ |
| 228 |
// this is where we build a query. |
| 229 |
if (!driver() || !driver()->isOpen() || driver()->isOpenError()) |
| 230 |
return false; |
| 231 |
|
| 232 |
d->cleanup(); |
| 233 |
|
| 234 |
setSelect(false); |
| 235 |
|
| 236 |
int res = sqlite3_prepare(d->access, query.utf8().data(), (query.length() + 1) * sizeof(TQChar), |
| 237 |
&d->stmt, 0); |
| 238 |
|
| 239 |
if (res != SQLITE_OK) { |
| 240 |
setLastError(qMakeError(d->access, "Unable to execute statement", TQSqlError::Statement, res)); |
| 241 |
d->finalize(); |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
d->skippedStatus = d->fetchNext(0); |
| 246 |
|
| 247 |
setSelect(!d->rInf.isEmpty()); |
| 248 |
setActive(true); |
| 249 |
return true; |
| 250 |
} |
| 251 |
|
| 252 |
bool TQSQLite3Result::gotoNext(TQtSqlCachedResult::RowCache* row) |
| 253 |
{ |
| 254 |
return d->fetchNext(row); |
| 255 |
} |
| 256 |
|
| 257 |
int TQSQLite3Result::size() |
| 258 |
{ |
| 259 |
return -1; |
| 260 |
} |
| 261 |
|
| 262 |
int TQSQLite3Result::numRowsAffected() |
| 263 |
{ |
| 264 |
return sqlite3_changes(d->access); |
| 265 |
} |
| 266 |
|
| 267 |
///////////////////////////////////////////////////////// |
| 268 |
|
| 269 |
TQSQLite3Driver::TQSQLite3Driver(TQObject * parent, const char *name) |
| 270 |
: TQSqlDriver(parent, name) |
| 271 |
{ |
| 272 |
d = new TQSQLite3DriverPrivate(); |
| 273 |
} |
| 274 |
|
| 275 |
TQSQLite3Driver::TQSQLite3Driver(sqlite3 *connection, TQObject *parent, const char *name) |
| 276 |
: TQSqlDriver(parent, name) |
| 277 |
{ |
| 278 |
d = new TQSQLite3DriverPrivate(); |
| 279 |
d->access = connection; |
| 280 |
setOpen(true); |
| 281 |
setOpenError(false); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
TQSQLite3Driver::~TQSQLite3Driver() |
| 286 |
{ |
| 287 |
delete d; |
| 288 |
} |
| 289 |
|
| 290 |
bool TQSQLite3Driver::hasFeature(DriverFeature f) const |
| 291 |
{ |
| 292 |
switch (f) { |
| 293 |
case Transactions: |
| 294 |
case Unicode: |
| 295 |
case BLOB: |
| 296 |
return true; |
| 297 |
default: |
| 298 |
break; |
| 299 |
} |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
/* |
| 304 |
SQLite dbs have no user name, passwords, hosts or ports. |
| 305 |
just file names. |
| 306 |
*/ |
| 307 |
bool TQSQLite3Driver::open(const TQString & db, const TQString &, const TQString &, const TQString &, int, const TQString &) |
| 308 |
{ |
| 309 |
if (isOpen()) |
| 310 |
close(); |
| 311 |
|
| 312 |
if (db.isEmpty()) |
| 313 |
return false; |
| 314 |
|
| 315 |
if (sqlite3_open(TQFile::encodeName(db), &d->access) == SQLITE_OK) { |
| 316 |
setOpen(true); |
| 317 |
setOpenError(false); |
| 318 |
return true; |
| 319 |
} else { |
| 320 |
setLastError(qMakeError(d->access, "Error opening database", |
| 321 |
TQSqlError::Connection)); |
| 322 |
setOpenError(true); |
| 323 |
return false; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void TQSQLite3Driver::close() |
| 328 |
{ |
| 329 |
if (isOpen()) { |
| 330 |
if (sqlite3_close(d->access) != SQLITE_OK) |
| 331 |
setLastError(qMakeError(d->access, "Error closing database", |
| 332 |
TQSqlError::Connection)); |
| 333 |
d->access = 0; |
| 334 |
setOpen(false); |
| 335 |
setOpenError(false); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
TQSqlQuery TQSQLite3Driver::createQuery() const |
| 340 |
{ |
| 341 |
return TQSqlQuery(new TQSQLite3Result(this)); |
| 342 |
} |
| 343 |
|
| 344 |
bool TQSQLite3Driver::beginTransaction() |
| 345 |
{ |
| 346 |
if (!isOpen() || isOpenError()) |
| 347 |
return false; |
| 348 |
|
| 349 |
TQSqlQuery q(createQuery()); |
| 350 |
if (!q.exec("BEGIN")) { |
| 351 |
setLastError(TQSqlError("Unable to begin transaction", |
| 352 |
q.lastError().databaseText(), TQSqlError::Transaction)); |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
return true; |
| 357 |
} |
| 358 |
|
| 359 |
bool TQSQLite3Driver::commitTransaction() |
| 360 |
{ |
| 361 |
if (!isOpen() || isOpenError()) |
| 362 |
return false; |
| 363 |
|
| 364 |
TQSqlQuery q(createQuery()); |
| 365 |
if (!q.exec("COMMIT")) { |
| 366 |
setLastError(TQSqlError("Unable to begin transaction", |
| 367 |
q.lastError().databaseText(), TQSqlError::Transaction)); |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
return true; |
| 372 |
} |
| 373 |
|
| 374 |
bool TQSQLite3Driver::rollbackTransaction() |
| 375 |
{ |
| 376 |
if (!isOpen() || isOpenError()) |
| 377 |
return false; |
| 378 |
|
| 379 |
TQSqlQuery q(createQuery()); |
| 380 |
if (!q.exec("ROLLBACK")) { |
| 381 |
setLastError(TQSqlError("Unable to begin transaction", |
| 382 |
q.lastError().databaseText(), TQSqlError::Transaction)); |
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
TQStringList TQSQLite3Driver::tables(const TQString &typeName) const |
| 390 |
{ |
| 391 |
TQStringList res; |
| 392 |
if (!isOpen()) |
| 393 |
return res; |
| 394 |
int type = typeName.toInt(); |
| 395 |
|
| 396 |
TQSqlQuery q = createQuery(); |
| 397 |
q.setForwardOnly(TRUE); |
| 398 |
#if (TQT_VERSION-0 >= 0x030200) |
| 399 |
if ((type & (int)TQSql::Tables) && (type & (int)TQSql::Views)) |
| 400 |
q.exec("SELECT name FROM sqlite_master WHERE type='table' OR type='view'"); |
| 401 |
else if (typeName.isEmpty() || (type & (int)TQSql::Tables)) |
| 402 |
q.exec("SELECT name FROM sqlite_master WHERE type='table'"); |
| 403 |
else if (type & (int)TQSql::Views) |
| 404 |
q.exec("SELECT name FROM sqlite_master WHERE type='view'"); |
| 405 |
#else |
| 406 |
q.exec("SELECT name FROM sqlite_master WHERE type='table' OR type='view'"); |
| 407 |
#endif |
| 408 |
|
| 409 |
|
| 410 |
if (q.isActive()) { |
| 411 |
while(q.next()) |
| 412 |
res.append(q.value(0).toString()); |
| 413 |
} |
| 414 |
|
| 415 |
#if (TQT_VERSION-0 >= 0x030200) |
| 416 |
if (type & (int)TQSql::SystemTables) { |
| 417 |
// there are no internal tables beside this one: |
| 418 |
res.append("sqlite_master"); |
| 419 |
} |
| 420 |
#endif |
| 421 |
|
| 422 |
return res; |
| 423 |
} |
| 424 |
|
| 425 |
TQSqlIndex TQSQLite3Driver::primaryIndex(const TQString &tblname) const |
| 426 |
{ |
| 427 |
TQSqlRecordInfo rec(recordInfo(tblname)); // expensive :( |
| 428 |
|
| 429 |
if (!isOpen()) |
| 430 |
return TQSqlIndex(); |
| 431 |
|
| 432 |
TQSqlQuery q = createQuery(); |
| 433 |
q.setForwardOnly(TRUE); |
| 434 |
// finrst find a UNIQUE INDEX |
| 435 |
q.exec("PRAGMA index_list('" + tblname + "');"); |
| 436 |
TQString indexname; |
| 437 |
while(q.next()) { |
| 438 |
if (q.value(2).toInt()==1) { |
| 439 |
indexname = q.value(1).toString(); |
| 440 |
break; |
| 441 |
} |
| 442 |
} |
| 443 |
if (indexname.isEmpty()) |
| 444 |
return TQSqlIndex(); |
| 445 |
|
| 446 |
q.exec("PRAGMA index_info('" + indexname + "');"); |
| 447 |
|
| 448 |
TQSqlIndex index(indexname); |
| 449 |
while(q.next()) { |
| 450 |
TQString name = q.value(2).toString(); |
| 451 |
TQSqlVariant::Type type = TQSqlVariant::Invalid; |
| 452 |
if (rec.contains(name)) |
| 453 |
type = rec.find(name).type(); |
| 454 |
index.append(TQSqlField(name, type)); |
| 455 |
} |
| 456 |
return index; |
| 457 |
} |
| 458 |
|
| 459 |
TQSqlRecordInfo TQSQLite3Driver::recordInfo(const TQString &tbl) const |
| 460 |
{ |
| 461 |
if (!isOpen()) |
| 462 |
return TQSqlRecordInfo(); |
| 463 |
|
| 464 |
TQSqlQuery q = createQuery(); |
| 465 |
q.setForwardOnly(TRUE); |
| 466 |
q.exec("SELECT * FROM " + tbl + " LIMIT 1"); |
| 467 |
return recordInfo(q); |
| 468 |
} |
| 469 |
|
| 470 |
TQSqlRecord TQSQLite3Driver::record(const TQString &tblname) const |
| 471 |
{ |
| 472 |
if (!isOpen()) |
| 473 |
return TQSqlRecord(); |
| 474 |
|
| 475 |
return recordInfo(tblname).toRecord(); |
| 476 |
} |
| 477 |
|
| 478 |
TQSqlRecord TQSQLite3Driver::record(const TQSqlQuery& query) const |
| 479 |
{ |
| 480 |
if (query.isActive() && query.driver() == this) { |
| 481 |
TQSQLite3Result* result = (TQSQLite3Result*)query.result(); |
| 482 |
return result->d->rInf; |
| 483 |
} |
| 484 |
return TQSqlRecord(); |
| 485 |
} |
| 486 |
|
| 487 |
TQSqlRecordInfo TQSQLite3Driver::recordInfo(const TQSqlQuery& query) const |
| 488 |
{ |
| 489 |
if (query.isActive() && query.driver() == this) { |
| 490 |
TQSQLite3Result* result = (TQSQLite3Result*)query.result(); |
| 491 |
return TQSqlRecordInfo(result->d->rInf); |
| 492 |
} |
| 493 |
return TQSqlRecordInfo(); |
| 494 |
} |