1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package data_service
- import (
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
- "git.sxidc.com/go-tools/utils/template"
- "git.sxidc.com/service-supports/ds-sdk/sdk"
- "github.com/pkg/errors"
- "time"
- )
- type Config struct {
- Token string `json:"token" yaml:"token"`
- Address string `json:"address" yaml:"address"`
- HttpPort string `json:"http_port" yaml:"http_port"`
- GrpcPort string `json:"grpc_port" yaml:"grpc_port"`
- Namespace string `json:"namespace" yaml:"namespace"`
- DataSource string `json:"data_source" yaml:"data_source"`
- HttpTimeoutSec int `json:"http_timeout_sec" yaml:"http_timeout_sec"`
- }
- type Executor struct{}
- func NewExecutor(conf *Config) (*Executor, error) {
- if conf.HttpTimeoutSec == 0 {
- conf.HttpTimeoutSec = 30
- }
- err := sdk.InitInstance(conf.Token, conf.Address, conf.HttpPort, conf.GrpcPort, conf.Namespace, conf.DataSource,
- sdk.WithTimeout(time.Duration(conf.HttpTimeoutSec)*time.Second))
- if err != nil {
- return nil, errors.New(err.Error())
- }
- return &Executor{}, nil
- }
- func DestroyExecutor(executor *Executor) error {
- if executor == nil {
- return nil
- }
- err := sdk.DestroyInstance()
- if err != nil {
- return errors.New(err.Error())
- }
- return nil
- }
- func (executor *Executor) ExecuteRawSql(sqlStr string, args ...any) ([]sql.Result, error) {
- return executor.ExecuteRawSqlTemplate(sqlStr, nil, args...)
- }
- func (executor *Executor) ExecuteRawSqlTemplate(sqlStr string, executeParams map[string]any, args ...any) ([]sql.Result, error) {
- parsedSql, err := template.ParseTemplateStringToString(sqlStr, executeParams)
- if err != nil {
- return nil, errors.New(err.Error())
- }
- tableRows, err := sdk.GetInstance().ExecuteRawSql(parsedSql, args...)
- if err != nil {
- return nil, errors.New(err.Error())
- }
- results := make([]sql.Result, len(tableRows))
- for i, row := range tableRows {
- results[i] = row
- }
- return results, nil
- }
|