Example #1
0
func analyzeSelectExprs(exprs sqlparser.SelectExprs, table *schema.Table) (selects []int, err error) {
	selects = make([]int, 0, len(exprs))
	for _, expr := range exprs {
		switch expr := expr.(type) {
		case *sqlparser.StarExpr:
			// Append all columns.
			for colIndex := range table.Columns {
				selects = append(selects, colIndex)
			}
		case *sqlparser.NonStarExpr:
			name := sqlparser.GetColName(expr.Expr)
			if name == "" {
				// Not a simple column name.
				return nil, nil
			}
			colIndex := table.FindColumn(name)
			if colIndex == -1 {
				return nil, fmt.Errorf("column %s not found in table %s", name, table.Name)
			}
			selects = append(selects, colIndex)
		default:
			panic("unreachable")
		}
	}
	return selects, nil
}
Example #2
0
func findSplitColumnsInSchema(
	splitColumnNames []sqlparser.ColIdent, tableSchema *schema.Table,
) ([]*schema.TableColumn, error) {
	result := make([]*schema.TableColumn, 0, len(splitColumnNames))
	for _, splitColumnName := range splitColumnNames {
		i := tableSchema.FindColumn(splitColumnName.Original())
		if i == -1 {
			return nil, fmt.Errorf("can't find split column: %v", splitColumnName)
		}
		result = append(result, &tableSchema.Columns[i])
	}
	return result, nil
}
Example #3
0
func (node *Node) execAnalyzeSelectExpressions(table *schema.Table) (selects []int) {
	selects = make([]int, 0, node.Len())
	for i := 0; i < node.Len(); i++ {
		if name := node.At(i).execAnalyzeSelectExpression(); name != "" {
			if name == "*" {
				for colIndex := range table.Columns {
					selects = append(selects, colIndex)
				}
			} else if colIndex := table.FindColumn(name); colIndex != -1 {
				selects = append(selects, colIndex)
			} else {
				panic(NewParserError("Column %s not found in table %s", name, table.Name))
			}
		} else {
			// Complex expression
			return nil
		}
	}
	return selects
}
Example #4
0
func execAnalyzeSelectExprs(exprs SelectExprs, table *schema.Table) (selects []int) {
	selects = make([]int, 0, len(exprs))
	for _, expr := range exprs {
		if name := execAnalyzeSelectExpr(expr); name != "" {
			if name == "*" {
				for colIndex := range table.Columns {
					selects = append(selects, colIndex)
				}
			} else if colIndex := table.FindColumn(name); colIndex != -1 {
				selects = append(selects, colIndex)
			} else {
				panic(NewParserError("column %s not found in table %s", name, table.Name))
			}
		} else {
			// Complex expression
			return nil
		}
	}
	return selects
}