MCPcopy Create free account
hub / github.com/KangLin/RabbitRemoteControl / AddFavorite

Method AddFavorite

App/Client/Favorite/FavoriteDatabase.cpp:157–257  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

155}
156
157int CFavoriteDatabase::AddFavorite(const QString &szFile,
158 const QString &szName, const QIcon &icon,
159 const QString szDescription, int parentId)
160{
161 int ret = 0;
162 if (szName.trimmed().isEmpty()) {
163 qWarning(log) << "Favorite name cannot be empty";
164 return ret;
165 }
166
167 if (szFile.trimmed().isEmpty()) {
168 qCritical(log) << "Favorite file cannot be empty";
169 return ret;
170 }
171
172 // 验证 parentId 是否存在(如果parentId不为0)
173 if (parentId > 0) {
174 if (GetNode(parentId).GetId() == 0) {
175 qWarning(log) << "Parent item with ID" << parentId << "does not exist";
176 return ret;
177 }
178 }
179
180 QSqlDatabase db = GetDatabase();
181 bool success = false;
182 // 使用事务确保数据一致性
183 if (!db.transaction()) {
184 SetError("Failed to start transaction: " + db.lastError().text());
185 qCritical(log) << GetError();
186 return ret;
187 }
188
189 try {
190 QSqlQuery query(db);
191 int key = 0;
192 //检查是否已存在
193 query.prepare(
194 "SELECT id FROM favorite "
195 "WHERE file=:file");
196 query.bindValue(":file", szFile);
197 if(query.exec() && query.next()) {
198 key = query.value(0).toInt();
199 }
200
201 if(0 == key) {
202 // 插入新记录
203 query.prepare(
204 "INSERT INTO favorite (name, icon, file, description)"
205 "VALUES (:name, :icon, :file, :description)"
206 );
207 query.bindValue(":name", szName);
208 query.bindValue(":icon", m_IconDB.GetIcon(icon));
209 query.bindValue(":file", szFile);
210 query.bindValue(":description", szDescription);
211
212 success = query.exec();
213 if (!success) {
214 QString szErr = "Failed to insert favorite table: " + query.lastError().text();

Callers

nothing calls this directly

Calls 5

SetKeyMethod · 0.80
SetParentIdMethod · 0.80
isEmptyMethod · 0.45
GetIdMethod · 0.45
GetIconMethod · 0.45

Tested by

no test coverage detected