mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package sql
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"github.com/hanzoai/tasks/common/config"
|
|
"github.com/hanzoai/tasks/common/log"
|
|
"github.com/hanzoai/tasks/common/metrics"
|
|
"github.com/hanzoai/tasks/common/persistence/sql/sqlplugin"
|
|
"github.com/hanzoai/tasks/common/resolver"
|
|
expmaps "golang.org/x/exp/maps"
|
|
)
|
|
|
|
var ErrPluginNotSupported = errors.New("plugin not supported")
|
|
|
|
var supportedPlugins = map[string]sqlplugin.Plugin{}
|
|
|
|
// RegisterPlugin will register a SQL plugin
|
|
func RegisterPlugin(pluginName string, plugin sqlplugin.Plugin) {
|
|
if _, ok := supportedPlugins[pluginName]; ok {
|
|
panic("plugin " + pluginName + " already registered")
|
|
}
|
|
supportedPlugins[pluginName] = plugin
|
|
}
|
|
|
|
// NewSQLDB creates a returns a reference to a logical connection to the
|
|
// underlying SQL database. The returned object is tied to a single
|
|
// SQL database and the object can be used to perform CRUD operations on
|
|
// the tables in the database.
|
|
func NewSQLDB(
|
|
dbKind sqlplugin.DbKind,
|
|
cfg *config.SQL,
|
|
r resolver.ServiceResolver,
|
|
logger log.Logger,
|
|
mh metrics.Handler,
|
|
) (sqlplugin.DB, error) {
|
|
return createDB[sqlplugin.DB](dbKind, cfg, r, logger, mh)
|
|
}
|
|
|
|
// NewSQLAdminDB returns a AdminDB.
|
|
func NewSQLAdminDB(
|
|
dbKind sqlplugin.DbKind,
|
|
cfg *config.SQL,
|
|
r resolver.ServiceResolver,
|
|
logger log.Logger,
|
|
mh metrics.Handler,
|
|
) (sqlplugin.AdminDB, error) {
|
|
return createDB[sqlplugin.AdminDB](dbKind, cfg, r, logger, mh)
|
|
}
|
|
|
|
func createDB[T any](
|
|
dbKind sqlplugin.DbKind,
|
|
cfg *config.SQL,
|
|
r resolver.ServiceResolver,
|
|
logger log.Logger,
|
|
mh metrics.Handler,
|
|
) (T, error) {
|
|
var res T
|
|
plugin, err := getPlugin(cfg.PluginName)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
db, err := plugin.CreateDB(dbKind, cfg, r, logger, mh)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
//revive:disable-next-line:unchecked-type-assertion
|
|
res = db.(T)
|
|
return res, err
|
|
}
|
|
|
|
func getPlugin(pluginName string) (sqlplugin.Plugin, error) {
|
|
plugin, ok := supportedPlugins[pluginName]
|
|
if !ok {
|
|
keys := expmaps.Keys(supportedPlugins)
|
|
slices.Sort(keys)
|
|
return nil, fmt.Errorf(
|
|
"%w: unknown plugin %q, supported plugins: %v",
|
|
ErrPluginNotSupported,
|
|
pluginName,
|
|
keys,
|
|
)
|
|
}
|
|
return plugin, nil
|
|
}
|
|
|
|
func GetPluginVisibilityQueryConverter(pluginName string) (sqlplugin.VisibilityQueryConverter, error) {
|
|
plugin, err := getPlugin(pluginName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return plugin.GetVisibilityQueryConverter(), nil
|
|
}
|