zombiezen / go-sqlite

Low-level Go interface to SQLite 3
https://pkg.go.dev/zombiezen.com/go/sqlite
ISC License
699 stars 16 forks source link

Stmt.findBindName shoud be public #95

Closed shaunco closed 2 months ago

shaunco commented 2 months ago

Stmt.ColumnIndex is public, but Stmt.findBindName is private. When writing generic helper functions, like one that serializes a protobuf object to/from a BLOB column/field, it is nice to be able to mimic the Set/Bind and Get/Column pattern that Stmt already uses, but it is currently not possible on the Set functions without duplicating all the code in my Bind since there isn't a public findBindName I can call.

zombiezen commented 2 months ago

I'm not sure what functionality you're unable to express. AFAIK findBindName should be equivalent to (untested):

func myFindBindName(stmt *sqlite.Stmt, name string) (int, bool) {
  for i, n := 1, stmt.BindParamCount(); i <= n; i++ {
    if stmt.BindParamName(i) == name {
      return i, true
    }
  }
  return 0, false
}

FWIW, sqlitex does roughly the same thing, so this is definitely possible without any additional API: https://github.com/zombiezen/go-sqlite/blob/e4d0fa1c279a2a6ae900c9695bb2527b3391a1b7/sqlitex/exec.go#L343-L359

shaunco commented 2 months ago

Sure, I can reimplement the findBindName code on my own, but it seemed more forward compatible to just expose the function in case you decide to optimize it to a map lookup or something else in the future.

zombiezen commented 2 months ago

Understood. If the implementation changes dramatically, I'll keep that in mind. For now, I'm going to close this because the public API as it exists now seems sufficient.