package mysqlite import ( "fmt" "reflect" "zombiezen.com/go/sqlite" ) type Db struct { Db *sqlite.Conn } func OpenDb(databaseSource string) (*Db, error) { conn, err := sqlite.OpenConn(databaseSource) if err != nil { return nil, err } return &Db{Db: conn}, nil } func (d *Db) Close() error { return d.Db.Close() } func (d *Db) QuerySingle(query string, args ...any) error { stmt, remaining, err := d.Db.PrepareTransient(query) if err != nil { return err } defer stmt.Finalize() if remaining != 0 { return fmt.Errorf("remaining bytes: %s", remaining) } rowReturned, err := stmt.Step() if err != nil { return err } if !rowReturned { return fmt.Errorf("did not return any rows") } if stmt.ColumnCount() != 1 { return fmt.Errorf("query returned %d rows while only one was expected", stmt.ColumnCount()) } for i, arg := range args { if asString, ok := arg.(*string); ok { *asString = stmt.ColumnText(i) } else if asInt, ok := arg.(*int); ok { *asInt = stmt.ColumnInt(i) } else if asBool, ok := arg.(*bool); ok { *asBool = stmt.ColumnBool(i) } else { return fmt.Errorf("unsupported column type at index %d", i) } } return nil }