Example #1
0
/**
 * データストアから指定された単語を削除する
 * API
 */
func delete(w http.ResponseWriter, r *http.Request) {
	var c appengine.Context
	var u *user.User
	var query *datastore.Query
	var word string
	var iterator *datastore.Iterator
	var key *datastore.Key
	var err error
	var entity Entity

	word = r.FormValue("word")
	if word == "" {
		return
	}

	c = appengine.NewContext(r)
	u = user.Current(c)
	query = datastore.NewQuery("words").Filter("UserID =", u.ID).Filter("Word =", word)
	iterator = query.Run(c)

	for {
		key, err = iterator.Next(&entity)
		if err != nil {
			break
		}
		err = datastore.Delete(c, key)
		Check(c, err)
	}
}
Example #2
0
//MultiQuery executes given query and returns slice of all the entities it returns, with their keys set.
func MultiQuery(c appengine.Context, typ reflect.Type, kind string, query *datastore.Query) (ms interface{}, err error) {
	is := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(typ)), 0, 0)

	iter := query.Run(c)

	for {
		val := reflect.New(typ)
		var key *datastore.Key
		key, err = iter.Next(val.Interface())
		if err != nil {
			if err == datastore.Done {
				err = nil
				val.MethodByName("SetKey").Call([]reflect.Value{reflect.ValueOf(key)})
				reflect.Append(is, val)
				break
			}
			return
		}
		val.MethodByName("SetKey").Call([]reflect.Value{reflect.ValueOf(key)})
		is = reflect.Append(is, val)
	}

	ms = is.Interface()

	return
}
Example #3
0
func ExecuteQuery(c appengine.Context, q *datastore.Query, start int, limit int, filter func(Post) bool, out chan<- Post, errout chan<- error) {

	defer close(out)
	defer close(errout)

	for t, i := q.Run(c), 0; ; i++ {
		if ((start < 0) || (limit < 0)) || (i < (start + limit)) {
			var x Post
			// key, err := t.Next(&x)
			_, err := t.Next(&x)

			if err == datastore.Done {
				return
			}
			if err != nil {
				errout <- err
				return
			}

			if !filter(x) {
				i--
				continue
			}

			if i < start {
				continue
			}

			out <- x
		} else {
			break
		}
	}
}
Example #4
0
func getRadarsFromQuery(
	c appengine.Context,
	query *datastore.Query,
	limit int) (radars []Radar, cursor_out string, err error) {

	radars = make([]Radar, 0, limit)
	count := 0
	iterator := query.Run(c)
	for {
		var radar Radar
		_, err := iterator.Next(&radar)
		if err == datastore.Done {
			return radars, "", nil
		} else if err != nil {
			c.Errorf("Radar iterator error: %v", err)
			continue
		} else {
			radars = append(radars, radar)
		}
		count = count + 1
		if count == limit {
			break
		}
	}
	// in case there are more radars, continue using a cursor based on the current position
	if cursor, err := iterator.Cursor(); err == nil {
		cursor_out = cursor.String()
	}
	return radars, cursor_out, nil
}
Example #5
0
func (fdb FlightDB) NewIter(q *datastore.Query) *FlightIterator {
	fi := FlightIterator{
		C:     fdb.C,
		Query: q,
		Iter:  q.Run(fdb.C),
	}
	return &fi
}
Example #6
0
func (cdb ComplaintDB) NewIter(q *datastore.Query) *ComplaintIterator {
	ci := ComplaintIterator{
		C:     cdb.C,
		Query: q,
		Iter:  q.Run(cdb.C),
	}
	return &ci
}
Example #7
0
// create an index for items, by calling the keyFunc function provided for each item
//  returning a map from those keys to the items
func (self *importer) indexItems(query *datastore.Query, item interface{},
	keyFunc func(*datastore.Key, interface{}) string) map[string]*datastore.Key {
	index := make(map[string]*datastore.Key)
	iter := query.Run(self.c)
	for dataKey, err := iter.Next(item); err == nil; dataKey, err = iter.Next(item) {
		indexKey := keyFunc(dataKey, item)
		if len(indexKey) > 0 {
			index[indexKey] = dataKey
		}
	}
	return index
}
Example #8
0
/**
 * すべての単語リストを取得する
 */
func get(c appengine.Context, u *user.User) []Entity {
	var result []Entity
	var query *datastore.Query
	var err error
	var iterator *datastore.Iterator
	var entity *Entity

	query = datastore.NewQuery("words").Filter("UserID =", u.ID)
	Check(c, err)

	err = nil
	result = make([]Entity, 0)
	entity = new(Entity)
	for iterator = query.Run(c); ; {
		_, err = iterator.Next(entity)
		if err != nil {
			break
		}
		result = append(result, *entity)
	}

	return result
}
Example #9
0
func GetPage(c appengine.Context, query *datastore.Query, limit int, keysOnly bool, dst interface{}) (*Page, error) {
	var dv reflect.Value
	var mat multiArgType
	var elemType reflect.Type

	if !keysOnly {
		dv = reflect.ValueOf(dst)
		if dv.Kind() != reflect.Ptr || dv.IsNil() {
			return nil, datastore.ErrInvalidEntityType
		}
		dv = dv.Elem()
		mat, elemType = checkMultiArg(dv)
		if mat == multiArgTypeInvalid || mat == multiArgTypeInterface {
			return nil, datastore.ErrInvalidEntityType
		}
	}

	var keys []*datastore.Key
	var cursor datastore.Cursor

	query = query.Limit(limit + 1)
	if keysOnly {
		query = query.KeysOnly()
	}
	t := query.Run(c)
	more := true
	for i := 0; i < limit; i++ {
		var ev reflect.Value
		if !keysOnly {
			ev = reflect.New(elemType)
		}
		k, err := t.Next(ev.Interface())
		if err == datastore.Done {
			more = false
			break
		}
		if err != nil {
			return nil, err
		}
		if !keysOnly {
			if mat != multiArgTypeStructPtr {
				ev = ev.Elem()
			}
			dv.Set(reflect.Append(dv, ev))
		}
		keys = append(keys, k)
	}

	if more {
		var err error
		cursor, err = t.Cursor()
		if err != nil {
			return nil, err
		}
		var ei interface{}
		if !keysOnly {
			ei = reflect.New(elemType).Interface()
		}
		_, err = t.Next(ei)
		if err == datastore.Done {
			more = false
		}
	}

	return &Page{
		Keys:  keys,
		Start: cursor,
		More:  more,
	}, nil
}
Example #10
0
// Run runs the query.
func (g *Goon) Run(q *datastore.Query) *Iterator {
	return &Iterator{
		g: g,
		i: q.Run(g.Context),
	}
}