| 613 | } |
| 614 | |
| 615 | int CDatabaseTree::Add(const TreeItem &item) |
| 616 | { |
| 617 | bool ok = false; |
| 618 | // Check if it is a leaf |
| 619 | if(item.IsNode()) { |
| 620 | qCritical(log) << "The item is not leaf"; |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | QSqlQuery query(GetDatabase()); |
| 625 | |
| 626 | // Check if it already exists |
| 627 | query.prepare( |
| 628 | "SELECT `id` FROM `" + m_szTableName + "` " |
| 629 | " WHERE `key`=:key AND `parent_id`=:parent_id" |
| 630 | ); |
| 631 | query.bindValue(":key", item.GetKey()); |
| 632 | query.bindValue(":parent_id", item.GetParentId()); |
| 633 | ok = query.exec(); |
| 634 | if(!ok) { |
| 635 | SetError("Failed to add tree: " + query.lastError().text() |
| 636 | + "; Sql: " + query.executedQuery()); |
| 637 | qCritical(log) << GetError(); |
| 638 | return 0; |
| 639 | } |
| 640 | if(query.next()) { |
| 641 | return query.value(0).toInt(); |
| 642 | } |
| 643 | |
| 644 | // Insert |
| 645 | query.prepare( |
| 646 | "INSERT INTO `" + m_szTableName + "` (`name`, `key`, " |
| 647 | "`created_time`, `modified_time`, `last_visit_time`, `parent_id`) " |
| 648 | "VALUES (:name, :key, " |
| 649 | ":created_time, :modified_time, :last_visit_time, :parent_id)" |
| 650 | ); |
| 651 | query.bindValue(":name", item.GetName()); |
| 652 | query.bindValue(":key", item.GetKey()); |
| 653 | QDateTime time = QDateTime::currentDateTime(); |
| 654 | query.bindValue(":created_time", time); |
| 655 | query.bindValue(":modified_time", time); |
| 656 | query.bindValue(":last_visit_time", time); |
| 657 | query.bindValue(":parent_id", item.GetParentId()); |
| 658 | |
| 659 | bool bRet = query.exec(); |
| 660 | if (!bRet) { |
| 661 | SetError("Failed to add tree item: " + query.lastError().text() |
| 662 | + "; Sql: " + query.executedQuery()); |
| 663 | qCritical(log) << GetError(); |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | int id = query.lastInsertId().toInt(); |
| 668 | if(0 < id) |
| 669 | emit sigAdd(id, item.GetParentId()); |
| 670 | return id; |
| 671 | } |
| 672 |
nothing calls this directly
no test coverage detected