Clear the economy database when creating it.

This commit is contained in:
Avi Weinstock 2021-05-28 16:14:31 -04:00
parent 3684cf0454
commit 5380fc4eac

View File

@ -70,19 +70,21 @@ fn economy_sqlite(world: &World, index: &Index) -> Result<(), Box<dyn Error>> {
let conn = Connection::open("economy.sqlite")?; let conn = Connection::open("economy.sqlite")?;
#[rustfmt::skip] #[rustfmt::skip]
conn.execute_batch(" conn.execute_batch("
CREATE TABLE IF NOT EXISTS site ( DROP TABLE IF EXISTS site;
CREATE TABLE site (
xcoord INTEGER NOT NULL, xcoord INTEGER NOT NULL,
ycoord INTEGER NUT NULL, ycoord INTEGER NUT NULL,
name TEXT NOT NULL name TEXT NOT NULL
); );
CREATE UNIQUE INDEX IF NOT EXISTS site_position ON site(xcoord, ycoord); CREATE UNIQUE INDEX site_position ON site(xcoord, ycoord);
CREATE TABLE IF NOT EXISTS site_price ( DROP TABLE IF EXISTS site_price;
CREATE TABLE site_price (
xcoord INTEGER NOT NULL, xcoord INTEGER NOT NULL,
ycoord INTEGER NOT NULL, ycoord INTEGER NOT NULL,
good TEXT NOT NULL, good TEXT NOT NULL,
price REAL NOT NULL price REAL NOT NULL
); );
CREATE UNIQUE INDEX IF NOT EXISTS site_good on site_price(xcoord, ycoord, good); CREATE UNIQUE INDEX site_good on site_price(xcoord, ycoord, good);
")?; ")?;
let mut all_goods = Vec::new(); let mut all_goods = Vec::new();
for good in Good::iter() { for good in Good::iter() {
@ -115,10 +117,11 @@ fn economy_sqlite(world: &World, index: &Index) -> Result<(), Box<dyn Error>> {
#[rustfmt::skip] #[rustfmt::skip]
let create_view = format!(" let create_view = format!("
CREATE VIEW IF NOT EXISTS site_price_tr (xcoord, ycoord {}) DROP VIEW IF EXISTS site_price_tr;
CREATE VIEW site_price_tr (xcoord, ycoord {})
AS SELECT xcoord, ycoord {} AS SELECT xcoord, ycoord {}
FROM site NATURAL JOIN site_price FROM site NATURAL JOIN site_price
GROUP BY xcoord, ycoord GROUP BY xcoord, ycoord;
", good_columns, good_exprs); ", good_columns, good_exprs);
conn.execute_batch(&create_view)?; conn.execute_batch(&create_view)?;
let mut insert_price_stmt = conn let mut insert_price_stmt = conn