func (g *gensql) generateQueryPrefix(message *generator.Descriptor, scanner *limbo.ScannerDescriptor) string { var ( buf bytes.Buffer model = limbo.GetModel(message) ) buf.WriteString("SELECT") for i, column := range scanner.Column { if i > 0 { buf.WriteByte(',') } buf.WriteByte(' ') if column.JoinedWith == "" { buf.WriteString(model.Table) buf.WriteByte('.') } buf.WriteString(column.Name) } buf.WriteString(" FROM ") buf.WriteString(model.Table) for _, join := range scanner.Join { buf.WriteString(" LEFT JOIN ") buf.WriteString(join.Table) buf.WriteString(" AS ") buf.WriteString(join.Name) buf.WriteString(" ON ") buf.WriteString(join.Name) buf.WriteByte('.') buf.WriteString(join.ForeignKey) buf.WriteString(" = ") if join.JoinedWith != "" { buf.WriteString(scanner.LookupJoin(join.JoinedWith).Name) } else { buf.WriteString(model.Table) } buf.WriteByte('.') buf.WriteString(join.Key) } return buf.String() }
func (g *gensql) populateScanner(msg *generator.Descriptor, scanner *limbo.ScannerDescriptor) { if len(scanner.Column) > 0 { return } var ( ops = strings.Split(scanner.Fields, ",") queue = ops model = limbo.GetModel(msg) ) queue = ops ops = nil for _, op := range queue { op = strings.TrimSpace(op) if op == "*" { for _, column := range model.Column { ops = append(ops, column.FieldName) } for _, join := range model.Join { ops = append(ops, join.FieldName) } } else { ops = append(ops, op) } } queue = ops ops = nil for _, op := range queue { var ( found bool isRemove bool name = op ) if strings.HasPrefix(op, "-") { name = op[1:] isRemove = true } if !strings.ContainsRune(name, ':') { name += ":" } for _, scanner := range model.DeepScanner { if scanner.Name == name { found = true for _, column := range scanner.Column { if !isRemove { ops = append(ops, column.FieldName) } else { ops = append(ops, "-"+column.FieldName) } } break } } if !found { ops = append(ops, op) } } selected := make(map[string]*limbo.ColumnDescriptor) queue = ops ops = nil for _, op := range queue { var ( found bool isRemove bool name = op ) if strings.HasPrefix(op, "-") { name = op[1:] isRemove = true } for _, column := range model.DeepColumn { if column.FieldName == name { found = true if !isRemove { selected[name] = column ops = append(ops, op) } else { selected[name] = nil } break } } if !found { g.gen.Fail("unknown column", name) } } var columns []*limbo.ColumnDescriptor queue = ops ops = nil for _, op := range queue { column := selected[op] if column != nil { columns = append(columns, column) selected[op] = nil } } scanner.Column = columns joinQueue := []string{} seenJoin := make(map[string]bool) for _, column := range scanner.Column { if column.JoinedWith == "" { continue } joinQueue = append(joinQueue, column.JoinedWith) } for len(joinQueue) > 0 { joinName := joinQueue[0] joinQueue = joinQueue[1:] if seenJoin[joinName] { continue } seenJoin[joinName] = true var found bool for _, join := range model.DeepJoin { if joinName != join.Name { continue } found = true scanner.Join = append(scanner.Join, join) if join.JoinedWith != "" { joinQueue = append(joinQueue, join.JoinedWith) } break } if !found { g.gen.Fail("unknown join", joinName) } } sort.Sort(limbo.SortedColumnDescriptors(scanner.Column)) sort.Sort(limbo.SortedJoinDescriptors(scanner.Join)) }