예제 #1
0
func NormalizeTree(tree *sqlparser.Node) (normalized string, bindVars map[string]interface{}, err error) {
	defer handleError(&err)

	bindVars = make(map[string]interface{})
	counter := 0
	NormalizeNode(tree, bindVars, &counter)
	return tree.String(), bindVars, nil
}
예제 #2
0
func NormalizeNode(self *sqlparser.Node, bindVars map[string]interface{}, counter *int) {
	for i := 0; i < self.Len(); i++ {
		switch self.At(i).Type {
		case sqlparser.STRING:
			*counter++
			bindVars[fmt.Sprintf("v%d", *counter)] = string(self.At(i).Value)
			self.Set(i, newArgumentNode(counter))
		case sqlparser.NUMBER:
			*counter++
			varname := fmt.Sprintf("v%d", *counter)
			valstr := string(self.At(i).Value)
			if ival, err := strconv.ParseInt(valstr, 0, 64); err == nil {
				bindVars[varname] = ival
			} else if uval, err := strconv.ParseUint(valstr, 0, 64); err == nil {
				bindVars[varname] = uval
			} else if fval, err := strconv.ParseFloat(valstr, 64); err == nil {
				bindVars[varname] = fval
			} else {
				panic(sqlparser.NewParserError("%v", err))
			}
			self.Set(i, newArgumentNode(counter))
		default:
			for i := 0; i < self.Len(); i++ {
				NormalizeNode(self.At(i), bindVars, counter)
			}
		}
	}
}