func (j *Join) selectM2M(db dber) error { path := j.JoinModel.Path() path = path[:len(path)-1] m2mModel := newM2MModel(j) m2mCols := columns(j.Rel.M2M.ModelName+".", j.Rel.M2MBaseFKs) m2mVals := values(j.BaseModel.Root(), path, j.BaseModel.Table().PKs) q := NewQuery(db, m2mModel) q.Columns("*") q.Join( "JOIN ? AS ? ON (?) IN (?)", types.F(j.Rel.M2M.Name), types.F(j.Rel.M2M.ModelName), types.Q(m2mCols), types.Q(m2mVals), ) for i, pk := range j.JoinModel.Table().PKs { q.Where( "?.? = ?.?", types.F(j.JoinModel.Table().ModelName), types.F(pk.SQLName), types.F(j.Rel.M2M.ModelName), types.F(j.Rel.M2MJoinFKs[i].SQLName), ) } err := q.Select() if err != nil { return err } return nil }
func FormatQuery(query string, params ...interface{}) (types.Q, error) { b, err := AppendQuery(nil, query, params...) if err != nil { return "", err } return types.Q(b), nil }
func (q *Query) Count(count *int) error { if q.err != nil { return q.err } q.columns = []types.ValueAppender{types.Q("COUNT(*)")} q.orders = nil q.limit = 0 q.offset = 0 joins := q.model.GetJoins() for i := range joins { j := &joins[i] if j.Rel.One { j.JoinOne(q) } } sel := &selectQuery{ Query: q, } _, err := q.db.Query(Scan(count), sel, q.model) if err != nil { return err } return nil }
func (j *Join) selectMany(db dber) error { root := j.JoinModel.Root() path := j.JoinModel.Path() path = path[:len(path)-1] manyModel := newManyModel(j) q := NewQuery(db, manyModel) q.columns = append(q.columns, types.F(j.JoinModel.Table().ModelName+".*")) cols := columns(j.JoinModel.Table().ModelName+".", j.Rel.FKs) vals := values(root, path, j.BaseModel.Table().PKs) q.Where(`(?) IN (?)`, types.Q(cols), types.Q(vals)) if j.Rel.Polymorphic != "" { q.Where(`? = ?`, types.F(j.Rel.Polymorphic+"type"), j.BaseModel.Table().ModelName) } err := q.Select() if err != nil { return err } return nil }