| 206 | } |
| 207 | |
| 208 | int CDatabaseNode::AddNode(const QString &name, int parentId) |
| 209 | { |
| 210 | bool bRet = false; |
| 211 | QSqlQuery query(GetDatabase()); |
| 212 | |
| 213 | // Check if it already exists |
| 214 | query.prepare("SELECT id FROM `" + m_szTableName + "` " |
| 215 | " WHERE `parent_id` = :parent_id " |
| 216 | " AND `name`=:name" |
| 217 | ); |
| 218 | query.bindValue(":parent_id", parentId); |
| 219 | query.bindValue(":name", name); |
| 220 | bRet = query.exec(); |
| 221 | if(!bRet) { |
| 222 | SetError("Failed to add folders: " + query.lastError().text() |
| 223 | + "; Sql: " + query.executedQuery()); |
| 224 | qCritical(log) << GetError(); |
| 225 | return 0; |
| 226 | } |
| 227 | if(query.next()) { |
| 228 | return query.value(0).toInt(); |
| 229 | } |
| 230 | |
| 231 | // 获取最大排序值 |
| 232 | query.prepare("SELECT MAX(sort_order) FROM `" + m_szTableName + "` " |
| 233 | " WHERE `parent_id` = :parent_id"); |
| 234 | query.bindValue(":parent_id", parentId); |
| 235 | bRet = query.exec(); |
| 236 | if(!bRet) { |
| 237 | SetError("Failed to get max sort_order: " + query.lastError().text() |
| 238 | + "; Sql: " + query.executedQuery()); |
| 239 | qCritical(log) << GetError(); |
| 240 | return 0; |
| 241 | } |
| 242 | int maxOrder = 0; |
| 243 | if (query.next()) { |
| 244 | maxOrder = query.value(0).toInt() + 1; |
| 245 | } |
| 246 | |
| 247 | query.prepare( |
| 248 | "INSERT INTO `" + m_szTableName + "` " |
| 249 | " (`name`, `parent_id`, `sort_order`) " |
| 250 | " VALUES (:name, :parent_id, :sort_order) " |
| 251 | ); |
| 252 | query.bindValue(":name", name); |
| 253 | query.bindValue(":parent_id", parentId); |
| 254 | query.bindValue(":sort_order", maxOrder); |
| 255 | |
| 256 | int id = 0; |
| 257 | bRet = query.exec(); |
| 258 | if (bRet) { |
| 259 | id = query.lastInsertId().toInt(); |
| 260 | if(id > 0) { |
| 261 | emit sigAddNode(id, parentId); |
| 262 | emit sigChanged(); |
| 263 | } |
| 264 | } else { |
| 265 | SetError("Failed to add folders: " + query.lastError().text() |
no outgoing calls
no test coverage detected