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"
|
2024-06-10 17:25:01 -04:00
|
|
|
"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
|
|
|
|
|
2024-06-17 17:25:53 -04:00
|
|
|
//go:embed sql/views.sql
|
|
|
|
var sqlViews string
|
|
|
|
|
2024-06-06 01:40:56 -04:00
|
|
|
type PostgresClient struct {
|
2024-06-10 17:25:01 -04:00
|
|
|
//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
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:25:53 -04:00
|
|
|
func (db *PostgresClient) CreateOrReplaceViews() error {
|
|
|
|
_, err := db.Pool.Exec(db.Ctx, sqlViews)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-29 18:19:31 -04:00
|
|
|
// InsertMembres inserts a slice of Membre into a database, returning the amount inserted and any error encountered
|
2024-06-17 17:25:53 -04:00
|
|
|
func (d *PostgresClient) InsertMembres(membres ...Membre) (inserted int64, err error) {
|
2024-06-10 17:25:01 -04:00
|
|
|
select {
|
|
|
|
case <-d.Ctx.Done():
|
|
|
|
return inserted, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
|
|
|
|
default:
|
|
|
|
tx, err := d.Pool.Begin(d.Ctx)
|
2023-05-29 18:19:31 -04:00
|
|
|
if err != nil {
|
2024-06-10 17:25:01 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-10 17:25:01 -04:00
|
|
|
if err = tx.Commit(d.Ctx); err != nil {
|
2023-05-29 18:19:31 -04:00
|
|
|
return 0, err
|
|
|
|
}
|
2023-03-06 14:01:18 -05:00
|
|
|
|
2024-06-10 17:25:01 -04:00
|
|
|
return inserted, err
|
2023-03-06 14:01:18 -05:00
|
|
|
}
|
2023-05-29 18:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-06-17 14:07:49 -04:00
|
|
|
func (d *PostgresClient) InsertProgrammes(programmes ...Programme) (inserted int64, err error) {
|
2024-06-10 17:25:01 -04:00
|
|
|
select {
|
|
|
|
case <-d.Ctx.Done():
|
|
|
|
return inserted, fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
|
|
|
|
default:
|
|
|
|
tx, err := d.Pool.Begin(d.Ctx)
|
2023-05-29 18:19:31 -04:00
|
|
|
if err != nil {
|
2024-06-10 17:25:01 -04:00
|
|
|
return inserted, err
|
2023-05-29 18:19:31 -04:00
|
|
|
}
|
2024-06-10 17:25:01 -04:00
|
|
|
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
|
2024-06-17 17:25:53 -04:00
|
|
|
(id, name)
|
2024-06-10 17:25:01 -04:00
|
|
|
VALUES ($1, $2) ON CONFLICT DO NOTHING;`,
|
|
|
|
programme.ID,
|
2024-06-17 17:25:53 -04:00
|
|
|
programme.Name)
|
2024-06-10 17:25:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
inserted += result.RowsAffected()
|
2023-05-29 18:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-06-10 17:25:01 -04:00
|
|
|
if err := tx.Commit(d.Ctx); err != nil {
|
|
|
|
return inserted, err
|
|
|
|
}
|
2023-05-29 18:19:31 -04:00
|
|
|
|
2024-06-10 17:25:01 -04:00
|
|
|
return inserted, err
|
2023-05-29 18:19:31 -04:00
|
|
|
}
|
2023-03-06 14:01:18 -05:00
|
|
|
}
|
|
|
|
|
2024-06-18 19:44:20 -04:00
|
|
|
func (d *PostgresClient) GetMembre(membreID string) (membre Membre, err error) {
|
|
|
|
select {
|
|
|
|
case <-d.Ctx.Done():
|
|
|
|
err = fmt.Errorf("PostgresClient.Ctx closed: %s", d.Ctx.Err())
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if err = d.Pool.QueryRow(d.Ctx, `
|
|
|
|
SELECT
|
|
|
|
"membres".id,
|
|
|
|
"membres".last_name,
|
|
|
|
"membres".first_name,
|
|
|
|
"membres".prefered_name,
|
|
|
|
"membres".programme_id
|
|
|
|
FROM
|
|
|
|
"membres"
|
|
|
|
WHERE
|
|
|
|
"membres".id = $1
|
|
|
|
LIMIT
|
|
|
|
1;
|
|
|
|
`, membreID).Scan(
|
|
|
|
&membre.ID,
|
|
|
|
&membre.LastName,
|
|
|
|
&membre.FirstName,
|
|
|
|
&membre.PreferedName,
|
|
|
|
&membre.ProgrammeID,
|
|
|
|
); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-05-29 18:19:31 -04:00
|
|
|
|
2024-06-18 19:44:20 -04:00
|
|
|
if membre.ID == "" {
|
|
|
|
return membre, fmt.Errorf("Aucun membre trouvé avec numéro '%s'", membre.ID)
|
2023-05-29 18:19:31 -04:00
|
|
|
}
|
|
|
|
|
2024-06-18 19:44:20 -04:00
|
|
|
return membre, nil
|
2023-05-29 18:19:31 -04:00
|
|
|
}
|
2023-03-06 14:01:18 -05:00
|
|
|
}
|
|
|
|
|
2024-06-10 17:25:01 -04: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)
|
2023-05-29 18:19:31 -04:00
|
|
|
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
|
|
|
}
|
2024-06-10 17:25:01 -04:00
|
|
|
*/
|
2023-09-19 19:09:51 -04:00
|
|
|
|
2024-06-18 21:21:30 -04:00
|
|
|
func (d *PostgresClient) GetMembres(limit int) (membres []Membre, err error) {
|
2024-06-10 17:25:01 -04:00
|
|
|
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
|
2024-06-18 21:21:30 -04:00
|
|
|
"membres".id,
|
|
|
|
"membres".last_name,
|
|
|
|
"membres".first_name,
|
|
|
|
"membres".prefered_name,
|
|
|
|
"membres".programme_id
|
2024-06-10 17:25:01 -04:00
|
|
|
FROM
|
2024-06-18 21:21:30 -04:00
|
|
|
"membres"
|
2024-06-10 17:25:01 -04:00
|
|
|
ORDER BY
|
2024-06-18 21:21:30 -04:00
|
|
|
"membres".id
|
|
|
|
LIMIT
|
|
|
|
$1;
|
|
|
|
`, limit)
|
2024-06-10 17:25:01 -04:00
|
|
|
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
|
|
|
|
}
|
2023-09-19 19:09:51 -04:00
|
|
|
}
|
2024-06-19 00:04:19 -04:00
|
|
|
|
|
|
|
func (d *PostgresClient) GetProgrammes(limit int) (programmes []Programme, 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
|
|
|
|
"programmes".id,
|
|
|
|
"programmes".name
|
|
|
|
FROM
|
|
|
|
"programmes"
|
|
|
|
ORDER BY
|
|
|
|
"programmes".id
|
|
|
|
LIMIT
|
|
|
|
$1;
|
|
|
|
`, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var programme Programme
|
|
|
|
|
|
|
|
if err = rows.Scan(
|
|
|
|
&programme.ID,
|
|
|
|
&programme.Name,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
programmes = append(programmes, programme)
|
|
|
|
}
|
|
|
|
if rows.Err() != nil {
|
|
|
|
return programmes, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return programmes, nil
|
|
|
|
}
|
|
|
|
}
|