bool GameLibraryModel::select() { const QString query = selectStatement(); if (query.isEmpty()) return false; beginResetModel(); // d->clearCache(); QSqlQuery qu(database()); qu.prepare(query); for (auto &val : params) { qu.addBindValue(val); } qu.exec(); setQuery(qu); if (!qu.isActive() || lastError().isValid()) { setTable(tableName()); // resets the record & index endResetModel(); return false; } endResetModel(); return true; }
/*! \since 5.0 Refreshes \a row in the model with values from the database table row matching on primary key values. Without a primary key, all column values must match. If no matching row is found, the model will show an empty row. Returns true if successful; otherwise returns false. \sa select() */ bool QSqlTableModel::selectRow(int row) { Q_D(QSqlTableModel); if (row < 0 || row >= rowCount()) return false; const int table_sort_col = d->sortColumn; d->sortColumn = -1; const QString table_filter = d->filter; d->filter = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement, d->tableName, d->primaryValues(row), false); static const QString wh = Sql::where() + Sql::sp(); if (d->filter.startsWith(wh, Qt::CaseInsensitive)) d->filter.remove(0, wh.length()); const QString stmt = selectStatement(); d->sortColumn = table_sort_col; d->filter = table_filter; QSqlQuery q(d->db); q.setForwardOnly(true); if (!q.exec(stmt)) return false; bool exists = q.next(); d->cache[row].refresh(exists, q.record()); emit headerDataChanged(Qt::Vertical, row, row); emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1)); return true; }
bool Repository::pull(StorageObject sObj, list<StorageObject> *&pResults) { QueryResult *queryResult = NULL; if(!selectStatement(sObj, queryResult)) return false; pResults = new list<StorageObject>(); instantiate(queryResult, pResults, sObj); return true; }
Vector<ParsedCookie*>* CookieDatabaseBackingStore::invokeGetCookiesWithLimit(unsigned int limit) { ASSERT(isCurrentThread()); // Check that the table exists to avoid doing an unnecessary request. if (!m_db.isOpen()) return 0; StringBuilder selectQuery; selectQuery.append("SELECT name, value, host, path, expiry, lastAccessed, isSecure, isHttpOnly, creationTime, protocol FROM "); selectQuery.append(m_tableName); if (limit > 0) { selectQuery.append(" ORDER BY lastAccessed ASC"); selectQuery.append(" LIMIT " + String::number(limit)); } selectQuery.append(";"); CookieLog("CookieBackingStore - invokeGetAllCookies with select query %s", selectQuery.toString().utf8().data()); SQLiteStatement selectStatement(m_db, selectQuery.toString()); if (selectStatement.prepare()) { LOG_ERROR("Cannot retrieved cookies from the database"); LOG_ERROR("SQLite Error Message: %s", m_db.lastErrorMsg()); return 0; } Vector<ParsedCookie*>* cookies = new Vector<ParsedCookie*>; while (selectStatement.step() == SQLResultRow) { // There is a row to fetch String name = selectStatement.getColumnText(0); String value = selectStatement.getColumnText(1); String domain = selectStatement.getColumnText(2); String path = selectStatement.getColumnText(3); double expiry = selectStatement.getColumnDouble(4); double lastAccessed = selectStatement.getColumnDouble(5); bool isSecure = selectStatement.getColumnInt(6); bool isHttpOnly = selectStatement.getColumnInt(7); double creationTime = selectStatement.getColumnDouble(8); String protocol = selectStatement.getColumnText(9); cookies->append(new ParsedCookie(name, value, domain, protocol, path, expiry, lastAccessed, creationTime, isSecure, isHttpOnly)); } return cookies; }
/*! Populates the model with data from the table that was set via setTable(), using the specified filter and sort condition, and returns true if successful; otherwise returns false. \sa setTable(), setFilter(), selectStatement() */ bool QSqlTableModel::select() { Q_D(QSqlTableModel); QString query = selectStatement(); if (query.isEmpty()) return false; revertAll(); QSqlQuery qu(query, d->db); setQuery(qu); if (!qu.isActive()) { // something went wrong - revert to non-select state d->initRecordAndPrimaryIndex(); return false; } return true; }
void CookieDatabaseBackingStore::getCookiesFromDatabase(CookieNode* tree) { if (!m_db.isOpen()) return; SQLiteStatement selectStatement(m_db, "SELECT name, value, host, path, expiry, lastAccessed, isSecure, isHttpOnly, creationTime, protocol FROM cookies ORDER BY lastAccessed ASC;"); if (selectStatement.prepare()) { LOG_ERROR("Cannot retrieved cookies from the database"); LOG_ERROR("SQLite Error Message: %s", m_db.lastErrorMsg()); return; } unsigned counter = 0; while (selectStatement.step() == SQLResultRow) { String name = selectStatement.getColumnText(0); String value = selectStatement.getColumnText(1); String domain = selectStatement.getColumnText(2); String path = selectStatement.getColumnText(3); double expiry = selectStatement.getColumnDouble(4); double lastAccessed = selectStatement.getColumnDouble(5); bool isSecure = selectStatement.getColumnInt(6); bool isHttpOnly = selectStatement.getColumnInt(7); double creationTime = selectStatement.getColumnDouble(8); String protocol = selectStatement.getColumnText(9); RefPtr<ParsedCookie> cookie = ParsedCookie::create(name, value, domain, protocol, path, expiry, lastAccessed, creationTime, isSecure, isHttpOnly); if (cookie->hasExpired()) { remove(cookie); continue; } Vector<String> splittedHost; cookie->domain().split(".", false, splittedHost); tree->insert(cookie, splittedHost, this, WithHttpOnlyCookies, OnlyInMemory); counter++; } if (counter >= s_globalMaxCookieCount) removeLastAccessedCookies(); }
/*! Populates the model with data from the table that was set via setTable(), using the specified filter and sort condition, and returns \c true if successful; otherwise returns \c false. \note Calling select() will revert any unsubmitted changes and remove any inserted columns. \sa setTable(), setFilter(), selectStatement() */ bool QSqlTableModel::select() { Q_D(QSqlTableModel); const QString query = selectStatement(); if (query.isEmpty()) return false; beginResetModel(); d->clearCache(); QSqlQuery qu(query, d->db); setQuery(qu); if (!qu.isActive() || lastError().isValid()) { // something went wrong - revert to non-select state d->initRecordAndPrimaryIndex(); endResetModel(); return false; } endResetModel(); return true; }
/*! \since 5.0 Refreshes \a row in the model with values from the database table row matching on primary key values. Without a primary key, all column values must match. If no matching row is found, the model will show an empty row. Returns \c true if successful; otherwise returns \c false. \sa select() */ bool QSqlTableModel::selectRow(int row) { Q_D(QSqlTableModel); if (row < 0 || row >= rowCount()) return false; const int table_sort_col = d->sortColumn; d->sortColumn = -1; const QString table_filter = d->filter; d->filter = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement, d->tableName, primaryValues(row), false); static const QString wh = Sql::where() + Sql::sp(); if (d->filter.startsWith(wh, Qt::CaseInsensitive)) d->filter.remove(0, wh.length()); QString stmt; if (!d->filter.isEmpty()) stmt = selectStatement(); d->sortColumn = table_sort_col; d->filter = table_filter; if (stmt.isEmpty()) return false; bool exists; QSqlRecord newValues; { QSqlQuery q(d->db); q.setForwardOnly(true); if (!q.exec(stmt)) return false; exists = q.next(); newValues = q.record(); } bool needsAddingToCache = !exists || d->cache.contains(row); if (!needsAddingToCache) { const QSqlRecord curValues = record(row); needsAddingToCache = curValues.count() != newValues.count(); if (!needsAddingToCache) { // Look for changed values. Primary key fields are customarily first // and probably change less often than other fields, so start at the end. for (int f = curValues.count() - 1; f >= 0; --f) { if (curValues.value(f) != newValues.value(f)) { needsAddingToCache = true; break; } } } } if (needsAddingToCache) { d->cache[row].refresh(exists, newValues); emit headerDataChanged(Qt::Vertical, row, row); emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1)); } return true; }