aboutsummaryrefslogtreecommitdiff
path: root/oblast.go
diff options
context:
space:
mode:
Diffstat (limited to 'oblast.go')
-rw-r--r--oblast.go19
1 files changed, 11 insertions, 8 deletions
diff --git a/oblast.go b/oblast.go
index 15f840a..7b40146 100644
--- a/oblast.go
+++ b/oblast.go
@@ -42,24 +42,23 @@ package oblast // import "go.xyrillian.de/oblast"
import (
"database/sql"
+ "fmt"
"reflect"
-
- "go.xyrillian.de/oblast/internal"
)
// PlanOption is an option that can be given to NewStore() to influence query planning for a certain type of record.
-type PlanOption func(*internal.PlanOpts)
+type PlanOption func(*planOpts)
// TableNameIs is a PlanOption for record types that correspond to exactly one database table (as opposed to a join of multiple tables).
// This option is required to enable any of the methods of [Store] that use partially or fully auto-generated query strings.
func TableNameIs(name string) PlanOption {
- return func(opts *internal.PlanOpts) { opts.TableName = name }
+ return func(opts *planOpts) { opts.TableName = name }
}
// PrimaryKeyIs is a PlanOption for record types that correspond to a database table with a primary key.
// This option is required to enable use of the [Store.Update] and [Store.Delete] methods.
func PrimaryKeyIs(columnNames ...string) PlanOption {
- return func(opts *internal.PlanOpts) { opts.PrimaryKeyColumnNames = columnNames }
+ return func(opts *planOpts) { opts.PrimaryKeyColumnNames = columnNames }
}
// Handle is an interface for functions providing direct DB access.
@@ -83,7 +82,7 @@ var (
// and can also be used to execute autogenerated queries if the respective [PlanOption] values were provided during [NewStore].
type Store[R any] struct {
dialect Dialect
- plan internal.Plan
+ plan plan
}
// NewStore initializes a store for record type R.
@@ -110,11 +109,15 @@ type Store[R any] struct {
// Besides the declaration of a column name, the following extra tags are understood (as a comma-separated list following the column name):
// - "auto": During [Store.Insert], do not store this field's value. Instead, the database will auto-generate a value, which will be read back into the record.
func NewStore[R any](dialect Dialect, opts ...PlanOption) (Store[R], error) {
- var popts internal.PlanOpts
+ var popts planOpts
for _, opt := range opts {
opt(&popts)
}
- plan, err := internal.BuildPlan(reflect.TypeFor[R](), dialect, popts)
+ plan, err := buildPlan(reflect.TypeFor[R](), dialect, popts)
+ if err != nil {
+ var zero R
+ return Store[R]{}, fmt.Errorf("cannot use type %T for queries: %w", zero, err)
+ }
return Store[R]{dialect, plan}, err
}