Пример #1
0
// Finds all results.
func (c *collection) FindAll(result interface{}, query *query) error {
	rval := reflect.ValueOf(result)
	if rval.Kind() != reflect.Ptr || rval.Elem().Kind() != reflect.Slice {
		return errors.New("result argument must be a slice address")
	}

	var stmt, args = query.makeSelectStmt("")
	var rows, err = c.Query(stmt, args...)
	if err != nil {
		return err
	}

	slice := rval.Elem()
	slice = slice.Slice(0, slice.Cap())
	elemType := slice.Type().Elem()

	var i = 0
	var meta = reflection.GetMeta(elemType)

	for ; rows.Next(); i++ {

		var id int64
		var data []byte
		err = rows.Scan(&id, &data)
		if err != nil {
			return err
		}

		var isnew = false
		var item interface{}
		if slice.Len() == i {
			isnew = true
			elemp := reflect.New(elemType)
			item = elemp.Interface()
		} else {
			item = slice.Index(i).Addr().Interface()
		}

		err = json.Unmarshal(data, item)
		if err != nil {
			return err
		}

		var sid = strconv.FormatInt(id, 10)
		meta.SetID(item, sid)

		if isnew {
			slice = reflect.Append(slice, reflect.ValueOf(item).Elem())
			slice = slice.Slice(0, slice.Cap())
		}
	}

	rval.Elem().Set(slice.Slice(0, i))

	return nil
}
Пример #2
0
func (v *view) unmarshal(c *cursor, result interface{}, meta *reflection.Meta) error {
	var err = json.Unmarshal(c.value(), result)
	if err != nil {
		return err
	}
	if meta == nil {
		meta = reflection.GetMeta(result)
	}
	meta.SetID(result, string(c.key()))
	return nil
}
Пример #3
0
// Insert given documents to the collection.
func (c *collection) Insert(docs ...interface{}) error {
	var now = time.Now().UTC()
	for _, doc := range docs {
		var meta = reflection.GetMeta(doc)
		meta.SetID(doc, bson.NewObjectId().Hex())
		meta.SetCreatedAt(doc, now)
		meta.SetUpdatedAt(doc, now)
	}
	var session = c.store.session.Copy()
	defer session.Close()
	var db = session.DB(c.store.dbname)
	var collection = db.C(c.name)
	return collection.Insert(docs...)
}
Пример #4
0
// Update given document.
func (c *collection) Update(selector interface{}, doc interface{}) error {
	var now = time.Now().UTC()
	var meta = reflection.GetMeta(doc)
	meta.SetUpdatedAt(doc, now)

	var json, err = marshal(doc)
	if err != nil {
		return err
	}

	var id, ok = selector.(string)
	if ok {
		tx, err := c.db.Begin(true)
		if err != nil {
			return err
		}

		defer tx.Rollback()

		bucket, err := tx.Bucket(c.name, false)
		if bucket == nil || err != nil {
			if err != nil {
				return err
			}
			return errNotFound
		}

		err = c.update(tx, bucket, doc, []byte(id), json)
		if err != nil {
			return err
		}

		return tx.Commit()
	}

	cursor, err := c.cursor(selector)
	if err != nil {
		return err
	}

	for cursor.next() {
		err = c.update(cursor.transaction(), cursor.bucket(), doc, cursor.key(), json)
		cursor.Close()
		return err
	}

	return errNotFound
}
Пример #5
0
// Update given document.
func (c *collection) Update(selector interface{}, doc interface{}) error {
	// update meta
	var meta = reflection.GetMeta(doc)
	meta.SetUpdatedAt(doc, time.Now().UTC())
	// commit to data store
	var session = c.store.session.Copy()
	defer session.Close()
	var db = session.DB(c.store.dbname)
	var collection = db.C(c.name)
	var id, ok = selector.(string)
	if ok {
		return collection.UpdateId(id, doc)
	} else {
		return collection.Update(mongoFilter([]interface{}{selector}), doc)
	}
}
Пример #6
0
// Insert given documents to the collection.
func (c *collection) Insert(docs ...interface{}) error {
	var tx, err = c.db.Begin(true)
	if err != nil {
		return err
	}

	defer tx.Rollback()

	bucket, err := tx.Bucket(c.name, false)
	if bucket == nil || err != nil {
		if err != nil {
			return err
		}
		return errNotFound
	}

	var now = time.Now().UTC()

	for _, doc := range docs {
		id, err := bucket.NextSequence()
		if err != nil {
			return debug.Err("bucket.NextSequence", err)
		}

		var meta = reflection.GetMeta(doc)
		meta.SetID(doc, id)
		meta.SetCreatedAt(doc, now)
		meta.SetUpdatedAt(doc, now)

		json, err := marshal(doc)
		if err != nil {
			return err
		}

		err = bucket.Set([]byte(id), json)
		if err != nil {
			return err
		}

		err = c.idx.update(tx, id, doc, nil)
		if err != nil {
			return debug.Err("index.Update", err)
		}
	}

	return tx.Commit()
}
Пример #7
0
// Update given document.
func (c *collection) Update(selector interface{}, doc interface{}) error {
	var b, err = json.Marshal(doc)
	if err != nil {
		return err
	}
	// update meta
	var meta = reflection.GetMeta(doc)
	meta.SetUpdatedAt(doc, time.Now().UTC())
	// commit to data store
	var json = string(b)
	if id := parseInt(selector); id != nil {
		_, err = c.Exec(fmt.Sprintf("UPDATE %s SET data=$1 WHERE id=$2", c.name), json, id)
		return err
	}
	var filter, args = makeFilter([]interface{}{selector})
	args = append([]interface{}{json}, args...)
	_, err = c.Exec(fmt.Sprintf("UPDATE %s SET data=$1 WHERE %s", c.name, filter), args...)
	return err
}
Пример #8
0
func (c *cursor) Next(result interface{}) (bool, error) {
	if !c.rows.Next() {
		return false, c.rows.Err()
	}
	var id int64
	var data []byte
	var err = c.rows.Scan(&id, &data)
	if err != nil {
		return false, err
	}
	err = json.Unmarshal(data, result)
	if err != nil {
		return false, err
	}
	var meta = reflection.GetMeta(result)
	var sid = strconv.FormatInt(id, 10)
	meta.SetID(result, sid)
	return true, nil
}
Пример #9
0
// All fetches all results within the result set.
func (v *view) All(result interface{}) error {
	rval := reflect.ValueOf(result)
	if rval.Kind() != reflect.Ptr || rval.Elem().Kind() != reflect.Slice {
		return errNotSliceAddr
	}

	var c, err = v.cursor(false)
	if err != nil {
		return err
	}

	slice := rval.Elem()
	slice = slice.Slice(0, slice.Cap())
	elemType := slice.Type().Elem()

	var i = 0
	var meta = reflection.GetMeta(elemType)

	for ; c.next(); i++ {
		if slice.Len() == i {
			elemp := reflect.New(elemType)
			item := elemp.Interface()
			err = v.unmarshal(c, item, meta)
			if err != nil {
				return err
			}
			slice = reflect.Append(slice, reflect.ValueOf(item).Elem())
			slice = slice.Slice(0, slice.Cap())
		} else {
			item := slice.Index(i).Addr().Interface()
			err = v.unmarshal(c, item, meta)
			if err != nil {
				return err
			}
		}
	}

	rval.Elem().Set(slice.Slice(0, i))

	return nil
}
Пример #10
0
// Finds one result.
func (c *collection) FindOne(result interface{}, query *query) error {
	var stmt, args = query.makeSelectStmt("")
	var row, err = c.QueryRow(stmt, args...)
	if err != nil {
		return err
	}
	var id int64
	var data []byte
	err = row.Scan(&id, &data)
	if err != nil {
		return err
	}
	err = json.Unmarshal(data, result)
	if err != nil {
		return err
	}
	var meta = reflection.GetMeta(result)
	var sid = strconv.FormatInt(id, 10)
	meta.SetID(result, sid)
	return nil
}
Пример #11
0
func (c *collection) insertOne(doc interface{}) error {
	var now = time.Now().UTC()
	var meta = reflection.GetMeta(doc)
	meta.SetCreatedAt(doc, now)
	meta.SetUpdatedAt(doc, now)
	var b, err = json.Marshal(doc)
	if err != nil {
		return err
	}
	var cmd = fmt.Sprintf("INSERT INTO %s (data) VALUES ('%s') RETURNING id", c.name, string(b))
	row, err := c.QueryRow(cmd)
	if err != nil {
		return err
	}
	var id int64
	err = row.Scan(&id)
	if err != nil {
		return err
	}
	var sid = strconv.FormatInt(id, 10)
	meta.SetID(doc, sid)
	return nil
}