Example #1
0
File: dh.go Project: worg/go
func (self *Dh) RemoveForeverByConditions(conditions interface{}) api.Err {
	var res db.Result
	var errs api.Err
	res = Collection.Find(conditions)
	exist, err := res.Count()

	if exist == 0 {
		errs.Push(api.Msg{
			Field: "Delete: ",
			Error: api.ErrNoSuchRow,
		})
		return errs
	}

	err = res.Remove()

	if err == nil {
		return errs
	} else {
		errs.Push(api.Msg{
			Field: "Database delete  error: ",
			Error: err.Error(),
		})
	}
	return errs
}
Example #2
0
File: dh.go Project: worg/go
func (self *Dh) Delete() api.Err {

	var res db.Result
	var errs api.Err

	id := reflect.ValueOf(self.Model).Elem().FieldByName("Id").Interface().(int64)
	res = Collection.Find(db.Cond{"id": id})
	exist, err := res.Count()

	if exist == 0 {
		errs.Push(api.Msg{
			Field: "Delete: ",
			Error: api.ErrNoSuchRow,
		})
		return errs
	}

	err = res.Remove()

	if err == nil {
		return errs
	} else {
		errs.Push(api.Msg{
			Field: "Database delete  error: ",
			Error: err.Error(),
		})
	}

	return errs
}
Example #3
0
File: dh.go Project: worg/go
func (self *Dh) Count(query string) int64 {
	var errs api.Err
	drv := self.SqlDriver()
	rows, err := drv.Query(query)
	if err != nil {
		errs.Push(api.Msg{
			Field: "Database select All  error: ",
			Error: err.Error(),
		})
		return 0
	}

	type Total struct {
		Count int64 `db:"count"`
	}

	total := []Total{}
	// Mapping to an array.
	if err = sqlutil.FetchRows(rows, &total); err != nil {
		errs.Push(api.Msg{
			Field: "Database select All  error: ",
			Error: err.Error(),
		})
	}

	defer rows.Close()
	return total[0].Count
}
Example #4
0
File: dh.go Project: worg/go
func (self *Dh) FindAllByConditionsSortBy(bar interface{}, conditions interface{}, sort string) api.Err {
	var res db.Result
	var errs api.Err
	res = Collection.Find(conditions).Sort(sort)
	err := res.All(bar)
	if err != nil {
		errs.Push(api.Msg{
			Field: "Not found",
			Error: api.ErrNoSuchRow,
		})
	}
	return errs
}
Example #5
0
File: dh.go Project: worg/go
func (self *Dh) FindAllSortBy(bar interface{}, sort string) api.Err {
	var res db.Result
	var errs api.Err
	res = Collection.Find().Sort(sort)
	count, _ := res.Count()
	if count == 0 {
		errs.Push(api.Msg{
			Field: "Select All",
			Error: api.ErrSelectAll,
		})
	}
	res.All(bar)
	return errs
}
Example #6
0
File: dh.go Project: worg/go
func (self *Dh) FindByConditions(conditions interface{}) (uint64, api.Err) {
	var errs api.Err
	var res db.Result
	res = Collection.Find(conditions)
	count, _ := res.Count()
	if count == 0 {
		errs.Push(api.Msg{
			Field: "Not found",
			Error: api.ErrNoSuchRow,
		})
		return count, errs
	}
	res.One(self.Model)
	return count, errs
}
Example #7
0
File: dh.go Project: worg/go
func (self *Dh) FindByFieldValue(field string, value interface{}) (interface{}, api.Err) {
	var errs api.Err
	var res db.Result
	res = Collection.Find(db.Cond{field: value})
	count, _ := res.Count()

	if count == 0 {
		errs.Push(api.Msg{
			Field: "Not found",
			Error: api.ErrNoSuchRow,
		})
		return nil, errs
	}
	res.One(self.Model)
	return self.Model, errs
}
Example #8
0
File: dh.go Project: worg/go
// FindById
// Podemos hacer busquedas por PK Id este metodo
// recibe como parametro la pk de la tabla y trae todos sus
// elementos.
func (self *Dh) FindById(id int64) (interface{}, api.Err) {
	var errs api.Err
	var res db.Result
	res = Collection.Find(db.Cond{"id": id})
	count, _ := res.Count()

	if count == 0 {
		errs.Push(api.Msg{
			Field: "Not found",
			Error: api.ErrNoSuchRow,
		})
		return nil, errs
	}

	res.One(self.Model)

	return self.Model, errs
}
Example #9
0
File: dh.go Project: worg/go
// Nombre: Save()
// Este metodo Guarda una estructura de datos en nuestra base de datos
func (self *Dh) Save() (int64, api.Err) {
	var errs api.Err
	if exist := self.Exist(); exist {
		_, errup := self.Update()
		return 0, errup
	}

	id, err := Collection.Append(self.Model)

	if err != nil {
		errs.Push(api.Msg{
			Field: "DataBase insert error:",
			Error: err.Error(),
		})
		return 0, errs
	}

	idInsert := id.(int64)
	return idInsert, errs
}
Example #10
0
File: dh.go Project: worg/go
// Nombre: Update
// Este metodo se encarga de actualizar un registro
// si ya fue creado en nuestra base de datos es llamado por el
// metodo save
func (self *Dh) Update() (interface{}, api.Err) {

	var errs api.Err
	var res db.Result

	id := reflect.ValueOf(self.Model).Elem().FieldByName("Id").Interface().(int64)
	res = Collection.Find(db.Cond{"id": id})
	err := res.Update(self.Model)

	if err != nil {
		errs.Push(api.Msg{
			Field: "Save",
			Error: err.Error(),
		})
		return nil, errs
	}

	dataUpdate, errr := self.FindById(id)
	return dataUpdate, errr
}
Example #11
0
File: dh.go Project: worg/go
func (self *Dh) DeleteLogic() api.Err {

	var errs api.Err
	var res db.Result

	id := reflect.ValueOf(self.Model).Elem().FieldByName("Id").Interface().(int64)
	res = Collection.Find(db.Cond{"id": id})
	res.One(self.Model)
	reflect.ValueOf(self.Model).Elem().FieldByName("Status").SetBool(false)
	// Estatus := reflect.ValueOf(self.Model).Elem().FieldByName("Estatus").Interface().(bool)
	err := res.Update(self.Model)

	if err != nil {
		errs.Push(api.Msg{
			Field: "Save",
			Error: err.Error(),
		})
		return errs
	}

	return errs
}