コード例 #1
0
ファイル: datastore.go プロジェクト: KojiTakahara/servant
func GreaterThanEqualQuery(query *datastore.Query, values url.Values, s string) *datastore.Query {
	if len(values[s]) != 0 {
		columnName := strings.TrimRight(strings.ToUpper(s[:1])+s[1:], "From")
		query = query.Filter(columnName+">=", ToInt(values[s][0])).Order(columnName)
	}
	return query
}
コード例 #2
0
ファイル: datastore.go プロジェクト: KojiTakahara/servant
func EqualQuery(query *datastore.Query, values url.Values, s string) *datastore.Query {
	if len(values[s]) != 0 {
		if values[s][0] == "true" || values[s][0] == "false" {
			value, _ := strconv.ParseBool(values[s][0])
			query = query.Filter(strings.ToUpper(s[:1])+s[1:]+"=", value)
		} else {
			query = query.Filter(strings.ToUpper(s[:1])+s[1:]+"=", values[s][0])
		}
	}
	return query
}
コード例 #3
0
ファイル: driver.go プロジェクト: rainycape/gondola
func (d *Driver) applyQuery(m driver.Model, dq *datastore.Query, q query.Q) (*datastore.Query, error) {
	var field *query.Field
	var op string
	switch x := q.(type) {
	case *query.Eq:
		field = &x.Field
		op = " ="
	case *query.Lt:
		field = &x.Field
		op = " <"
	case *query.Lte:
		field = &x.Field
		op = " <="
	case *query.Gt:
		field = &x.Field
		op = " >"
	case *query.Gte:
		field = &x.Field
		op = " >="
	case *query.And:
		var err error
		for _, v := range x.Conditions {
			dq, err = d.applyQuery(m, dq, v)
			if err != nil {
				return nil, err
			}
		}
	case nil:
	default:
		return nil, fmt.Errorf("datastore does not support %T queries", q)
	}
	if field != nil {
		if _, ok := field.Value.(query.F); ok {
			return nil, fmt.Errorf("datastore queries can't reference other properties (%v)", field.Value)
		}
		name := field.Field
		fields := m.Fields()
		idx, ok := fields.QNameMap[name]
		if !ok {
			return nil, fmt.Errorf("can't map field %q to a datastore name", name)
		}
		if strings.IndexByte(name, '.') >= 0 {
			// GAE flattens embedded fields, so we must remove
			// the parts of the field which refer to a flattened
			// field.
			indexes := fields.Indexes[idx]
			parts := strings.Split(name, ".")
			if len(indexes) == len(parts) {
				var final []string
				typ := fields.Type
				for ii, v := range indexes {
					f := typ.Field(v)
					if !f.Anonymous {
						final = append(final, parts[ii])
					}
					typ = f.Type
				}
				name = strings.Join(final, ".")
			}
		}
		log.Debugf("DATASTORE: filter %s %s %v", m, name+op, field.Value)
		dq = dq.Filter(name+op, field.Value)
	}
	return dq, nil
}