| 491 | } |
| 492 | |
| 493 | int CDatabaseIcon::GetIcon(const QIcon &icon) |
| 494 | { |
| 495 | bool bRet = false; |
| 496 | if(icon.isNull()) return 0; |
| 497 | |
| 498 | QString szSql; |
| 499 | QSqlQuery query(GetDatabase()); |
| 500 | QString szName = icon.name(); |
| 501 | if(szName.isEmpty()) { |
| 502 | // Check hash and data |
| 503 | QByteArray data = RabbitCommon::CIconUtils::iconToByteArray(icon); |
| 504 | QString szHash = RabbitCommon::CIconUtils::hashIconData(data); |
| 505 | if(data.isEmpty() || szHash.isEmpty()) |
| 506 | return 0; |
| 507 | szSql = "SELECT id, data FROM " + m_szTableName + " WHERE hash=:hash"; |
| 508 | query.prepare(szSql); |
| 509 | query.bindValue(":hash", szHash); |
| 510 | // qDebug(log) << "prepare:" << query.executedQuery(); |
| 511 | // qDebug(log) << "Bound values:" << query.boundValues(); |
| 512 | bRet = query.exec(); |
| 513 | if(!bRet) { |
| 514 | qDebug(log) << "Failed to select icon hash:" << szHash |
| 515 | << "Error:" << query.lastError().text() |
| 516 | << "Sql:" << query.executedQuery(); |
| 517 | return 0; |
| 518 | } |
| 519 | while (query.next()) { |
| 520 | // check a same data |
| 521 | if(data == query.value(1).toByteArray()) { |
| 522 | return query.value(0).toInt(); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | szSql = "INSERT INTO " + m_szTableName + " (hash, data) " |
| 527 | "VALUES (:hash, :data)"; |
| 528 | query.prepare(szSql); |
| 529 | query.bindValue(":hash", szHash); |
| 530 | query.bindValue(":data", data); |
| 531 | bRet = query.exec(); |
| 532 | if(!bRet) { |
| 533 | SetError("Failed to insert icon hash: " + szHash |
| 534 | + "; Error: " + query.lastError().text() |
| 535 | + "; Sql: " + query.executedQuery()); |
| 536 | qCritical(log) << GetError(); |
| 537 | return 0; |
| 538 | } |
| 539 | return query.lastInsertId().toInt(); |
| 540 | } |
| 541 | |
| 542 | // Check name |
| 543 | szSql = "SELECT id FROM " + m_szTableName + " WHERE name=:name"; |
| 544 | query.prepare(szSql); |
| 545 | query.bindValue(":name", szName); |
| 546 | bRet = query.exec(); |
| 547 | if(!bRet) { |
| 548 | SetError("Failed to select icon name: " + szName |
| 549 | + "; Error: " + query.lastError().text() |
| 550 | + "; Sql: " + query.executedQuery()); |