diff options
Diffstat (limited to 'src/dbx')
-rw-r--r-- | src/dbx/.gitignore | 1 | ||||
-rw-r--r-- | src/dbx/mintages.go | 140 | ||||
-rw-r--r-- | src/dbx/sql/last.sql | 141 |
3 files changed, 267 insertions, 15 deletions
diff --git a/src/dbx/.gitignore b/src/dbx/.gitignore deleted file mode 100644 index d14a707..0000000 --- a/src/dbx/.gitignore +++ /dev/null @@ -1 +0,0 @@ -sql/last.sql
\ No newline at end of file diff --git a/src/dbx/mintages.go b/src/dbx/mintages.go index d78e59c..9b98bd6 100644 --- a/src/dbx/mintages.go +++ b/src/dbx/mintages.go @@ -5,9 +5,14 @@ import ( "slices" ) -type MintageData struct { - Standard []MSRow - Commemorative []MCRow +type CountryMintageData struct { + Standard []MSCountryRow + Commemorative []MCCountryRow +} + +type YearMintageData struct { + Standard []MSYearRow + Commemorative []MCYearRow } type msRowInternal struct { @@ -31,14 +36,14 @@ type mcRowInternal struct { Reference sql.Null[string] } -type MSRow struct { +type MSCountryRow struct { Year int Mintmark string Mintages [ndenoms]int References []string } -type MCRow struct { +type MCCountryRow struct { Year int Name string Number int @@ -47,6 +52,22 @@ type MCRow struct { Reference string } +type MSYearRow struct { + Country string + Mintmark string + Mintages [ndenoms]int + References []string +} + +type MCYearRow struct { + Country string + Name string + Number int + Mintmark string + Mintage int + Reference string +} + type MintageType int /* DO NOT REORDER! */ @@ -73,15 +94,106 @@ 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 GetMintagesByYear(year int, typ MintageType) (YearMintageData, error) { + var ( + zero YearMintageData + xs []MSYearRow + ys []MCYearRow + ) + + rs, err := db.Queryx(` + SELECT * FROM mintages_s + WHERE year = ? AND type = ? + ORDER BY country, mintmark, denomination + `, year, typ) + if err != nil { + return zero, err + } + + for rs.Next() { + var x msRowInternal + if err = rs.StructScan(&x); err != nil { + return zero, err + } + + loop: + msr := MSYearRow{ + Country: x.Country, + 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) + } + + for rs.Next() { + var y msRowInternal + if err = rs.StructScan(&y); err != nil { + return zero, err + } + + 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) + } + } + + xs = append(xs, msr) + } + + if err = rs.Err(); err != nil { + return zero, err + } + + rs, err = db.Queryx(` + SELECT * FROM mintages_c + WHERE year = ? AND type = ? + ORDER BY country, mintmark, number + `, year, typ) + if err != nil { + return zero, err + } + + for rs.Next() { + var y mcRowInternal + if err = rs.StructScan(&y); err != nil { + return zero, err + } + ys = append(ys, MCYearRow{ + Country: y.Country, + Name: y.Name, + Number: y.Number, + Mintmark: sqlOr(y.Mintmark, ""), + Mintage: sqlOr(y.Mintage, MintageUnknown), + Reference: sqlOr(y.Reference, ""), + }) + } + + return YearMintageData{xs, ys}, nil } -func GetMintages(country string, typ MintageType) (MintageData, error) { +func GetMintagesByCountry(country string, typ MintageType) (CountryMintageData, error) { var ( - zero MintageData - xs []MSRow - ys []MCRow + zero CountryMintageData + xs []MSCountryRow + ys []MCCountryRow ) rs, err := db.Queryx(` @@ -100,7 +212,7 @@ func GetMintages(country string, typ MintageType) (MintageData, error) { } loop: - msr := MSRow{ + msr := MSCountryRow{ Year: x.Year, Mintmark: sqlOr(x.Mintmark, ""), References: make([]string, 0, ndenoms), @@ -154,7 +266,7 @@ func GetMintages(country string, typ MintageType) (MintageData, error) { if err = rs.StructScan(&y); err != nil { return zero, err } - ys = append(ys, MCRow{ + ys = append(ys, MCCountryRow{ Year: y.Year, Name: y.Name, Number: y.Number, @@ -164,7 +276,7 @@ func GetMintages(country string, typ MintageType) (MintageData, error) { }) } - return MintageData{xs, ys}, rs.Err() + return CountryMintageData{xs, ys}, rs.Err() } func sqlOr[T any](v sql.Null[T], dflt T) T { diff --git a/src/dbx/sql/last.sql b/src/dbx/sql/last.sql new file mode 100644 index 0000000..fa2adbb --- /dev/null +++ b/src/dbx/sql/last.sql @@ -0,0 +1,141 @@ +DELETE FROM mintages_s; +DELETE FROM mintages_c; + +INSERT INTO mintages_s ( + country, + type, + year, + denomination, + mintmark, + mintage, + reference +) VALUES + ('at', 0, 2017, 0.01, NULL, 37700000, NULL), + ('at', 0, 2017, 0.02, NULL, 57200000, NULL), + ('at', 0, 2017, 0.05, NULL, 35200000, NULL), + ('at', 0, 2017, 0.10, NULL, 39500000, NULL), + ('at', 0, 2017, 0.20, NULL, 30000000, NULL), + ('at', 0, 2017, 0.50, NULL, 15000000, NULL), + ('at', 0, 2017, 1.00, NULL, 8000000, NULL), + ('at', 0, 2017, 2.00, NULL, 17700000, NULL), + ('de', 0, 2017, 0.01, 'A', 81600000, NULL), + ('de', 0, 2017, 0.02, 'A', 72200000, NULL), + ('de', 0, 2017, 0.05, 'A', 30000000, NULL), + ('de', 0, 2017, 0.10, 'A', 25200000, NULL), + ('de', 0, 2017, 0.20, 'A', 21600000, NULL), + ('de', 0, 2017, 0.50, 'A', 0, NULL), + ('de', 0, 2017, 1.00, 'A', 0, NULL), + ('de', 0, 2017, 2.00, 'A', 18120000, NULL), + ('de', 0, 2017, 0.01, 'D', 85680000, NULL), + ('de', 0, 2017, 0.02, 'D', 75810000, NULL), + ('de', 0, 2017, 0.05, 'D', 31500000, NULL), + ('de', 0, 2017, 0.10, 'D', 26460000, NULL), + ('de', 0, 2017, 0.20, 'D', 22680000, NULL), + ('de', 0, 2017, 0.50, 'D', 0, NULL), + ('de', 0, 2017, 1.00, 'D', 0, NULL), + ('de', 0, 2017, 2.00, 'D', 19110000, NULL), + ('ad', 0, 2014, 0.01, NULL, 60000, NULL), + ('ad', 0, 2014, 0.02, NULL, 60000, NULL), + ('ad', 0, 2014, 0.05, NULL, 860000, NULL), + ('ad', 0, 2014, 0.10, NULL, 860000, NULL), + ('ad', 0, 2014, 0.20, NULL, 860000, NULL), + ('ad', 0, 2014, 0.50, NULL, 340000, NULL), + ('ad', 0, 2014, 1.00, NULL, 511843, NULL), + ('ad', 0, 2014, 2.00, NULL, 360000, NULL), + ('ad', 0, 2015, 0.01, NULL, 0, NULL), + ('ad', 0, 2015, 0.02, NULL, 0, NULL), + ('ad', 0, 2015, 0.05, NULL, 0, NULL), + ('ad', 0, 2015, 0.10, NULL, 0, NULL), + ('ad', 0, 2015, 0.20, NULL, 0, NULL), + ('ad', 0, 2015, 0.50, NULL, 0, NULL), + ('ad', 0, 2015, 1.00, NULL, 0, NULL), + ('ad', 0, 2015, 2.00, NULL, 1072400, NULL), + ('ad', 0, 2016, 0.01, NULL, 0, NULL), + ('ad', 0, 2016, 0.02, NULL, 0, NULL), + ('ad', 0, 2016, 0.05, NULL, 0, NULL), + ('ad', 0, 2016, 0.10, NULL, 0, NULL), + ('ad', 0, 2016, 0.20, NULL, 0, NULL), + ('ad', 0, 2016, 0.50, NULL, 0, NULL), + ('ad', 0, 2016, 1.00, NULL, 2339200, NULL), + ('ad', 0, 2016, 2.00, NULL, 0, NULL), + ('ad', 0, 2017, 0.01, NULL, 2582395, NULL), + ('ad', 0, 2017, 0.02, NULL, 1515000, NULL), + ('ad', 0, 2017, 0.05, NULL, 2191421, NULL), + ('ad', 0, 2017, 0.10, NULL, 1103000, NULL), + ('ad', 0, 2017, 0.20, NULL, 1213000, NULL), + ('ad', 0, 2017, 0.50, NULL, 968800, NULL), + ('ad', 0, 2017, 1.00, NULL, 17000, NULL), + ('ad', 0, 2017, 2.00, NULL, 794588, NULL), + ('ad', 0, 2018, 0.01, NULL, 2430000, NULL), + ('ad', 0, 2018, 0.02, NULL, 2550000, NULL), + ('ad', 0, 2018, 0.05, NULL, 1800000, NULL), + ('ad', 0, 2018, 0.10, NULL, 980000, NULL), + ('ad', 0, 2018, 0.20, NULL, 1014000, NULL), + ('ad', 0, 2018, 0.50, NULL, 890000, NULL), + ('ad', 0, 2018, 1.00, NULL, 0, NULL), + ('ad', 0, 2018, 2.00, NULL, 868000, NULL), + ('ad', 0, 2019, 0.01, NULL, 2447000, NULL), + ('ad', 0, 2019, 0.02, NULL, 1727000, NULL), + ('ad', 0, 2019, 0.05, NULL, 2100000, NULL), + ('ad', 0, 2019, 0.10, NULL, 1610000, NULL), + ('ad', 0, 2019, 0.20, NULL, 1570000, NULL), + ('ad', 0, 2019, 0.50, NULL, 930000, NULL), + ('ad', 0, 2019, 1.00, NULL, 0, NULL), + ('ad', 0, 2019, 2.00, NULL, 1058310, NULL), + ('ad', 0, 2020, 0.01, NULL, 0, NULL), + ('ad', 0, 2020, 0.02, NULL, 0, NULL), + ('ad', 0, 2020, 0.05, NULL, 0, NULL), + ('ad', 0, 2020, 0.10, NULL, 860000, NULL), + ('ad', 0, 2020, 0.20, NULL, 175000, NULL), + ('ad', 0, 2020, 0.50, NULL, 740000, NULL), + ('ad', 0, 2020, 1.00, NULL, 0, NULL), + ('ad', 0, 2020, 2.00, NULL, 1500000, NULL), + ('ad', 0, 2021, 0.01, NULL, 200000, NULL), + ('ad', 0, 2021, 0.02, NULL, 700000, NULL), + ('ad', 0, 2021, 0.05, NULL, 0, NULL), + ('ad', 0, 2021, 0.10, NULL, 1400000, NULL), + ('ad', 0, 2021, 0.20, NULL, 1420000, NULL), + ('ad', 0, 2021, 0.50, NULL, 600000, NULL), + ('ad', 0, 2021, 1.00, NULL, 50000, NULL), + ('ad', 0, 2021, 2.00, NULL, 1474500, NULL), + ('ad', 0, 2022, 0.01, NULL, 700000, NULL), + ('ad', 0, 2022, 0.02, NULL, 450000, NULL), + ('ad', 0, 2022, 0.05, NULL, 400000, NULL), + ('ad', 0, 2022, 0.10, NULL, 700000, NULL), + ('ad', 0, 2022, 0.20, NULL, 700000, NULL), + ('ad', 0, 2022, 0.50, NULL, 380000, NULL), + ('ad', 0, 2022, 1.00, NULL, 0, NULL), + ('ad', 0, 2022, 2.00, NULL, 1708000, NULL), + ('ad', 0, 2023, 0.01, NULL, 0, NULL), + ('ad', 0, 2023, 0.02, NULL, 0, NULL), + ('ad', 0, 2023, 0.05, NULL, 0, NULL), + ('ad', 0, 2023, 0.10, NULL, 0, NULL), + ('ad', 0, 2023, 0.20, NULL, 0, NULL), + ('ad', 0, 2023, 0.50, NULL, 0, NULL), + ('ad', 0, 2023, 1.00, NULL, 0, NULL), + ('ad', 0, 2023, 2.00, NULL, 2075250, NULL), + ('ad', 0, 2024, 0.01, NULL, 0, NULL), + ('ad', 0, 2024, 0.02, NULL, 900300, NULL), + ('ad', 0, 2024, 0.05, NULL, 1950000, NULL), + ('ad', 0, 2024, 0.10, NULL, 1000000, NULL), + ('ad', 0, 2024, 0.20, NULL, 700000, NULL), + ('ad', 0, 2024, 0.50, NULL, 500000, NULL), + ('ad', 0, 2024, 1.00, NULL, 1050000, NULL), + ('ad', 0, 2024, 2.00, NULL, 1601200, NULL); + +INSERT INTO mintages_c ( + country, + type, + year, + name, + number, + mintmark, + mintage, + reference +) VALUES + ('sk', 0, 2014, 'Slovak Republic to the EU', 1, NULL, 1000000, NULL), + ('sk', 0, 2015, 'Ľudovít Štúr', 1, NULL, 1000000, NULL), + ('sk', 0, 2015, 'EU Flag', 2, NULL, 1000000, NULL), + ('fr', 0, 2015, 'Peace and security', 1, NULL, 4000000, NULL), + ('fr', 0, 2015, 'Fête de la Fédération', 2, NULL, 4000000, NULL), + ('fr', 0, 2015, 'EU Flag', 3, NULL, 4000000, NULL);
\ No newline at end of file |