Documentation ¶
Index ¶
- Variables
- func InitTable(db *gorm.DB) error
- type Article
- type ArticleDAO
- type DBProvider
- type GORMArticleDAO
- type GORMUserDAO
- func (dao *GORMUserDAO) FindByEmail(ctx context.Context, email string) (User, error)
- func (dao *GORMUserDAO) FindById(ctx context.Context, id int64) (User, error)
- func (dao *GORMUserDAO) FindByPhone(ctx context.Context, phone string) (User, error)
- func (dao *GORMUserDAO) FindByWechat(ctx context.Context, openID string) (User, error)
- func (dao *GORMUserDAO) Insert(ctx context.Context, u User) error
- type User
- type UserDAO
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrUserDuplicate = errors.New("邮箱冲突") ErrUserNotFound = gorm.ErrRecordNotFound )
Functions ¶
Types ¶
type Article ¶
type Article struct { Id int64 `gorm:"primaryKey,autoIncrement"` // 长度 1024 Title string `gorm:"type=varchar(1024)"` Content string `gorm:"type=BLOB"` // 在 author_id 上创建索引 AuthorId int64 `gorm:"index"` //AuthorId int64 `gorm:"index=aid_ctime"` //Ctime int64 `gorm:"index=aid_ctime"` Ctime int64 Utime int64 }
Article 这是制作库的 准备在 articles 表中准备十万/一百万条数据,author_id 各不相同(或者部分相同) 准备 author_id = 123 的,插入两百条数据 执行 SELECT * FROM articles WHERE author_id = 123 ORDER BY ctime DESC 比较两种索引的性能
type ArticleDAO ¶
type ArticleDAO interface { Insert(ctx context.Context, art Article) (int64, error) UpdateById(ctx context.Context, article Article) error }
func NewGORMArticleDAO ¶
func NewGORMArticleDAO(db *gorm.DB) ArticleDAO
type DBProvider ¶
type GORMArticleDAO ¶
type GORMArticleDAO struct {
// contains filtered or unexported fields
}
func (*GORMArticleDAO) UpdateById ¶
func (dao *GORMArticleDAO) UpdateById(ctx context.Context, art Article) error
type GORMUserDAO ¶
type GORMUserDAO struct {
// contains filtered or unexported fields
}
func (*GORMUserDAO) FindByEmail ¶
func (*GORMUserDAO) FindByPhone ¶
func (*GORMUserDAO) FindByWechat ¶
type User ¶
type User struct { Id int64 `gorm:"primaryKey,autoIncrement"` // 全部用户唯一 Email sql.NullString `gorm:"unique"` Password string // 唯一索引允许有多个空值 // 但是不能有多个 "" Phone sql.NullString `gorm:"unique"` // 如果要创建联合索引,<unionid, openid>,用 openid 查询的时候不会走索引 // <openid, unionid> 用 unionid 查询的时候,不会走索引 // 微信的字段 WechatUnionID sql.NullString WechatOpenID sql.NullString `gorm:"unique"` // 创建时间,毫秒数 Ctime int64 // 更新时间,毫秒数 Utime int64 }
User 直接对应数据库表结构 有些人叫做 entity,有些人叫做 model,有些人叫做 PO(persistent object)
type UserDAO ¶
type UserDAO interface { FindByEmail(ctx context.Context, email string) (User, error) FindById(ctx context.Context, id int64) (User, error) FindByPhone(ctx context.Context, phone string) (User, error) Insert(ctx context.Context, u User) error FindByWechat(ctx context.Context, openID string) (User, error) }
func NewUserDAO ¶
func NewUserDAOV1 ¶
func NewUserDAOV1(p DBProvider) UserDAO
Click to show internal directories.
Click to hide internal directories.