feat: add oliver elastic support; update db(sqlite)

This commit is contained in:
zhaoyupeng
2024-09-20 15:14:49 +08:00
parent f9d59b99a0
commit c117d363a0
14 changed files with 399 additions and 85 deletions

View File

@ -2,25 +2,29 @@ package db
import (
"context"
"gorm.io/gorm"
"io"
"ultone/internal/opt"
"ultone/internal/tool"
"gorm.io/gorm"
)
var (
cli = &client{}
Default *Client
)
type client struct {
cli *gorm.DB
ttype string
type Client struct {
ctx context.Context
cli *gorm.DB
ttype string
cfgSqlite *cfgSqlite
}
func Type() string {
return cli.ttype
func (c *Client) Type() string {
return c.ttype
}
func New(ctxs ...context.Context) *gorm.DB {
func (c *Client) Session(ctxs ...context.Context) *gorm.DB {
var ctx context.Context
if len(ctxs) > 0 && ctxs[0] != nil {
ctx = ctxs[0]
@ -28,7 +32,7 @@ func New(ctxs ...context.Context) *gorm.DB {
ctx = tool.Timeout(30)
}
session := cli.cli.Session(&gorm.Session{Context: ctx})
session := c.cli.Session(&gorm.Session{Context: ctx})
if opt.Debug {
session = session.Debug()
@ -36,3 +40,22 @@ func New(ctxs ...context.Context) *gorm.DB {
return session
}
func (c *Client) Close() {
d, _ := c.cli.DB()
d.Close()
}
// Dump
// Only for sqlite with mem mode to dump data to bytes(io.Reader)
func (c *Client) Dump() (reader io.ReadSeekCloser, ok bool) {
if c.ttype != "sqlite" {
return nil, false
}
if c.cfgSqlite.fsType != "mem" {
return nil, false
}
return c.cfgSqlite.memDump.Dump(), true
}