Compare commits
34 Commits
Author | SHA1 | Date |
---|---|---|
dtapps | 5f70e83104 | 3 weeks ago |
dtapps | 8dbbff4a27 | 1 month ago |
dtapps | 0ed080ee44 | 1 month ago |
dtapps | b99cbca10a | 2 months ago |
dtapps | 8238616ab5 | 2 months ago |
dtapps | b4cf941dd9 | 2 months ago |
dtapps | 1f3831a39c | 2 months ago |
dtapps | bb1ce11d98 | 3 months ago |
dtapps | 0799a47c4d | 3 months ago |
dtapps | 986c3d81aa | 5 months ago |
李光春 | ce71070f19 | 1 year 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 |
@ -1,9 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
type BeegoClient struct {
|
||||
Db orm.Ormer // 驱动
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func NewBeegoMysqlClient(dns string) (*BeegoClient, error) {
|
||||
|
||||
c := &BeegoClient{}
|
||||
|
||||
err := orm.RegisterDataBase("default", "mysql", dns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Db = orm.NewOrm()
|
||||
|
||||
return c, err
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func NewBeegoPostgresClient(dns string) (*BeegoClient, error) {
|
||||
|
||||
c := &BeegoClient{}
|
||||
|
||||
err := orm.RegisterDataBase("default", "postgres", dns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Db = orm.NewOrm()
|
||||
|
||||
return c, err
|
||||
}
|
@ -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,10 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (c *BunClient) GetDb() *bun.DB {
|
||||
return c.Db
|
||||
}
|
@ -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,87 +1,57 @@
|
||||
module go.dtapp.net/dorm
|
||||
|
||||
go 1.19
|
||||
go 1.22.1
|
||||
|
||||
require (
|
||||
github.com/basgys/goxml2json v1.1.0
|
||||
github.com/beego/beego/v2 v2.0.5
|
||||
github.com/go-redis/redis/v9 v9.0.0-beta.2
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/kamva/mgm/v3 v3.4.1
|
||||
github.com/lib/pq v1.10.6
|
||||
github.com/qiniu/qmgo v1.1.2
|
||||
github.com/rs/xid v1.4.0
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/upper/db/v4 v4.6.0
|
||||
github.com/uptrace/bun v1.1.8
|
||||
github.com/uptrace/bun/dialect/mysqldialect v1.1.8
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.8
|
||||
github.com/uptrace/bun/driver/pgdriver v1.1.8
|
||||
go.dtapp.net/gotime v1.0.5
|
||||
go.dtapp.net/goxml v1.0.1
|
||||
go.mongodb.org/mongo-driver v1.10.1
|
||||
gorm.io/driver/mysql v1.3.6
|
||||
gorm.io/driver/postgres v1.3.9
|
||||
gorm.io/gorm v1.23.8
|
||||
xorm.io/xorm v1.3.2
|
||||
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/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.11.0 // indirect
|
||||
github.com/goccy/go-json v0.9.11 // 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/hashicorp/golang-lru v0.5.4 // indirect
|
||||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
|
||||
github.com/jackc/pgconn v1.13.0 // indirect
|
||||
github.com/jackc/pgio v1.0.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
||||
github.com/jackc/pgtype v1.12.0 // indirect
|
||||
github.com/jackc/pgx/v4 v4.17.2 // 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/klauspost/compress v1.15.9 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible // 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/montanaflynn/stats v0.6.6 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/segmentio/fasthash v1.0.3 // indirect
|
||||
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
|
||||
github.com/stretchr/testify v1.8.0 // 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-20220829220503-c86fa9a7ed90 // indirect
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
|
||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
|
||||
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde // indirect
|
||||
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
golang.org/x/tools v0.1.11 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
lukechampine.com/uint128 v1.2.0 // indirect
|
||||
mellium.im/sasl v0.3.0 // indirect
|
||||
modernc.org/ccgo/v3 v3.16.7 // indirect
|
||||
modernc.org/libc v1.16.14 // indirect
|
||||
modernc.org/opt v0.1.3 // indirect
|
||||
modernc.org/sqlite v1.17.3 // indirect
|
||||
modernc.org/strutil v1.1.2 // indirect
|
||||
xorm.io/builder v0.3.12 // 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,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
|
||||
}
|
@ -1,8 +1,16 @@
|
||||
package dorm
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"database/sql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (c *GormClient) GetDb() *gorm.DB {
|
||||
return c.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,8 +1,8 @@
|
||||
package dorm
|
||||
|
||||
import "github.com/go-redis/redis/v9"
|
||||
import "github.com/redis/go-redis/v9"
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (r *RedisClient) GetDb() *redis.Client {
|
||||
return r.Db
|
||||
return r.db
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package dorm
|
@ -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,10 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"github.com/upper/db/v4"
|
||||
)
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (c *UpperClient) GetDb() *db.Session {
|
||||
return c.Db
|
||||
}
|
@ -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
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package dorm
|
||||
|
||||
// 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,10 +0,0 @@
|
||||
package dorm
|
||||
|
||||
import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// GetDb 获取驱动
|
||||
func (c *XormClient) GetDb() *xorm.Engine {
|
||||
return c.Db
|
||||
}
|
@ -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()
|
||||
//}
|
Loading…
Reference in new issue