1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/ferret.go
2021-05-04 15:01:27 -04:00

65 lines
1.3 KiB
Go

package ferret
import (
"context"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type Instance struct {
compiler *compiler.Compiler
drivers *drivers.Container
}
func New(setters ...Option) *Instance {
opts := NewOptions(setters)
return &Instance{
compiler: compiler.New(opts.compiler...),
drivers: drivers.NewContainer(),
}
}
func (i *Instance) Functions() core.Namespace {
return i.compiler
}
func (i *Instance) Drivers() *drivers.Container {
return i.drivers
}
func (i *Instance) Compile(query string) (*runtime.Program, error) {
return i.compiler.Compile(query)
}
func (i *Instance) MustCompile(query string) *runtime.Program {
return i.compiler.MustCompile(query)
}
func (i *Instance) Exec(ctx context.Context, query string, opts ...runtime.Option) ([]byte, error) {
p, err := i.Compile(query)
if err != nil {
return nil, err
}
for _, drv := range i.drivers.GetAll() {
ctx = drivers.WithContext(ctx, drv)
}
return p.Run(ctx, opts...)
}
func (i *Instance) MustExec(ctx context.Context, query string, opts ...runtime.Option) []byte {
out, err := i.Exec(ctx, query, opts...)
if err != nil {
panic(err)
}
return out
}