aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dbx/mintages.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/dbx/mintages.go')
-rw-r--r--src/dbx/mintages.go186
1 files changed, 108 insertions, 78 deletions
diff --git a/src/dbx/mintages.go b/src/dbx/mintages.go
index d78e59c..2223eff 100644
--- a/src/dbx/mintages.go
+++ b/src/dbx/mintages.go
@@ -1,16 +1,22 @@
package dbx
import (
+ "context"
"database/sql"
"slices"
)
-type MintageData struct {
- Standard []MSRow
- Commemorative []MCRow
+type CountryMintageData struct {
+ Standard []MSCountryRow
+ Commemorative []MCommemorative
}
-type msRowInternal struct {
+type YearMintageData struct {
+ Standard []MSYearRow
+ Commemorative []MCommemorative
+}
+
+type msRow struct {
Country string
Type MintageType
Year int
@@ -20,7 +26,21 @@ type msRowInternal struct {
Reference sql.Null[string]
}
-type mcRowInternal struct {
+type MSCountryRow struct {
+ Year int
+ Mintmark sql.Null[string]
+ Mintages [ndenoms]sql.Null[int]
+ References [ndenoms]sql.Null[string]
+}
+
+type MSYearRow struct {
+ Country string
+ Mintmark sql.Null[string]
+ Mintages [ndenoms]sql.Null[int]
+ References [ndenoms]sql.Null[string]
+}
+
+type MCommemorative struct {
Country string
Type MintageType
Year int
@@ -31,22 +51,6 @@ type mcRowInternal struct {
Reference sql.Null[string]
}
-type MSRow struct {
- Year int
- Mintmark string
- Mintages [ndenoms]int
- References []string
-}
-
-type MCRow struct {
- Year int
- Name string
- Number int
- Mintmark string
- Mintage int
- Reference string
-}
-
type MintageType int
/* DO NOT REORDER! */
@@ -56,12 +60,6 @@ const (
TypeProof
)
-/* DO NOT REORDER! */
-const (
- MintageUnknown = -iota - 1
- MintageInvalid
-)
-
const ndenoms = 8
func NewMintageType(s string) MintageType {
@@ -73,64 +71,57 @@ func NewMintageType(s string) MintageType {
case "proof":
return TypeProof
}
- /* TODO: Handle this */
- panic("TODO")
+ /* We can get here if the user sends a request manually, so just
+ fallback to this */
+ return TypeCirc
}
-func GetMintages(country string, typ MintageType) (MintageData, error) {
+func GetMintagesByYear(year int, typ MintageType) (YearMintageData, error) {
var (
- zero MintageData
- xs []MSRow
- ys []MCRow
+ zero YearMintageData
+ xs []MSYearRow
+ ys []MCommemorative
)
- rs, err := db.Queryx(`
+ rs, err := db.QueryxContext(context.TODO(), `
SELECT * FROM mintages_s
- WHERE country = ? AND type = ?
- ORDER BY year, mintmark, denomination
- `, country, typ)
+ WHERE year = ? AND type = ?
+ ORDER BY country, mintmark, denomination
+ `, year, typ)
if err != nil {
return zero, err
}
for rs.Next() {
- var x msRowInternal
+ var x msRow
if err = rs.StructScan(&x); err != nil {
return zero, err
}
loop:
- msr := MSRow{
- Year: x.Year,
- Mintmark: sqlOr(x.Mintmark, ""),
- References: make([]string, 0, ndenoms),
- }
- for i := range msr.Mintages {
- msr.Mintages[i] = MintageUnknown
- }
- msr.Mintages[denomToIdx(x.Denomination)] =
- sqlOr(x.Mintage, MintageUnknown)
- if x.Reference.Valid {
- msr.References = append(msr.References, x.Reference.V)
+ msr := MSYearRow{
+ Country: x.Country,
+ Mintmark: x.Mintmark,
}
+ i := denomToIdx(x.Denomination)
+ msr.Mintages[i] = x.Mintage
+ msr.References[i] = x.Reference
for rs.Next() {
- var y msRowInternal
+ var y msRow
if err = rs.StructScan(&y); err != nil {
return zero, err
}
- if x.Year != y.Year || x.Mintmark != y.Mintmark {
+ if x.Country != y.Country || x.Mintmark != y.Mintmark {
x = y
xs = append(xs, msr)
goto loop
}
- msr.Mintages[denomToIdx(y.Denomination)] =
- sqlOr(y.Mintage, MintageUnknown)
- if y.Reference.Valid {
- msr.References = append(msr.References, y.Reference.V)
- }
+ i = denomToIdx(y.Denomination)
+ msr.Mintages[i] = y.Mintage
+ msr.References[i] = y.Reference
}
xs = append(xs, msr)
@@ -140,38 +131,77 @@ func GetMintages(country string, typ MintageType) (MintageData, error) {
return zero, err
}
- rs, err = db.Queryx(`
- SELECT * FROM mintages_c
- WHERE country = ? AND type = ?
- ORDER BY year, mintmark, number
+ db.SelectContext(context.TODO(), &ys, `
+ SELECT * FROM mintages_c
+ WHERE year = ? and type = ?
+ ORDER BY country, mintmark, number
+ `, year, typ)
+
+ return YearMintageData{xs, ys}, nil
+}
+
+func GetMintagesByCountry(country string, typ MintageType) (CountryMintageData, error) {
+ var (
+ zero CountryMintageData
+ xs []MSCountryRow
+ ys []MCommemorative
+ )
+
+ rs, err := db.QueryxContext(context.TODO(), `
+ SELECT * FROM mintages_s
+ WHERE country = ? AND type = ?
+ ORDER BY year, mintmark, denomination
`, country, typ)
if err != nil {
return zero, err
}
for rs.Next() {
- var y mcRowInternal
- if err = rs.StructScan(&y); err != nil {
+ var x msRow
+ if err = rs.StructScan(&x); err != nil {
return zero, err
}
- ys = append(ys, MCRow{
- Year: y.Year,
- Name: y.Name,
- Number: y.Number,
- Mintmark: sqlOr(y.Mintmark, ""),
- Mintage: sqlOr(y.Mintage, MintageUnknown),
- Reference: sqlOr(y.Reference, ""),
- })
- }
- return MintageData{xs, ys}, rs.Err()
-}
+ loop:
+ msr := MSCountryRow{
+ Year: x.Year,
+ Mintmark: x.Mintmark,
+ }
+ i := denomToIdx(x.Denomination)
+ msr.Mintages[i] = x.Mintage
+ msr.References[i] = x.Reference
+
+ for rs.Next() {
+ var y msRow
+ if err = rs.StructScan(&y); err != nil {
+ return zero, err
+ }
+
+ if x.Year != y.Year || x.Mintmark != y.Mintmark {
+ x = y
+ xs = append(xs, msr)
+ goto loop
+ }
+
+ i = denomToIdx(y.Denomination)
+ msr.Mintages[i] = y.Mintage
+ msr.References[i] = y.Reference
+ }
-func sqlOr[T any](v sql.Null[T], dflt T) T {
- if v.Valid {
- return v.V
+ xs = append(xs, msr)
}
- return dflt
+
+ if err = rs.Err(); err != nil {
+ return zero, err
+ }
+
+ db.SelectContext(context.TODO(), &ys, `
+ SELECT * FROM mintages_c
+ WHERE country = ? and type = ?
+ ORDER BY year, mintmark, number
+ `, country, typ)
+
+ return CountryMintageData{xs, ys}, rs.Err()
}
func denomToIdx(d float64) int {