svc_db

package module
v0.0.0-...-5fb5118 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

* db
生成规则请查看插件文档 [[https://github.com/walleframe/wplugins/blob/main/cmd/wdb/readme.org][wdb插件文档]] 
** 示例
*** 用户信息表
表定义 
#+begin_src protobuf
sql.db = "db_user"; // 定义当前文件所有表是在'db_user'数据库

message user_info {
	sql.pk = "uid" // Primary Key 
	sql.unique.name = "name" // name字段做唯一索引
	sql.index.email = "email" // email字段做索引
	int64 uid = 1 {
		sql.auto_incr = true  // 自增
	};
	string name = 2 {
		sql.size = 128
	};
	string email = 3;
}
#+end_src

生成代码调用
#+begin_src go
  var (
	  user *dbop.UserInfo
	  users []*dbop.UserInfo
	  err error
    uid int64 = 1
	  res sql.Result
	)
  // 插入信息
  res, err = db_user.UserInfoOP().Insert(ctx, user)
  uid, err := res.LastInsertId() //
  // 更新
  res, err = db_user.UserInfoOP().Update(ctx, user)
  // merge
  res, err = db_user.UserInfoOP().Upsert(ctx, user)

  // 查找指定用户
	user, err = db_user.UserInfoOP().Find(ctx, uid)
  // 通过索引查找
  users, err = db_user.UserInfoOP().FindByIndexEmail(ctx, "xxx@xx.com", 5, 0) // limit 0,5

  // 自定义数据查询
  users,err = db_user.UserInfoOP().Select(ctx, nil) // 查询全部数据
  users,err = db_user.UserInfoOP().Select(ctx, db_user.UserInfoOP().Where(32).Uid().LessEqual(1000)) // 查询uid小于1000的数据
#+end_src
接口示例(Ex结构体内包含 ModifyStamp,CreateStamp.):
#+begin_src go
type UserInfoKey = int64

type UserInfoOperation interface {
	Insert(ctx context.Context, data *dbop.UserInfo) (res sql.Result, err error)
	InsertMany(ctx context.Context, datas []*dbop.UserInfo) (res sql.Result, err error)

	Update(ctx context.Context, data *dbop.UserInfo) (res sql.Result, err error)
	Upsert(ctx context.Context, data *dbop.UserInfo) (res sql.Result, err error)
	UpsertMany(ctx context.Context, datas []*dbop.UserInfo) (res sql.Result, err error)

	Find(ctx context.Context, uid int64) (data *dbop.UserInfo, err error)
	FindEx(ctx context.Context, uid int64) (data *dbop.UserInfoEx, err error)
	Delete(ctx context.Context, uid int64) (res sql.Result, err error)

	FindByKey(ctx context.Context, id UserInfoKey) (data *dbop.UserInfo, err error)
	FindExByKey(ctx context.Context, id UserInfoKey) (data *dbop.UserInfoEx, err error)
	DeleteByKey(ctx context.Context, id UserInfoKey) (res sql.Result, err error)

	FindByKeyArray(ctx context.Context, ids []UserInfoKey) (datas []*dbop.UserInfo, err error)
	FindExByKeyArray(ctx context.Context, ids []UserInfoKey) (datas []*dbop.UserInfoEx, err error)
	DeleteByKeyArray(ctx context.Context, ids []UserInfoKey) (res sql.Result, err error)

	FindByIndexEmail(ctx context.Context, email string, limit, offset int) (datas []*dbop.UserInfo, err error)
	FindExByIndexEmail(ctx context.Context, email string, limit, offset int) (datas []*dbop.UserInfoEx, err error)
	CountByIndexEmail(ctx context.Context, email string) (count int, err error)
	DeleteByIndexEmail(ctx context.Context, email string) (res sql.Result, err error)

	FindByIndexName(ctx context.Context, name string, limit, offset int) (datas []*dbop.UserInfo, err error)
	FindExByIndexName(ctx context.Context, name string, limit, offset int) (datas []*dbop.UserInfoEx, err error)
	CountByIndexName(ctx context.Context, name string) (count int, err error)
	DeleteByIndexName(ctx context.Context, name string) (res sql.Result, err error)

	Where(bufSize int) *UserInfoWhereStmt
	Select(ctx context.Context, where *UserInfoWhereStmt) (datas []*dbop.UserInfo, err error)
	SelectEx(ctx context.Context, where *UserInfoWhereStmt) (datas []*dbop.UserInfoEx, err error)
	Count(ctx context.Context, where *UserInfoWhereStmt) (count int, err error)

	DeleteMany(ctx context.Context, where *UserInfoWhereStmt) (res sql.Result, err error)

	RangeAll(ctx context.Context, where *UserInfoWhereStmt, f func(ctx context.Context, row *dbop.UserInfo) bool) error
	RangeAllEx(ctx context.Context, where *UserInfoWhereStmt, f func(ctx context.Context, row *dbop.UserInfoEx) bool) error
	AllData(ctx context.Context, where *UserInfoWhereStmt) (datas []*dbop.UserInfo, err error)
	AllDataEx(ctx context.Context, where *UserInfoWhereStmt) (datas []*dbop.UserInfoEx, err error)

	// use for custom named sql
	DB() *sqlx.DB
}

// 数据库操作接口
var UserInfoOP = func() UserInfoOperation

// 自定义sql语句生成. 注意: 仅辅助生成sql语句.
func UserInfoNamedSQL(bufSize int) *UserInfoSQLWriter 

// 同步表字段,索引到数据库.(表不存在创建,已经存在,对比列,如果列不存在则创建,已经存在列,不会检查类型,需要业务方保证)
func SyncUserInfoDBTable(ctx context.Context, db *sqlx.DB) (err error)

// 结构体到Primary Key 转换
func UserInfoToPrimaryKeys(rows []*dbop.UserInfo) (ids []UserInfoKey)
func UserInfoExToPrimaryKeysEx(rows []*dbop.UserInfoEx) (ids []UserInfoKey)

// 手动创建
func NewUserInfoOperation(db *sqlx.DB) (_ *xUserInfoOperation, err error) 
#+end_src
*** 好友列表
#+begin_src protobuf
message user_friend {
	sql.pk = "uid,fid" // Primary Key 是 uid,fid 
	sql.index.uid = "uid" // 使用uid做索引 
	int64 uid = 1 ;
	int64 fid = 2 ;
	int8 state = 3;
}
#+end_src
生成接口 
#+begin_src go
type UserFriendKey struct {
	Uid int64
	Fid int64
}

type UserFriendOperation interface {
	Insert(ctx context.Context, data *dbop.UserFriend) (res sql.Result, err error)
	InsertMany(ctx context.Context, datas []*dbop.UserFriend) (res sql.Result, err error)

	Update(ctx context.Context, data *dbop.UserFriend) (res sql.Result, err error)
	Upsert(ctx context.Context, data *dbop.UserFriend) (res sql.Result, err error)
	UpsertMany(ctx context.Context, datas []*dbop.UserFriend) (res sql.Result, err error)

	Find(ctx context.Context, uid int64, fid int64) (data *dbop.UserFriend, err error)
	FindEx(ctx context.Context, uid int64, fid int64) (data *dbop.UserFriendEx, err error)
	Delete(ctx context.Context, uid int64, fid int64) (res sql.Result, err error)

	FindByKey(ctx context.Context, id UserFriendKey) (data *dbop.UserFriend, err error)
	FindExByKey(ctx context.Context, id UserFriendKey) (data *dbop.UserFriendEx, err error)
	DeleteByKey(ctx context.Context, id UserFriendKey) (res sql.Result, err error)

	FindByKeyArray(ctx context.Context, ids []UserFriendKey) (datas []*dbop.UserFriend, err error)
	FindExByKeyArray(ctx context.Context, ids []UserFriendKey) (datas []*dbop.UserFriendEx, err error)
	DeleteByKeyArray(ctx context.Context, ids []UserFriendKey) (res sql.Result, err error)

	FindByIndexUid(ctx context.Context, uid int64, limit, offset int) (datas []*dbop.UserFriend, err error)
	FindExByIndexUid(ctx context.Context, uid int64, limit, offset int) (datas []*dbop.UserFriendEx, err error)
	CountByIndexUid(ctx context.Context, uid int64) (count int, err error)
	DeleteByIndexUid(ctx context.Context, uid int64) (res sql.Result, err error)

	Where(bufSize int) *UserFriendWhereStmt
	Select(ctx context.Context, where *UserFriendWhereStmt) (datas []*dbop.UserFriend, err error)
	SelectEx(ctx context.Context, where *UserFriendWhereStmt) (datas []*dbop.UserFriendEx, err error)
	Count(ctx context.Context, where *UserFriendWhereStmt) (count int, err error)

	DeleteMany(ctx context.Context, where *UserFriendWhereStmt) (res sql.Result, err error)

	RangeAll(ctx context.Context, where *UserFriendWhereStmt, f func(ctx context.Context, row *dbop.UserFriend) bool) error
	RangeAllEx(ctx context.Context, where *UserFriendWhereStmt, f func(ctx context.Context, row *dbop.UserFriendEx) bool) error
	AllData(ctx context.Context, where *UserFriendWhereStmt) (datas []*dbop.UserFriend, err error)
	AllDataEx(ctx context.Context, where *UserFriendWhereStmt) (datas []*dbop.UserFriendEx, err error)

	// use for custom named sql
	DB() *sqlx.DB
}
#+end_src
 
*** 操作日志
#+begin_src protobuf
message user_xx_log {
	sql.engine = "MyISAM"
	sql.ex = false // 不生成扩展结构体
	sql.update = false // 不生成update方法
	sql.upsert = false // 不生成upsret方法
	int64 id = 1 {
		sql.auto_incr = true
      sql.pk = true
	}
	int64 uid = 2;
	int64 xx = 3;
	string x2 = 4;
	int64 create_stamp = 5{ // 记录写入时间
		sql.type = "timestamp default current_timestamp"
	}
}
#+end_src

#+begin_src go
type UserXxLogKey = int64

type UserXxLogOperation interface {
	Insert(ctx context.Context, data *dbop.UserXxLog) (res sql.Result, err error)
	InsertMany(ctx context.Context, datas []*dbop.UserXxLog) (res sql.Result, err error)

	Find(ctx context.Context, id int64) (data *dbop.UserXxLog, err error)
	Delete(ctx context.Context, id int64) (res sql.Result, err error)

	FindByKey(ctx context.Context, id UserXxLogKey) (data *dbop.UserXxLog, err error)
	DeleteByKey(ctx context.Context, id UserXxLogKey) (res sql.Result, err error)

	FindByKeyArray(ctx context.Context, ids []UserXxLogKey) (datas []*dbop.UserXxLog, err error)
	DeleteByKeyArray(ctx context.Context, ids []UserXxLogKey) (res sql.Result, err error)

	Where(bufSize int) *UserXxLogWhereStmt
	Select(ctx context.Context, where *UserXxLogWhereStmt) (datas []*dbop.UserXxLog, err error)
	Count(ctx context.Context, where *UserXxLogWhereStmt) (count int, err error)

	DeleteMany(ctx context.Context, where *UserXxLogWhereStmt) (res sql.Result, err error)

	RangeAll(ctx context.Context, where *UserXxLogWhereStmt, f func(ctx context.Context, row *dbop.UserXxLog) bool) error
	AllData(ctx context.Context, where *UserXxLogWhereStmt) (datas []*dbop.UserXxLog, err error)

	// use for custom named sql
	DB() *sqlx.DB
}
#+end_src
** 服务及配置初始化


Documentation

Index

Constants

View Source
const (
	Columns   = "__COLUMNS__"
	TableName = "__TABLE__"
)

Variables

View Source
var RegisterDB func(driver, db, tblName string, swapDB func(db *sqlx.DB) error) = svc.RegisterDB

RegisterDB 注册数据库相关接口

View Source
var RegisterSyncDBTable = func(driver, db, tblName string, syncFunc func(ctx context.Context, db *sqlx.DB) (err error)) {

}
View Source
var SyncTableColumnsAndIndex = true

SyncTableColumnsAndIndex 服务启动自动同步表字段级索引信息

Functions

func AnyFromAny

func AnyFromAny[T any](val T) any

func AnyFromMap

func AnyFromMap[K comparable, V any](val map[K]V) any

use for map

func AnyFromObject

func AnyFromObject[T any](val *T) any

use for struct

func AnyFromSlice

func AnyFromSlice[T any](val []T) any

use for map

func AnyFromStampInt64

func AnyFromStampInt64(val int64) any

func RawToBinary

func RawToBinary(val sql.RawBytes) ([]byte, error)

func RawToBool

func RawToBool(val sql.RawBytes) (bool, error)

func RawToFloat32

func RawToFloat32(val sql.RawBytes) (float32, error)

func RawToFloat64

func RawToFloat64(val sql.RawBytes) (float64, error)

func RawToInt16

func RawToInt16(val sql.RawBytes) (int16, error)

func RawToInt32

func RawToInt32(val sql.RawBytes) (int32, error)

func RawToInt64

func RawToInt64(val sql.RawBytes) (int64, error)

func RawToInt8

func RawToInt8(val sql.RawBytes) (int8, error)

func RawToMap

func RawToMap[K comparable, V any](val sql.RawBytes) (map[K]V, error)

use for map

func RawToObject

func RawToObject[T any](val sql.RawBytes) (*T, error)

use for struct

func RawToSlice

func RawToSlice[T any](val sql.RawBytes) ([]T, error)

use for map

func RawToStampInt64

func RawToStampInt64(val sql.RawBytes) (int64, error)

func RawToString

func RawToString(val sql.RawBytes) (string, error)

func RawToUint16

func RawToUint16(val sql.RawBytes) (uint16, error)

func RawToUint32

func RawToUint32(val sql.RawBytes) (uint32, error)

func RawToUint64

func RawToUint64(val sql.RawBytes) (uint64, error)

func RawToUint8

func RawToUint8(val sql.RawBytes) (uint8, error)

func SyncTableColumns

func SyncTableColumns(ctx context.Context, db *sqlx.DB, tableName string, createTable string, alterSql map[string]string) (err error)

SyncTableColumns sync table columns, do not check column type, only add missing columns.if table not exists, create it.

func SyncTableIndex

func SyncTableIndex(ctx context.Context, db *sqlx.DB, tableName string, alterSql map[string]string) (err error)

SyncTableIndex sync table index, create missing index.

Types

type DatabaseService

type DatabaseService struct {
	app.NoopService
	// contains filtered or unexported fields
}

DatabaseService mysql链接管理

func NewService

func NewService() *DatabaseService

func (*DatabaseService) Init

func (svc *DatabaseService) Init(s app.Stoper) (err error)

func (*DatabaseService) Name

func (svc *DatabaseService) Name() string

func (*DatabaseService) RegisterDB

func (svc *DatabaseService) RegisterDB(driver, db, tblName string, swapDB func(db *sqlx.DB) error)

func (*DatabaseService) Stop

func (svc *DatabaseService) Stop()

type EmptyResult

type EmptyResult struct{}

func (EmptyResult) LastInsertId

func (EmptyResult) LastInsertId() (int64, error)

LastInsertId returns the integer generated by the database in response to a command. Typically this will be from an "auto increment" column when inserting a new row. Not all databases support this feature, and the syntax of such statements varies.

func (EmptyResult) RowsAffected

func (EmptyResult) RowsAffected() (int64, error)

RowsAffected returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.

type IntSignedCondition

type IntSignedCondition[T any, V int | int8 | int16 | int32 | int64] struct {
	// contains filtered or unexported fields
}

func NewIntSignedCondition

func NewIntSignedCondition[T any, V int | int8 | int16 | int32 | int64](w *T, buf *util.Builder, name string) *IntSignedCondition[T, V]

func (*IntSignedCondition[T, V]) Between

func (c *IntSignedCondition[T, V]) Between(min, max V) *T

func (*IntSignedCondition[T, V]) Equal

func (c *IntSignedCondition[T, V]) Equal(v V) *T

func (*IntSignedCondition[T, V]) GreaterEqual

func (c *IntSignedCondition[T, V]) GreaterEqual(v V) *T

func (*IntSignedCondition[T, V]) GreaterThen

func (c *IntSignedCondition[T, V]) GreaterThen(v V) *T

func (*IntSignedCondition[T, V]) In

func (c *IntSignedCondition[T, V]) In(vals ...V) *T

func (*IntSignedCondition[T, V]) LessEqual

func (c *IntSignedCondition[T, V]) LessEqual(v V) *T

func (*IntSignedCondition[T, V]) LessThen

func (c *IntSignedCondition[T, V]) LessThen(v V) *T

func (*IntSignedCondition[T, V]) NotEqual

func (c *IntSignedCondition[T, V]) NotEqual(v V) *T

type IntUnSignedCondition

type IntUnSignedCondition[T any, V uint | uint8 | uint16 | uint32 | uint64] struct {
	// contains filtered or unexported fields
}

func NewIntUnSignedCondition

func NewIntUnSignedCondition[T any, V uint | uint8 | uint16 | uint32 | uint64](w *T, buf *util.Builder, name string) *IntUnSignedCondition[T, V]

func (*IntUnSignedCondition[T, V]) Between

func (c *IntUnSignedCondition[T, V]) Between(min, max V) *T

func (*IntUnSignedCondition[T, V]) Equal

func (c *IntUnSignedCondition[T, V]) Equal(v V) *T

func (*IntUnSignedCondition[T, V]) GreaterEqual

func (c *IntUnSignedCondition[T, V]) GreaterEqual(v V) *T

func (*IntUnSignedCondition[T, V]) GreaterThen

func (c *IntUnSignedCondition[T, V]) GreaterThen(v V) *T

func (*IntUnSignedCondition[T, V]) In

func (c *IntUnSignedCondition[T, V]) In(vals ...V) *T

func (*IntUnSignedCondition[T, V]) LessEqual

func (c *IntUnSignedCondition[T, V]) LessEqual(v V) *T

func (*IntUnSignedCondition[T, V]) LessThen

func (c *IntUnSignedCondition[T, V]) LessThen(v V) *T

func (*IntUnSignedCondition[T, V]) NotEqual

func (c *IntUnSignedCondition[T, V]) NotEqual(v V) *T

type StringCondition

type StringCondition[T any] struct {
	// contains filtered or unexported fields
}

func NewStringCondition

func NewStringCondition[T any](w *T, buf *util.Builder, name string) *StringCondition[T]

func (*StringCondition[T]) Equal

func (c *StringCondition[T]) Equal(v string) *T

func (*StringCondition[T]) In

func (c *StringCondition[T]) In(vals ...string) *T

func (*StringCondition[T]) Like

func (c *StringCondition[T]) Like(v string) *T

func (*StringCondition[T]) NotEqual

func (c *StringCondition[T]) NotEqual(v string) *T

Directories

Path Synopsis
example
dbop/db_user
Code generated by wpb.
Code generated by wpb.
Code generated by "gogen cfggen"; DO NOT EDIT.
Code generated by "gogen cfggen"; DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL