コード例 #1
0
ファイル: mysql_test.go プロジェクト: haibudi/dbox
func TestSelectFilter(t *testing.T) {
	c, e := prepareConnection()
	if e != nil {
		t.Errorf("Unable to connect %s \n", e.Error())
		return
	}
	defer c.Close()

	csr, e := c.NewQuery().
		Select("id", "name", "umur").
		From("tes").
		//Where(dbox.Eq("name", "Bourne")).
		//Where(dbox.Neq("name", "Bourne")).
		//Where(dbox.Gt("umur", 25)).
		//Where(dbox.Gte("umur", 25)).
		//Where(dbox.Lt("umur", 25)).
		//Where(dbox.Lte("umur", 25)).
		//Where(dbox.In("name", "vidal", "bourne")).
		//Where(dbox.In("umur", "25", "30")).
		Where(dbox.Nin("umur", "25", "30")).
		//Where(dbox.In("tanggal", "2016-01-12 14:35:54", "2016-01-12 14:36:15")).
		//Where(dbox.And(dbox.Gt("umur", 25), dbox.Eq("name", "Roy"))).
		Cursor(nil)
	if e != nil {
		t.Errorf("Cursor pre error: %s \n", e.Error())
		return
	}
	if csr == nil {
		t.Errorf("Cursor not initialized")
		return
	}
	defer csr.Close()

	results := make([]map[string]interface{}, 0)
	err := csr.Fetch(&results, 0, false)
	if err != nil {
		t.Errorf("Unable to fetch: %s \n", err.Error())
	} else {
		fmt.Println("======================")
		fmt.Println("Select with FILTER")
		fmt.Println("======================")
		for _, val := range results {
			fmt.Printf("Fetch N OK. Result: %v \n",
				toolkit.JsonString(val))
		}
	}
}
コード例 #2
0
func (d *DataSourceController) filterParse(where toolkit.M) *dbox.Filter {
	field := where.Get("field", "").(string)
	value := fmt.Sprintf("%v", where["value"])

	if key := where.Get("key", "").(string); key == "Eq" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Eq(field, valueInt)
		} else {
			return dbox.Eq(field, value)
		}
	} else if key == "Ne" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Ne(field, valueInt)
		} else {
			return dbox.Ne(field, value)
		}
	} else if key == "Lt" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Lt(field, valueInt)
		} else {
			return dbox.Lt(field, value)
		}
	} else if key == "Lte" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Lte(field, valueInt)
		} else {
			return dbox.Lte(field, value)
		}
	} else if key == "Gt" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Gt(field, valueInt)
		} else {
			return dbox.Gt(field, value)
		}
	} else if key == "Gte" {
		valueInt, errv := strconv.Atoi(fmt.Sprintf("%v", where["value"]))
		if errv == nil {
			return dbox.Gte(field, valueInt)
		} else {
			return dbox.Gte(field, value)
		}
	} else if key == "In" {
		valueArray := []interface{}{}
		for _, e := range strings.Split(value, ",") {
			valueArray = append(valueArray, strings.Trim(e, ""))
		}
		return dbox.In(field, valueArray...)
	} else if key == "Nin" {
		valueArray := []interface{}{}
		for _, e := range strings.Split(value, ",") {
			valueArray = append(valueArray, strings.Trim(e, ""))
		}
		return dbox.Nin(field, valueArray...)
	} else if key == "Contains" {
		return dbox.Contains(field, value)
	} else if key == "Or" {
		subs := where.Get("value", []interface{}{}).([]interface{})
		filtersToMerge := []*dbox.Filter{}
		for _, eachSub := range subs {
			eachWhere, _ := toolkit.ToM(eachSub)
			filtersToMerge = append(filtersToMerge, d.filterParse(eachWhere))
		}
		return dbox.Or(filtersToMerge...)
	} else if key == "And" {
		subs := where.Get("value", []interface{}{}).([]interface{})
		filtersToMerge := []*dbox.Filter{}
		for _, eachSub := range subs {
			eachWhere, _ := toolkit.ToM(eachSub)
			filtersToMerge = append(filtersToMerge, d.filterParse(eachWhere))
		}
		return dbox.And(filtersToMerge...)
	}

	return nil
}
コード例 #3
0
ファイル: getdatabase.go プロジェクト: novalagung/sedotan
func filterCondition(cond toolkit.M) *dbox.Filter {
	fb := new(dbox.Filter)

	for key, val := range cond {
		if key == "$and" || key == "$or" {
			afb := []*dbox.Filter{}
			for _, sVal := range val.([]interface{}) {
				rVal := sVal.(map[string]interface{})
				mVal := toolkit.M{}
				for rKey, mapVal := range rVal {
					mVal.Set(rKey, mapVal)
				}

				afb = append(afb, filterCondition(mVal))
			}

			if key == "$and" {
				fb = dbox.And(afb...)
			} else {
				fb = dbox.Or(afb...)
			}

		} else {
			if toolkit.TypeName(val) == "map[string]interface {}" {
				mVal := val.(map[string]interface{})
				tomVal, _ := toolkit.ToM(mVal)
				switch {
				case tomVal.Has("$eq"):
					fb = dbox.Eq(key, tomVal["$eq"])
				case tomVal.Has("$ne"):
					fb = dbox.Ne(key, tomVal["$ne"])
				case tomVal.Has("$regex"):
					fb = dbox.Contains(key, toolkit.ToString(tomVal["$regex"]))
				case tomVal.Has("$gt"):
					fb = dbox.Gt(key, tomVal["$gt"])
				case tomVal.Has("$gte"):
					fb = dbox.Gte(key, tomVal["$gte"])
				case tomVal.Has("$lt"):
					fb = dbox.Lt(key, tomVal["$lt"])
				case tomVal.Has("$lte"):
					fb = dbox.Lte(key, tomVal["$lte"])
				case tomVal.Has("$in"):
					tval := make([]interface{}, 0, 0)
					if toolkit.TypeName(tomVal["$in"]) == "[]interface {}" {
						for _, tin := range tomVal["$in"].([]interface{}) {
							tval = append(tval, tin)
						}
					} else {
						tval = append(tval, tomVal["$in"])
					}

					fb = dbox.In(key, tval...)
				case tomVal.Has("$nin"):
					tval := make([]interface{}, 0, 0)
					if toolkit.TypeName(tomVal["$nin"]) == "[]interface {}" {
						for _, tin := range tomVal["$nin"].([]interface{}) {
							tval = append(tval, tin)
						}
					} else {
						tval = append(tval, tomVal["$nin"])
					}

					fb = dbox.Nin(key, tval...)
				}
			} else {
				fb = dbox.Eq(key, val)
			}
		}
	}

	return fb
}
コード例 #4
0
ファイル: hypothesis.go プロジェクト: yanda15/powerplant
func (c *HypothesisController) Initiate(k *knot.WebContext) interface{} {
	k.Config.OutputType = knot.OutputJson

	type ReturnValue struct {
		PlantList        []MasterPlant
		PlantListH2      []MasterPlant
		EQTypeList       []MasterEquipmentType
		MROElementList   []MasterMROElement
		OrderTypeList    []MasterOrderType
		ActivityTypeList []MasterActivityType
		FailureCode      []MasterFailureCode
		UnitList         []tk.M
	}

	var (
		Result ReturnValue
		e      error
	)

	d := struct {
		selectedPlant []string
	}{}
	e = k.GetPayload(&d)
	// tk.Printf("d: %#v \n\n", d)
	curr, _ := c.DB().Find(&MasterPlant{}, nil)

	defer curr.Close()

	e = curr.Fetch(&Result.PlantList, 0, false)

	// tk.Printf("Result.PlantList: %#v \n\n", Result.PlantList)

	filter := tk.M{}

	filter.Set("where", dbox.Nin("Plant", "Qurayyah CC", "PP9"))

	curr = nil
	curr, e = c.DB().Find(&MasterPlant{}, filter)
	e = curr.Fetch(&Result.PlantListH2, 0, false)

	// tk.Printf("Result.PlantListH2: %#v \n\n", Result.PlantListH2)

	curr = nil
	curr, e = c.DB().Find(&MasterEquipmentType{}, nil)
	e = curr.Fetch(&Result.EQTypeList, 0, false)

	// tk.Printf("Result.EQTypeList: %#v \n\n", Result.EQTypeList)

	curr = nil
	curr, e = c.DB().Find(&MasterMROElement{}, nil)
	e = curr.Fetch(&Result.MROElementList, 0, false)

	// tk.Printf("Result.MROElementList: %#v \n\n", Result.MROElementList)

	curr = nil
	curr, e = c.DB().Find(&MasterOrderType{}, nil)
	e = curr.Fetch(&Result.OrderTypeList, 0, false)

	// tk.Printf("Result.OrderTypeList: %#v \n\n", Result.OrderTypeList)

	curr = nil
	curr, e = c.DB().Find(&MasterActivityType{}, nil)
	e = curr.Fetch(&Result.ActivityTypeList, 0, false)

	// tk.Printf("Result.ActivityTypeList: %#v \n\n", Result.ActivityTypeList)

	curr = nil
	curr, e = c.DB().Find(&MasterFailureCode{}, nil)
	e = curr.Fetch(&Result.FailureCode, 0, false)

	// tk.Printf("Result.FailureCode: %#v \n\n", Result.FailureCode)

	var filter1 []*dbox.Filter

	if len(d.selectedPlant) != 0 {
		filter1 = append(filter1, dbox.In("Plant", d.selectedPlant))
	} else {
		filter1 = append(filter1, dbox.Eq("1", "1"))
	}

	/*pipes = append(pipes, tk.M{"$group": tk.M{
		"_id": "$unit",
	}})*/

	curr = nil

	curr, e = c.DB().Connection.NewQuery().
		Select("Unit").
		From(new(MasterUnitPlant).TableName()).
		Where(filter1...).
		Group("Unit").
		Cursor(nil)

	e = curr.Fetch(&Result.UnitList, 0, true)

	// tk.Printf("Result.UnitList: %#v \n\n", Result.UnitList)

	return ResultInfo(Result, e)
}