diff --git a/src/database/database.cc b/src/database/database.cc index 4928f6e66c..de0b8eb913 100644 --- a/src/database/database.cc +++ b/src/database/database.cc @@ -1117,7 +1117,7 @@ error Database::loadUsersRelatedData(locked &user) { return err; } - err = loadProjects(user->ID(), user->GetRelatedData()->Projects); + err = loadModels(user->ID(), user->GetRelatedData()->Projects); if (err != noError) { return err; } @@ -1259,93 +1259,6 @@ error Database::LoadUserByID( return noError; } -error Database::loadProjects(const Poco::UInt64 &UID, - ProtectedContainer &list) { - - if (!UID) { - return error("Cannot load user projects without an user ID"); - } - - try { - list.clear(); - - Poco::Mutex::ScopedLock lock(session_m_); - - Poco::Data::Statement select(*session_); - select << - "SELECT projects.local_id, projects.id, projects.uid, " - "projects.name, projects.guid, projects.wid, projects.color, projects.cid, " - "projects.active, projects.billable, projects.client_guid, " - "clients.name as client_name " - "FROM projects " - "LEFT JOIN clients on projects.cid = clients.id " - "LEFT JOIN workspaces on projects.wid = workspaces.id " - "WHERE projects.uid = :uid " - "ORDER BY workspaces.name COLLATE NOCASE ASC," - "client_name COLLATE NOCASE ASC," - "projects.name COLLATE NOCASE ASC;", - useRef(UID); - error err = last_error("loadProjects"); - if (err != noError) { - return err; - } - Poco::Data::RecordSet rs(select); - while (!select.done()) { - select.execute(); - bool more = rs.moveFirst(); - while (more) { - auto model = list.create(); - model->SetLocalID(rs[0].convert()); - if (rs[1].isEmpty()) { - model->SetID(0); - } else { - model->SetID(rs[1].convert()); - } - model->SetUID(rs[2].convert()); - model->SetName(rs[3].convert()); - if (rs[4].isEmpty()) { - model->SetGUID(""); - } else { - model->SetGUID(rs[4].convert()); - } - model->SetWID(rs[5].convert()); - if (rs[6].isEmpty()) { - model->SetColor(""); - } else { - model->SetColor(rs[6].convert()); - } - if (rs[7].isEmpty()) { - model->SetCID(0); - } else { - model->SetCID(rs[7].convert()); - } - model->SetActive(rs[8].convert()); - model->SetBillable(rs[9].convert()); - if (rs[10].isEmpty()) { - model->SetClientGUID(""); - } else { - model->SetClientGUID(rs[10].convert()); - } - if (rs[11].isEmpty()) { - model->SetClientName(""); - } else { - model->SetClientName(rs[11].convert()); - } - model->ClearDirty(); - - more = rs.moveNext(); - } - } - } catch(const Poco::Exception& exc) { - return exc.displayText(); - } catch(const std::exception& ex) { - return ex.what(); - } catch(const std::string & ex) { - return ex; - } - return last_error("loadProjects"); -} - template error Database::saveRelatedModels(const Poco::UInt64 UID, const std::string &table_name, diff --git a/src/database/database.h b/src/database/database.h index cf6cf68d50..1f5ac98bd6 100644 --- a/src/database/database.h +++ b/src/database/database.h @@ -292,10 +292,6 @@ class TOGGL_INTERNAL_EXPORT Database { const Poco::UInt64 &UID, ProtectedContainer &list); - error loadProjects( - const Poco::UInt64 &UID, - ProtectedContainer &list); - template error saveRelatedModels( const Poco::UInt64 UID, diff --git a/src/model/project.h b/src/model/project.h index 33f26e5b76..8fdd3cbd61 100644 --- a/src/model/project.h +++ b/src/model/project.h @@ -14,7 +14,52 @@ namespace toggl { class TOGGL_INTERNAL_EXPORT Project : public BaseModel { - public: + inline static const std::string modelName{ kModelProject }; + inline static const Query query{ + Query::Table{"projects"}, + Query::Columns { + { "name", true }, + { "wid", true }, + { "color", false }, + { "cid", false }, + { "active", true }, + { "billable", true }, + { "client_guid", false }, + { "clients.name", false } + }, + Query::Join{ + "LEFT JOIN clients ON projects.cid = clients.id", + "LEFT JOIN workspaces ON projects.wid = workspaces.id" + }, + Query::OrderBy{ + "workspaces.name COLLATE NOCASE ASC", + "clients.name COLLATE NOCASE ASC", + "projects.name COLLATE NOCASE ASC" + }, + &BaseModel::query + }; + Project(ProtectedBase *container, Poco::Data::RecordSet &rs) + : BaseModel(container, rs) + { + size_t ptr{ query.Offset() }; + load(rs, query.IsRequired(ptr), ptr, name_); + ptr++; + load(rs, query.IsRequired(ptr), ptr, wid_); + ptr++; + load(rs, query.IsRequired(ptr), ptr, color_); + ptr++; + load(rs, query.IsRequired(ptr), ptr, cid_); + ptr++; + load(rs, query.IsRequired(ptr), ptr, active_); + ptr++; + load(rs, query.IsRequired(ptr), ptr, billable_); + ptr++; + load(rs, query.IsRequired(ptr), ptr, client_guid_); + ptr++; + load(rs, query.IsRequired(ptr), ptr, client_name_); + ptr++; + ClearDirty(); + } Project(ProtectedBase *container) : BaseModel(container) {}