Compare commits
58 Commits
Author | SHA1 | Date |
---|---|---|
dtapps | 5f70e83104 | 1 month ago |
dtapps | 8dbbff4a27 | 2 months ago |
dtapps | 0ed080ee44 | 2 months ago |
dtapps | b99cbca10a | 2 months ago |
dtapps | 8238616ab5 | 2 months ago |
dtapps | b4cf941dd9 | 2 months ago |
dtapps | 1f3831a39c | 2 months ago |
dtapps | bb1ce11d98 | 4 months ago |
dtapps | 0799a47c4d | 4 months ago |
dtapps | 986c3d81aa | 6 months ago |
李光春 | ce71070f19 | 2 years ago |
李光春 | 2480a2f0fb | 2 years ago |
李光春 | 7bd2d5fea4 | 2 years ago |
李光春 | 002b617947 | 2 years ago |
李光春 | 093c682a11 | 2 years ago |
李光春 | a92d63c3fa | 2 years ago |
李光春 | fbb0a0ef38 | 2 years ago |
李光春 | cda216ae0e | 2 years ago |
李光春 | 5cf7e80176 | 2 years ago |
李光春 | 0b0d3b35ad | 2 years ago |
李光春 | 3b662a2351 | 2 years ago |
李光春 | a383dc98c2 | 2 years ago |
李光春 | dbcb880465 | 2 years ago |
李光春 | 284e13594a | 2 years ago |
李光春 | f1530064e9 | 2 years ago |
李光春 | 54ca833aa5 | 2 years ago |
李光春 | a184e64f15 | 2 years ago |
李光春 | 37f574b436 | 2 years ago |
李光春 | c65de2d85b | 2 years ago |
李光春 | aa56a7a4eb | 2 years ago |
李光春 | f420131a37 | 2 years ago |
李光春 | 7c835284e1 | 2 years ago |
李光春 | 83ef4742cb | 2 years ago |
李光春 | 64404d1295 | 2 years ago |
李光春 | 3aa6b9c942 | 2 years ago |
李光春 | bbf1a72f89 | 2 years ago |
李光春 | c6f0cf2554 | 2 years ago |
李光春 | 184ce91506 | 2 years ago |
李光春 | 36cb324abe | 2 years ago |
李光春 | 38bdd453c7 | 2 years ago |
李光春 | e8c2b726bf | 2 years ago |
李光春 | 2f71ce5847 | 2 years ago |
李光春 | 981bb39993 | 2 years ago |
李光春 | 30eff780b1 | 2 years ago |
李光春 | 841f55a29a | 2 years ago |
李光春 | 389bd8e65c | 2 years ago |
李光春 | 8e27344f05 | 2 years ago |
李光春 | 75a6f160b9 | 2 years ago |
李光春 | 6a2d8ffdae | 2 years ago |
李光春 | 26a28acf7b | 2 years ago |
李光春 | 3035cd19ef | 2 years ago |
李光春 | 981768b7b0 | 2 years ago |
李光春 | c58e6fe761 | 2 years ago |
李光春 | 388310c804 | 2 years ago |
李光春 | 6559dff6e2 | 2 years ago |
李光春 | bd8d1252d1 | 2 years ago |
李光春 | 903ce4cbb2 | 2 years ago |
李光春 | 3186fe56c6 | 2 years ago |
@ -1,16 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
type ConfigBeegoClient struct {
|
||||
Dns string // 地址
|
||||
}
|
||||
|
||||
// BeegoClient
|
||||
// https://beego.vip/
|
||||
type BeegoClient struct {
|
||||
Db *orm.Ormer // 驱动
|
||||
config *ConfigBeegoClient // 配置
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func NewBeegoMysqlClient(config *ConfigBeegoClient) (*BeegoClient, error) {
|
||||
|
||||
var err error
|
||||
c := &BeegoClient{config: config}
|
||||
|
||||
err = orm.RegisterDriver("mysql", orm.DRMySQL)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("加载驱动失败:%v", err))
|
||||
}
|
||||
|
||||
var db *sql.DB
|
||||
o, err := orm.NewOrmWithDB("mysql", "default", db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Db = &o
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func NewBeegoOracleClient(config *ConfigBeegoClient) (*BeegoClient, error) {
|
||||
|
||||
var err error
|
||||
c := &BeegoClient{config: config}
|
||||
|
||||
err = orm.RegisterDriver("oracle", orm.DROracle)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("加载驱动失败:%v", err))
|
||||
}
|
||||
|
||||
var db *sql.DB
|
||||
o, err := orm.NewOrmWithDB("oracle", "default", db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Db = &o
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func NewBeegoPgsqlClient(config *ConfigBeegoClient) (*BeegoClient, error) {
|
||||
|
||||
var err error
|
||||
c := &BeegoClient{config: config}
|
||||
|
||||
err = orm.RegisterDriver("pgsql", orm.DRPostgres)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("加载驱动失败:%v", err))
|
||||
}
|
||||
|
||||
var db *sql.DB
|
||||
o, err := orm.NewOrmWithDB("pgsql", "default", db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Db = &o
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func NewBeegoTidbClient(config *ConfigBeegoClient) (*BeegoClient, error) {
|
||||
|
||||
var err error
|
||||
c := &BeegoClient{config: config}
|
||||
|
||||
err = orm.RegisterDriver("TiDB", orm.DRTiDB)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("加载驱动失败:%v", err))
|
||||
}
|
||||
|
||||
var db *sql.DB
|
||||
o, err := orm.NewOrmWithDB("TiDB", "default", db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Db = &o
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type ConfigBunClient struct {
|
||||
Dns string // 地址
|
||||
}
|
||||
|
||||
// BunClient
|
||||
// https://bun.uptrace.dev/
|
||||
type BunClient struct {
|
||||
Db *bun.DB // 驱动
|
||||
config *ConfigBunClient // 配置
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/denisenkom/go-mssqldb"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/mssqldialect"
|
||||
)
|
||||
|
||||
func NewBunMssqlClient(config *ConfigBunClient) (*BunClient, error) {
|
||||
|
||||
var err error
|
||||
c := &BunClient{config: config}
|
||||
|
||||
sqlDb, err := sql.Open("sqlserver", c.config.Dns)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("加载驱动失败:%v", err))
|
||||
}
|
||||
|
||||
c.Db = bun.NewDB(sqlDb, mssqldialect.New())
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/mysqldialect"
|
||||
)
|
||||
|
||||
func NewBunMysqlClient(config *ConfigBunClient) (*BunClient, error) {
|
||||
|
||||
var err error
|
||||
c := &BunClient{config: config}
|
||||
|
||||
sqlDb, err := sql.Open("mysql", c.config.Dns)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("加载驱动失败:%v", err))
|
||||
}
|
||||
|
||||
c.Db = bun.NewDB(sqlDb, mysqldialect.New())
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/pgdialect"
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
)
|
||||
|
||||
func NewBunPgsqlClient(config *ConfigBunClient) (*BunClient, error) {
|
||||
|
||||
c := &BunClient{config: config}
|
||||
|
||||
sqlDb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(c.config.Dns)))
|
||||
|
||||
c.Db = bun.NewDB(sqlDb, pgdialect.New())
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/sqlitedialect"
|
||||
"github.com/uptrace/bun/driver/sqliteshim"
|
||||
)
|
||||
|
||||
func NewBunSqliteClient(config *ConfigBunClient) (*BunClient, error) {
|
||||
|
||||
var err error
|
||||
c := &BunClient{config: config}
|
||||
|
||||
sqlDb, err := sql.Open(sqliteshim.ShimName, c.config.Dns)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("加载驱动失败:%v", err))
|
||||
}
|
||||
|
||||
c.Db = bun.NewDB(sqlDb, sqlitedialect.New())
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,126 +1,57 @@
|
||||
module go.dtapp.net/dorm
|
||||
|
||||
go 1.18
|
||||
go 1.22.1
|
||||
|
||||
require (
|
||||
gitee.com/chunanyong/dm v1.8.5
|
||||
gitee.com/chunanyong/zorm v1.5.6
|
||||
github.com/basgys/goxml2json v1.1.0
|
||||
github.com/beego/beego/v2 v2.0.4
|
||||
github.com/bmizerany/pq v0.0.0-20131128184720-da2b95e392c1
|
||||
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d
|
||||
github.com/denisenkom/go-mssqldb v0.12.2
|
||||
github.com/glebarez/sqlite v1.4.6
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/go-redis/redis/v9 v9.0.0-beta.1
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/godror/godror v0.34.0
|
||||
github.com/gomodule/redigo v2.0.0+incompatible
|
||||
github.com/kamva/mgm/v3 v3.4.1
|
||||
github.com/lib/pq v1.10.5
|
||||
github.com/mailru/go-clickhouse/v2 v2.0.0
|
||||
github.com/mattn/go-sqlite3 v1.14.14
|
||||
github.com/qiniu/qmgo v1.1.1
|
||||
github.com/rs/xid v1.2.1
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/upper/db/v4 v4.5.4
|
||||
github.com/uptrace/bun v1.1.6
|
||||
github.com/uptrace/bun/dialect/mssqldialect v1.1.6
|
||||
github.com/uptrace/bun/dialect/mysqldialect v1.1.6
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.6
|
||||
github.com/uptrace/bun/dialect/sqlitedialect v1.1.6
|
||||
github.com/uptrace/bun/driver/pgdriver v1.1.6
|
||||
github.com/uptrace/bun/driver/sqliteshim v1.1.6
|
||||
go.dtapp.net/gotime v1.0.4
|
||||
go.mongodb.org/mongo-driver v1.9.1
|
||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
|
||||
gorm.io/driver/mysql v1.3.4
|
||||
gorm.io/driver/postgres v1.3.7
|
||||
gorm.io/driver/sqlite v1.3.6
|
||||
gorm.io/driver/sqlserver v1.3.2
|
||||
gorm.io/gorm v1.23.8
|
||||
xorm.io/xorm v1.3.1
|
||||
github.com/redis/go-redis/v9 v9.5.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
go.dtapp.net/gojson v1.0.4
|
||||
go.dtapp.net/gotime v1.0.8
|
||||
go.mongodb.org/mongo-driver v1.15.0
|
||||
gorm.io/driver/mysql v1.5.6
|
||||
gorm.io/driver/postgres v1.5.7
|
||||
gorm.io/gen v0.3.26
|
||||
gorm.io/gorm v1.25.10
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bitly/go-simplejson v0.5.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/basgys/goxml2json v1.1.0 // indirect
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/edsrzf/mmap-go v1.0.0 // indirect
|
||||
github.com/glebarez/go-sqlite v1.17.3 // indirect
|
||||
github.com/go-logfmt/logfmt v0.5.1 // indirect
|
||||
github.com/go-logr/logr v1.2.3 // indirect
|
||||
github.com/go-playground/locales v0.13.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.17.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.4.1 // indirect
|
||||
github.com/go-stack/stack v1.8.1 // indirect
|
||||
github.com/goccy/go-json v0.8.1 // indirect
|
||||
github.com/godror/knownpb v0.1.0 // indirect
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
||||
github.com/golang-sql/sqlexp v0.1.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
|
||||
github.com/jackc/pgconn v1.12.1 // indirect
|
||||
github.com/jackc/pgio v1.0.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
||||
github.com/jackc/pgtype v1.11.0 // indirect
|
||||
github.com/jackc/pgx/v4 v4.16.1 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
|
||||
github.com/jackc/pgx/v5 v5.5.5 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
|
||||
github.com/klauspost/compress v1.15.6 // indirect
|
||||
github.com/leodido/go-urn v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/klauspost/compress v1.17.8 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
|
||||
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
|
||||
github.com/stretchr/testify v1.7.1 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.1 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.3 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
|
||||
golang.org/x/sys v0.0.0-20220708085239-5a0f0661e09d // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
golang.org/x/tools v0.1.11 // indirect
|
||||
google.golang.org/protobuf v1.27.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||
lukechampine.com/uint128 v1.2.0 // indirect
|
||||
mellium.im/sasl v0.2.1 // indirect
|
||||
modernc.org/b v1.0.2 // indirect
|
||||
modernc.org/cc/v3 v3.36.0 // indirect
|
||||
modernc.org/ccgo/v3 v3.16.7 // indirect
|
||||
modernc.org/db v1.0.3 // indirect
|
||||
modernc.org/file v1.0.3 // indirect
|
||||
modernc.org/fileutil v1.0.0 // indirect
|
||||
modernc.org/golex v1.0.1 // indirect
|
||||
modernc.org/internal v1.0.2 // indirect
|
||||
modernc.org/libc v1.16.14 // indirect
|
||||
modernc.org/lldb v1.0.2 // indirect
|
||||
modernc.org/mathutil v1.4.1 // indirect
|
||||
modernc.org/memory v1.1.1 // indirect
|
||||
modernc.org/opt v0.1.3 // indirect
|
||||
modernc.org/ql v1.4.0 // indirect
|
||||
modernc.org/sortutil v1.1.0 // indirect
|
||||
modernc.org/sqlite v1.17.3 // indirect
|
||||
modernc.org/strutil v1.1.2 // indirect
|
||||
modernc.org/token v1.0.0 // indirect
|
||||
modernc.org/zappy v1.0.3 // indirect
|
||||
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
|
||||
github.com/xdg-go/scram v1.1.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
|
||||
golang.org/x/arch v0.7.0 // indirect
|
||||
golang.org/x/crypto v0.22.0 // indirect
|
||||
golang.org/x/mod v0.17.0 // indirect
|
||||
golang.org/x/net v0.24.0 // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/tools v0.20.0 // indirect
|
||||
gorm.io/datatypes v1.2.0 // indirect
|
||||
gorm.io/hints v1.1.2 // indirect
|
||||
gorm.io/plugin/dbresolver v1.5.1 // indirect
|
||||
)
|
||||
|
@ -0,0 +1,18 @@
|
||||
package dorm
|
||||
|
||||
import "database/sql"
|
||||
|
||||
// Ping ping
|
||||
func (c *GormClient) Ping() error {
|
||||
return c.sqlDd.Ping()
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func (c *GormClient) Close() error {
|
||||
return c.sqlDd.Close()
|
||||
}
|
||||
|
||||
// Stats 返回数据库统计信息
|
||||
func (c *GormClient) Stats() sql.DBStats {
|
||||
return c.sqlDd.Stats()
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package dorm
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
var (
|
||||
// GormNotFound 没有数据
|
||||
GormNotFound = gorm.ErrRecordNotFound
|
||||
)
|
@ -0,0 +1,29 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormGenClientFun *GormClient 驱动
|
||||
type GormGenClientFun func() *GormGenClient
|
||||
|
||||
// GormGenClientTableFun
|
||||
// *GormClient 驱动
|
||||
// string 表名
|
||||
type GormGenClientTableFun func() (*GormGenClient, string)
|
||||
|
||||
// GormGenClientConfig 配置
|
||||
type GormGenClientConfig struct {
|
||||
Dns string // dns地址
|
||||
Db *gorm.DB // db驱动
|
||||
Config gen.Config // gen配置
|
||||
}
|
||||
|
||||
// GormGenClient
|
||||
// https://gorm.io/zh_CN/gen/index.html
|
||||
type GormGenClient struct {
|
||||
db *gorm.DB // 驱动
|
||||
generator *gen.Generator // 驱动
|
||||
config *GormGenClientConfig // 配置
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (c *GormGenClient) GetDb() *gorm.DB {
|
||||
return c.db
|
||||
}
|
||||
|
||||
// GetGenerator 获取驱动
|
||||
func (c *GormGenClient) GetGenerator() *gen.Generator {
|
||||
return c.generator
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NewGormGenMysqlClient 创建GormGenClient实例 mysql
|
||||
func NewGormGenMysqlClient(ctx context.Context, config *GormGenClientConfig) (*GormGenClient, error) {
|
||||
|
||||
c := &GormGenClient{config: config}
|
||||
|
||||
c.generator = gen.NewGenerator(config.Config)
|
||||
|
||||
if c.config.Dns != "" {
|
||||
c.db, _ = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{})
|
||||
c.generator.UseDB(c.db)
|
||||
} else {
|
||||
c.generator.UseDB(c.config.Db)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NewGormGenPostgresClient 创建GormGenClient实例 postgres
|
||||
func NewGormGenPostgresClient(ctx context.Context, config *GormGenClientConfig) (*GormGenClient, error) {
|
||||
|
||||
c := &GormGenClient{config: config}
|
||||
|
||||
c.generator = gen.NewGenerator(config.Config)
|
||||
|
||||
if c.config.Dns != "" {
|
||||
c.db, _ = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{})
|
||||
c.generator.UseDB(c.db)
|
||||
} else {
|
||||
c.generator.UseDB(c.config.Db)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NewGormGenPostgresqlClient 创建GormGenClient实例 postgresql
|
||||
func NewGormGenPostgresqlClient(ctx context.Context, config *GormGenClientConfig) (*GormGenClient, error) {
|
||||
|
||||
c := &GormGenClient{config: config}
|
||||
|
||||
c.generator = gen.NewGenerator(config.Config)
|
||||
|
||||
if c.config.Dns != "" {
|
||||
c.db, _ = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{})
|
||||
c.generator.UseDB(c.db)
|
||||
} else {
|
||||
c.generator.UseDB(c.config.Db)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (c *GormClient) GetDb() *gorm.DB {
|
||||
return c.db
|
||||
}
|
||||
|
||||
// GetSqlDb 获取驱动
|
||||
func (c *GormClient) GetSqlDb() *sql.DB {
|
||||
return c.sqlDd
|
||||
}
|
@ -1 +0,0 @@
|
||||
package dorm
|
@ -0,0 +1,60 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type MongoCollectionOptions struct {
|
||||
db *mongo.Client // 驱动
|
||||
configDatabaseName string // 库名
|
||||
dbCollection *mongo.Collection // 集合
|
||||
}
|
||||
|
||||
// Collection 选择集合
|
||||
func (cd *MongoDatabaseOptions) Collection(name string, opts ...*options.CollectionOptions) *MongoCollectionOptions {
|
||||
return &MongoCollectionOptions{
|
||||
db: cd.db, // 驱动
|
||||
configDatabaseName: cd.configDatabaseName, // 库名
|
||||
dbCollection: cd.dbDatabase.Collection(name, opts...), // 集合
|
||||
}
|
||||
}
|
||||
|
||||
// CreateOneIndexes 创建一个索引
|
||||
func (cc *MongoCollectionOptions) CreateOneIndexes(ctx context.Context, key string, value string) (string, error) {
|
||||
return cc.dbCollection.Indexes().CreateOne(ctx, mongo.IndexModel{
|
||||
Keys: bson.D{{
|
||||
Key: key,
|
||||
Value: value,
|
||||
}},
|
||||
})
|
||||
}
|
||||
|
||||
// CreateOneUniqueIndexes 创建一个唯一索引
|
||||
func (cc *MongoCollectionOptions) CreateOneUniqueIndexes(ctx context.Context, key string, value string) (string, error) {
|
||||
return cc.dbCollection.Indexes().CreateOne(ctx, mongo.IndexModel{
|
||||
Keys: bson.D{{
|
||||
Key: key,
|
||||
Value: value,
|
||||
}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
})
|
||||
}
|
||||
|
||||
// CreateOneUniqueIndexesOpts 创建一个索引
|
||||
func (cc *MongoCollectionOptions) CreateOneUniqueIndexesOpts(ctx context.Context, key string, value string, opts *options.IndexOptions) (string, error) {
|
||||
return cc.dbCollection.Indexes().CreateOne(ctx, mongo.IndexModel{
|
||||
Keys: bson.D{{
|
||||
Key: key,
|
||||
Value: value,
|
||||
}},
|
||||
Options: opts,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateManyIndexes 创建多个索引
|
||||
func (cc *MongoCollectionOptions) CreateManyIndexes(ctx context.Context, models []mongo.IndexModel) ([]string, error) {
|
||||
return cc.dbCollection.Indexes().CreateMany(ctx, models)
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// InsertOne 插入一个文档
|
||||
func (cc *MongoCollectionOptions) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
|
||||
return cc.dbCollection.InsertOne(ctx, document, opts...)
|
||||
}
|
||||
|
||||
// InsertMany 插入多个文档
|
||||
func (cc *MongoCollectionOptions) InsertMany(ctx context.Context, document []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
|
||||
return cc.dbCollection.InsertMany(ctx, document, opts...)
|
||||
}
|
||||
|
||||
// DeleteOne 删除一个文档
|
||||
func (cc *MongoCollectionOptions) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
||||
return cc.dbCollection.DeleteOne(ctx, filter, opts...)
|
||||
}
|
||||
|
||||
// DeleteMany 删除多个文档
|
||||
func (cc *MongoCollectionOptions) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
||||
return cc.dbCollection.DeleteMany(ctx, filter, opts...)
|
||||
}
|
||||
|
||||
// UpdateByID 按ID更新
|
||||
func (cc *MongoCollectionOptions) UpdateByID(ctx context.Context, id interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return cc.dbCollection.UpdateByID(ctx, id, update, opts...)
|
||||
}
|
||||
|
||||
// UpdateOne 更新一个文档
|
||||
func (cc *MongoCollectionOptions) UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return cc.dbCollection.UpdateOne(ctx, filter, update, opts...)
|
||||
}
|
||||
|
||||
// UpdateMany 更新多个文档
|
||||
func (cc *MongoCollectionOptions) UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return cc.dbCollection.UpdateMany(ctx, filter, update, opts...)
|
||||
}
|
||||
|
||||
// ReplaceOne 替换一个文档
|
||||
func (cc *MongoCollectionOptions) ReplaceOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error) {
|
||||
return cc.dbCollection.ReplaceOne(ctx, filter, update, opts...)
|
||||
}
|
||||
|
||||
// Aggregate 统计分析
|
||||
func (cc *MongoCollectionOptions) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error) {
|
||||
return cc.dbCollection.Aggregate(ctx, pipeline, opts...)
|
||||
}
|
||||
|
||||
// CountDocuments 计数文档
|
||||
func (cc *MongoCollectionOptions) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error) {
|
||||
return cc.dbCollection.CountDocuments(ctx, filter, opts...)
|
||||
}
|
||||
|
||||
// EstimatedDocumentCount 估计文档计数
|
||||
func (cc *MongoCollectionOptions) EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error) {
|
||||
return cc.dbCollection.EstimatedDocumentCount(ctx, opts...)
|
||||
}
|
||||
|
||||
func (cc *MongoCollectionOptions) Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error) {
|
||||
return cc.dbCollection.Distinct(ctx, fieldName, filter, opts...)
|
||||
}
|
||||
|
||||
// Find 查询多个文档
|
||||
func (cc *MongoCollectionOptions) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
|
||||
return cc.dbCollection.Find(ctx, filter, opts...)
|
||||
}
|
||||
|
||||
// FindOne 查询一个文档
|
||||
func (cc *MongoCollectionOptions) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult {
|
||||
return cc.dbCollection.FindOne(ctx, filter, opts...)
|
||||
}
|
||||
|
||||
func (cc *MongoCollectionOptions) FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult {
|
||||
return cc.dbCollection.FindOneAndDelete(ctx, filter, opts...)
|
||||
}
|
||||
|
||||
func (cc *MongoCollectionOptions) FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult {
|
||||
return cc.dbCollection.FindOneAndReplace(ctx, filter, replacement, opts...)
|
||||
}
|
||||
|
||||
func (cc *MongoCollectionOptions) FindOneAndUpdate(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult {
|
||||
return cc.dbCollection.FindOneAndUpdate(ctx, filter, replacement, opts...)
|
||||
}
|
||||
|
||||
func (cc *MongoCollectionOptions) Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) {
|
||||
return cc.dbCollection.Watch(ctx, pipeline, opts...)
|
||||
}
|
||||
|
||||
func (cc *MongoCollectionOptions) Indexes(ctx context.Context) mongo.IndexView {
|
||||
return cc.dbCollection.Indexes()
|
||||
}
|
||||
|
||||
func (cc *MongoCollectionOptions) Drop(ctx context.Context) error {
|
||||
return cc.dbCollection.Drop(ctx)
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type FindResult struct {
|
||||
cursor *mongo.Cursor
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *FindResult) Many(result interface{}) error {
|
||||
return f.cursor.All(context.TODO(), result)
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type FindManyResult struct {
|
||||
cursor *mongo.Cursor
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *FindManyResult) Many(result interface{}) error {
|
||||
return f.cursor.All(context.TODO(), result)
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import "go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
type FindOneResult struct {
|
||||
singleResult *mongo.SingleResult
|
||||
}
|
||||
|
||||
func (f *FindOneResult) One(result interface{}) error {
|
||||
return f.singleResult.Decode(result)
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type MongoDatabaseOptions struct {
|
||||
db *mongo.Client // 驱动
|
||||
configDatabaseName string // 库名
|
||||
dbDatabase *mongo.Database // 数据库
|
||||
}
|
||||
|
||||
// Database 选择数据库
|
||||
func (c *MongoClient) Database(name string, opts ...*options.DatabaseOptions) *MongoDatabaseOptions {
|
||||
return &MongoDatabaseOptions{
|
||||
db: c.db, // 驱动
|
||||
configDatabaseName: c.configDatabaseName, // 库名
|
||||
dbDatabase: c.db.Database(name, opts...), // 数据库
|
||||
}
|
||||
}
|
||||
|
||||
// CreateCollection 创建集合
|
||||
func (cd *MongoDatabaseOptions) CreateCollection(ctx context.Context, name string, opts ...*options.CreateCollectionOptions) error {
|
||||
return cd.dbDatabase.CreateCollection(ctx, name, opts...)
|
||||
}
|
||||
|
||||
// CreateTimeSeriesCollection 创建时间序列集合
|
||||
func (cd *MongoDatabaseOptions) CreateTimeSeriesCollection(ctx context.Context, name string, timeField string) error {
|
||||
return cd.dbDatabase.CreateCollection(ctx, name, options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField(timeField)))
|
||||
}
|
||||
|
||||
// Model 传入模型自动获取库名和表名
|
||||
// https://studygolang.com/articles/896
|
||||
// DatabaseName 库名
|
||||
// CollectionName 集合名
|
||||
func (c *MongoClient) Model(value interface{}) *MongoCollectionOptions {
|
||||
|
||||
var databaseOptions *MongoDatabaseOptions
|
||||
var collectionOptions *MongoCollectionOptions
|
||||
|
||||
val := reflect.ValueOf(value)
|
||||
|
||||
methodDatabaseNameValue := val.MethodByName("DatabaseName")
|
||||
if methodDatabaseNameValue.IsValid() {
|
||||
databaseName := methodDatabaseNameValue.Call(nil)[0].String()
|
||||
databaseOptions = c.Database(databaseName)
|
||||
} else {
|
||||
databaseOptions = c.Database(c.configDatabaseName)
|
||||
}
|
||||
|
||||
methodCollectionNameValue := val.MethodByName("CollectionName")
|
||||
if methodCollectionNameValue.IsValid() {
|
||||
collectionName := methodCollectionNameValue.Call(nil)[0].String()
|
||||
collectionOptions = databaseOptions.Collection(collectionName)
|
||||
} else {
|
||||
methodTableNameValue := val.MethodByName("TableName")
|
||||
if methodTableNameValue.IsValid() {
|
||||
collectionName := methodTableNameValue.Call(nil)[0].String()
|
||||
collectionOptions = databaseOptions.Collection(collectionName)
|
||||
} else {
|
||||
panic(NoConfigCollectionName)
|
||||
}
|
||||
}
|
||||
|
||||
return collectionOptions
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package dorm
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
NoConfigDatabaseName = errors.New("没有配置库名")
|
||||
NoConfigCollectionName = errors.New("没有配置集合名")
|
||||
)
|
@ -1 +0,0 @@
|
||||
package dorm
|
@ -0,0 +1,59 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"log"
|
||||
)
|
||||
|
||||
type MongoSessionOptions struct {
|
||||
db *mongo.Client // 驱动
|
||||
configDatabaseName string // 库名
|
||||
session mongo.Session // 会话
|
||||
sessionContext mongo.SessionContext // 会话上下文
|
||||
}
|
||||
|
||||
// Begin 开始事务,会同时创建开始会话需要在退出时关闭会话
|
||||
func (c *MongoClient) Begin() *MongoSessionOptions {
|
||||
|
||||
var ctx = context.TODO()
|
||||
var err error
|
||||
ms := &MongoSessionOptions{}
|
||||
|
||||
ms.db = c.GetDb()
|
||||
ms.configDatabaseName = c.configDatabaseName
|
||||
|
||||
// 开始会话
|
||||
ms.session, err = ms.db.StartSession()
|
||||
if err != nil {
|
||||
log.Println("开始会话异常:", err)
|
||||
}
|
||||
|
||||
// 会话上下文
|
||||
ms.sessionContext = mongo.NewSessionContext(ctx, ms.session)
|
||||
|
||||
// 会话开启事务
|
||||
err = ms.session.StartTransaction()
|
||||
|
||||
return ms
|
||||
}
|
||||
|
||||
// Rollback 回滚事务
|
||||
func (cs *MongoSessionOptions) Rollback() {
|
||||
var ctx = context.TODO()
|
||||
err := cs.session.AbortTransaction(ctx)
|
||||
if err != nil {
|
||||
log.Println("回滚事务异常:", err)
|
||||
}
|
||||
cs.session.EndSession(ctx)
|
||||
}
|
||||
|
||||
// Commit 提交事务
|
||||
func (cs *MongoSessionOptions) Commit() {
|
||||
var ctx = context.TODO()
|
||||
err := cs.session.CommitTransaction(ctx)
|
||||
if err != nil {
|
||||
log.Println("提交事务异常:", err)
|
||||
}
|
||||
cs.session.EndSession(ctx)
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type MongoSessionCollectionOptions struct {
|
||||
db *mongo.Client // 驱动
|
||||
configDatabaseName string // 库名
|
||||
session mongo.Session // 会话
|
||||
sessionContext mongo.SessionContext // 会话上下文
|
||||
dbCollection *mongo.Collection // 集合
|
||||
}
|
||||
|
||||
// Collection 选择集合
|
||||
func (csd *MongoSessionDatabaseOptions) Collection(name string, opts ...*options.CollectionOptions) *MongoSessionCollectionOptions {
|
||||
return &MongoSessionCollectionOptions{
|
||||
db: csd.db, // 驱动
|
||||
configDatabaseName: csd.configDatabaseName, // 库名
|
||||
session: csd.session, // 会话
|
||||
sessionContext: csd.sessionContext, // 会话上下文
|
||||
dbCollection: csd.dbDatabase.Collection(name, opts...), // 集合
|
||||
}
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// InsertOne 插入一个文档
|
||||
func (csc *MongoSessionCollectionOptions) InsertOne(document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
|
||||
return csc.dbCollection.InsertOne(csc.sessionContext, document, opts...)
|
||||
}
|
||||
|
||||
// InsertMany 插入多个文档
|
||||
func (csc *MongoSessionCollectionOptions) InsertMany(document []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) {
|
||||
return csc.dbCollection.InsertMany(csc.sessionContext, document, opts...)
|
||||
}
|
||||
|
||||
// DeleteOne 删除一个文档
|
||||
func (csc *MongoSessionCollectionOptions) DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
||||
return csc.dbCollection.DeleteOne(csc.sessionContext, filter, opts...)
|
||||
}
|
||||
|
||||
// DeleteMany 删除多个文档
|
||||
func (csc *MongoSessionCollectionOptions) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) {
|
||||
return csc.dbCollection.DeleteMany(csc.sessionContext, filter, opts...)
|
||||
}
|
||||
|
||||
// UpdateByID 按ID更新
|
||||
func (csc *MongoSessionCollectionOptions) UpdateByID(id interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return csc.dbCollection.UpdateByID(csc.sessionContext, id, update, opts...)
|
||||
}
|
||||
|
||||
// UpdateOne 更新一个文档
|
||||
func (csc *MongoSessionCollectionOptions) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return csc.dbCollection.UpdateOne(csc.sessionContext, filter, update, opts...)
|
||||
}
|
||||
|
||||
// UpdateMany 更新多个文档
|
||||
func (csc *MongoSessionCollectionOptions) UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
|
||||
return csc.dbCollection.UpdateMany(csc.sessionContext, filter, update, opts...)
|
||||
}
|
||||
|
||||
// ReplaceOne 替换一个文档
|
||||
func (csc *MongoSessionCollectionOptions) ReplaceOne(filter interface{}, update interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error) {
|
||||
return csc.dbCollection.ReplaceOne(csc.sessionContext, filter, update, opts...)
|
||||
}
|
||||
|
||||
// Aggregate 统计分析
|
||||
func (csc *MongoSessionCollectionOptions) Aggregate(pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error) {
|
||||
return csc.dbCollection.Aggregate(csc.sessionContext, pipeline, opts...)
|
||||
}
|
||||
|
||||
// CountDocuments 计数文档
|
||||
func (csc *MongoSessionCollectionOptions) CountDocuments(filter interface{}, opts ...*options.CountOptions) (int64, error) {
|
||||
return csc.dbCollection.CountDocuments(csc.sessionContext, filter, opts...)
|
||||
}
|
||||
|
||||
// EstimatedDocumentCount 估计文档计数
|
||||
func (csc *MongoSessionCollectionOptions) EstimatedDocumentCount(opts ...*options.EstimatedDocumentCountOptions) (int64, error) {
|
||||
return csc.dbCollection.EstimatedDocumentCount(csc.sessionContext, opts...)
|
||||
}
|
||||
|
||||
func (csc *MongoSessionCollectionOptions) Distinct(fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error) {
|
||||
return csc.dbCollection.Distinct(csc.sessionContext, fieldName, filter, opts...)
|
||||
}
|
||||
|
||||
// Find 查询多个文档
|
||||
func (csc *MongoSessionCollectionOptions) Find(filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) {
|
||||
return csc.dbCollection.Find(csc.sessionContext, filter, opts...)
|
||||
}
|
||||
|
||||
// FindOne 查询一个文档
|
||||
func (csc *MongoSessionCollectionOptions) FindOne(filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult {
|
||||
return csc.dbCollection.FindOne(csc.sessionContext, filter, opts...)
|
||||
}
|
||||
|
||||
func (csc *MongoSessionCollectionOptions) FindOneAndDelete(filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult {
|
||||
return csc.dbCollection.FindOneAndDelete(csc.sessionContext, filter, opts...)
|
||||
}
|
||||
|
||||
func (csc *MongoSessionCollectionOptions) FindOneAndReplace(filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult {
|
||||
return csc.dbCollection.FindOneAndReplace(csc.sessionContext, filter, replacement, opts...)
|
||||
}
|
||||
|
||||
func (csc *MongoSessionCollectionOptions) FindOneAndUpdate(filter interface{}, replacement interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult {
|
||||
return csc.dbCollection.FindOneAndUpdate(csc.sessionContext, filter, replacement, opts...)
|
||||
}
|
||||
|
||||
func (csc *MongoSessionCollectionOptions) Watch(pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) {
|
||||
return csc.dbCollection.Watch(csc.sessionContext, pipeline, opts...)
|
||||
}
|
||||
|
||||
func (csc *MongoSessionCollectionOptions) Indexes() mongo.IndexView {
|
||||
return csc.dbCollection.Indexes()
|
||||
}
|
||||
|
||||
func (csc *MongoSessionCollectionOptions) Drop() error {
|
||||
return csc.dbCollection.Drop(csc.sessionContext)
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type MongoSessionDatabaseOptions struct {
|
||||
db *mongo.Client // 驱动
|
||||
configDatabaseName string // 库名
|
||||
session mongo.Session // 会话
|
||||
sessionContext mongo.SessionContext // 会话上下文
|
||||
dbDatabase *mongo.Database // 数据库
|
||||
}
|
||||
|
||||
// Database 选择数据库
|
||||
func (cs *MongoSessionOptions) Database(name string, opts ...*options.DatabaseOptions) *MongoSessionDatabaseOptions {
|
||||
return &MongoSessionDatabaseOptions{
|
||||
db: cs.db, // 驱动
|
||||
configDatabaseName: cs.configDatabaseName, // 库名
|
||||
session: cs.session, // 会话
|
||||
sessionContext: cs.sessionContext, // 会话上下文
|
||||
dbDatabase: cs.db.Database(name, opts...), // 数据库
|
||||
}
|
||||
}
|
||||
|
||||
// Model 传入模型自动获取库名和表名
|
||||
// https://studygolang.com/articles/896
|
||||
// DatabaseName 库名
|
||||
// CollectionName 集合名
|
||||
func (cs *MongoSessionOptions) Model(value interface{}) *MongoSessionCollectionOptions {
|
||||
|
||||
var sessionDatabaseOptions *MongoSessionDatabaseOptions
|
||||
var sessionCollectionOptions *MongoSessionCollectionOptions
|
||||
|
||||
val := reflect.ValueOf(value)
|
||||
|
||||
methodDatabaseNameValue := val.MethodByName("DatabaseName")
|
||||
if methodDatabaseNameValue.IsValid() {
|
||||
databaseName := methodDatabaseNameValue.Call(nil)[0].String()
|
||||
sessionDatabaseOptions = cs.Database(databaseName)
|
||||
} else {
|
||||
sessionDatabaseOptions = cs.Database(cs.configDatabaseName)
|
||||
}
|
||||
|
||||
methodCollectionNameValue := val.MethodByName("CollectionName")
|
||||
if methodCollectionNameValue.IsValid() {
|
||||
collectionName := methodCollectionNameValue.Call(nil)[0].String()
|
||||
sessionCollectionOptions = sessionDatabaseOptions.Collection(collectionName)
|
||||
} else {
|
||||
methodTableNameValue := val.MethodByName("TableName")
|
||||
if methodTableNameValue.IsValid() {
|
||||
collectionName := methodTableNameValue.Call(nil)[0].String()
|
||||
sessionCollectionOptions = sessionDatabaseOptions.Collection(collectionName)
|
||||
} else {
|
||||
panic(NoConfigCollectionName)
|
||||
}
|
||||
}
|
||||
|
||||
return sessionCollectionOptions
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package dorm
|
||||
|
||||
import "go.mongodb.org/mongo-driver/mongo"
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (cs *MongoSessionOptions) GetDb() *mongo.Client {
|
||||
return cs.db
|
||||
}
|
||||
|
||||
// GetSession 获取会话
|
||||
func (cs *MongoSessionOptions) GetSession() mongo.Session {
|
||||
return cs.session
|
||||
}
|
||||
|
||||
// GetSessionContext 获取会话上下文
|
||||
func (cs *MongoSessionOptions) GetSessionContext() mongo.SessionContext {
|
||||
return cs.sessionContext
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import "reflect"
|
||||
|
||||
// Database 设置库名
|
||||
func (c *MongoClient) Database(databaseName string) *MongoClient {
|
||||
c.databaseName = databaseName
|
||||
return c
|
||||
}
|
||||
|
||||
// Collection 设置表名
|
||||
func (c *MongoClient) Collection(collectionName string) *MongoClient {
|
||||
c.collectionName = collectionName
|
||||
return c
|
||||
}
|
||||
|
||||
// Model 传入模型自动获取库名和表名
|
||||
func (c *MongoClient) Model(value interface{}) *MongoClient {
|
||||
// https://studygolang.com/articles/896
|
||||
val := reflect.ValueOf(value)
|
||||
if methodValue := val.MethodByName("Database"); methodValue.IsValid() {
|
||||
c.databaseName = methodValue.Call(nil)[0].String()
|
||||
}
|
||||
if methodValue := val.MethodByName("TableName"); methodValue.IsValid() {
|
||||
c.collectionName = methodValue.Call(nil)[0].String()
|
||||
}
|
||||
return c
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type MongoTransaction struct {
|
||||
startSession mongo.Session
|
||||
Session mongo.SessionContext
|
||||
db *mongo.Client // 驱动
|
||||
databaseName string // 库名
|
||||
collectionName string // 表名
|
||||
}
|
||||
|
||||
// Begin 开始事务,会同时创建开始会话需要在退出时关闭会话
|
||||
func (c *MongoClient) Begin() (ms MongoTransaction, err error) {
|
||||
|
||||
ms.db = c.Db
|
||||
|
||||
// 开始会话
|
||||
ms.startSession, err = ms.db.StartSession()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 会话上下文
|
||||
ms.Session = mongo.NewSessionContext(context.Background(), ms.startSession)
|
||||
|
||||
// 会话开启事务
|
||||
err = ms.startSession.StartTransaction()
|
||||
return ms, err
|
||||
}
|
||||
|
||||
// Close 关闭会话
|
||||
func (ms *MongoTransaction) Close() {
|
||||
ms.startSession.EndSession(context.TODO())
|
||||
}
|
||||
|
||||
// Rollback 回滚事务
|
||||
func (ms *MongoTransaction) Rollback() error {
|
||||
return ms.startSession.AbortTransaction(context.Background())
|
||||
}
|
||||
|
||||
// Commit 提交事务
|
||||
func (ms *MongoTransaction) Commit() error {
|
||||
return ms.startSession.CommitTransaction(context.Background())
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package dorm
|
||||
|
||||
// 获取库名
|
||||
func (ms *MongoTransaction) getDatabaseName() string {
|
||||
return ms.databaseName
|
||||
}
|
||||
|
||||
// 获取表名
|
||||
func (ms *MongoTransaction) getCollectionName() string {
|
||||
return ms.collectionName
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import "reflect"
|
||||
|
||||
// Database 设置库名
|
||||
func (ms *MongoTransaction) Database(databaseName string) *MongoTransaction {
|
||||
ms.databaseName = databaseName
|
||||
return ms
|
||||
}
|
||||
|
||||
// Collection 设置表名
|
||||
func (ms *MongoTransaction) Collection(collectionName string) *MongoTransaction {
|
||||
ms.collectionName = collectionName
|
||||
return ms
|
||||
}
|
||||
|
||||
// Model 传入模型自动获取库名和表名
|
||||
func (ms *MongoTransaction) Model(value interface{}) *MongoTransaction {
|
||||
// https://studygolang.com/articles/896
|
||||
val := reflect.ValueOf(value)
|
||||
if methodValue := val.MethodByName("Database"); methodValue.IsValid() {
|
||||
ms.databaseName = methodValue.Call(nil)[0].String()
|
||||
}
|
||||
if methodValue := val.MethodByName("TableName"); methodValue.IsValid() {
|
||||
ms.collectionName = methodValue.Call(nil)[0].String()
|
||||
}
|
||||
return ms
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RedisClient6
|
||||
// https://redis.uptrace.dev/
|
||||
type RedisClient6 struct {
|
||||
Db *redis.Client // 驱动
|
||||
config *ConfigRedisClient // 配置
|
||||
}
|
||||
|
||||
// NewRedisClient6 Redis 6
|
||||
func NewRedisClient6(config *ConfigRedisClient) (*RedisClient6, error) {
|
||||
|
||||
c := &RedisClient6{}
|
||||
c.config = config
|
||||
if c.config.PoolSize == 0 {
|
||||
c.config.PoolSize = 100
|
||||
}
|
||||
|
||||
c.Db = redis.NewClient(&redis.Options{
|
||||
Addr: c.config.Addr, // 地址
|
||||
Password: c.config.Password, // 密码
|
||||
DB: c.config.DB, // 数据库
|
||||
PoolSize: c.config.PoolSize, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := c.Db.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *RedisClient6) GetDb() *redis.Client {
|
||||
return c.Db
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-redis/redis/v9"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RedisClient7
|
||||
// https://redis.uptrace.dev/
|
||||
type RedisClient7 struct {
|
||||
Db *redis.Client // 驱动
|
||||
config *ConfigRedisClient // 配置
|
||||
}
|
||||
|
||||
// NewRedisClient7 Redis 7
|
||||
func NewRedisClient7(config *ConfigRedisClient) (*RedisClient7, error) {
|
||||
|
||||
c := &RedisClient7{}
|
||||
c.config = config
|
||||
if c.config.PoolSize == 0 {
|
||||
c.config.PoolSize = 100
|
||||
}
|
||||
|
||||
c.Db = redis.NewClient(&redis.Options{
|
||||
Addr: c.config.Addr, // 地址
|
||||
Password: c.config.Password, // 密码
|
||||
DB: c.config.DB, // 数据库
|
||||
PoolSize: c.config.PoolSize, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := c.Db.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *RedisClient7) GetDb() *redis.Client {
|
||||
return c.Db
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Subscribe 订阅channel
|
||||
func (r *RedisClient) Subscribe(ctx context.Context, channels ...string) *redis.PubSub {
|
||||
return r.db.Subscribe(ctx, channels...)
|
||||
}
|
||||
|
||||
// PSubscribe 订阅channel支持通配符匹配
|
||||
func (r *RedisClient) PSubscribe(ctx context.Context, channels ...string) *redis.PubSub {
|
||||
return r.db.PSubscribe(ctx, channels...)
|
||||
}
|
||||
|
||||
// Publish 将信息发送到指定的channel
|
||||
func (r *RedisClient) Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd {
|
||||
return r.db.Publish(ctx, channel, message)
|
||||
}
|
||||
|
||||
// PubSubChannels 查询活跃的channel
|
||||
func (r *RedisClient) PubSubChannels(ctx context.Context, pattern string) *redis.StringSliceCmd {
|
||||
return r.db.PubSubChannels(ctx, pattern)
|
||||
}
|
||||
|
||||
// PubSubNumSub 查询指定的channel有多少个订阅者
|
||||
func (r *RedisClient) PubSubNumSub(ctx context.Context, channels ...string) *redis.MapStringIntCmd {
|
||||
return r.db.PubSubNumSub(ctx, channels...)
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package dorm
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// RedisKeysNotFound keys没有数据
|
||||
RedisKeysNotFound = errors.New("ERR wrong number of arguments for 'mget' command")
|
||||
)
|
@ -0,0 +1,8 @@
|
||||
package dorm
|
||||
|
||||
import "github.com/redis/go-redis/v9"
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (r *RedisClient) GetDb() *redis.Client {
|
||||
return r.db
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RedisClientLock https://github.com/go-redis/redis
|
||||
type RedisClientLock struct {
|
||||
operation *RedisClient // 操作
|
||||
}
|
||||
|
||||
// NewLock 实例化锁
|
||||
func (r *RedisClient) NewLock() *RedisClientLock {
|
||||
return &RedisClientLock{r}
|
||||
}
|
||||
|
||||
// Lock 上锁
|
||||
// key 锁名
|
||||
// val 锁内容
|
||||
// ttl 锁过期时间
|
||||
func (rl *RedisClientLock) Lock(ctx context.Context, key string, val string, ttl time.Duration) (resp string, err error) {
|
||||
if ttl <= 0 {
|
||||
return resp, errors.New("长期请使用 LockForever 方法")
|
||||
}
|
||||
// 获取
|
||||
get, err := rl.operation.Get(ctx, key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
// 设置
|
||||
err = rl.operation.Set(ctx, key, val, ttl).Err()
|
||||
if err != nil {
|
||||
return resp, errors.New(fmt.Sprintf("上锁失败:%s", err.Error()))
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
if get != "" {
|
||||
return resp, errors.New("上锁失败,已存在")
|
||||
}
|
||||
return resp, errors.New(fmt.Sprintf("获取异常:%s", err.Error()))
|
||||
}
|
||||
|
||||
// Unlock 解锁
|
||||
// key 锁名
|
||||
func (rl *RedisClientLock) Unlock(ctx context.Context, key string) error {
|
||||
_, err := rl.operation.Del(ctx, key).Result()
|
||||
if err != nil {
|
||||
return errors.New(fmt.Sprintf("解锁失败:%s", err.Error()))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// LockForever 永远上锁
|
||||
// key 锁名
|
||||
// val 锁内容
|
||||
func (rl *RedisClientLock) LockForever(ctx context.Context, key string, val string) (resp string, err error) {
|
||||
// 获取
|
||||
get, err := rl.operation.Get(ctx, key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
// 设置
|
||||
err = rl.operation.Set(ctx, key, val, 0).Err()
|
||||
if err != nil {
|
||||
return resp, errors.New(fmt.Sprintf("上锁失败:%s", err.Error()))
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
if get != "" {
|
||||
return resp, errors.New("上锁失败,已存在")
|
||||
}
|
||||
return resp, errors.New(fmt.Sprintf("获取异常:%s", err.Error()))
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package dorm
|
||||
|
||||
// Bool 复制 bool 对象,并返回复制体
|
||||
func Bool(b bool) bool {
|
||||
return b
|
||||
}
|
||||
|
||||
// Uint8 复制 int8 对象,并返回复制体
|
||||
func Uint8(ui uint8) uint8 {
|
||||
return ui
|
||||
}
|
||||
|
||||
// Uint16 复制 uint16 对象,并返回复制体
|
||||
func Uint16(ui uint16) uint16 {
|
||||
return ui
|
||||
}
|
||||
|
||||
// Uint32 复制 uint32 对象,并返回复制体
|
||||
func Uint32(ui uint32) uint32 {
|
||||
return ui
|
||||
}
|
||||
|
||||
// Uint64 复制 uint64 对象,并返回复制体
|
||||
func Uint64(ui uint64) uint64 {
|
||||
return ui
|
||||
}
|
||||
|
||||
// Int8 复制 int8 对象,并返回复制体
|
||||
func Int8(i int8) int8 {
|
||||
return i
|
||||
}
|
||||
|
||||
// Int16 复制 int16 对象,并返回复制体
|
||||
func Int16(i int16) int16 {
|
||||
return i
|
||||
}
|
||||
|
||||
// Int32 复制 int64 对象,并返回复制体
|
||||
func Int32(i int32) int32 {
|
||||
return i
|
||||
}
|
||||
|
||||
// Int64 复制 int64 对象,并返回复制体
|
||||
func Int64(i int64) int64 {
|
||||
return i
|
||||
}
|
||||
|
||||
// Float32 复制 float32 对象,并返回复制体
|
||||
func Float32(f float32) float32 {
|
||||
return f
|
||||
}
|
||||
|
||||
// Float64 复制 float64 对象,并返回复制体
|
||||
func Float64(f float64) float64 {
|
||||
return f
|
||||
}
|
||||
|
||||
// String 复制 string 对象,并返回复制体
|
||||
func String(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
// Int 复制 int 对象,并返回复制体
|
||||
func Int(i int) int {
|
||||
return i
|
||||
}
|
||||
|
||||
// Uint 复制 uint 对象,并返回复制体
|
||||
func Uint(ui uint) uint {
|
||||
return ui
|
||||
}
|
||||
|
||||
// Any 复制 any 对象,并返回复制体
|
||||
func Any(a any) any {
|
||||
return a
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import "github.com/upper/db/v4"
|
||||
|
||||
// UpperClient
|
||||
// https://upper.io/
|
||||
type UpperClient struct {
|
||||
Db *db.Session // 驱动
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/upper/db/v4/adapter/cockroachdb"
|
||||
)
|
||||
|
||||
func NewUpperCockroachdbClient(settings cockroachdb.ConnectionURL) (*UpperClient, error) {
|
||||
|
||||
var err error
|
||||
c := &UpperClient{}
|
||||
|
||||
sess, err := cockroachdb.Open(settings)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
defer sess.Close()
|
||||
|
||||
c.Db = &sess
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/upper/db/v4/adapter/mongo"
|
||||
)
|
||||
|
||||
func NewUpperMongodbClient(settings mongo.ConnectionURL) (*UpperClient, error) {
|
||||
|
||||
var err error
|
||||
c := &UpperClient{}
|
||||
|
||||
sess, err := mongo.Open(settings)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
defer sess.Close()
|
||||
|
||||
c.Db = &sess
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/upper/db/v4/adapter/mssql"
|
||||
)
|
||||
|
||||
func NewUpperMssqlClient(settings mssql.ConnectionURL) (*UpperClient, error) {
|
||||
|
||||
var err error
|
||||
c := &UpperClient{}
|
||||
|
||||
sess, err := mssql.Open(settings)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
defer sess.Close()
|
||||
|
||||
c.Db = &sess
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/upper/db/v4/adapter/mysql"
|
||||
)
|
||||
|
||||
func NewUpperMysqlClient(settings mysql.ConnectionURL) (*UpperClient, error) {
|
||||
|
||||
var err error
|
||||
c := &UpperClient{}
|
||||
|
||||
sess, err := mysql.Open(settings)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
defer sess.Close()
|
||||
|
||||
c.Db = &sess
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/upper/db/v4/adapter/postgresql"
|
||||
)
|
||||
|
||||
func NewUpperPostgresqlClient(settings postgresql.ConnectionURL) (*UpperClient, error) {
|
||||
|
||||
var err error
|
||||
c := &UpperClient{}
|
||||
|
||||
sess, err := postgresql.Open(settings)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
defer sess.Close()
|
||||
|
||||
c.Db = &sess
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/upper/db/v4/adapter/ql"
|
||||
)
|
||||
|
||||
func NewUpperQlClient(settings ql.ConnectionURL) (*UpperClient, error) {
|
||||
|
||||
var err error
|
||||
c := &UpperClient{}
|
||||
|
||||
sess, err := ql.Open(settings)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
defer sess.Close()
|
||||
|
||||
c.Db = &sess
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
package dorm
|
||||
|
||||
const Version = "1.0.9"
|
||||
// Version 版本
|
||||
const Version = "1.0.59"
|
||||
|
@ -1,16 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type ConfigXormClient struct {
|
||||
Dns string // 地址
|
||||
}
|
||||
|
||||
// XormClient
|
||||
// https://xorm.io/
|
||||
type XormClient struct {
|
||||
Db *xorm.Engine // 驱动
|
||||
config *ConfigXormClient // 配置
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func NewXormMysqlClient(config *ConfigXormClient) (*XormClient, error) {
|
||||
|
||||
var err error
|
||||
c := &XormClient{config: config}
|
||||
|
||||
c.Db, err = xorm.NewEngine("mysql", c.config.Dns)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func NewXormPostgresClient(config *ConfigXormClient) (*XormClient, error) {
|
||||
|
||||
var err error
|
||||
c := &XormClient{config: config}
|
||||
|
||||
c.Db, err = xorm.NewEngine("postgres", c.config.Dns)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// XormClientSession https://xorm.io/zh/docs/chapter-10/readme/
|
||||
type XormClientSession struct {
|
||||
*xorm.Session
|
||||
}
|
||||
|
||||
// Begin 开始事务,需要创建 Session 对象
|
||||
//func (c *XormClient) Begin() (*XormClientSession, error) {
|
||||
// session := c.Db.NewSession()
|
||||
// defer session.Close()
|
||||
// return &session, session.Begin()
|
||||
//}
|
||||
|
||||
// Rollback 回滚事务
|
||||
//func (c *XormClientSession) Rollback() error {
|
||||
// return c.Rollback()
|
||||
//}
|
||||
|
||||
// Commit 提交事务
|
||||
//func (c *XormClientSession) Commit() error {
|
||||
// return c.Commit()
|
||||
//}
|
@ -1,17 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"gitee.com/chunanyong/zorm"
|
||||
)
|
||||
|
||||
type ConfigZormClient struct {
|
||||
Dns string // 地址
|
||||
}
|
||||
|
||||
// ZormClient
|
||||
// https://zorm.cn/
|
||||
// https://www.yuque.com/u27016943/nrgi00
|
||||
type ZormClient struct {
|
||||
Db *zorm.DBDao // 驱动
|
||||
config *ConfigZormClient // 配置
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gitee.com/chunanyong/zorm"
|
||||
_ "github.com/mailru/go-clickhouse/v2"
|
||||
)
|
||||
|
||||
func NewZormClickhouseClient(config *ConfigZormClient) (*ZormClient, error) {
|
||||
|
||||
var err error
|
||||
c := &ZormClient{config: config}
|
||||
|
||||
c.Db, err = zorm.NewDBDao(&zorm.DataSourceConfig{
|
||||
DSN: c.config.Dns,
|
||||
DriverName: "chhttp", // 数据库驱动名称
|
||||
DBType: "clickhouse", // 数据库类型
|
||||
PrintSQL: true, // 是否打印sql
|
||||
MaxOpenConns: 0, // 数据库最大连接数,默认50
|
||||
MaxIdleConns: 0, // 数据库最大空闲连接数,默认50
|
||||
ConnMaxLifetimeSecond: 0, // 连接存活秒时间. 默认600(10分钟)后连接被销毁重建.
|
||||
// 避免数据库主动断开连接,造成死连接.MySQL默认wait_timeout 28800秒(8小时)
|
||||
DefaultTxOptions: nil, // 事务隔离级别的默认配置,默认为nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "gitee.com/chunanyong/dm"
|
||||
"gitee.com/chunanyong/zorm"
|
||||
)
|
||||
|
||||
func NewZormDmClient(config *ConfigZormClient) (*ZormClient, error) {
|
||||
|
||||
var err error
|
||||
c := &ZormClient{config: config}
|
||||
|
||||
c.Db, err = zorm.NewDBDao(&zorm.DataSourceConfig{
|
||||
DSN: c.config.Dns,
|
||||
DriverName: "dm", // 数据库驱动名称
|
||||
DBType: "dm", // 数据库类型
|
||||
PrintSQL: true, // 是否打印sql
|
||||
MaxOpenConns: 0, // 数据库最大连接数,默认50
|
||||
MaxIdleConns: 0, // 数据库最大空闲连接数,默认50
|
||||
ConnMaxLifetimeSecond: 0, // 连接存活秒时间. 默认600(10分钟)后连接被销毁重建.
|
||||
// 避免数据库主动断开连接,造成死连接.MySQL默认wait_timeout 28800秒(8小时)
|
||||
DefaultTxOptions: nil, // 事务隔离级别的默认配置,默认为nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1 +0,0 @@
|
||||
package dorm
|
@ -1 +0,0 @@
|
||||
package dorm
|
@ -1,31 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gitee.com/chunanyong/zorm"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func NewZormMysqlClient(config *ConfigZormClient) (*ZormClient, error) {
|
||||
|
||||
var err error
|
||||
c := &ZormClient{config: config}
|
||||
|
||||
c.Db, err = zorm.NewDBDao(&zorm.DataSourceConfig{
|
||||
DSN: c.config.Dns,
|
||||
DriverName: "mysql", // 数据库驱动名称
|
||||
DBType: "mysql", // 数据库类型
|
||||
PrintSQL: true, // 是否打印sql
|
||||
MaxOpenConns: 0, // 数据库最大连接数,默认50
|
||||
MaxIdleConns: 0, // 数据库最大空闲连接数,默认50
|
||||
ConnMaxLifetimeSecond: 0, // 连接存活秒时间. 默认600(10分钟)后连接被销毁重建.
|
||||
// 避免数据库主动断开连接,造成死连接.MySQL默认wait_timeout 28800秒(8小时)
|
||||
DefaultTxOptions: nil, // 事务隔离级别的默认配置,默认为nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("连接失败:%v", err))
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
@ -1 +0,0 @@
|
||||
package dorm
|
Loading…
Reference in new issue