chore: downgrade go version 1.20

This commit is contained in:
loveuer
2024-12-27 11:13:28 +08:00
parent e5ae2efcef
commit 5c9d91ff63
11 changed files with 66 additions and 216 deletions

View File

@ -5,22 +5,30 @@ import (
"fmt"
"strings"
"github.com/glebarez/sqlite"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func New(ctx context.Context, uri string, opts ...Option) (*Client, error) {
const tip = `example:
for sqlite -> sqlite::<filepath>
sqlite::data.sqlite
sqlite::/data/data.db
for mysql -> mysql::<gorm_dsn>
mysql::user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local
for postgres -> postgres::<gorm_dsn>
postgres::host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai
`
func New(ctx context.Context, uri string) (*Client, error) {
parts := strings.SplitN(uri, "::", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("db.Init: opt db uri invalid: %s", uri)
return nil, fmt.Errorf("db.Init: db uri invalid\n%s", tip)
}
c := &Client{cfgSqlite: &cfgSqlite{fsType: "file"}}
for _, f := range opts {
f(c)
}
c := &Client{}
var (
err error
@ -30,7 +38,7 @@ func New(ctx context.Context, uri string, opts ...Option) (*Client, error) {
switch parts[0] {
case "sqlite":
c.dbType = DBTypeSqlite
err = openSqlite(c, dsn)
c.cli, err = gorm.Open(sqlite.Open(dsn))
case "mysql":
c.dbType = DBTypeMysql
c.cli, err = gorm.Open(mysql.Open(dsn))
@ -48,8 +56,8 @@ func New(ctx context.Context, uri string, opts ...Option) (*Client, error) {
return c, nil
}
func Init(ctx context.Context, uri string, opts ...Option) (err error) {
if Default, err = New(ctx, uri, opts...); err != nil {
func Init(ctx context.Context, uri string) (err error) {
if Default, err = New(ctx, uri); err != nil {
return err
}