Documentation ¶
Index ¶
- Constants
- Variables
- func GetRepositoryEntityMap() map[types.TableName]*RepositoryValue
- func ParseOperationType(sql string) (types.OperationType, error)
- func ParseSQL(repository IRepository, operationType types.OperationType, ...) (*scheme.SQLScheme, error)
- func Row(db IDataBase, sql string, argList []interface{}, entityInfo *types.EntityInfo) interface{}
- func Rows(db IDataBase, sql string, argList []interface{}, entityInfo *types.EntityInfo) interface{}
- func Transaction(funcHandle interface{}) error
- type DataBase
- func (db *DataBase) Begin() (IDataBase, error)
- func (db *DataBase) BeginTx(ctx context.Context, opts *sql.TxOptions) (IDataBase, error)
- func (db *DataBase) CreateTable(entity types.IEntity, migrator bool, autoIncrement uint) error
- func (db *DataBase) GetDataSourceName() string
- func (db *DataBase) GetDriverName() string
- func (db *DataBase) GetUUID() string
- func (db *DataBase) Prepare(query string) (IDataBaseStmt, error)
- func (db *DataBase) PrepareContext(ctx context.Context, query string) (IDataBaseStmt, error)
- type DataBaseConfig
- type DataBaseStmt
- type DataBaseTx
- type EntityRepository
- type FunctionType
- type IDataBase
- type IDataBaseConfig
- type IDataBaseStmt
- type IDataBaseTx
- type IEntityRepository
- type IRepository
- type MemCacheRepository
- func (repository *MemCacheRepository) CommitTransaction()
- func (repository *MemCacheRepository) Create(target types.IEntity) error
- func (repository *MemCacheRepository) CreateTransaction(parentDB ...IDataBase) IEntityRepository
- func (repository *MemCacheRepository) DeleteByID(ID types.PrimaryKey) error
- func (repository *MemCacheRepository) Entity() *types.EntityInfo
- func (repository *MemCacheRepository) FindAll() []types.IEntity
- func (repository *MemCacheRepository) FreeMemCache()
- func (repository *MemCacheRepository) GetByID(ID types.PrimaryKey) types.IEntity
- func (repository *MemCacheRepository) GetOrm() IDataBase
- func (repository *MemCacheRepository) IsMemCache() bool
- func (repository *MemCacheRepository) MemCache() *mem.Cache
- func (repository *MemCacheRepository) ReloadMemCache(empty bool) bool
- func (repository *MemCacheRepository) RollbackTransaction()
- func (repository *MemCacheRepository) Save(target types.IEntity) error
- func (repository *MemCacheRepository) TableName() types.TableName
- func (repository *MemCacheRepository) ToMapByPrimary() map[types.PrimaryKey]types.IEntity
- func (repository *MemCacheRepository) UpdateByID(ID types.PrimaryKey, params map[string]interface{}) error
- type Pagination
- type Repository
- func (repository *Repository) CommitTransaction()
- func (repository *Repository) Create(target types.IEntity) error
- func (repository *Repository) CreateTransaction(parentDB ...IDataBase) IEntityRepository
- func (repository *Repository) DeleteByID(ID types.PrimaryKey) error
- func (repository *Repository) Entity() *types.EntityInfo
- func (repository *Repository) FindAll() []types.IEntity
- func (repository *Repository) FreeMemCache()
- func (repository *Repository) GetByID(ID types.PrimaryKey) types.IEntity
- func (repository *Repository) GetOrm() IDataBase
- func (repository *Repository) IsMemCache() bool
- func (repository *Repository) MemCache() *mem.Cache
- func (repository *Repository) ReloadMemCache(empty bool) bool
- func (repository *Repository) RollbackTransaction()
- func (repository *Repository) Save(value types.IEntity) error
- func (repository *Repository) TableName() types.TableName
- func (repository *Repository) ToMapByPrimary() map[types.PrimaryKey]types.IEntity
- func (repository *Repository) UpdateByID(ID types.PrimaryKey, value map[string]interface{}) error
- type RepositoryValue
Constants ¶
View Source
const AutoCreate = "AutoCreate"
View Source
const Default = "Default"
View Source
const IgnoreCase = "IgnoreCase"
Variables ¶
View Source
var Logger *glog.Logger = glog.NewLogger("gpa")
View Source
var PaginationSize = 10
Functions ¶
func GetRepositoryEntityMap ¶
func GetRepositoryEntityMap() map[types.TableName]*RepositoryValue
func ParseOperationType ¶
func ParseOperationType(sql string) (types.OperationType, error)
func ParseSQL ¶
func ParseSQL(repository IRepository, operationType types.OperationType, entity *types.EntityInfo, where interface{}, params []interface{}, order interface{}, isMany, isLimit bool, showLog string) (*scheme.SQLScheme, error)
func Row ¶
func Row(db IDataBase, sql string, argList []interface{}, entityInfo *types.EntityInfo) interface{}
func Rows ¶
func Rows(db IDataBase, sql string, argList []interface{}, entityInfo *types.EntityInfo) interface{}
func Transaction ¶
func Transaction(funcHandle interface{}) error
todo:去掉 orm参数,改用 缓存中的 orm,相关于多个 CreateTransaction 组成,使用上下文功能,形成原子操作,其中有一个 EntityRepository 失败,则全部回滚
Types ¶
type DataBase ¶
type DataBase struct { *DataBaseConfig *sql.DB }
func (*DataBase) CreateTable ¶
func (*DataBase) GetDataSourceName ¶
func (*DataBase) GetDriverName ¶
func (*DataBase) PrepareContext ¶
type DataBaseConfig ¶
type DataBaseStmt ¶
type DataBaseStmt struct { *DataBaseConfig *sql.Stmt }
func (*DataBaseStmt) GetDataSourceName ¶
func (stmt *DataBaseStmt) GetDataSourceName() string
func (*DataBaseStmt) GetDriverName ¶
func (stmt *DataBaseStmt) GetDriverName() string
func (*DataBaseStmt) GetUUID ¶
func (stmt *DataBaseStmt) GetUUID() string
type DataBaseTx ¶
type DataBaseTx struct { *DataBaseConfig *sql.Tx }
func (*DataBaseTx) GetDataSourceName ¶
func (d *DataBaseTx) GetDataSourceName() string
func (*DataBaseTx) GetDriverName ¶
func (d *DataBaseTx) GetDriverName() string
func (*DataBaseTx) GetUUID ¶
func (d *DataBaseTx) GetUUID() string
type EntityRepository ¶
type EntityRepository struct {
// contains filtered or unexported fields
}
func NewEntityRepository ¶
func NewEntityRepository(entityRepository IEntityRepository, tableName types.TableName) *EntityRepository
func (*EntityRepository) GetInstance ¶
func (e *EntityRepository) GetInstance() IEntityRepository
func (*EntityRepository) GetTableName ¶
func (e *EntityRepository) GetTableName() types.TableName
func (*EntityRepository) NewEntityRepository ¶
func (e *EntityRepository) NewEntityRepository() IEntityRepository
type FunctionType ¶
type IDataBase ¶
type IDataBase interface { IDataBaseConfig Exec(query string, args ...interface{}) (sql.Result, error) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) }
func GenerateForGorm ¶
type IDataBaseConfig ¶
type IDataBaseStmt ¶
type IDataBaseStmt interface { IDataBaseConfig Exec(args ...interface{}) (sql.Result, error) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row QueryRow(args ...interface{}) *sql.Row QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error) Query(args ...interface{}) (*sql.Rows, error) }
type IDataBaseTx ¶
type IEntityRepository ¶
type IEntityRepository interface { IRepository Repository() IRepository }
实体类的接口
func Bind ¶
func Bind(entityRepository IEntityRepository, entity types.IEntity, orm func() IDataBase, memCache ...bool) IEntityRepository
type IRepository ¶
type IRepository interface { CreateTransaction(parentDB ...IDataBase) IEntityRepository RollbackTransaction() Entity() *types.EntityInfo GetOrm() IDataBase UpdateByID(ID types.PrimaryKey, params map[string]interface{}) error DeleteByID(ID types.PrimaryKey) error GetByID(ID types.PrimaryKey) types.IEntity ToMapByPrimary() map[types.PrimaryKey]types.IEntity Create(target types.IEntity) error Save(target types.IEntity) error FindAll() []types.IEntity CommitTransaction() FreeMemCache() ReloadMemCache(coverEmpty bool) bool // coverEmpty 如果遇到空的数据表,是否覆盖 IsMemCache() bool MemCache() *mem.Cache TableName() types.TableName }
实体类公共操作的接口
type MemCacheRepository ¶
type MemCacheRepository Repository
func (*MemCacheRepository) CommitTransaction ¶
func (repository *MemCacheRepository) CommitTransaction()
func (*MemCacheRepository) Create ¶
func (repository *MemCacheRepository) Create(target types.IEntity) error
func (*MemCacheRepository) CreateTransaction ¶
func (repository *MemCacheRepository) CreateTransaction(parentDB ...IDataBase) IEntityRepository
func (*MemCacheRepository) DeleteByID ¶
func (repository *MemCacheRepository) DeleteByID(ID types.PrimaryKey) error
func (*MemCacheRepository) Entity ¶
func (repository *MemCacheRepository) Entity() *types.EntityInfo
func (*MemCacheRepository) FindAll ¶
func (repository *MemCacheRepository) FindAll() []types.IEntity
func (*MemCacheRepository) FreeMemCache ¶
func (repository *MemCacheRepository) FreeMemCache()
func (*MemCacheRepository) GetByID ¶
func (repository *MemCacheRepository) GetByID(ID types.PrimaryKey) types.IEntity
func (*MemCacheRepository) GetOrm ¶
func (repository *MemCacheRepository) GetOrm() IDataBase
func (*MemCacheRepository) IsMemCache ¶
func (repository *MemCacheRepository) IsMemCache() bool
func (*MemCacheRepository) MemCache ¶
func (repository *MemCacheRepository) MemCache() *mem.Cache
func (*MemCacheRepository) ReloadMemCache ¶
func (repository *MemCacheRepository) ReloadMemCache(empty bool) bool
func (*MemCacheRepository) RollbackTransaction ¶
func (repository *MemCacheRepository) RollbackTransaction()
func (*MemCacheRepository) Save ¶
func (repository *MemCacheRepository) Save(target types.IEntity) error
func (*MemCacheRepository) TableName ¶
func (repository *MemCacheRepository) TableName() types.TableName
func (*MemCacheRepository) ToMapByPrimary ¶
func (repository *MemCacheRepository) ToMapByPrimary() map[types.PrimaryKey]types.IEntity
func (*MemCacheRepository) UpdateByID ¶
func (repository *MemCacheRepository) UpdateByID(ID types.PrimaryKey, params map[string]interface{}) error
func (repository *MemCacheRepository) UpdateConditions(value interface{}, where interface{}, args ...interface{}) (int, error) { memCache := repository.MemCache() repositoryValue := getRepositoryValue(repository.EntityRepositoryName) _, newKind, ev := GetValueElem(reflect.ValueOf(value)) var changeMap map[string]interface{} switch newKind { case reflect.Struct: changeMap = funcs.GetStructNotZero(ev) case reflect.Map: if ev.Type().Key().Kind() != reflect.String && ev.Type().Elem().Kind() != reflect.Interface { return 0, errors.New(fmt.Sprintf("不支持修改参数值(value)非map[string]interface{}类型:%v", newKind)) } changeMap = value.(map[string]interface{}) default: return 0, errors.New(fmt.Sprintf("不支持修改参数值(value)%v类型", newKind)) } RowsAffected, Error := (*Repository)(repository).UpdateConditions(value, where, args...) //repository.orm().Model(repositoryValue.Entity.NewEntity()).Where(where, args...).Updates(changeMap) //RowsAffected := db.GetRowsAffected() //Error := db.GetError() if Error == nil { wexp, err := ParseSQL(repository,types.UpdateOperationType, repositoryValue.Entity, where, args, nil, true, false, "MemCacheRepository => UpdateConditions") if glog.Error(err) { panic(err) } wexp.GenerateFieldCompareMethod(repository.Entity(), string(repository.EntityRepositoryName+"=>UpdateConditions")) list := repository.FindAll() for index := 0; index < len(list); index++ { if wexp.Condition(list[index]) { memCache.Update(list[index], changeMap) } } } return RowsAffected, Error }
type Pagination ¶
func RawPaging ¶
func RawPaging(db IDataBase, sqlScheme *scheme.SQLScheme, argList []interface{}, pageIndex, pageSize int, entityInfo *types.EntityInfo) (p Pagination)
return entityType(is elem)
func basePaging(db *gorm.DB, pageIndex, pageSize int, entityType reflect.Type) (Pagination, error) { var list = reflect.New(reflect.SliceOf(entityType)) var total int64 = 0 db.Count(&total) //scheme:=sql.NewSQLScheme(db.TableName,db.AggregateFunction,db.Conditions,db.OrderBy,db.GroupBy,db.IsMany,db.IsLimit,db.ShowLog) //scheme.AggregateFunction = []sql.AggregateFunction{{Columns:[]string{"*"},Function:"COUNT",AreaName: "Total"}} //scheme.CreateSQL() /*if pageIndex < 0 { err := db.Find(list.Interface()).Error return Pagination{List: list.Elem().Interface(), Total: int(total), Size: pageSize, Index: 0}, err }*/ if pageIndex < 0 { pageIndex = 0 } if pageSize > 30000 { pageSize = 30000 } if pageSize < 10 { pageSize = 10 } /*if pageSize <= 0 { err := db.Find(list.Interface()).Error return Pagination{List: list.Elem().Interface(), Total: int(total), Size: pageSize, Index: 0}, err }*/ //当总条数发生改变时,重新计算 pageIndex // args:offset pageIndex /*GetOffset := func() (int, int) { if pageIndex*pageSize > int(total) { //如果当前页查不内容时,pageIndex保持不变 pageIndex = int(math.Floor(float64(total) / float64(pageSize))) } return pageIndex * pageSize, pageIndex }*/ //_offset, _pageIndex := GetOffset() var err error if pageSize == 0 { err = db.Find(list.Interface()).Error glog.Error(err) } else { err = db.Limit(pageSize).Offset(pageIndex * pageSize).Find(list.Interface()).Error glog.Error(err) } return Pagination{ List: list.Elem().Interface(), Total: int(total), Size: pageSize, Index: pageIndex, }, err }
type Repository ¶
type Repository struct { Value *RepositoryValue // contains filtered or unexported fields }
func (*Repository) CommitTransaction ¶
func (repository *Repository) CommitTransaction()
func (*Repository) Create ¶
func (repository *Repository) Create(target types.IEntity) error
func (repository *Repository) FindPagination(order interface{}, index, size int, where interface{}, args ...interface{}) (p Pagination) {
exp, err := ParseSQL(repository,types.FindOperationType, repository.Entity(), where, args, order, true, true, string(repository.EntityRepositoryName)) if glog.CheckError(err) { return } _, err = exp.GenerateSQL() if glog.CheckError(err) { return } log.Println(exp.SQL) rows, err := repository.GetOrm().Query(exp.SQL, append(args, index*size, size)...) if glog.CheckError(err) { return } log.Println(rows) list, _ := exp.Scans(rows, repository.Entity()) sqlText, _, err := exp.GenerateQueryCountSQL() if glog.CheckError(err) { return } row := repository.GetOrm().QueryRow(sqlText, args...) if glog.CheckError(err) { return } var count int err = row.Scan(&count) if glog.CheckError(err) { return } return Pagination{ List: list, Total: count, Index: index, Size: size, }
} func (repository *Repository) FindOrderWhere(order interface{}, where interface{}, args ...interface{}) (list []types.IEntity) {
exp, err := ParseSQL(repository,types.FindOperationType, repository.Entity(), where, args, order, true, false, string(repository.EntityRepositoryName)) if glog.Error(err) { return } _, err = exp.GenerateSQL() if glog.Error(err) { return } rows, err := repository.GetOrm().Query(exp.SQL, args...) if glog.Error(err) { return } _list, err := exp.Scans(rows, repository.Entity()) if glog.Error(err) { return } return convertIEntitySlice(reflect.ValueOf(_list)) }
func (*Repository) CreateTransaction ¶
func (repository *Repository) CreateTransaction(parentDB ...IDataBase) IEntityRepository
func (*Repository) DeleteByID ¶
func (repository *Repository) DeleteByID(ID types.PrimaryKey) error
func (*Repository) Entity ¶
func (repository *Repository) Entity() *types.EntityInfo
func (*Repository) FindAll ¶
func (repository *Repository) FindAll() []types.IEntity
func (*Repository) FreeMemCache ¶
func (repository *Repository) FreeMemCache()
func (*Repository) GetByID ¶
func (repository *Repository) GetByID(ID types.PrimaryKey) types.IEntity
func (*Repository) GetOrm ¶
func (repository *Repository) GetOrm() IDataBase
func (*Repository) IsMemCache ¶
func (repository *Repository) IsMemCache() bool
func (*Repository) MemCache ¶
func (repository *Repository) MemCache() *mem.Cache
func (*Repository) ReloadMemCache ¶
func (repository *Repository) ReloadMemCache(empty bool) bool
func (*Repository) RollbackTransaction ¶
func (repository *Repository) RollbackTransaction()
func (*Repository) TableName ¶
func (repository *Repository) TableName() types.TableName
func (*Repository) ToMapByPrimary ¶
func (repository *Repository) ToMapByPrimary() map[types.PrimaryKey]types.IEntity
func (*Repository) UpdateByID ¶
func (repository *Repository) UpdateByID(ID types.PrimaryKey, value map[string]interface{}) error
func (repository *Repository) DeleteConditions(where interface{}, args ...interface{}) (int, error) {
exp, err := ParseSQL(repository, types.DeleteOperationType,repository.Entity(), where, args, nil, false, false, string(repository.EntityRepositoryName)) if glog.CheckError(err) { return 0, err } _, err = exp.GenerateSQL() if glog.CheckError(err) { return 0, err } //log.Println(exp.SQL) var result sql.Result result, err = repository.GetOrm().Exec(exp.SQL, args...) if glog.CheckError(err) { return 0, err } num, _ := result.RowsAffected() return int(num), nil
}
type RepositoryValue ¶
type RepositoryValue struct { Entity *types.EntityInfo EntityRepository *EntityRepository SQLSchemeMap map[string]*scheme.SQLScheme // contains filtered or unexported fields }
func (*RepositoryValue) AddFunctionType ¶
func (e *RepositoryValue) AddFunctionType(functionTypeName string, Type reflect.Type)
func (*RepositoryValue) GetFunctionType ¶
func (e *RepositoryValue) GetFunctionType(functionTypeName string) *FunctionType
func (*RepositoryValue) GetTableName ¶
func (e *RepositoryValue) GetTableName() types.TableName
Source Files ¶
Click to show internal directories.
Click to hide internal directories.