summaryrefslogtreecommitdiff
path: root/internal/queries/queries.sql.go
blob: 3da8b98d4f4c224e4da394cde2236b265ed36f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: queries.sql

package queries

import (
	"context"
	"database/sql"
	"time"
)

const createClient = `-- name: CreateClient :one
insert into client (name, email, billable_rate)
values (?1, ?2, ?3)
returning id, name, email, billable_rate, created_at
`

type CreateClientParams struct {
	Name         string
	Email        sql.NullString
	BillableRate sql.NullInt64
}

func (q *Queries) CreateClient(ctx context.Context, arg CreateClientParams) (Client, error) {
	row := q.db.QueryRowContext(ctx, createClient, arg.Name, arg.Email, arg.BillableRate)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Email,
		&i.BillableRate,
		&i.CreatedAt,
	)
	return i, err
}

const createProject = `-- name: CreateProject :one
insert into project (name, client_id, billable_rate)
values (?1, ?2, ?3)
returning id, name, client_id, billable_rate, created_at
`

type CreateProjectParams struct {
	Name         string
	ClientID     int64
	BillableRate sql.NullInt64
}

func (q *Queries) CreateProject(ctx context.Context, arg CreateProjectParams) (Project, error) {
	row := q.db.QueryRowContext(ctx, createProject, arg.Name, arg.ClientID, arg.BillableRate)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ClientID,
		&i.BillableRate,
		&i.CreatedAt,
	)
	return i, err
}

const createTimeEntry = `-- name: CreateTimeEntry :one
insert into time_entry (start_time, description, client_id, project_id, billable_rate)
values (
    datetime('now', 'utc'), 
    ?1, 
    ?2, 
    ?3,
    coalesce(
        ?4,
        (select p.billable_rate from project p where p.id = ?3),
        (select c.billable_rate from client c where c.id = ?2)
    )
)
returning id, start_time, end_time, description, client_id, project_id, billable_rate
`

type CreateTimeEntryParams struct {
	Description  sql.NullString
	ClientID     int64
	ProjectID    sql.NullInt64
	BillableRate interface{}
}

func (q *Queries) CreateTimeEntry(ctx context.Context, arg CreateTimeEntryParams) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, createTimeEntry,
		arg.Description,
		arg.ClientID,
		arg.ProjectID,
		arg.BillableRate,
	)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const createTimeEntryWithTimes = `-- name: CreateTimeEntryWithTimes :one
insert into time_entry (start_time, end_time, description, client_id, project_id, billable_rate)
values (
    ?1, 
    ?2, 
    ?3, 
    ?4, 
    ?5,
    coalesce(
        ?6,
        (select p.billable_rate from project p where p.id = ?5),
        (select c.billable_rate from client c where c.id = ?4)
    )
)
returning id, start_time, end_time, description, client_id, project_id, billable_rate
`

type CreateTimeEntryWithTimesParams struct {
	StartTime    time.Time
	EndTime      sql.NullTime
	Description  sql.NullString
	ClientID     int64
	ProjectID    sql.NullInt64
	BillableRate interface{}
}

func (q *Queries) CreateTimeEntryWithTimes(ctx context.Context, arg CreateTimeEntryWithTimesParams) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, createTimeEntryWithTimes,
		arg.StartTime,
		arg.EndTime,
		arg.Description,
		arg.ClientID,
		arg.ProjectID,
		arg.BillableRate,
	)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const findClient = `-- name: FindClient :many
select c1.id, c1.name, c1.email, c1.billable_rate, c1.created_at from client c1 where c1.id = cast(?1 as integer)
union all
select c2.id, c2.name, c2.email, c2.billable_rate, c2.created_at from client c2 where c2.name = ?2
`

type FindClientParams struct {
	ID   int64
	Name string
}

func (q *Queries) FindClient(ctx context.Context, arg FindClientParams) ([]Client, error) {
	rows, err := q.db.QueryContext(ctx, findClient, arg.ID, arg.Name)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Client
	for rows.Next() {
		var i Client
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Email,
			&i.BillableRate,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const findProject = `-- name: FindProject :many
select p1.id, p1.name, p1.client_id, p1.billable_rate, p1.created_at from project p1 where p1.id = cast(?1 as integer)
union all
select p2.id, p2.name, p2.client_id, p2.billable_rate, p2.created_at from project p2 where p2.name = ?2
`

type FindProjectParams struct {
	ID   int64
	Name string
}

func (q *Queries) FindProject(ctx context.Context, arg FindProjectParams) ([]Project, error) {
	rows, err := q.db.QueryContext(ctx, findProject, arg.ID, arg.Name)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Project
	for rows.Next() {
		var i Project
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.ClientID,
			&i.BillableRate,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getActiveTimeEntry = `-- name: GetActiveTimeEntry :one
select id, start_time, end_time, description, client_id, project_id, billable_rate from time_entry 
where end_time is null
order by start_time desc
limit 1
`

func (q *Queries) GetActiveTimeEntry(ctx context.Context) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, getActiveTimeEntry)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const getClientByName = `-- name: GetClientByName :one
select id, name, email, billable_rate, created_at from client where name = ?1 limit 1
`

func (q *Queries) GetClientByName(ctx context.Context, name string) (Client, error) {
	row := q.db.QueryRowContext(ctx, getClientByName, name)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Email,
		&i.BillableRate,
		&i.CreatedAt,
	)
	return i, err
}

const getMonthSummaryByProject = `-- name: GetMonthSummaryByProject :many
select 
    p.id as project_id,
    p.name as project_name,
    c.id as client_id,
    c.name as client_name,
    cast(sum(
        case 
            when te.end_time is null then 
                (julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
            else 
                (julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
        end
    ) as integer) as total_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where date(te.start_time) >= date('now', 'start of month')
    and date(te.start_time) <= date('now')
group by p.id, p.name, c.id, c.name
order by c.name, p.name
`

type GetMonthSummaryByProjectRow struct {
	ProjectID    sql.NullInt64
	ProjectName  sql.NullString
	ClientID     int64
	ClientName   string
	TotalSeconds int64
}

func (q *Queries) GetMonthSummaryByProject(ctx context.Context) ([]GetMonthSummaryByProjectRow, error) {
	rows, err := q.db.QueryContext(ctx, getMonthSummaryByProject)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetMonthSummaryByProjectRow
	for rows.Next() {
		var i GetMonthSummaryByProjectRow
		if err := rows.Scan(
			&i.ProjectID,
			&i.ProjectName,
			&i.ClientID,
			&i.ClientName,
			&i.TotalSeconds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getMostRecentTimeEntry = `-- name: GetMostRecentTimeEntry :one
select id, start_time, end_time, description, client_id, project_id, billable_rate from time_entry 
order by start_time desc
limit 1
`

func (q *Queries) GetMostRecentTimeEntry(ctx context.Context) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, getMostRecentTimeEntry)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}

const getProjectByNameAndClient = `-- name: GetProjectByNameAndClient :one
select id, name, client_id, billable_rate, created_at from project where name = ?1 and client_id = ?2 limit 1
`

type GetProjectByNameAndClientParams struct {
	Name     string
	ClientID int64
}

func (q *Queries) GetProjectByNameAndClient(ctx context.Context, arg GetProjectByNameAndClientParams) (Project, error) {
	row := q.db.QueryRowContext(ctx, getProjectByNameAndClient, arg.Name, arg.ClientID)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ClientID,
		&i.BillableRate,
		&i.CreatedAt,
	)
	return i, err
}

const getWeekSummaryByProject = `-- name: GetWeekSummaryByProject :many
select 
    p.id as project_id,
    p.name as project_name,
    c.id as client_id,
    c.name as client_name,
    cast(sum(
        case 
            when te.end_time is null then 
                (julianday('now', 'utc') - julianday(te.start_time)) * 24 * 60 * 60
            else 
                (julianday(te.end_time) - julianday(te.start_time)) * 24 * 60 * 60
        end
    ) as integer) as total_seconds
from time_entry te
join client c on te.client_id = c.id
left join project p on te.project_id = p.id
where date(te.start_time) >= date('now', 'weekday 1', '-6 days')
    and date(te.start_time) <= date('now')
group by p.id, p.name, c.id, c.name
order by c.name, p.name
`

type GetWeekSummaryByProjectRow struct {
	ProjectID    sql.NullInt64
	ProjectName  sql.NullString
	ClientID     int64
	ClientName   string
	TotalSeconds int64
}

func (q *Queries) GetWeekSummaryByProject(ctx context.Context) ([]GetWeekSummaryByProjectRow, error) {
	rows, err := q.db.QueryContext(ctx, getWeekSummaryByProject)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetWeekSummaryByProjectRow
	for rows.Next() {
		var i GetWeekSummaryByProjectRow
		if err := rows.Scan(
			&i.ProjectID,
			&i.ProjectName,
			&i.ClientID,
			&i.ClientName,
			&i.TotalSeconds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listAllClients = `-- name: ListAllClients :many
select id, name, email, billable_rate, created_at from client
order by name
`

func (q *Queries) ListAllClients(ctx context.Context) ([]Client, error) {
	rows, err := q.db.QueryContext(ctx, listAllClients)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Client
	for rows.Next() {
		var i Client
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Email,
			&i.BillableRate,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listAllProjects = `-- name: ListAllProjects :many
select p.id, p.name, p.client_id, p.billable_rate, p.created_at, c.name as client_name from project p
join client c on p.client_id = c.id
order by c.name, p.name
`

type ListAllProjectsRow struct {
	ID           int64
	Name         string
	ClientID     int64
	BillableRate sql.NullInt64
	CreatedAt    sql.NullTime
	ClientName   string
}

func (q *Queries) ListAllProjects(ctx context.Context) ([]ListAllProjectsRow, error) {
	rows, err := q.db.QueryContext(ctx, listAllProjects)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListAllProjectsRow
	for rows.Next() {
		var i ListAllProjectsRow
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.ClientID,
			&i.BillableRate,
			&i.CreatedAt,
			&i.ClientName,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const stopTimeEntry = `-- name: StopTimeEntry :one
update time_entry 
set end_time = datetime('now', 'utc')
where id = (
	select id
	from time_entry
	where end_time is null
	order by start_time desc
	limit 1
)
returning id, start_time, end_time, description, client_id, project_id, billable_rate
`

func (q *Queries) StopTimeEntry(ctx context.Context) (TimeEntry, error) {
	row := q.db.QueryRowContext(ctx, stopTimeEntry)
	var i TimeEntry
	err := row.Scan(
		&i.ID,
		&i.StartTime,
		&i.EndTime,
		&i.Description,
		&i.ClientID,
		&i.ProjectID,
		&i.BillableRate,
	)
	return i, err
}