package queries import ( "backend/typesdefs" "fmt" "github.com/georgysavva/scany/v2/pgxscan" "github.com/jackc/pgx/v5" ) func ImagesGetAll() ([]typesdefs.MiogestImagesRef_Table, error) { var images []typesdefs.MiogestImagesRef_Table err := pgxscan.Select(Db.Ctx, Db.Dbpool, &images, "SELECT * FROM public.miogest_images_ref") if err != nil && err != pgx.ErrNoRows { return nil, fmt.Errorf("failed to get all images: %w", err) } return images, nil } func ImagesBatchInsert(annunci *typesdefs.AnnunciXML) error { batch := &pgx.Batch{} for _, annuncio := range annunci.Annuncio { batch.Queue(` INSERT INTO public.miogest_images_ref (codice, foto) VALUES ($1, $2) ON CONFLICT (codice) DO UPDATE SET foto = $2 `, &annuncio.Codice, &annuncio.Foto) } if err := Db.Dbpool.SendBatch(Db.Ctx, batch).Close(); err != nil { return fmt.Errorf("failed to insert/update images batch: %w", err) } return nil } func ImagesClear() error { _, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.miogest_images_ref") if err != nil { return fmt.Errorf("failed to clear images: %w", err) } return nil }