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,91 @@
/*
* 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 entity
type DataType string
const (
TypeInt DataType = "INT"
TypeVarchar DataType = "VARCHAR"
TypeText DataType = "TEXT"
TypeBoolean DataType = "BOOLEAN"
TypeJson DataType = "JSON"
TypeTimestamp DataType = "TIMESTAMP"
TypeFloat DataType = "FLOAT"
TypeBigInt DataType = "BIGINT"
TypeDouble DataType = "DOUBLE"
)
type IndexType string
const (
PrimaryKey IndexType = "PRIMARY KEY"
UniqueKey IndexType = "UNIQUE KEY"
NormalKey IndexType = "KEY"
)
// AlterTableAction 定义修改表的动作类型
type AlterTableAction string
const (
AddColumn AlterTableAction = "ADD COLUMN"
DropColumn AlterTableAction = "DROP COLUMN"
ModifyColumn AlterTableAction = "MODIFY COLUMN"
RenameColumn AlterTableAction = "RENAME COLUMN"
AddIndex AlterTableAction = "ADD INDEX"
)
type LogicalOperator string
const (
AND LogicalOperator = "AND"
OR LogicalOperator = "OR"
)
type Operator string
const (
OperatorEqual Operator = "="
OperatorNotEqual Operator = "!="
OperatorGreater Operator = ">"
OperatorGreaterEqual Operator = ">="
OperatorLess Operator = "<"
OperatorLessEqual Operator = "<="
OperatorLike Operator = "LIKE"
OperatorNotLike Operator = "NOT LIKE"
OperatorIn Operator = "IN"
OperatorNotIn Operator = "NOT IN"
OperatorIsNull Operator = "IS NULL"
OperatorIsNotNull Operator = "IS NOT NULL"
)
type SortDirection string
const (
SortDirectionAsc SortDirection = "ASC" // 升序
SortDirectionDesc SortDirection = "DESC" // 降序
)
type SQLType int32
const (
SQLType_Parameterized SQLType = 0
SQLType_Raw SQLType = 1 // Complete/raw SQL
)

View File

@@ -0,0 +1,54 @@
/*
* 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 entity
type Column struct {
Name string // 保证唯一性
DataType DataType
Length *int
NotNull bool
DefaultValue *string
AutoIncrement bool // 表示该列是否为自动递增
Comment *string
}
type Index struct {
Name string
Type IndexType
Columns []string
}
type TableOption struct {
Collate *string
AutoIncrement *int64 // 设置表的自动递增初始值
Comment *string
}
type Table struct {
Name string // 保证唯一性
Columns []*Column
Indexes []*Index
Options *TableOption
CreatedAt int64
UpdatedAt int64
}
type ResultSet struct {
Columns []string
Rows []map[string]interface{}
AffectedRows int64
}

View File

@@ -0,0 +1,189 @@
/*
* 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 rdb
import (
"context"
"github.com/coze-dev/coze-studio/backend/infra/contract/rdb/entity"
)
//go:generate mockgen -destination ../../../internal/mock/infra/contract/rdb/rdb_mock.go --package rdb -source rdb.go
type RDB interface {
CreateTable(ctx context.Context, req *CreateTableRequest) (*CreateTableResponse, error)
AlterTable(ctx context.Context, req *AlterTableRequest) (*AlterTableResponse, error)
DropTable(ctx context.Context, req *DropTableRequest) (*DropTableResponse, error)
GetTable(ctx context.Context, req *GetTableRequest) (*GetTableResponse, error)
InsertData(ctx context.Context, req *InsertDataRequest) (*InsertDataResponse, error)
UpdateData(ctx context.Context, req *UpdateDataRequest) (*UpdateDataResponse, error)
DeleteData(ctx context.Context, req *DeleteDataRequest) (*DeleteDataResponse, error)
SelectData(ctx context.Context, req *SelectDataRequest) (*SelectDataResponse, error)
UpsertData(ctx context.Context, req *UpsertDataRequest) (*UpsertDataResponse, error)
ExecuteSQL(ctx context.Context, req *ExecuteSQLRequest) (*ExecuteSQLResponse, error)
}
// CreateTableRequest 创建表请求
type CreateTableRequest struct {
Table *entity.Table
}
// CreateTableResponse 创建表响应
type CreateTableResponse struct {
Table *entity.Table
}
// AlterTableOperation 修改表操作
type AlterTableOperation struct {
Action entity.AlterTableAction
Column *entity.Column
OldName *string
Index *entity.Index
IndexName *string
NewTableName *string
}
// AlterTableRequest 修改表请求
type AlterTableRequest struct {
TableName string
Operations []*AlterTableOperation
}
// AlterTableResponse 修改表响应
type AlterTableResponse struct {
Table *entity.Table
}
// DropTableRequest 删除表请求
type DropTableRequest struct {
TableName string
IfExists bool
}
// DropTableResponse 删除表响应
type DropTableResponse struct {
Success bool
}
// GetTableRequest 获取表信息请求
type GetTableRequest struct {
TableName string
}
// GetTableResponse 获取表信息响应
type GetTableResponse struct {
Table *entity.Table
}
// InsertDataRequest 插入数据请求
type InsertDataRequest struct {
TableName string
Data []map[string]interface{}
}
// InsertDataResponse 插入数据响应
type InsertDataResponse struct {
AffectedRows int64
}
// Condition 定义查询条件
type Condition struct {
Field string
Operator entity.Operator
Value interface{}
}
// ComplexCondition 复杂条件
type ComplexCondition struct {
Conditions []*Condition
NestedConditions []*ComplexCondition // 与 Conditions互斥 example: WHERE (age >= 18 AND status = 'active') OR (age >= 21 AND status = 'pending')
Operator entity.LogicalOperator
}
// UpdateDataRequest 更新数据请求
type UpdateDataRequest struct {
TableName string
Data map[string]interface{}
Where *ComplexCondition
Limit *int
}
// UpdateDataResponse 更新数据响应
type UpdateDataResponse struct {
AffectedRows int64
}
// DeleteDataRequest 删除数据请求
type DeleteDataRequest struct {
TableName string
Where *ComplexCondition
Limit *int
}
// DeleteDataResponse 删除数据响应
type DeleteDataResponse struct {
AffectedRows int64
}
type OrderBy struct {
Field string // 排序字段
Direction entity.SortDirection // 排序方向
}
// SelectDataRequest 查询数据请求
type SelectDataRequest struct {
TableName string
Fields []string // 要查询的字段,如果为空则查询所有字段
Where *ComplexCondition
OrderBy []*OrderBy // 排序条件
Limit *int // 限制返回行数
Offset *int // 偏移量
}
// SelectDataResponse 查询数据响应
type SelectDataResponse struct {
ResultSet *entity.ResultSet
Total int64 // 符合条件的总记录数(不考虑分页)
}
type UpsertDataRequest struct {
TableName string
Data []map[string]interface{} // 要更新或插入的数据
Keys []string // 用于标识唯一记录的列名,为空的话默认使用主键
}
type UpsertDataResponse struct {
AffectedRows int64 // 受影响的行数
InsertedRows int64 // 新插入的行数
UpdatedRows int64 // 更新的行数
UnchangedRows int64 // 不变的行数(没有插入或更新的行数)
}
// ExecuteSQLRequest 执行SQL请求
type ExecuteSQLRequest struct {
SQL string
Params []interface{} // 用于参数化查询
// SQLType indicates the type of SQL: parameterized or raw SQL. It takes effect if OperateType is 0.
SQLType entity.SQLType
}
// ExecuteSQLResponse 执行SQL响应
type ExecuteSQLResponse struct {
ResultSet *entity.ResultSet
}