From 471df4d7a1654f35756a202e26a5d28815222012 Mon Sep 17 00:00:00 2001 From: David Schlachter Date: Thu, 8 Jan 2026 11:14:39 -0500 Subject: More flexible and better documentation --- setup.go | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'setup.go') 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( -- cgit v1.2.3