本文整理汇总了Golang中github.com/waterlink/rebecca/context.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Where
// Where is for fetching specific records
func (d *Driver) Where(tablename string, fields []field.Field, ctx context.Context, where string, args ...interface{}) ([][]field.Field, error) {
if tx := ctx.GetTx(); tx != nil {
return tx.(*Driver).Where(tablename, fields, ctx.SetTx(nil), where, args...)
}
result := [][]field.Field{}
fn, ok := d.whereRegistry[where]
if !ok {
return nil, fmt.Errorf(
"Fake driver has no '%s' where query registerd, please register with RegisterWhere",
where,
)
}
for _, record := range d.getTable(tablename) {
ok, err := fn(record, args...)
if err != nil {
return nil, fmt.Errorf("Registered query '%s' returned error - %s", where, err)
}
if ok {
result = append(result, record)
}
}
return result, nil
}
示例2: All
// All is for fetching all records
func (d *Driver) All(tablename string, fields []field.Field, ctx context.Context) ([][]field.Field, error) {
if tx := ctx.GetTx(); tx != nil {
return tx.(*Driver).All(tablename, fields, ctx.SetTx(nil))
}
return d.getTable(tablename), nil
}
示例3: All
// All is for fetching all records in current context
func (d *Driver) All(tablename string, fields []field.Field, ctx context.Context) ([][]field.Field, error) {
names := fieldNames(fields)
query := "SELECT %s FROM %s %s"
query = fmt.Sprintf(query, namesRepr(names), tablename, contextFor(ctx))
return d.readRows(ctx.GetTx(), fields, query)
}
示例4: First
// First is for fetching only first specific record from current context matching given where query and arguments
func (d *Driver) First(tablename string, fields []field.Field, ctx context.Context, where string, args ...interface{}) ([]field.Field, error) {
firstCtx := ctx.SetLimit(1)
names := fieldNames(fields)
query := "SELECT %s FROM %s WHERE %s %s"
query = fmt.Sprintf(query, namesRepr(names), tablename, where, contextFor(firstCtx))
return d.readRow(ctx.GetTx(), fields, query, args...)
}
示例5: First
// First is for fetching first specific record
func (d *Driver) First(tablename string, fields []field.Field, ctx context.Context, where string, args ...interface{}) ([]field.Field, error) {
if tx := ctx.GetTx(); tx != nil {
return tx.(*Driver).First(tablename, fields, ctx.SetTx(nil), where, args...)
}
records, err := d.Where(tablename, fields, ctx, where, args...)
if err != nil {
return nil, fmt.Errorf("Unable to get first record - %s", err)
}
if len(records) == 0 {
return nil, fmt.Errorf("Record not found with where query '%s'", where)
}
return records[0], nil
}
示例6: contextFor
func contextFor(ctx context.Context) string {
queryCtx := ""
if group := ctx.GetGroup(); group != "" {
queryCtx = queryCtx + fmt.Sprintf(" GROUP BY %s", group)
}
if order := ctx.GetOrder(); order != "" {
queryCtx = queryCtx + fmt.Sprintf(" ORDER BY %s", order)
}
if limit := ctx.GetLimit(); limit > 0 {
queryCtx = queryCtx + fmt.Sprintf(" LIMIT %d", limit)
}
if skip := ctx.GetSkip(); skip > 0 {
queryCtx = queryCtx + fmt.Sprintf(" OFFSET %d", skip)
}
return queryCtx
}