Esempio n. 1
0
// WhereAndFetchOne returns one document that match the filter
func (d *Default) WhereAndFetchOne(filter interface{}, result interface{}) error {
	cursor, err := r.Table(d.table).Filter(filter).Run(d.session)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	if err := cursor.One(result); err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 2
0
// WhereAndFetchLimit returns paginated list of document
func (d *Default) WhereAndFetchLimit(filter interface{}, paginator *api.Pagination, results interface{}) error {
	cursor, err := r.Table(d.table).Filter(filter).Run(d.session)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	if err := cursor.All(results); err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 3
0
// Find a document match given id
func (d *Default) Find(id interface{}, value interface{}) error {
	cursor, err := r.Table(d.table).Get(id).Run(d.session)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	if err := cursor.One(value); err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 4
0
// FindOneBy a couple (k = v) in the database
func (d *Default) FindOneBy(key string, value interface{}, result interface{}) error {

	cursor, err := r.Table(d.table).GetAllByIndex(key, value).Run(d.session)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	if err := cursor.One(result); err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 5
0
// FindBy all couples (k = v) in the database
func (d *Default) FindBy(key string, value interface{}, results interface{}) error {
	cursor, err := r.Table(d.table).Filter(func(row r.Term) r.Term {
		return row.Field(key).Eq(value)
	}).Run(d.session)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	if err := cursor.All(results); err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 6
0
// Search all entities in the database
func (d *Default) Search(results interface{}, filter interface{}, sortParams *api.SortParameters, pagination *api.Pagination) error {
	term := r.Table(d.table)

	// Filter
	if filter != nil {
		term = term.Filter(filter)
	}

	// Get total
	if pagination != nil {
		total, err := d.WhereCount(filter)
		if err != nil {
			return api.NewDatabaseError(d, err, "")
		}
		pagination.SetTotal(uint(total))
	}

	// Sort
	if sortParams != nil {
		term = term.OrderBy(ConvertSortParameters(*sortParams)...)
	}

	// Slice result
	if pagination != nil {
		term = term.Slice(pagination.Offset(), pagination.Offset()+pagination.PerPage)
	}

	// Run the query
	cursor, err := term.Run(d.session)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	// Fetch cursor
	err = cursor.All(results)
	if err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return err
	}

	return nil
}
Esempio n. 7
0
// InsertOrUpdate a document occording to ID presence in database
func (d *Default) InsertOrUpdate(id interface{}, data interface{}) error {
	_, err := r.Table(d.table).Insert(data, r.InsertOpts{Conflict: "update"}).RunWrite(d.session)
	if err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 8
0
// Insert inserts a document into the database
func (d *Default) Insert(data interface{}) error {
	_, err := r.Table(d.table).Insert(data).RunWrite(d.session)
	if err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 9
0
// Delete a document from the database
func (d *Default) Delete(id interface{}) error {
	_, err := r.Table(d.table).Get(id).Delete().RunWrite(d.session)
	if err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 10
0
// Update a document that match the selector
func (d *Default) Update(selector interface{}, data interface{}) error {
	_, err := r.Table(d.table).Filter(selector).Update(data).RunWrite(d.session)
	if err != nil {
		if err == r.ErrEmptyResult {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 11
0
// Search all entities from the database
func (d *Default) Search(results interface{}, filter interface{}, sortParams *api.SortParameters, pagination *api.Pagination) error {
	session := d.session.Clone()
	defer session.Close()

	// Apply Filter
	if filter == nil {
		filter = bson.M{}
	}

	// Get total
	if pagination != nil {
		total, err := d.WhereCount(filter)
		if err != nil {
			return api.NewDatabaseError(d, err, "")
		}
		pagination.SetTotal(uint(total))
	}

	// Prepare the query
	query := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter)

	// Apply sorts
	if sortParams != nil {
		sort := ConvertSortParameters(*sortParams)
		if len(sort) > 0 {
			query = query.Sort(sort...)
		}
	}

	// Paginate
	if pagination != nil {
		query = query.Limit(int(pagination.PerPage)).Skip(int(pagination.Offset()))
	}

	err := query.All(results)
	if err != nil {
		return api.NewDatabaseError(d, err, "")
	}

	return err
}
Esempio n. 12
0
// UpdateID performs an update on an existing resource with ID that equals the id argument
func (d *Default) UpdateID(id interface{}, data interface{}) error {
	session := d.session.Clone()
	defer session.Close()

	err := session.DB(d.GetDBName()).C(d.GetTableName()).UpdateId(id.(string), data)
	if err != nil {
		if err == mgo.ErrNotFound {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 13
0
// WhereAndFetchOne filters with multiple fields and then fills result with the first found resource
func (d *Default) WhereAndFetchOne(filter interface{}, result interface{}) error {
	session := d.session.Clone()
	defer session.Close()

	err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter).One(result)
	if err != nil {
		if err == mgo.ErrNotFound {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 14
0
// WhereAndFetchLimit filters with multiple fields and then fills results with all found resources
func (d *Default) WhereAndFetchLimit(filter interface{}, paginator *api.Pagination, results interface{}) error {
	session := d.session.Clone()
	defer session.Close()

	err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter).Limit(int(paginator.PerPage)).Skip(int(paginator.Offset())).All(results)
	if err != nil {
		if err == mgo.ErrNotFound {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 15
0
// WhereCount allows counting with multiple fields
func (d *Default) WhereCount(filter interface{}) (int, error) {
	session := d.session.Clone()
	defer session.Close()

	count, err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filter).Count()
	if err != nil {
		if err == mgo.ErrNotFound {
			return 0, api.ErrNoResult
		}
		return 0, api.NewDatabaseError(d, err, "")
	}

	return count, nil
}
Esempio n. 16
0
// FindFetchOne searches for a resource and then unmarshals the first row into value
func (d *Default) FindFetchOne(id string, value interface{}) error {
	session := d.session.Clone()
	defer session.Close()

	err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(bson.M{"_id": id}).One(value)
	if err != nil {
		if err == mgo.ErrNotFound {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 17
0
// DeleteAll deletes resources that match the passed filter
func (d *Default) DeleteAll(pred interface{}) error {
	session := d.session.Clone()
	defer session.Close()

	_, err := session.DB(d.GetDBName()).C(d.GetTableName()).RemoveAll(pred)
	if err != nil {
		if err == mgo.ErrNotFound {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 18
0
// WhereCount returns the document count that match the filter
func (d *Default) WhereCount(filter interface{}) (int, error) {
	cursor, err := r.Table(d.table).Filter(filter).Count().Run(d.session)
	if err != nil {
		return 0, err
	}

	var count int
	if err := cursor.One(&count); err != nil {
		if err == r.ErrEmptyResult {
			return 0, api.ErrNoResult
		}
		return 0, api.NewDatabaseError(d, err, "")
	}

	return count, nil
}
Esempio n. 19
0
// FindByAndFetch retrieves a value by key and then fills results with the result.
func (d *Default) FindByAndFetch(key string, value interface{}, results interface{}) error {
	filterMap := bson.M{
		key: value,
	}
	session := d.session.Clone()
	defer session.Close()

	err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filterMap).All(results)
	if err != nil {
		if err == mgo.ErrNotFound {
			return api.ErrNoResult
		}
		return api.NewDatabaseError(d, err, "")
	}

	return nil
}
Esempio n. 20
0
// FindByAndCount returns the number of elements that match the filter
func (d *Default) FindByAndCount(key string, value interface{}) (int, error) {
	filterMap := bson.M{
		key: value,
	}
	session := d.session.Clone()
	defer session.Close()

	n, err := session.DB(d.GetDBName()).C(d.GetTableName()).Find(filterMap).Count()
	if err != nil {
		if err == mgo.ErrNotFound {
			return 0, api.ErrNoResult
		}
		return 0, api.NewDatabaseError(d, err, "")
	}

	return n, nil
}
Esempio n. 21
0
// FindByAndCount is used to count object that matchs the (key = value) predicate
func (d *Default) FindByAndCount(key string, value interface{}) (int, error) {
	cursor, err := r.Table(d.table).Filter(func(row r.Term) r.Term {
		return row.Field(key).Eq(value)
	}).Count().Run(d.session)
	if err != nil {
		return 0, err
	}

	var count int
	if err := cursor.One(&count); err != nil {
		if err == r.ErrEmptyResult {
			return 0, api.ErrNoResult
		}
		return 0, api.NewDatabaseError(d, err, "")
	}

	return count, nil
}