# 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, "[email protected]", 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 ** 服务及配置初始化