void guaranteeRange(int rowIndex) { if (!dataByRowIndex.contains(rowIndex)) { int pageStart = (rowIndex / pageSize) * pageSize; qDebug() << "Fetching " << pageSize << " items starting from " << pageStart; static const QString stmt( "\nSELECT" "\n Events.*," "\n PhoneNumbers.LineIdentification AS PhoneNumberIDRepresentation" "\nFROM" "\n Events" "" "\n LEFT JOIN" "\n PhoneNumbers" "\n ON" "\n PhoneNumbers.ID = Events.PhoneNumberID" "\n%1" "\nORDER BY" "\n Events.TimeStamp DESC" "\nLIMIT :pageSize" "\nOFFSET :pageStart"); QString whereClause; Database::SqlParameters params; makeWhereClause(&whereClause, ¶ms); QString selectStmt = stmt.arg(whereClause); params.insert(":pageSize", pageSize); params.insert(":pageStart", pageStart); QScopedPointer< SqlCursor > cursor(db->select(selectStmt, params)); QStringList columns = cursor->columns(); for (int i = 0; cursor->next(); i++) { QHash< QString, QVariant > record; for (int j = 0; j < columns.size(); j++) record.insert(columns[j], cursor->value(columns[j])); dataByRowIndex.insert(pageStart + i, record); oidToRowIndex.insert(record.value("ID").toInt(), pageStart + i); } } }
bool PhoneNumbersTableModel::contains(const QString& lineIdentification) { static QString stmt( "SELECT ID FROM PhoneNumbers WHERE LineIdentification = :lineIdentificaiton"); Database::SqlParameters params; params.insert(QLatin1String(":lineIdentification"), lineIdentification); QScopedPointer< SqlCursor > cursor(d->db->select(stmt, params)); return cursor->next(); }
int PhoneNumbersTableModel::getIdByLineIdentification(const QString& lineIdentification) { qDebug(); int id = -1; static QString selectStatement( "SELECT ID FROM PhoneNumbers WHERE LineIdentification = :lineIdentification"); Database::SqlParameters params; params.insert(QLatin1String(":lineIdentification"), lineIdentification); QScopedPointer< SqlCursor > cursor(d->db->select(selectStatement, params)); // TODO: process errors when selecting from DB if (cursor.isNull()) { qCritical() << QLatin1String(": unable to retrieve PhoneNumbers.ID for ") << lineIdentification; } // phone number was already contacted else if (cursor->next()) { id = cursor->value(QLatin1String("ID")).toInt(); } // no data found - insert new row into PhoneNumbers and return its id else { static QString insertStatement( "INSERT INTO PhoneNumbers(LineIdentification) VALUES(:lineIdentification)"); Database::SqlParameters params; params.insert(":lineIdentification", lineIdentification); id = d->db->insert(insertStatement, params); } return id; }