feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,209 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dal
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"github.com/coze-dev/coze-studio/backend/api/model/conversation/common"
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/conversation"
"github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/entity"
"github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/internal/dal/model"
"github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/internal/dal/query"
"github.com/coze-dev/coze-studio/backend/infra/contract/idgen"
"github.com/coze-dev/coze-studio/backend/pkg/lang/slices"
)
type ConversationDAO struct {
idgen idgen.IDGenerator
db *gorm.DB
query *query.Query
}
func NewConversationDAO(db *gorm.DB, generator idgen.IDGenerator) *ConversationDAO {
return &ConversationDAO{
idgen: generator,
db: db,
query: query.Use(db),
}
}
func (dao *ConversationDAO) Create(ctx context.Context, msg *entity.Conversation) (*entity.Conversation, error) {
poData := dao.conversationDO2PO(ctx, msg)
ids, err := dao.idgen.GenMultiIDs(ctx, 2)
if err != nil {
return nil, err
}
poData.ID = ids[0]
poData.SectionID = ids[1]
err = dao.query.Conversation.WithContext(ctx).Create(poData)
if err != nil {
return nil, err
}
return dao.conversationPO2DO(ctx, poData), nil
}
func (dao *ConversationDAO) GetByID(ctx context.Context, id int64) (*entity.Conversation, error) {
poData, err := dao.query.Conversation.WithContext(ctx).Debug().Where(dao.query.Conversation.ID.Eq(id)).First()
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return dao.conversationPO2DO(ctx, poData), nil
}
func (dao *ConversationDAO) UpdateSection(ctx context.Context, id int64) (int64, error) {
updateColumn := make(map[string]interface{})
table := dao.query.Conversation
newSectionID, err := dao.idgen.GenID(ctx)
if err != nil {
return 0, err
}
updateColumn[table.SectionID.ColumnName().String()] = newSectionID
updateColumn[table.UpdatedAt.ColumnName().String()] = time.Now().UnixMilli()
_, err = dao.query.Conversation.WithContext(ctx).Where(dao.query.Conversation.ID.Eq(id)).UpdateColumns(updateColumn)
if err != nil {
return 0, err
}
return newSectionID, nil
}
func (dao *ConversationDAO) Delete(ctx context.Context, id int64) (int64, error) {
table := dao.query.Conversation
updateColumn := make(map[string]interface{})
updateColumn[table.UpdatedAt.ColumnName().String()] = time.Now().UnixMilli()
updateColumn[table.Status.ColumnName().String()] = conversation.ConversationStatusDeleted
updateRes, err := dao.query.Conversation.WithContext(ctx).Where(dao.query.Conversation.ID.Eq(id)).UpdateColumns(updateColumn)
if err != nil {
return 0, err
}
return updateRes.RowsAffected, err
}
func (dao *ConversationDAO) Get(ctx context.Context, userID int64, agentID int64, scene int32, connectorID int64) (*entity.Conversation, error) {
po, err := dao.query.Conversation.WithContext(ctx).Debug().
Where(dao.query.Conversation.CreatorID.Eq(userID)).
Where(dao.query.Conversation.AgentID.Eq(agentID)).
Where(dao.query.Conversation.Scene.Eq(scene)).
Where(dao.query.Conversation.ConnectorID.Eq(connectorID)).
Where(dao.query.Conversation.Status.Eq(int32(conversation.ConversationStatusNormal))).
Order(dao.query.Conversation.CreatedAt.Desc()).
First()
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return dao.conversationPO2DO(ctx, po), nil
}
func (dao *ConversationDAO) List(ctx context.Context, userID int64, agentID int64, connectorID int64, scene int32, limit int, page int) ([]*entity.Conversation, bool, error) {
var hasMore bool
do := dao.query.Conversation.WithContext(ctx).Debug()
do = do.Where(dao.query.Conversation.CreatorID.Eq(userID)).
Where(dao.query.Conversation.AgentID.Eq(agentID)).
Where(dao.query.Conversation.Scene.Eq(scene)).
Where(dao.query.Conversation.ConnectorID.Eq(connectorID))
do = do.Offset((page - 1) * limit)
if limit > 0 {
do = do.Limit(int(limit) + 1)
}
poList, err := do.Find()
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, hasMore, nil
}
if err != nil {
return nil, hasMore, err
}
if len(poList) == 0 {
return nil, hasMore, nil
}
if len(poList) > limit {
hasMore = true
return dao.conversationBatchPO2DO(ctx, poList[:(len(poList)-1)]), hasMore, nil
}
return dao.conversationBatchPO2DO(ctx, poList), hasMore, nil
}
func (dao *ConversationDAO) conversationDO2PO(ctx context.Context, conversation *entity.Conversation) *model.Conversation {
return &model.Conversation{
ID: conversation.ID,
SectionID: conversation.SectionID,
ConnectorID: conversation.ConnectorID,
AgentID: conversation.AgentID,
CreatorID: conversation.CreatorID,
Scene: int32(conversation.Scene),
Status: int32(conversation.Status),
Ext: conversation.Ext,
CreatedAt: time.Now().UnixMilli(),
UpdatedAt: time.Now().UnixMilli(),
}
}
func (dao *ConversationDAO) conversationPO2DO(ctx context.Context, c *model.Conversation) *entity.Conversation {
return &entity.Conversation{
ID: c.ID,
SectionID: c.SectionID,
ConnectorID: c.ConnectorID,
AgentID: c.AgentID,
CreatorID: c.CreatorID,
Scene: common.Scene(c.Scene),
Status: conversation.ConversationStatus(c.Status),
Ext: c.Ext,
CreatedAt: c.CreatedAt,
UpdatedAt: c.UpdatedAt,
}
}
func (dao *ConversationDAO) conversationBatchPO2DO(ctx context.Context, conversations []*model.Conversation) []*entity.Conversation {
return slices.Transform(conversations, func(c *model.Conversation) *entity.Conversation {
return &entity.Conversation{
ID: c.ID,
SectionID: c.SectionID,
ConnectorID: c.ConnectorID,
AgentID: c.AgentID,
CreatorID: c.CreatorID,
Scene: common.Scene(c.Scene),
Status: conversation.ConversationStatus(c.Status),
Ext: c.Ext,
CreatedAt: c.CreatedAt,
UpdatedAt: c.UpdatedAt,
}
})
}

View File

@@ -0,0 +1,26 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package model
const TableNameConversation = "conversation"
// Conversation 会话信息表
type Conversation struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键ID" json:"id"` // 主键ID
ConnectorID int64 `gorm:"column:connector_id;not null;comment:业务线 ID" json:"connector_id"` // 业务线 ID
AgentID int64 `gorm:"column:agent_id;not null;comment:agent_id" json:"agent_id"` // agent_id
Scene int32 `gorm:"column:scene;not null;comment:会话场景" json:"scene"` // 会话场景
SectionID int64 `gorm:"column:section_id;not null;comment:最新section_id" json:"section_id"` // 最新section_id
CreatorID int64 `gorm:"column:creator_id;comment:创建者id" json:"creator_id"` // 创建者id
Ext string `gorm:"column:ext;comment:扩展字段" json:"ext"` // 扩展字段
Status int32 `gorm:"column:status;not null;default:1;comment:status: 1-normal 2-deleted" json:"status"` // status: 1-normal 2-deleted
CreatedAt int64 `gorm:"column:created_at;not null;autoCreateTime:milli;comment:创建时间" json:"created_at"` // 创建时间
UpdatedAt int64 `gorm:"column:updated_at;not null;autoUpdateTime:milli;comment:更新时间" json:"updated_at"` // 更新时间
}
// TableName Conversation's table name
func (*Conversation) TableName() string {
return TableNameConversation
}

View File

@@ -0,0 +1,417 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package query
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gen"
"gorm.io/gen/field"
"gorm.io/plugin/dbresolver"
"github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/internal/dal/model"
)
func newConversation(db *gorm.DB, opts ...gen.DOOption) conversation {
_conversation := conversation{}
_conversation.conversationDo.UseDB(db, opts...)
_conversation.conversationDo.UseModel(&model.Conversation{})
tableName := _conversation.conversationDo.TableName()
_conversation.ALL = field.NewAsterisk(tableName)
_conversation.ID = field.NewInt64(tableName, "id")
_conversation.ConnectorID = field.NewInt64(tableName, "connector_id")
_conversation.AgentID = field.NewInt64(tableName, "agent_id")
_conversation.Scene = field.NewInt32(tableName, "scene")
_conversation.SectionID = field.NewInt64(tableName, "section_id")
_conversation.CreatorID = field.NewInt64(tableName, "creator_id")
_conversation.Ext = field.NewString(tableName, "ext")
_conversation.Status = field.NewInt32(tableName, "status")
_conversation.CreatedAt = field.NewInt64(tableName, "created_at")
_conversation.UpdatedAt = field.NewInt64(tableName, "updated_at")
_conversation.fillFieldMap()
return _conversation
}
// conversation 会话信息表
type conversation struct {
conversationDo
ALL field.Asterisk
ID field.Int64 // 主键ID
ConnectorID field.Int64 // 业务线 ID
AgentID field.Int64 // agent_id
Scene field.Int32 // 会话场景
SectionID field.Int64 // 最新section_id
CreatorID field.Int64 // 创建者id
Ext field.String // 扩展字段
Status field.Int32 // status: 1-normal 2-deleted
CreatedAt field.Int64 // 创建时间
UpdatedAt field.Int64 // 更新时间
fieldMap map[string]field.Expr
}
func (c conversation) Table(newTableName string) *conversation {
c.conversationDo.UseTable(newTableName)
return c.updateTableName(newTableName)
}
func (c conversation) As(alias string) *conversation {
c.conversationDo.DO = *(c.conversationDo.As(alias).(*gen.DO))
return c.updateTableName(alias)
}
func (c *conversation) updateTableName(table string) *conversation {
c.ALL = field.NewAsterisk(table)
c.ID = field.NewInt64(table, "id")
c.ConnectorID = field.NewInt64(table, "connector_id")
c.AgentID = field.NewInt64(table, "agent_id")
c.Scene = field.NewInt32(table, "scene")
c.SectionID = field.NewInt64(table, "section_id")
c.CreatorID = field.NewInt64(table, "creator_id")
c.Ext = field.NewString(table, "ext")
c.Status = field.NewInt32(table, "status")
c.CreatedAt = field.NewInt64(table, "created_at")
c.UpdatedAt = field.NewInt64(table, "updated_at")
c.fillFieldMap()
return c
}
func (c *conversation) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
_f, ok := c.fieldMap[fieldName]
if !ok || _f == nil {
return nil, false
}
_oe, ok := _f.(field.OrderExpr)
return _oe, ok
}
func (c *conversation) fillFieldMap() {
c.fieldMap = make(map[string]field.Expr, 10)
c.fieldMap["id"] = c.ID
c.fieldMap["connector_id"] = c.ConnectorID
c.fieldMap["agent_id"] = c.AgentID
c.fieldMap["scene"] = c.Scene
c.fieldMap["section_id"] = c.SectionID
c.fieldMap["creator_id"] = c.CreatorID
c.fieldMap["ext"] = c.Ext
c.fieldMap["status"] = c.Status
c.fieldMap["created_at"] = c.CreatedAt
c.fieldMap["updated_at"] = c.UpdatedAt
}
func (c conversation) clone(db *gorm.DB) conversation {
c.conversationDo.ReplaceConnPool(db.Statement.ConnPool)
return c
}
func (c conversation) replaceDB(db *gorm.DB) conversation {
c.conversationDo.ReplaceDB(db)
return c
}
type conversationDo struct{ gen.DO }
type IConversationDo interface {
gen.SubQuery
Debug() IConversationDo
WithContext(ctx context.Context) IConversationDo
WithResult(fc func(tx gen.Dao)) gen.ResultInfo
ReplaceDB(db *gorm.DB)
ReadDB() IConversationDo
WriteDB() IConversationDo
As(alias string) gen.Dao
Session(config *gorm.Session) IConversationDo
Columns(cols ...field.Expr) gen.Columns
Clauses(conds ...clause.Expression) IConversationDo
Not(conds ...gen.Condition) IConversationDo
Or(conds ...gen.Condition) IConversationDo
Select(conds ...field.Expr) IConversationDo
Where(conds ...gen.Condition) IConversationDo
Order(conds ...field.Expr) IConversationDo
Distinct(cols ...field.Expr) IConversationDo
Omit(cols ...field.Expr) IConversationDo
Join(table schema.Tabler, on ...field.Expr) IConversationDo
LeftJoin(table schema.Tabler, on ...field.Expr) IConversationDo
RightJoin(table schema.Tabler, on ...field.Expr) IConversationDo
Group(cols ...field.Expr) IConversationDo
Having(conds ...gen.Condition) IConversationDo
Limit(limit int) IConversationDo
Offset(offset int) IConversationDo
Count() (count int64, err error)
Scopes(funcs ...func(gen.Dao) gen.Dao) IConversationDo
Unscoped() IConversationDo
Create(values ...*model.Conversation) error
CreateInBatches(values []*model.Conversation, batchSize int) error
Save(values ...*model.Conversation) error
First() (*model.Conversation, error)
Take() (*model.Conversation, error)
Last() (*model.Conversation, error)
Find() ([]*model.Conversation, error)
FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Conversation, err error)
FindInBatches(result *[]*model.Conversation, batchSize int, fc func(tx gen.Dao, batch int) error) error
Pluck(column field.Expr, dest interface{}) error
Delete(...*model.Conversation) (info gen.ResultInfo, err error)
Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
Updates(value interface{}) (info gen.ResultInfo, err error)
UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
UpdateFrom(q gen.SubQuery) gen.Dao
Attrs(attrs ...field.AssignExpr) IConversationDo
Assign(attrs ...field.AssignExpr) IConversationDo
Joins(fields ...field.RelationField) IConversationDo
Preload(fields ...field.RelationField) IConversationDo
FirstOrInit() (*model.Conversation, error)
FirstOrCreate() (*model.Conversation, error)
FindByPage(offset int, limit int) (result []*model.Conversation, count int64, err error)
ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
Scan(result interface{}) (err error)
Returning(value interface{}, columns ...string) IConversationDo
UnderlyingDB() *gorm.DB
schema.Tabler
}
func (c conversationDo) Debug() IConversationDo {
return c.withDO(c.DO.Debug())
}
func (c conversationDo) WithContext(ctx context.Context) IConversationDo {
return c.withDO(c.DO.WithContext(ctx))
}
func (c conversationDo) ReadDB() IConversationDo {
return c.Clauses(dbresolver.Read)
}
func (c conversationDo) WriteDB() IConversationDo {
return c.Clauses(dbresolver.Write)
}
func (c conversationDo) Session(config *gorm.Session) IConversationDo {
return c.withDO(c.DO.Session(config))
}
func (c conversationDo) Clauses(conds ...clause.Expression) IConversationDo {
return c.withDO(c.DO.Clauses(conds...))
}
func (c conversationDo) Returning(value interface{}, columns ...string) IConversationDo {
return c.withDO(c.DO.Returning(value, columns...))
}
func (c conversationDo) Not(conds ...gen.Condition) IConversationDo {
return c.withDO(c.DO.Not(conds...))
}
func (c conversationDo) Or(conds ...gen.Condition) IConversationDo {
return c.withDO(c.DO.Or(conds...))
}
func (c conversationDo) Select(conds ...field.Expr) IConversationDo {
return c.withDO(c.DO.Select(conds...))
}
func (c conversationDo) Where(conds ...gen.Condition) IConversationDo {
return c.withDO(c.DO.Where(conds...))
}
func (c conversationDo) Order(conds ...field.Expr) IConversationDo {
return c.withDO(c.DO.Order(conds...))
}
func (c conversationDo) Distinct(cols ...field.Expr) IConversationDo {
return c.withDO(c.DO.Distinct(cols...))
}
func (c conversationDo) Omit(cols ...field.Expr) IConversationDo {
return c.withDO(c.DO.Omit(cols...))
}
func (c conversationDo) Join(table schema.Tabler, on ...field.Expr) IConversationDo {
return c.withDO(c.DO.Join(table, on...))
}
func (c conversationDo) LeftJoin(table schema.Tabler, on ...field.Expr) IConversationDo {
return c.withDO(c.DO.LeftJoin(table, on...))
}
func (c conversationDo) RightJoin(table schema.Tabler, on ...field.Expr) IConversationDo {
return c.withDO(c.DO.RightJoin(table, on...))
}
func (c conversationDo) Group(cols ...field.Expr) IConversationDo {
return c.withDO(c.DO.Group(cols...))
}
func (c conversationDo) Having(conds ...gen.Condition) IConversationDo {
return c.withDO(c.DO.Having(conds...))
}
func (c conversationDo) Limit(limit int) IConversationDo {
return c.withDO(c.DO.Limit(limit))
}
func (c conversationDo) Offset(offset int) IConversationDo {
return c.withDO(c.DO.Offset(offset))
}
func (c conversationDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IConversationDo {
return c.withDO(c.DO.Scopes(funcs...))
}
func (c conversationDo) Unscoped() IConversationDo {
return c.withDO(c.DO.Unscoped())
}
func (c conversationDo) Create(values ...*model.Conversation) error {
if len(values) == 0 {
return nil
}
return c.DO.Create(values)
}
func (c conversationDo) CreateInBatches(values []*model.Conversation, batchSize int) error {
return c.DO.CreateInBatches(values, batchSize)
}
// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (c conversationDo) Save(values ...*model.Conversation) error {
if len(values) == 0 {
return nil
}
return c.DO.Save(values)
}
func (c conversationDo) First() (*model.Conversation, error) {
if result, err := c.DO.First(); err != nil {
return nil, err
} else {
return result.(*model.Conversation), nil
}
}
func (c conversationDo) Take() (*model.Conversation, error) {
if result, err := c.DO.Take(); err != nil {
return nil, err
} else {
return result.(*model.Conversation), nil
}
}
func (c conversationDo) Last() (*model.Conversation, error) {
if result, err := c.DO.Last(); err != nil {
return nil, err
} else {
return result.(*model.Conversation), nil
}
}
func (c conversationDo) Find() ([]*model.Conversation, error) {
result, err := c.DO.Find()
return result.([]*model.Conversation), err
}
func (c conversationDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Conversation, err error) {
buf := make([]*model.Conversation, 0, batchSize)
err = c.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
defer func() { results = append(results, buf...) }()
return fc(tx, batch)
})
return results, err
}
func (c conversationDo) FindInBatches(result *[]*model.Conversation, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return c.DO.FindInBatches(result, batchSize, fc)
}
func (c conversationDo) Attrs(attrs ...field.AssignExpr) IConversationDo {
return c.withDO(c.DO.Attrs(attrs...))
}
func (c conversationDo) Assign(attrs ...field.AssignExpr) IConversationDo {
return c.withDO(c.DO.Assign(attrs...))
}
func (c conversationDo) Joins(fields ...field.RelationField) IConversationDo {
for _, _f := range fields {
c = *c.withDO(c.DO.Joins(_f))
}
return &c
}
func (c conversationDo) Preload(fields ...field.RelationField) IConversationDo {
for _, _f := range fields {
c = *c.withDO(c.DO.Preload(_f))
}
return &c
}
func (c conversationDo) FirstOrInit() (*model.Conversation, error) {
if result, err := c.DO.FirstOrInit(); err != nil {
return nil, err
} else {
return result.(*model.Conversation), nil
}
}
func (c conversationDo) FirstOrCreate() (*model.Conversation, error) {
if result, err := c.DO.FirstOrCreate(); err != nil {
return nil, err
} else {
return result.(*model.Conversation), nil
}
}
func (c conversationDo) FindByPage(offset int, limit int) (result []*model.Conversation, count int64, err error) {
result, err = c.Offset(offset).Limit(limit).Find()
if err != nil {
return
}
if size := len(result); 0 < limit && 0 < size && size < limit {
count = int64(size + offset)
return
}
count, err = c.Offset(-1).Limit(-1).Count()
return
}
func (c conversationDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = c.Count()
if err != nil {
return
}
err = c.Offset(offset).Limit(limit).Scan(result)
return
}
func (c conversationDo) Scan(result interface{}) (err error) {
return c.DO.Scan(result)
}
func (c conversationDo) Delete(models ...*model.Conversation) (result gen.ResultInfo, err error) {
return c.DO.Delete(models)
}
func (c *conversationDo) withDO(do gen.Dao) *conversationDo {
c.DO = *do.(*gen.DO)
return c
}

View File

@@ -0,0 +1,103 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package query
import (
"context"
"database/sql"
"gorm.io/gorm"
"gorm.io/gen"
"gorm.io/plugin/dbresolver"
)
var (
Q = new(Query)
Conversation *conversation
)
func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
*Q = *Use(db, opts...)
Conversation = &Q.Conversation
}
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
return &Query{
db: db,
Conversation: newConversation(db, opts...),
}
}
type Query struct {
db *gorm.DB
Conversation conversation
}
func (q *Query) Available() bool { return q.db != nil }
func (q *Query) clone(db *gorm.DB) *Query {
return &Query{
db: db,
Conversation: q.Conversation.clone(db),
}
}
func (q *Query) ReadDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Read))
}
func (q *Query) WriteDB() *Query {
return q.ReplaceDB(q.db.Clauses(dbresolver.Write))
}
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
return &Query{
db: db,
Conversation: q.Conversation.replaceDB(db),
}
}
type queryCtx struct {
Conversation IConversationDo
}
func (q *Query) WithContext(ctx context.Context) *queryCtx {
return &queryCtx{
Conversation: q.Conversation.WithContext(ctx),
}
}
func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
}
func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
tx := q.db.Begin(opts...)
return &QueryTx{Query: q.clone(tx), Error: tx.Error}
}
type QueryTx struct {
*Query
Error error
}
func (q *QueryTx) Commit() error {
return q.db.Commit().Error
}
func (q *QueryTx) Rollback() error {
return q.db.Rollback().Error
}
func (q *QueryTx) SavePoint(name string) error {
return q.db.SavePoint(name).Error
}
func (q *QueryTx) RollbackTo(name string) error {
return q.db.RollbackTo(name).Error
}