Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
2586f0749f | |||
1387eecc96 |
22
db.go
22
db.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
@@ -21,6 +22,22 @@ func (db *DB) Open() error {
|
|||||||
return fmt.Errorf("database path not set")
|
return fmt.Errorf("database path not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(db.path)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
log.Printf("Database file does not exist at %s, creating", db.path)
|
||||||
|
file, err := os.Create(db.path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create database file: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Database created at %s", db.path)
|
||||||
|
file.Close()
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("failed to open database file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
sql.Register("spellfixlite", &sqlite3.SQLiteDriver{
|
sql.Register("spellfixlite", &sqlite3.SQLiteDriver{
|
||||||
Extensions: []string{"spellfix"},
|
Extensions: []string{"spellfix"},
|
||||||
})
|
})
|
||||||
@@ -95,6 +112,11 @@ func (db *DB) Init(ddl string) error {
|
|||||||
_, err = db.writeConn.Exec(ddl)
|
_, err = db.writeConn.Exec(ddl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("%++v", err)
|
Error.Printf("%++v", err)
|
||||||
|
log.Printf("%#v", "Rolling back")
|
||||||
|
_, err2 := db.writeConn.Exec("ROLLBACK;")
|
||||||
|
if err2 != nil {
|
||||||
|
Error.Printf("Error rollingback! %++v", err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
food.ddl
5
food.ddl
@@ -1,4 +1,4 @@
|
|||||||
begin transaction;
|
begin;
|
||||||
|
|
||||||
create table weight (
|
create table weight (
|
||||||
date datetime default (datetime('now', '+2 hours')),
|
date datetime default (datetime('now', '+2 hours')),
|
||||||
@@ -124,7 +124,6 @@ where not exists (
|
|||||||
);
|
);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
-- Spellfix search example
|
|
||||||
with search_results as (
|
with search_results as (
|
||||||
select word, score, f.rowid, f.*
|
select word, score, f.rowid, f.*
|
||||||
from foodfix
|
from foodfix
|
||||||
@@ -134,7 +133,7 @@ with search_results as (
|
|||||||
select rowid, food, score, date, description, amount, per100, energy
|
select rowid, food, score, date, description, amount, per100, energy
|
||||||
from search_results
|
from search_results
|
||||||
group by food
|
group by food
|
||||||
order by score asc, date desc
|
order by score asc, date desc;
|
||||||
|
|
||||||
create index dailyIdx on food(strftime('%Y-%m-%d', date));
|
create index dailyIdx on food(strftime('%Y-%m-%d', date));
|
||||||
create index weeklyIdx on food(strftime('%Y-%W', date));
|
create index weeklyIdx on food(strftime('%Y-%W', date));
|
||||||
|
@@ -99,8 +99,9 @@
|
|||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
function setInputVal(val: string) {
|
function setInputVal(food: main.Food) {
|
||||||
name = val;
|
name = food.food;
|
||||||
|
per100 = String(food.per100)
|
||||||
hiLiteIndex = null;
|
hiLiteIndex = null;
|
||||||
foodSearch = [];
|
foodSearch = [];
|
||||||
}
|
}
|
||||||
@@ -163,7 +164,7 @@
|
|||||||
<FoodSearchEntry
|
<FoodSearchEntry
|
||||||
itemLabel={f.food}
|
itemLabel={f.food}
|
||||||
highlighted={i == hiLiteIndex}
|
highlighted={i == hiLiteIndex}
|
||||||
on:click={() => setInputVal(f.food)}
|
on:click={() => setInputVal(f)}
|
||||||
/>
|
/>
|
||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</ul>
|
||||||
|
6
main.go
6
main.go
@@ -56,7 +56,11 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
db.Init(foodDDL)
|
err = db.Init(foodDDL)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error initializing database: %++v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
settingsService = &SettingsService{db: &db}
|
settingsService = &SettingsService{db: &db}
|
||||||
err = settingsService.LoadSettings()
|
err = settingsService.LoadSettings()
|
||||||
|
Reference in New Issue
Block a user