comparison OrthancServer/DatabaseWrapper.cpp @ 1240:62c35e4b67db

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Dec 2014 17:12:35 +0100
parents 92c6b3b57699
children 90d2f320862d
comparison
equal deleted inserted replaced
1239:92c6b3b57699 1240:62c35e4b67db
599 void DatabaseWrapper::LogChange(int64_t internalId, 599 void DatabaseWrapper::LogChange(int64_t internalId,
600 const ServerIndexChange& change) 600 const ServerIndexChange& change)
601 { 601 {
602 if (change.GetChangeType() <= ChangeType_INTERNAL_LastLogged) 602 if (change.GetChangeType() <= ChangeType_INTERNAL_LastLogged)
603 { 603 {
604 const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
605
606 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); 604 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)");
607 s.BindInt(0, change.GetChangeType()); 605 s.BindInt(0, change.GetChangeType());
608 s.BindInt64(1, internalId); 606 s.BindInt64(1, internalId);
609 s.BindInt(2, change.GetResourceType()); 607 s.BindInt(2, change.GetResourceType());
610 s.BindString(3, boost::posix_time::to_iso_string(now)); 608 s.BindString(3, change.GetDate());
611 s.Run(); 609 s.Run();
612 } 610 }
613 611
614 listener_.SignalChange(change); 612 listener_.SignalChange(change);
615 } 613 }
616 614
617 615
618 void DatabaseWrapper::GetChangesInternal(Json::Value& target, 616 void DatabaseWrapper::GetChangesInternal(std::list<ServerIndexChange>& target,
617 bool& done,
619 SQLite::Statement& s, 618 SQLite::Statement& s,
620 int64_t since,
621 unsigned int maxResults) 619 unsigned int maxResults)
622 { 620 {
623 Json::Value changes = Json::arrayValue; 621 while (target.size() < maxResults && s.Step())
624 int64_t last = since;
625
626 while (changes.size() < maxResults && s.Step())
627 { 622 {
628 int64_t seq = s.ColumnInt64(0); 623 int64_t seq = s.ColumnInt64(0);
629 ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1)); 624 ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1));
630 int64_t internalId = s.ColumnInt64(2);
631 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3)); 625 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3));
632 const std::string& date = s.ColumnString(4); 626 const std::string& date = s.ColumnString(4);
627
628 int64_t internalId = s.ColumnInt64(2);
633 std::string publicId = GetPublicId(internalId); 629 std::string publicId = GetPublicId(internalId);
634 630
635 Json::Value item = Json::objectValue; 631 target.push_back(ServerIndexChange(seq, changeType, resourceType, publicId, date));
636 item["Seq"] = static_cast<int>(seq); 632 }
637 item["ChangeType"] = EnumerationToString(changeType); 633
638 item["ResourceType"] = EnumerationToString(resourceType); 634 done = !(target.size() == maxResults && s.Step());
639 item["ID"] = publicId; 635 }
640 item["Path"] = GetBasePath(resourceType, publicId); 636
641 item["Date"] = date; 637
642 last = seq; 638 void DatabaseWrapper::GetChanges(std::list<ServerIndexChange>& target,
643 639 bool& done,
644 changes.append(item);
645 }
646
647 target = Json::objectValue;
648 target["Changes"] = changes;
649 target["Done"] = !(changes.size() == maxResults && s.Step());
650 target["Last"] = static_cast<int>(last);
651 }
652
653
654 void DatabaseWrapper::GetChanges(Json::Value& target,
655 int64_t since, 640 int64_t since,
656 unsigned int maxResults) 641 unsigned int maxResults)
657 { 642 {
658 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); 643 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?");
659 s.BindInt64(0, since); 644 s.BindInt64(0, since);
660 s.BindInt(1, maxResults + 1); 645 s.BindInt(1, maxResults + 1);
661 GetChangesInternal(target, s, since, maxResults); 646 GetChangesInternal(target, done, s, maxResults);
662 } 647 }
663 648
664 void DatabaseWrapper::GetLastChange(Json::Value& target) 649 void DatabaseWrapper::GetLastChange(std::list<ServerIndexChange>& target)
665 { 650 {
651 bool done; // Ignored
666 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); 652 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1");
667 GetChangesInternal(target, s, 0, 1); 653 GetChangesInternal(target, done, s, 1);
668 } 654 }
669 655
670 656
671 void DatabaseWrapper::LogExportedResource(ResourceType resourceType, 657 void DatabaseWrapper::LogExportedResource(ResourceType resourceType,
672 const std::string& publicId, 658 const std::string& publicId,