Пример #1
0
func (r Repository) All(c decode.Criteria) (repository.Result, error) {
	var models []Model

	binds := map[string]string{
		"created_at": "to_timestamp(?)",
		"updated_at": "to_timestamp(?)",
	}

	builders := map[string]func(decode.Filter) (string, string, []interface{}){
		"After":  repository.NewAfterCursor(rockTable, c),
		"Before": repository.NewBeforeCursor(rockTable, c),
	}

	opts := sql.SelectOpts{
		Table:        rockTable,
		C:            c,
		Binds:        binds,
		Builders:     builders,
		Db:           r.Db,
		DefaultOrder: "ID DESC",
		DefaultLimit: 100,
	}

	sqlBuilder := sql.Builder{}
	q, args := sqlBuilder.Select(opts)

	err := r.Db.Select(&models, q, args...)

	switch err {
	case dbSql.ErrNoRows:
		err = repository.NoRows{Msg: err.Error()}
	}

	b := make([]interface{}, len(models))
	for i := range models {
		b[i] = models[i]
	}

	var nullCount null.Int
	if err == nil && c.Pagination == true {
		var totalCount int
		cq, args := sqlBuilder.SelectCount(opts)
		err = r.Db.Get(&totalCount, cq, args...)

		nullCount = null.IntFrom(int64(totalCount))
	}

	result := repository.Result{
		Data:       b,
		TotalCount: nullCount,
	}

	return result, err
}
Пример #2
0
		sc := func(s SelectCase) {
			// Given
			builder := sql.Builder{}
			opts := sql.SelectOpts{
				Db:           db,
				Table:        "users",
				C:            s.criteria,
				Columns:      s.columns,
				Binds:        s.binds,
				Builders:     s.builders,
				DefaultOrder: s.defaultOrder,
				DefaultLimit: s.defaultLimit,
			}

			// When
			q, args := builder.Select(opts)

			// Expect
			Expect(q).To(Equal(s.expectedQuery))
			Expect(args).To(Equal(s.expectedArgs))
		}

		DescribeTable("criteria",
			sc,
			Entry("without criteria", SelectCase{
				expectedQuery: "SELECT users.* FROM users",
			}),
			Entry("with columns", SelectCase{
				columns:       []string{"users.first_name", "users.last_name"},
				expectedQuery: "SELECT users.first_name, users.last_name FROM users",
			}),
Пример #3
0
func (r Repository) All(c decode.Criteria) (repository.Result, error) {
	var models []Model

	binds := map[string]string{
		"created_at": "to_timestamp(?)",
		"updated_at": "to_timestamp(?)",
	}

	builders := map[string]func(decode.Filter) (string, string, []interface{}){
		"Bounds": func(filter decode.Filter) (string, string, []interface{}) {
			innerJoin := fmt.Sprintf(`rocks ON rocks.id=%s.rock_id`, climbTable)
			where := `rocks.latitude > ? AND rocks.longitude > ? AND rocks.latitude < ? AND rocks.longitude < ?`
			strs := strings.Split(filter.Value, ",")

			var strArgs []interface{}
			for _, str := range strs {
				strArgs = append(strArgs, str)
			}

			return innerJoin, where, strArgs
		},
		"After":  repository.NewAfterCursor(climbTable, c),
		"Before": repository.NewBeforeCursor(climbTable, c),
	}

	opts := sql.SelectOpts{
		Table:        climbTable,
		C:            c,
		Binds:        binds,
		Builders:     builders,
		Db:           r.Db,
		DefaultOrder: "ID DESC",
		DefaultLimit: 100,
	}

	sqlBuilder := sql.Builder{}
	q, args := sqlBuilder.Select(opts)

	err := r.Db.Select(&models, q, args...)

	switch err {
	case dbSql.ErrNoRows:
		err = repository.NoRows{Msg: err.Error()}
	}

	b := make([]interface{}, len(models))
	for i := range models {
		b[i] = models[i]
	}

	var nullCount null.Int
	if err == nil && c.Pagination == true {
		var totalCount int
		cq, args := sqlBuilder.SelectCount(opts)
		err = r.Db.Get(&totalCount, cq, args...)

		nullCount = null.IntFrom(int64(totalCount))
	}

	result := repository.Result{
		Data:       b,
		TotalCount: nullCount,
	}

	return result, err
}