bottin/db.go

191 lines
3.7 KiB
Go
Raw Normal View History

2024-06-06 01:40:56 -04:00
package main
2023-03-06 14:01:18 -05:00
import (
2024-06-06 01:40:56 -04:00
"context"
_ "embed"
"fmt"
2023-03-06 14:01:18 -05:00
2024-06-06 01:40:56 -04:00
"github.com/jackc/pgx/v5/pgxpool"
2023-03-06 14:01:18 -05:00
)
2024-06-06 01:40:56 -04:00
//go:embed sql/schema.sql
var sqlSchema string
type PostgresClient struct {
//TODO move context out of client
2024-06-06 01:40:56 -04:00
Ctx context.Context
Pool *pgxpool.Pool
}
func (db *PostgresClient) CreateOrReplaceSchema() error {
_, err := db.Pool.Exec(db.Ctx, sqlSchema)
return err
}
// InsertMembres inserts a slice of Membre into a database, returning the amount inserted and any error encountered
func (d *PostgresClient) InsertMembres(membres []Membre) (inserted int64, err error) {
select {
case <-d.Ctx.Done():
return inserted, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
default:
tx, err := d.Pool.Begin(d.Ctx)
if err != nil {
return inserted, err
}
defer tx.Rollback(d.Ctx)
for i, membre := range membres {
if membre.ID == "" {
return inserted, fmt.Errorf("insertion ligne %d: membre requiert numéro étudiant valide", i)
}
result, err := tx.Exec(d.Ctx, `
INSERT INTO membres
(id, last_name, first_name, prefered_name, programme_id)
VALUES
($1, $2, $3, $4, $5)
ON CONFLICT (id) DO NOTHING;`,
membre.ID,
membre.LastName,
membre.FirstName,
membre.PreferedName,
membre.ProgrammeID,
)
if err != nil {
return 0, err
}
inserted += result.RowsAffected()
2023-03-06 14:01:18 -05:00
}
if err = tx.Commit(d.Ctx); err != nil {
return 0, err
}
2023-03-06 14:01:18 -05:00
return inserted, err
2023-03-06 14:01:18 -05:00
}
}
2024-06-17 14:07:49 -04:00
func (d *PostgresClient) InsertProgrammes(programmes ...Programme) (inserted int64, err error) {
select {
case <-d.Ctx.Done():
return inserted, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
default:
tx, err := d.Pool.Begin(d.Ctx)
if err != nil {
return inserted, err
}
defer tx.Rollback(d.Ctx)
for _, programme := range programmes {
if programme.ID == "" {
return 0, fmt.Errorf("Cannot insert programme with no programme_id")
}
result, err := tx.Exec(d.Ctx, `
INSERT INTO programmes
(id, titre)
VALUES ($1, $2) ON CONFLICT DO NOTHING;`,
programme.ID,
programme.Titre)
if err != nil {
return 0, err
}
inserted += result.RowsAffected()
}
if err := tx.Commit(d.Ctx); err != nil {
return inserted, err
}
return inserted, err
}
2023-03-06 14:01:18 -05:00
}
/*
func (d *PostgresClient) GetMembre(membreID string) (Membre, error) {
2024-06-06 01:40:56 -04:00
var membre Membre
rows, err := d.Pool.Queryx("SELECT * FROM membres WHERE id = $1 LIMIT 1;", membreID)
if err != nil {
return membre, err
}
for rows.Next() {
err := rows.StructScan(&membre)
if err != nil {
return membre, err
}
}
if membre.ID == "" {
return membre, fmt.Errorf("No membre by that id was found")
}
return membre, nil
2023-03-06 14:01:18 -05:00
}
*/
2023-03-06 14:01:18 -05:00
/*
func (d *PostgresClient) UpdateMembreName(membreID, newName string) (int64, error) {
result, err := d.Pool.Exec("UPDATE membres SET prefered_name = $1 WHERE id = $2;", newName, membreID)
if err != nil {
return 0, err
}
rows, err := result.RowsAffected()
if err != nil {
return rows, err
}
return rows, nil
2023-03-06 14:01:18 -05:00
}
*/
func (d *PostgresClient) GetMembres() (membres []Membre, err error) {
select {
case <-d.Ctx.Done():
return nil, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
default:
rows, err := d.Pool.Query(d.Ctx, `
SELECT
membres.id,
membres.last_name,
membres.first_name,
membres.prefered_name,
membres.programme_id
FROM
membres
LIMIT
10000
ORDER BY
membres.id;`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var membre Membre
if err = rows.Scan(
&membre.ID,
&membre.LastName,
&membre.FirstName,
&membre.PreferedName,
&membre.ProgrammeID,
); err != nil {
return nil, err
}
membres = append(membres, membre)
}
if rows.Err() != nil {
return membres, rows.Err()
}
return membres, nil
}
}