summaryrefslogtreecommitdiff
path: root/setup.go
diff options
context:
space:
mode:
Diffstat (limited to 'setup.go')
-rw-r--r--setup.go27
1 files changed, 11 insertions, 16 deletions
diff --git a/setup.go b/setup.go
index 2b15acb..d88db48 100644
--- a/setup.go
+++ b/setup.go
@@ -12,39 +12,34 @@ import (
"github.com/goccy/go-json"
)
-func setupDatabase() (*sql.DB, error) {
- db, err := sql.Open("sqlite3", dictionary)
+func setupDatabase(rawDictionary string, db *sql.DB) error {
+ _, err := db.Exec("create table IF NOT EXISTS words (word text not null, definition text);")
if err != nil {
- return nil, fmt.Errorf("opening DB '%s': %s", dictionary, err)
- }
-
- _, err = db.Exec("create table IF NOT EXISTS words (word text not null, definition text);")
- if err != nil {
- return nil, fmt.Errorf("creating table: %s", err)
+ return fmt.Errorf("creating table: %s", err)
}
row := db.QueryRow(`SELECT count(*) as count from words`)
var count int
err = row.Scan(&count)
if err != nil {
- return nil, fmt.Errorf("counting rows: %s", err)
+ return fmt.Errorf("counting rows: %s", err)
}
// Only populate the database if it is empty.
if count > 0 {
- return db, nil
+ return nil
}
// Faster import performance.
_, err = db.Exec("PRAGMA synchronous = OFF;")
if err != nil {
- return nil, fmt.Errorf("setting risky writes: %s", err)
+ return fmt.Errorf("setting risky writes: %s", err)
}
- if err = populateDictionary(db); err != nil {
- return nil, fmt.Errorf("failed to prepare dictionary: %s", err)
+ if err = populateDictionary(rawDictionary, db); err != nil {
+ return fmt.Errorf("failed to prepare dictionary: %s", err)
}
- return db, nil
+ return nil
}
type rawDictionaryEntry struct {
@@ -84,8 +79,8 @@ type SenseForDictionaryEntry struct {
Example string
}
-func populateDictionary(db *sql.DB) error {
- log.Printf("preparing list of dictionary words...")
+func populateDictionary(rawDictionary string, db *sql.DB) error {
+ log.Printf("preparing sqlite database from raw dictionary data...")
// Set up the template
tmpl, err := template.New("entry").Parse(