Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
Implement the new constructor for every model except Project (lib)
Browse files Browse the repository at this point in the history
Notice how the deleted number count is higher than added now
  • Loading branch information
MartinBriza committed Mar 30, 2020
1 parent 722eb62 commit 9bf8bc9
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 596 deletions.
555 changes: 8 additions & 547 deletions src/database/database.cc

Large diffs are not rendered by default.

36 changes: 0 additions & 36 deletions src/database/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,46 +292,10 @@ class TOGGL_INTERNAL_EXPORT Database {
const Poco::UInt64 &UID,
ProtectedContainer<T> &list);

error loadClients(
const Poco::UInt64 &UID,
ProtectedContainer<Client> &list);

error loadProjects(
const Poco::UInt64 &UID,
ProtectedContainer<Project> &list);

error loadTasks(
const Poco::UInt64 &UID,
ProtectedContainer<Task> &list);

error loadTags(
const Poco::UInt64 &UID,
ProtectedContainer<Tag> &list);

error loadAutotrackerRules(
const Poco::UInt64 &UID,
ProtectedContainer<AutotrackerRule> &list);

error loadObmActions(
const Poco::UInt64 &UID,
ProtectedContainer<ObmAction> &list);

error loadObmExperiments(
const Poco::UInt64 &UID,
ProtectedContainer<ObmExperiment> &list);

error loadTimeEntries(
const Poco::UInt64 &UID,
ProtectedContainer<TimeEntry> &list);

error loadTimelineEvents(
const Poco::UInt64 &UID,
ProtectedContainer<TimelineEvent> &list);

error loadTimeEntriesFromSQLStatement(
Poco::Data::Statement *select,
ProtectedContainer<TimeEntry> &list);

template <typename T>
error saveRelatedModels(
const Poco::UInt64 UID,
Expand Down
13 changes: 11 additions & 2 deletions src/database/migrations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ error Migrations::migrateObmActions() {
if (err != noError) {
return err;
}

return noError;
}

error Migrations::migrateObmExperiments() {
Expand Down Expand Up @@ -121,8 +123,8 @@ error Migrations::migrateAutotracker() {

err = db_->Migrate(
"autotracker_settings.guid",
"ALTER TABLE autotracker_settings "
"ADD COLUMN guid VARCHAR;"
"ALTER TABLE autotracker_settings ADD COLUMN guid VARCHAR;"
"ALTER TABLE autotracker_settings ADD COLUMN id INTEGER;"
"CREATE UNIQUE INDEX id_autotracker_settings_guid ON autotracker_settings (uid, guid);");
if (err != noError) {
return err;
Expand Down Expand Up @@ -571,6 +573,13 @@ error Migrations::migrateTimeline() {
return err;
}

err = db_->Migrate(
"timeline_events.id",
"ALTER TABLE timeline_events ADD COLUMN id INTEGER;");
if (err != noError) {
return err;
}

return noError;
}

Expand Down
24 changes: 24 additions & 0 deletions src/model/autotracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@
namespace toggl {

class TOGGL_INTERNAL_EXPORT AutotrackerRule : public BaseModel {
inline static const std::string modelName{ kModelAutotrackerRule };
inline static const Query query{
Query::Table{"autotracker_settings"},
Query::Columns {
{ "term", true },
{ "pid", false },
{ "tid", true }
},
Query::Join{},
Query::OrderBy{"term"},
&BaseModel::query
};
AutotrackerRule(ProtectedBase *container, Poco::Data::RecordSet &rs)
: BaseModel(container, rs)
{
size_t ptr{ query.Offset() };
load(rs, query.IsRequired(ptr), ptr, term_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, pid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, tid_);
ptr++;
ClearDirty();
}
AutotrackerRule(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down
12 changes: 8 additions & 4 deletions src/model/base_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,14 @@ class TOGGL_INTERNAL_EXPORT BaseModel {
: container_(container)
{
size_t ptr { query.Offset() };
load(rs, query.IsRequired(ptr), ptr++, local_id_);
load(rs, query.IsRequired(ptr), ptr++, id_);
load(rs, query.IsRequired(ptr), ptr++, uid_);
load(rs, query.IsRequired(ptr), ptr++, guid_);
load(rs, query.IsRequired(ptr), ptr, local_id_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, id_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, uid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, guid_);
ptr++;
ClearDirty();
}

Expand Down
21 changes: 21 additions & 0 deletions src/model/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@
namespace toggl {

class TOGGL_INTERNAL_EXPORT Client : public BaseModel {
inline static const std::string modelName{ kModelClient };
inline static const Query query{
Query::Table{"clients"},
Query::Columns {
{ "name", true },
{ "wid", true }
},
Query::Join{},
Query::OrderBy{"name"},
&BaseModel::query
};
Client(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++;
ClearDirty();
}
Client(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down
75 changes: 75 additions & 0 deletions src/model/obm_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@
namespace toggl {

class TOGGL_INTERNAL_EXPORT ObmAction : public BaseModel {
inline static const std::string modelName{ kModelObmAction };
inline static const Query query{
Query::Table{"obm_actions"},
Query::Columns {
{ "local_id", true },
{ "guid", false },
{ "experiment_id", true },
{ "key", true },
{ "value", true }
},
Query::Join{},
Query::OrderBy{},
// doesn't actually use stuff from BaseModel
nullptr
};
ObmAction(ProtectedBase *container, Poco::Data::RecordSet &rs)
: BaseModel(container, rs)
{
size_t ptr{ query.Offset() };
// hacky
Poco::Int64 localId;
load(rs, query.IsRequired(ptr), ptr, localId);
SetLocalID(localId);
ptr++;
guid uuid;
load(rs, query.IsRequired(ptr), ptr, uuid);
SetGUID(uuid);
ptr++;
load(rs, query.IsRequired(ptr), ptr, experiment_id_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, key_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, value_);
ptr++;
ClearDirty();
}
ObmAction(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down Expand Up @@ -48,6 +84,45 @@ class TOGGL_INTERNAL_EXPORT ObmAction : public BaseModel {
};

class TOGGL_INTERNAL_EXPORT ObmExperiment : public BaseModel {
inline static const std::string modelName{ kModelObmExperiment };
inline static const Query query{
Query::Table{"obm_experiments"},
Query::Columns {
{ "local_id", true },
{ "guid", false },
{ "nr", true },
{ "included", true },
{ "has_seen", true },
{ "actions", true }
},
Query::Join{},
Query::OrderBy{},
// doesn't actually use stuff from BaseModel
nullptr
};
ObmExperiment(ProtectedBase *container, Poco::Data::RecordSet &rs)
: BaseModel(container, rs)
{
size_t ptr{ query.Offset() };
// hacky
Poco::Int64 localId;
load(rs, query.IsRequired(ptr), ptr, localId);
SetLocalID(localId);
ptr++;
guid uuid;
load(rs, query.IsRequired(ptr), ptr, uuid);
SetGUID(uuid);
ptr++;
load(rs, query.IsRequired(ptr), ptr, nr_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, included_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, has_seen_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, actions_);
ptr++;
ClearDirty();
}
ObmExperiment(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down
21 changes: 21 additions & 0 deletions src/model/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
namespace toggl {

class TOGGL_INTERNAL_EXPORT Tag : public BaseModel {
inline static const std::string modelName{ kModelTag };
inline static const Query query{
Query::Table{"tags"},
Query::Columns {
{ "name", true },
{ "wid", true }
},
Query::Join{},
Query::OrderBy{"name"},
&BaseModel::query
};
Tag(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++;
ClearDirty();
}
Tag(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down
27 changes: 27 additions & 0 deletions src/model/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@
namespace toggl {

class TOGGL_INTERNAL_EXPORT Task : public BaseModel {
inline static const std::string modelName{ kModelTask };
inline static const Query query{
Query::Table{"tasks"},
Query::Columns {
{ "name", true },
{ "wid", true },
{ "pid", false },
{ "active", true }
},
Query::Join{},
Query::OrderBy{"name"},
&BaseModel::query
};
Task(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, pid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, active_);
ptr++;
ClearDirty();
}
Task(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down
81 changes: 81 additions & 0 deletions src/model/time_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,87 @@
namespace toggl {

class TOGGL_INTERNAL_EXPORT TimeEntry : public BaseModel, public TimedEvent {
inline static const std::string modelName{ kModelTimeEntry };
inline static const Query query{
Query::Table{"time_entries"},
Query::Columns {
{ "description", false },
{ "wid", true },
{ "pid", false },
{ "tid", false },
{ "billable", true },
{ "duronly", true },
{ "ui_modified_at", false },
{ "start", true },
{ "stop", false },
{ "duration", true },
{ "tags", false },
{ "created_with", false },
{ "deleted_at", false },
{ "updated_at", false },
{ "project_guid", false },
{ "validation_error", false }
},
Query::Join{},
Query::OrderBy{"start DESC"},
&BaseModel::query
};
TimeEntry(ProtectedBase *container, Poco::Data::RecordSet &rs)
: BaseModel(container, rs)
{
size_t ptr{ query.Offset() };
load(rs, query.IsRequired(ptr), ptr, description_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, wid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, pid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, tid_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, billable_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, duronly_);
ptr++;
// HACKS AHEAD
Poco::Int64 modifiedAt;
load(rs, query.IsRequired(ptr), ptr, modifiedAt);
SetUIModifiedAt(modifiedAt);
ptr++;
load(rs, query.IsRequired(ptr), ptr, start_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, stop_);
ptr++;
load(rs, query.IsRequired(ptr), ptr, duration_in_seconds_);
ptr++;
// MORE HACKS
std::string tags;
load(rs, query.IsRequired(ptr), ptr, tags);
SetTags(tags);
ptr++;
load(rs, query.IsRequired(ptr), ptr, created_with_);
ptr++;
// HACK
Poco::Int64 deletedAt;
load(rs, query.IsRequired(ptr), ptr, deletedAt);
SetDeletedAt(deletedAt);
ptr++;
// HACK
Poco::Int64 updatedAt;
load(rs, query.IsRequired(ptr), ptr, updatedAt);
SetUpdatedAt(updatedAt);
ptr++;
load(rs, query.IsRequired(ptr), ptr, project_guid_);
ptr++;
error validationError;
load(rs, query.IsRequired(ptr), ptr, validationError);
SetValidationError(validationError);
ptr++;

ClearDirty();
EnsureGUID();
if (Dirty())
SetUIModified();
}
TimeEntry(ProtectedBase *container)
: BaseModel(container)
{}
Expand Down
Loading

0 comments on commit 9bf8bc9

Please sign in to comment.