58 static const QLatin1String SQL_CLEAR_TAGS ( "UPDATE history SET tagged = 0 WHERE tagged = 1" ); |
58 static const QLatin1String SQL_CLEAR_TAGS ( "UPDATE history SET tagged = 0 WHERE tagged = 1" ); |
59 |
59 |
60 //static static const QLatin1String SQL_FIND_ITEM_BY_ID( "SELECT * FROM history WHERE id = ?" ); |
60 //static static const QLatin1String SQL_FIND_ITEM_BY_ID( "SELECT * FROM history WHERE id = ?" ); |
61 static const QLatin1String SQL_TOGGLE_TAG ( "UPDATE history SET tagged = ? WHERE id = ?" ); |
61 static const QLatin1String SQL_TOGGLE_TAG ( "UPDATE history SET tagged = ? WHERE id = ?" ); |
62 |
62 |
|
63 static const QLatin1String SQL_DELETE_ITEM_FORMAT_STR ( "DELETE FROM history WHERE id = %1" ); |
|
64 static const QLatin1String SQL_REMOVE_TAG_FORMAT_STR ( "UPDATE history SET tagged = 0 WHERE id = %1" ); |
|
65 static const QLatin1String OR_ID_IS_FORMAT_STR (" OR id = %1"); |
|
66 |
|
67 static const int MAX_ID_COUNT_IN_QUERY = 5; |
|
68 |
63 #ifdef LOGGING_ENABLED |
69 #ifdef LOGGING_ENABLED |
64 # define GET_ERR( param ) GETSTRING( param.lastError().text() ) |
70 # define GET_ERR( param ) GETSTRING( param.lastError().text() ) |
65 # define GET_ERR_PTR( param ) GETSTRING( param->lastError().text() ) |
71 # define GET_ERR_PTR( param ) GETSTRING( param->lastError().text() ) |
66 #endif // LOGGING_ENABLED |
72 #endif // LOGGING_ENABLED |
67 |
73 |
260 } |
266 } |
261 |
267 |
262 /*! |
268 /*! |
263 * |
269 * |
264 */ |
270 */ |
|
271 void RadioHistoryModelPrivate::removeByModelIndices( QModelIndexList& indices, bool removeTags ) |
|
272 { |
|
273 if ( !mQueryModel ) { |
|
274 return; |
|
275 } |
|
276 QString sqlStr = ""; |
|
277 int rowIndex = -1; |
|
278 |
|
279 QSqlQuery query( *mDatabase ); |
|
280 mDatabase->transaction(); |
|
281 // List needs to be sorted and indices needs to go throught from largest to smallest. |
|
282 // This is for keeping QmodelIndexing in sync after begin- and endremoverows |
|
283 // calls when content is not yet actually removed. |
|
284 // Real removal happens in QSqlQuery::exec |
|
285 qSort(indices); |
|
286 QModelIndexList::const_iterator iter = indices.constEnd(); |
|
287 QModelIndexList::const_iterator begin = indices.constBegin(); |
|
288 for ( int counter = 1; iter != begin; ) { |
|
289 iter--; |
|
290 rowIndex = (*iter).row(); |
|
291 if( rowIndex > -1 ) { |
|
292 QSqlRecord record = mQueryModel->record(rowIndex); |
|
293 |
|
294 if( counter > 1 ) { |
|
295 sqlStr += QString( OR_ID_IS_FORMAT_STR ).arg(record.value("id").toInt()); |
|
296 } else { |
|
297 sqlStr = QString( removeTags ? SQL_REMOVE_TAG_FORMAT_STR |
|
298 : SQL_DELETE_ITEM_FORMAT_STR ).arg(record.value("id").toInt()); |
|
299 } |
|
300 // adding max MAX_ID_COUNT_IN_QUERY ids to Query |
|
301 if( counter == MAX_ID_COUNT_IN_QUERY ) { |
|
302 if( !prepareAndExec( query, sqlStr ) ) { |
|
303 // error, do not proceed |
|
304 break; |
|
305 } |
|
306 counter = 1; |
|
307 sqlStr = ""; |
|
308 } else { |
|
309 counter++; |
|
310 } |
|
311 q_ptr->beginRemoveRows( QModelIndex(), rowIndex, rowIndex ); |
|
312 q_ptr->endRemoveRows(); |
|
313 } |
|
314 } |
|
315 if( !query.lastError().isValid() && sqlStr.length() ) { |
|
316 prepareAndExec( query, sqlStr ); |
|
317 } |
|
318 if( query.lastError().isValid() ) { |
|
319 // in case of error, rollback everyhing and reset model |
|
320 mDatabase->rollback(); |
|
321 q_ptr->reset(); |
|
322 } else { |
|
323 mDatabase->commit(); |
|
324 refreshModel(); |
|
325 } |
|
326 } |
|
327 |
|
328 /*! |
|
329 * |
|
330 */ |
265 void RadioHistoryModelPrivate::setViewMode( ViewMode mode ) |
331 void RadioHistoryModelPrivate::setViewMode( ViewMode mode ) |
266 { |
332 { |
267 if ( !mQueryModel ) { |
333 if ( !mQueryModel ) { |
268 return; |
334 return; |
269 } |
335 } |
280 QSqlQuery updateQuery = beginTransaction(); |
346 QSqlQuery updateQuery = beginTransaction(); |
281 |
347 |
282 updateQuery.prepare( SQL_TOGGLE_TAG ); |
348 updateQuery.prepare( SQL_TOGGLE_TAG ); |
283 updateQuery.addBindValue( item.isTagged() ? 0 : 1 ); |
349 updateQuery.addBindValue( item.isTagged() ? 0 : 1 ); |
284 updateQuery.addBindValue( item.id() ); |
350 updateQuery.addBindValue( item.id() ); |
285 |
|
286 Operation operation = ChangeData; |
351 Operation operation = ChangeData; |
287 if ( mViewMode == ShowTagged && item.isTagged() ) { |
352 if ( mViewMode == ShowTagged && item.isTagged() ) { |
288 operation = RemoveRows; |
353 operation = RemoveRows; |
289 } |
354 } |
290 commitTransaction( updateQuery, operation, row ); |
355 commitTransaction( updateQuery, operation, row ); |
357 LOG_FORMAT( "RadioHistoryModelPrivate::commitTransaction FAILED, rolling back: error = %s", GET_ERR( query ) ); |
422 LOG_FORMAT( "RadioHistoryModelPrivate::commitTransaction FAILED, rolling back: error = %s", GET_ERR( query ) ); |
358 success = mDatabase->rollback(); |
423 success = mDatabase->rollback(); |
359 LOG_ASSERT( success, LOG_FORMAT( "Rollback failed! err: %s", GET_ERR_PTR( mDatabase ) ) ); |
424 LOG_ASSERT( success, LOG_FORMAT( "Rollback failed! err: %s", GET_ERR_PTR( mDatabase ) ) ); |
360 } |
425 } |
361 } |
426 } |
|
427 |
|
428 bool RadioHistoryModelPrivate::prepareAndExec( QSqlQuery& query, const QString& sqlStr ) |
|
429 { |
|
430 bool isOk = true; |
|
431 isOk = query.prepare(sqlStr); |
|
432 |
|
433 if(isOk) { |
|
434 isOk = query.exec(); |
|
435 } |
|
436 return isOk; |
|
437 } |