Beispiel #1
0
func (this *Table) GetLink(chain string, foreignKeys coll.Collection) *LinkNav {
	idx := strings.Index(chain, FK_NAV_SEP)
	var link Str
	if idx > 0 {
		link = Str(chain[:idx])
	} else {
		link = Str(chain)
	}

	// check columns
	o, _ := this.columnsMap.Get(link)
	c, _ := o.(*Column)

	if c != nil {
		return NewLinkNav(foreignKeys, c)
	}

	var fk *Association
	if this.associationMap != nil {
		o, _ = this.associationMap.Get(link)
		fk, _ = o.(*Association)
	}

	if fk != nil {
		if idx < 0 {
			foreignKeys.Add(fk)
			return NewLinkNav(foreignKeys, c)
		}
		return fk.GetLink(chain[idx+1:], this, foreignKeys)
	}
	return nil
}
Beispiel #2
0
func NewLinkNav(fks coll.Collection, o interface{}) *LinkNav {
	this := new(LinkNav)
	if fks != nil && fks.Size() != 0 {
		this.foreignKey = fks
	}
	this.object = o
	return this
}
Beispiel #3
0
func (this *Association) GetLink(chain string, from *Table, foreignKeys coll.Collection) *LinkNav {
	// verifica as tabelas
	var table *Table
	if this.tableFrom.Equals(from) {
		table = this.tableTo
	} else {
		table = this.tableFrom
	}

	foreignKeys.Add(this)
	return table.GetLink(chain, foreignKeys)
}
Beispiel #4
0
func (this *EntityTransformer) OnTransformation(result coll.Collection, instance interface{}) {
	if instance != nil {
		result.Add(instance)
	}
}
Beispiel #5
0
func QueryForPage(
	query *db.Query,
	criteria Criteria,
	target interface{},
	transformer func(in interface{}) interface{},
) (Page, error) {
	max := criteria.PageSize
	first := (criteria.Page - 1) * max
	// for the first page the offset is zero
	query.Skip(first)
	if max > 0 {
		query.Limit(max + 1)
	}

	var entities coll.Collection
	var err error
	var results []interface{}

	if reflect.TypeOf(target).Kind() == reflect.Func {
		results, err = query.ListInto(target)
	} else if _, ok := target.(tk.Hasher); ok {
		entities, err = query.ListFlatTreeOf(target)
	} else {
		entities, err = query.ListOf(target)
	}
	if err != nil {
		return Page{}, err
	}

	if results == nil {
		results = entities.Elements()
	}

	page := Page{}
	size := int64(len(results))
	if max > 0 && size > max {
		page.Last = false
		page.Results = results[:max]
	} else {
		page.Last = true
		page.Results = results
	}

	// transform results
	if transformer != nil {
		for k, v := range page.Results {
			page.Results[k] = transformer(v)
		}
	}

	// count records
	if criteria.CountRecords {
		DB := query.GetDb()
		cnt := DB.Query(query.GetTable())
		cnt.Copy(query)
		cnt.ColumnsReset()
		cnt.CountAll()
		cnt.OrdersReset()
		var recs int64
		_, err = cnt.SelectInto(&recs)
		if err != nil {
			return Page{}, err
		}
		page.Count = Int64(recs)
	}

	return page, nil
}
func (this *EntityTreeTransformer) OnTransformation(result coll.Collection, instance interface{}) {
	this.crawler.Rewind()
	if instance != nil && (!this.reuse || !result.Contains(instance)) {
		result.Add(instance)
	}
}