예제 #1
0
파일: fake.go 프로젝트: waterlink/rebecca
// 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
}
예제 #2
0
파일: fake.go 프로젝트: waterlink/rebecca
// 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
}
예제 #3
0
파일: fake.go 프로젝트: waterlink/rebecca
// 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
}