diff options
| author | David Schlachter <t480-debian-git@schlachter.ca> | 2026-01-08 11:14:39 -0500 |
|---|---|---|
| committer | David Schlachter <t480-debian-git@schlachter.ca> | 2026-01-08 11:14:39 -0500 |
| commit | 471df4d7a1654f35756a202e26a5d28815222012 (patch) | |
| tree | 743a150149b1c0083c125da4c7c0ca487b2d600f /setup.go | |
| parent | 10f8e8c5ea5d3dd191d7e51682efc237d34cded4 (diff) | |
More flexible and better documentation
Diffstat (limited to 'setup.go')
| -rw-r--r-- | setup.go | 27 |
1 files changed, 11 insertions, 16 deletions
@@ -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( |
