Esempio n. 1
0
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))
}
Esempio n. 2
0
func (g *gensql) populateMessage(file *generator.FileDescriptor, msg *generator.Descriptor) {
	model := limbo.GetModel(msg)
	model.MessageType = "." + file.GetPackage() + "." + msg.GetName()

	{ // default scanner
		var found bool
		for _, scanner := range model.Scanner {
			if scanner.Name == "" {
				found = true
				break
			}
		}
		if !found {
			model.Scanner = append(model.Scanner, &limbo.ScannerDescriptor{Fields: "*"})
		}
	}

	for _, scanner := range model.Scanner {
		scanner.MessageType = "." + file.GetPackage() + "." + msg.GetName()
	}

	for _, field := range msg.GetField() {
		if column := limbo.GetColumn(field); column != nil {
			column.MessageType = "." + file.GetPackage() + "." + msg.GetName()
			column.FieldName = field.GetName()
			if column.Name == "" {
				column.Name = field.GetName()
			}

			model.Column = append(model.Column, column)
		}

		if join := limbo.GetJoin(field); join != nil {
			if field.GetType() != pb.FieldDescriptorProto_TYPE_MESSAGE {
				g.gen.Fail(field.GetName(), "in", msg.GetName(), "must be a message")
			}

			join.MessageType = "." + file.GetPackage() + "." + msg.GetName()
			join.FieldName = field.GetName()
			join.ForeignMessageType = field.GetTypeName()

			if join.Name == "" {
				join.Name = field.GetName()
			}

			if join.Key == "" {
				join.Key = field.GetName() + "_id"
			}

			if join.ForeignKey == "" {
				join.ForeignKey = "id"
			}

			model.Join = append(model.Join, join)
		}
	}

	sort.Sort(limbo.SortedColumnDescriptors(model.Column))
	sort.Sort(limbo.SortedJoinDescriptors(model.Join))
	sort.Sort(limbo.SortedScannerDescriptors(model.Scanner))
}