Exemple #1
0
func (s *ValueStore) FindByKeys(dto *models.ValueDTO, mode string) (res []models.Value, err error) {
	if err := models.V.StructPartial(dto, "Keys"); err != nil {
		return nil, err
	}

	if _, exist := map[string]bool{
		"&&": true,
		"@>": true,
		"=":  true,
	}[mode]; !exist {
		mode = "="
	}

	model := models.NewValue()
	model.TransformFrom(dto)

	fields, modelValues := model.Fields()

	query := SqlSelect(model.TableName(), fields)
	// any - "&&", contains - "@>"", equal - "="
	if mode == "=" {
		query += fmt.Sprintf(" WHERE sort_text_array(keys) %s sort_text_array(?) AND is_removed = false", mode)
	} else {
		query += fmt.Sprintf(" WHERE keys %s ? AND is_removed = false", mode)
	}

	query = FormateToPQuery(query)

	var rows *pgx.Rows

	if dto.Tx != nil {
		rows, err = dto.Tx.Query(query, model.Keys)
	} else {
		rows, err = s.db.Query(query, model.Keys)
	}

	defer rows.Close()

	if err != nil {
		s.ErrorLog("action", "поиск записей по keys", "err", err, "keys", model.Keys)
		return
	}

	for rows.Next() {
		if err = rows.Scan(modelValues...); err != nil {
			s.ErrorLog("action", "поиск записей по keys", "subaction", "сканирование строки", "err", err, "keys", model.Keys)
			return
		}

		// res = append(res, *model)
		_model := models.NewValue()
		_model.TransformFrom(model)
		res = append(res, *_model)

		// TODO: reset for maps
		model.Value = models.InterfaceMap{}
	}

	return res, nil
}
Exemple #2
0
func (s *ValueStore) Delete(dto *models.ValueDTO) (models.Model, error) {
	if err := models.V.StructPartial(dto, "ValueId"); err != nil {
		return nil, err
	}

	model := models.NewValue()
	model.TransformFrom(dto)

	if err := DeleteModel(model, s.db, dto.Tx, " AND is_removed = false"); err != nil {
		return nil, err
	}

	return model, nil
}
Exemple #3
0
func (s *ValueStore) GetOne(dto *models.ValueDTO) (models.Model, error) {
	model := models.NewValue()
	model.TransformFrom(dto)

	if uuid.Equal(model.ValueId, uuid.Nil) {
		return nil, models.ErrNotValid
	}

	if err := FindModel(model, s.db, dto.Tx, " AND is_removed = false"); err != nil {
		return nil, err
	}

	return model, nil
}
Exemple #4
0
func (s *ValueStore) Create(dto *models.ValueDTO) (models.Model, error) {
	if err := models.V.StructPartial(dto, "Keys", "Value"); err != nil {
		return nil, err
	}

	model := models.NewValue()
	model.TransformFrom(dto)
	model.ValueId = uuid.NewV1()

	model.BeforeCreate()
	model.BeforeSave()

	var err error

	query := fmt.Sprintf("INSERT INTO %s(keys, value_id, value, is_removed, created_at, updated_at) VALUES(sort_text_array(?), ?, ?, ?, ?, ?)", model.TableName())
	where := fmt.Sprintf(" RETURNING %s", model.PrimaryName())

	query = FormateToPQuery(query + where)

	if dto.Tx != nil {
		err = dto.Tx.QueryRow(query,
			model.Keys,
			model.ValueId,
			model.Value,
			model.IsRemoved,
			model.CreatedAt,
			model.UpdatedAt,
		).Scan(&model.ValueId)

	} else {
		err = s.db.QueryRow(query,
			model.Keys,
			model.ValueId,
			model.Value,
			model.IsRemoved,
			model.CreatedAt,
			model.UpdatedAt,
		).Scan(&model.ValueId)
	}

	return model, err
}