commit b07356835d48aed99ad9097d85af67e292a7f3d9
parent 30beeae0a524473b78a9c7a0c10983f6660d4dff
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Mon, 18 Mar 2024 19:33:26 -0700
Provide access to other periods in gettimes()
This required changing and adding some of the SQL statements aswell
as updating calls to gettimes() with the added 'period' argument.
Diffstat:
6 files changed, 17 insertions(+), 9 deletions(-)
diff --git a/pages/export.c b/pages/export.c
@@ -46,7 +46,7 @@ pageexport(struct pagedata *pd)
serialize = serialize_epoch;
}
- if (gettimes(pd, pd->user->hash, ×))
+ if (gettimes(pd, pd->user->hash, 0, ×))
return errorpage(pd, KHTTP_500);
if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS],
diff --git a/pages/main.c b/pages/main.c
@@ -378,7 +378,7 @@ pagemain(struct pagedata *pd)
if (status != KCGI_OK)
return status;
- if (gettimes(pd, pd->user->hash, ×))
+ if (gettimes(pd, pd->user->hash, 0, ×))
err(1, "gettimes");
else if (!times && !(times = newtimesheet()))
err(1, "gettimes");
diff --git a/stmt.c b/stmt.c
@@ -70,7 +70,11 @@ struct sqlbox_pstmt pstmts[] = {
},
[StmtGetTimes] = { .stmt =
"SELECT period, start, startbreak, endbreak, end FROM times\n"
- " WHERE userid IS ? AND period ISNULL"
+ " WHERE userid IS"
+ },
+ [StmtGetTimePeriod] = { .stmt =
+ "SELECT period, start, startbreak, endbreak, end FROM times\n"
+ " WHERE userid IS ? AND period IS ?"
},
[StmtBreakTime] = { .stmt =
"UPDATE times SET period =\n"
diff --git a/stmt.h b/stmt.h
@@ -13,6 +13,7 @@ enum StmtID {
StmtEndBreakTime,
StmtEndTime,
StmtGetTimes,
+ StmtGetTimePeriod,
StmtBreakTime,
StmtMax
};
diff --git a/times.c b/times.c
@@ -110,24 +110,27 @@ timesheet_set(struct timesheet *ts, enum time_field f, time_t v)
}
int
-gettimes(struct pagedata *pd, char *hash, struct timesheet **rtimes)
+gettimes(struct pagedata *pd, char *hash, int period, struct timesheet **rtimes)
{
size_t stmtid;
- struct sqlbox_parm p = {
- .sparm = hash, .type = SQLBOX_PARM_STRING
+ struct sqlbox_parm p[] = {
+ { .sparm = hash, .type = SQLBOX_PARM_STRING },
+ { .iparm = period, .type = (period <= 0 ? SQLBOX_PARM_NULL : SQLBOX_PARM_INT) },
};
struct sqlbox_parmset *r;
struct timesheet *times, *oldtime, *time;
unsigned int i;
- stmtid = sqlbox_prepare_bind(pd->db, pd->dbid, StmtGetTimes, 1, &p, 0);
+ stmtid = sqlbox_prepare_bind(pd->db, pd->dbid,
+ period < 0 ? StmtGetTimes : StmtGetTimePeriod,
+ period < 0 ? 1 : 2, p, 0);
if (stmtid == 0)
err(1, "prepare bind");
times = oldtime = NULL;
while ((r = sqlbox_step(pd->db, stmtid)) &&
!(!r->psz && r->code == SQLBOX_CODE_OK)) {
- assert(r->psz == 4);
+ assert(r->psz == 5);
oldtime = time;
time = newtimesheet();
if (!time) {
diff --git a/times.h b/times.h
@@ -30,5 +30,5 @@ void freetimesheet(struct timesheet *ts);
struct timesheet *newtimesheet(void);
struct timesheet *inserttimesheet(struct timesheet *ts, struct timesheet *list);
void timesheet_set(struct timesheet *ts, enum time_field f, time_t v);
-int gettimes(struct pagedata *pd, char *hash, struct timesheet **rtimes);
+int gettimes(struct pagedata *pd, char *hash, int period, struct timesheet **rtimes);
time_t getduration(struct timesheet *ts);