github.com/MontFerret/ferret@v0.18.0/ferret.go (about) 1 package ferret 2 3 import ( 4 "context" 5 6 "github.com/MontFerret/ferret/pkg/compiler" 7 "github.com/MontFerret/ferret/pkg/drivers" 8 "github.com/MontFerret/ferret/pkg/runtime" 9 "github.com/MontFerret/ferret/pkg/runtime/core" 10 ) 11 12 type Instance struct { 13 compiler *compiler.Compiler 14 drivers *drivers.Container 15 } 16 17 func New(setters ...Option) *Instance { 18 opts := NewOptions(setters) 19 20 return &Instance{ 21 compiler: compiler.New(opts.compiler...), 22 drivers: drivers.NewContainer(), 23 } 24 } 25 26 func (i *Instance) Functions() core.Namespace { 27 return i.compiler 28 } 29 30 func (i *Instance) Drivers() *drivers.Container { 31 return i.drivers 32 } 33 34 func (i *Instance) Compile(query string) (*runtime.Program, error) { 35 return i.compiler.Compile(query) 36 } 37 38 func (i *Instance) MustCompile(query string) *runtime.Program { 39 return i.compiler.MustCompile(query) 40 } 41 42 func (i *Instance) Exec(ctx context.Context, query string, opts ...runtime.Option) ([]byte, error) { 43 p, err := i.Compile(query) 44 45 if err != nil { 46 return nil, err 47 } 48 49 ctx = i.drivers.WithContext(ctx) 50 51 return p.Run(ctx, opts...) 52 } 53 54 func (i *Instance) MustExec(ctx context.Context, query string, opts ...runtime.Option) []byte { 55 out, err := i.Exec(ctx, query, opts...) 56 57 if err != nil { 58 panic(err) 59 } 60 61 return out 62 } 63 64 func (i *Instance) Run(ctx context.Context, program *runtime.Program, opts ...runtime.Option) ([]byte, error) { 65 if program == nil { 66 return nil, core.Error(core.ErrInvalidArgument, "program") 67 } 68 69 ctx = i.drivers.WithContext(ctx) 70 71 return program.Run(ctx, opts...) 72 } 73 74 func (i *Instance) MustRun(ctx context.Context, program *runtime.Program, opts ...runtime.Option) []byte { 75 out, err := i.Run(ctx, program, opts...) 76 77 if err != nil { 78 panic(err) 79 } 80 81 return out 82 }