| 94 | } |
| 95 | |
| 96 | int CDatabaseUrl::AddUrl(const QString &url, const QString &title, const QIcon &icon) |
| 97 | { |
| 98 | int nId = 0; |
| 99 | if (url.isEmpty()) return 0; |
| 100 | |
| 101 | QSqlQuery query(GetDatabase()); |
| 102 | |
| 103 | // 检查URL是否已存在 |
| 104 | query.prepare("SELECT id, title, icon FROM " + m_szTableName + " WHERE url = :url"); |
| 105 | query.bindValue(":url", url); |
| 106 | if (!query.exec()) { |
| 107 | SetError("Failed to exec: " + query.executedQuery() |
| 108 | + "; Error: " + query.lastError().text()); |
| 109 | qCritical(log) << GetError(); |
| 110 | return -1; |
| 111 | } |
| 112 | if(query.next()) { |
| 113 | // 更新现有记录 |
| 114 | nId = query.value(0).toInt(); |
| 115 | QString szTitle = query.value(1).toString(); |
| 116 | if(!title.isEmpty()) |
| 117 | szTitle = title; |
| 118 | int iconID = query.value(2).toInt(); |
| 119 | if(!icon.isNull()) |
| 120 | iconID = m_iconDB.GetIcon(icon); |
| 121 | query.prepare( |
| 122 | "UPDATE " + m_szTableName + " SET " |
| 123 | "title = :title, " |
| 124 | "icon = :icon, " |
| 125 | "visit_time = :visit_time " |
| 126 | "WHERE id = :id" |
| 127 | ); |
| 128 | query.bindValue(":title", szTitle); |
| 129 | query.bindValue(":icon", iconID); |
| 130 | query.bindValue(":visit_time", QDateTime::currentDateTime()); |
| 131 | query.bindValue(":id", nId); |
| 132 | } else { |
| 133 | // 插入新记录 |
| 134 | query.prepare( |
| 135 | "INSERT INTO " + m_szTableName + " (url, title, icon) " |
| 136 | "VALUES (:url, :title, :icon)" |
| 137 | ); |
| 138 | query.bindValue(":url", url); |
| 139 | QString szTitle = title; |
| 140 | if(szTitle.isEmpty()) |
| 141 | szTitle = url; |
| 142 | query.bindValue(":title", szTitle); |
| 143 | query.bindValue(":icon", m_iconDB.GetIcon(icon)); |
| 144 | } |
| 145 | |
| 146 | qDebug(log) << "Sql:" << query.executedQuery(); |
| 147 | qDebug(log) << "Bound values:" << query.boundValues(); |
| 148 | bool success = query.exec(); |
| 149 | if (!success) { |
| 150 | nId = 0; |
| 151 | SetError("Failed to add " + m_szTableName + ": " + url |
| 152 | + "; Error: " + query.lastError().text() |
| 153 | + "; Sql: " + query.executedQuery()); |
no test coverage detected