Esempio n. 1
0
func (sb *InfoBinder) bindColumnInTableSources(cn *ast.ColumnName, tableSources []*ast.TableSource) (done bool) {
	var matchedResultField *ast.ResultField
	if cn.Table.L != "" {
		var matchedTable ast.ResultSetNode
		for _, ts := range tableSources {
			if cn.Table.L == ts.AsName.L {
				// different table name.
				matchedTable = ts
				break
			}
			if tn, ok := ts.Source.(*ast.TableName); ok {
				if cn.Table.L == tn.Name.L {
					matchedTable = ts
				}
			}
		}
		if matchedTable != nil {
			resultFields := matchedTable.GetResultFields()
			for _, rf := range resultFields {
				if rf.ColumnAsName.L == cn.Name.L || rf.Column.Name.L == cn.Name.L {
					// bind column.
					matchedResultField = rf
					break
				}
			}
		}
	} else {
		for _, ts := range tableSources {
			rfs := ts.GetResultFields()
			for _, rf := range rfs {
				matchAsName := rf.ColumnAsName.L != "" && rf.ColumnAsName.L == cn.Name.L
				matchColumnName := rf.ColumnAsName.L == "" && rf.Column.Name.L == cn.Name.L
				if matchAsName || matchColumnName {
					if matchedResultField != nil {
						sb.Err = errors.Errorf("column %s is ambiguous.", cn.Name.O)
						return true
					}
					matchedResultField = rf
				}
			}
		}
	}
	if matchedResultField != nil {
		// bind column.
		cn.ColumnInfo = matchedResultField.Column
		cn.TableInfo = matchedResultField.Table
		return true
	}
	return false
}
Esempio n. 2
0
func (sb *InfoBinder) bindColumnInResultFields(cn *ast.ColumnName, rfs []*ast.ResultField) bool {
	var matchedResultField *ast.ResultField
	for _, rf := range rfs {
		matchAsName := rf.ColumnAsName.L != "" && rf.ColumnAsName.L == cn.Name.L
		matchColumnName := rf.ColumnAsName.L == "" && rf.Column.Name.L == cn.Name.L
		if matchAsName || matchColumnName {
			if matchedResultField != nil {
				sb.Err = errors.Errorf("column %s is ambiguous.", cn.Name.O)
				return false
			}
			matchedResultField = rf
		}
	}
	if matchedResultField != nil {
		// bind column.
		cn.ColumnInfo = matchedResultField.Column
		cn.TableInfo = matchedResultField.Table
		return true
	}
	return false
}