// FIXME: use sqlx's rebind, which was written after it had been created for modl
// ReBind formats the bindvars in the query string (these are '?') for the dialect.
510836
func ReBind(query string, dialect Dialect) string {

	binder := dialect.BindVar(0)
	if binder == "?" {
		return query
	}

	for i, j := 0, strings.Index(query, "?"); j >= 0; i++ {
		query = strings.Replace(query, "?", dialect.BindVar(i), 1)
		j = strings.Index(query, "?")
	}
	return query
}
508909
func cloneBindVariables(bindVariables map[string]*querypb.BindVariable) map[string]*querypb.BindVariable {
	result := make(map[string]*querypb.BindVariable)
	for key, value := range bindVariables {
		result[key] = value
	}
	return result
}
1308754
func (f *Pub) Bind(rx Publisher, cl bool) {
	f.branches.Add(rx)
	rx.UseRoot(f)

	if cl {
		f.enders.Add(rx)
	}
}
515224
func (mp *Proxy) doSet(ctx context.Context, session *ProxySession, sql string, bindVariables map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
	vals, _, err := sqlparser.ExtractSetValues(sql)
	if err != nil {
		return nil, err
	}

	for k, v := range vals {
		switch k.Key {
		case "autocommit":
			val, ok := v.(int64)
			if !ok {
				return nil, fmt.Errorf("unexpected value type for autocommit: %T", v)
			}
			switch val {
			case 0:
				session.Autocommit = false
			case 1:
				if !session.Autocommit && session.TransactionID != 0 {
					if err := mp.doCommit(ctx, session); err != nil {
						return nil, err
					}
				}
				session.Autocommit = true
			default:
				return nil, fmt.Errorf("unexpected value for autocommit: %d", val)
			}
		case "charset", "names":
			val, ok := v.(string)
			if !ok {
				return nil, fmt.Errorf("unexpected value type for charset/names: %T", v)
			}
			switch val {
			case "", "utf8", "utf8mb4", "latin1", "default":
				break
			default:
				return nil, fmt.Errorf("unexpected value for charset/names: %v", val)
			}
		default:
			log.Warningf("Ignored inapplicable SET %v = %v", k, v)
		}
	}

	return &sqltypes.Result{}, nil
}
1311020
func isBind(options []string) (bool, []string) {
	bindRemountOpts := []string{"remount"}
	bind := false

	if len(options) != 0 {
		for _, option := range options {
			switch option {
			case "bind":
				bind = true
				break
			case "remount":
				break
			default:
				bindRemountOpts = append(bindRemountOpts, option)
			}
		}
	}

	return bind, bindRemountOpts
}
27148
func (h *HTTPExtender) Bind(binding *v1.Binding) error {
	var result schedulerapi.ExtenderBindingResult
	if !h.IsBinder() {
		// This shouldn't happen as this extender wouldn't have become a Binder.
		return fmt.Errorf("Unexpected empty bindVerb in extender")
	}
	req := &schedulerapi.ExtenderBindingArgs{
		PodName:      binding.Name,
		PodNamespace: binding.Namespace,
		PodUID:       binding.UID,
		Node:         binding.Target.Name,
	}
	if err := h.send(h.bindVerb, &req, &result); err != nil {
		return err
	}
	if result.Error != "" {
		return fmt.Errorf(result.Error)
	}
	return nil
}
538200
func (scope *Scope) AddToVars(value interface{}) string {
	_, skipBindVar := scope.InstanceGet("skip_bindvar")

	if expr, ok := value.(*expr); ok {
		exp := expr.expr
		for _, arg := range expr.args {
			if skipBindVar {
				scope.AddToVars(arg)
			} else {
				exp = strings.Replace(exp, "?", scope.AddToVars(arg), 1)
			}
		}
		return exp
	}

	scope.SQLVars = append(scope.SQLVars, value)

	if skipBindVar {
		return "?"
	}
	return scope.Dialect().BindVar(len(scope.SQLVars))
}
434772
func RewriteSelect(m *SqlSelect) {
	originalCols := m.Columns
	m.Columns = make(Columns, 0, len(originalCols)+5)
	rewriteIntoProjection(m, originalCols)
	rewriteIntoProjection(m, m.GroupBy)
	if m.Where != nil {
		colsToAdd := expr.FindAllIdentityField(m.Where.Expr)
		addIntoProjection(m, colsToAdd)
	}
	rewriteIntoProjection(m, m.OrderBy)
}
1005153
func (q *Queryx) BindMap(arg map[string]interface{}) *Queryx {
	arglist, err := bindMapArgs(q.Names, arg)
	if err != nil {
		q.err = fmt.Errorf("bind error: %s", err)
	} else {
		q.err = nil
		q.Bind(arglist...)
	}

	return q
}
1074421
func appendBind(
	buf []byte,
	destinationPortal,
	preparedStatement string,
	connInfo *pgtype.ConnInfo,
	parameterOIDs []pgtype.OID,
	arguments []interface{},
	resultFormatCodes []int16,
) ([]byte, error) {
	buf = append(buf, 'B')
	sp := len(buf)
	buf = pgio.AppendInt32(buf, -1)
	buf = append(buf, destinationPortal...)
	buf = append(buf, 0)
	buf = append(buf, preparedStatement...)
	buf = append(buf, 0)

	var err error
	arguments, err = convertDriverValuers(arguments)
	if err != nil {
		return nil, err
	}

	buf = pgio.AppendInt16(buf, int16(len(parameterOIDs)))
	for i, oid := range parameterOIDs {
		buf = pgio.AppendInt16(buf, chooseParameterFormatCode(connInfo, oid, arguments[i]))
	}

	buf = pgio.AppendInt16(buf, int16(len(arguments)))
	for i, oid := range parameterOIDs {
		var err error
		buf, err = encodePreparedStatementArgument(connInfo, buf, oid, arguments[i])
		if err != nil {
			return nil, err
		}
	}

	buf = pgio.AppendInt16(buf, int16(len(resultFormatCodes)))
	for _, fc := range resultFormatCodes {
		buf = pgio.AppendInt16(buf, fc)
	}
	pgio.SetInt32(buf[sp:], int32(len(buf[sp:])))

	return buf, nil
}
1363915
func (nz *normalizer) convertSQLVal(node *SQLVal) {
	bval := nz.sqlToBindvar(node)
	if bval == nil {
		return
	}

	bvname := nz.newName()
	nz.bindVars[bvname] = bval

	node.Type = ValArg
	node.Val = append([]byte(":"), bvname...)
}
1282429
func Binding(dn, pwd string) Option {
	return func(r *Realm) {
		r.bindDN = dn
		r.bindPwd = pwd
	}
}
1252265
func (db *DB) Queryx(query string, args ...interface{}) (*Rows, error) {
	r, err := db.DB.Query(query, args...)
	if err != nil {
		return nil, err
	}
	return &Rows{Rows: r, unsafe: db.unsafe, Mapper: db.Mapper}, err
}
530972
func Bind(params *Params, name string, typ reflect.Type) reflect.Value {
	if binder, found := binderForType(typ); found {
		return binder.Bind(params, name, typ)
	}
	return reflect.Zero(typ)
}
515742
func BindDomain(ctx sessionctx.Context, domain *Domain) {
	ctx.SetValue(domainKey, domain)
}
1372713
func populateNewBindVariable(
	bindVariableName string,
	bindVariableValue *querypb.BindVariable,
	resultBindVariables map[string]*querypb.BindVariable) {
	_, alreadyInMap := resultBindVariables[bindVariableName]
	if alreadyInMap {
		panic(fmt.Sprintf(
			"bindVariable %v already exists in map: %v. bindVariableValue given: %v",
			bindVariableName,
			resultBindVariables,
			bindVariableValue))
	}
	resultBindVariables[bindVariableName] = bindVariableValue
}
1224533
func (b *binder) Bind(binding *v1.Binding) error {
	klog.V(3).Infof("Attempting to bind %v to %v", binding.Name, binding.Target.Name)
	return b.Client.CoreV1().Pods(binding.Namespace).Bind(binding)
}