// TODO(austin) Move doAssign to here
/*
 * Statement compiler
 */
340376
func (a *stmtCompiler) compile(s ast.Stmt) {
	if a.block.inner != nil {
		log.Panic("Child scope still entered")
	}

	notimpl := false
	switch s := s.(type) {
	case *ast.BadStmt:
		// Error already reported by parser.
		a.silentErrors++

	case *ast.DeclStmt:
		a.compileDeclStmt(s)

	case *ast.EmptyStmt:
		// Do nothing.

	case *ast.LabeledStmt:
		a.compileLabeledStmt(s)

	case *ast.ExprStmt:
		a.compileExprStmt(s)

	case *ast.IncDecStmt:
		a.compileIncDecStmt(s)

	case *ast.AssignStmt:
		a.compileAssignStmt(s)

	case *ast.GoStmt:
		notimpl = true

	case *ast.DeferStmt:
		notimpl = true

	case *ast.ReturnStmt:
		a.compileReturnStmt(s)

	case *ast.BranchStmt:
		a.compileBranchStmt(s)

	case *ast.BlockStmt:
		a.compileBlockStmt(s)

	case *ast.IfStmt:
		a.compileIfStmt(s)

	case *ast.CaseClause:
		a.diag("case clause outside switch")

	case *ast.SwitchStmt:
		a.compileSwitchStmt(s)

	case *ast.TypeSwitchStmt:
		notimpl = true

	case *ast.CommClause:
		notimpl = true

	case *ast.SelectStmt:
		notimpl = true

	case *ast.ForStmt:
		a.compileForStmt(s)

	case *ast.RangeStmt:
		notimpl = true

	default:
		log.Panicf("unexpected ast node type %T", s)
	}

	if notimpl {
		a.diag("%T statement node not implemented", s)
	}

	if a.block.inner != nil {
		log.Panic("Forgot to exit child scope")
	}
}
322793
func (b *BindObject) Assign(ctx []reflect.Value, toSetter func(reflect.Value)) {
	if b.BindType == Dynamic {
		toSetter(b.ReturnValue(ctx))
		return
	}
	toSetter(b.Value)
}
659007
func (lx *lexer) assignComments() {
	// Generate preorder and postorder lists.
	lx.order(lx.prog)

	// Split into whole-line comments and suffix comments.
	var line, suffix []Comment
	for _, com := range lx.comments {
		if com.Suffix {
			suffix = append(suffix, com)
		} else {
			line = append(line, com)
		}
	}

	// Assign line comments to syntax immediately following.
	for _, x := range lx.pre {
		start := x.GetSpan().Start
		xcom := x.GetComments()
		for len(line) > 0 && start.Byte >= line[0].Start.Byte {
			xcom.Before = append(xcom.Before, line[0])
			line = line[1:]
		}
	}

	// Remaining line comments go at end of file.
	lx.prog.Comments.After = append(lx.prog.Comments.After, line...)

	// Assign suffix comments to syntax immediately before.
	for i := len(lx.post) - 1; i >= 0; i-- {
		x := lx.post[i]

		// Do not assign suffix comments to call, list, end-of-list, or whole file.
		// Instead assign them to the last argument, element, or rule.
		/*
			switch x.(type) {
			case *CallExpr, *ListExpr, *End, *File:
				continue
			}
		*/

		// Do not assign suffix comments to something that starts
		// on an earlier line, so that in
		//
		//	tags = [ "a",
		//		"b" ], # comment
		//
		// we assign the comment to "b" and not to tags = [ ... ].
		span := x.GetSpan()
		start, end := span.Start, span.End
		if start.Line != end.Line {
			continue
		}
		xcom := x.GetComments()
		for len(suffix) > 0 && end.Byte <= suffix[len(suffix)-1].Start.Byte {
			xcom.Suffix = append(xcom.Suffix, suffix[len(suffix)-1])
			suffix = suffix[:len(suffix)-1]
		}
	}

	// We assigned suffix comments in reverse.
	// If multiple suffix comments were appended to the same
	// expression node, they are now in reverse. Fix that.
	for _, x := range lx.post {
		reverseComments(x.GetComments().Suffix)
	}

	// Remaining suffix comments go at beginning of file.
	lx.prog.Comments.Before = append(lx.prog.Comments.Before, suffix...)
}
1183708
func do(f func()) {
	done := make(chan bool, 1)
	mainfunc <- func() {
		f()
		done <- true
	}
	<-done
}
803681
func (c *Client) Assignment() (map[string][]int32, error) {
	request := &clientRequest{
		Key: RequestKeyAssignments,
	}

	assignments := make(map[string][]int32)
	err := c.handleRequest(request, &assignments)
	return assignments, err
}
1263870
func parseAssignments(statements string) (*map[string]string, error) {
	var cursor int
	result := make(map[string]string)
	var errorMsg = fmt.Errorf("malformed header value: %s", statements)
	for {
		// parse key
		equalIndex := nextOccurrence(statements, cursor, "=")
		if equalIndex == -1 {
			return nil, errorMsg
		}
		key := strings.TrimSpace(statements[cursor:equalIndex])

		// parse value
		cursor = nextNoneSpace(statements, equalIndex+1)
		if cursor == -1 {
			return nil, errorMsg
		}
		// case: value is quoted
		if statements[cursor] == '"' {
			cursor = cursor + 1
			// like I said, not handling escapes, but this will skip any comma that's
			// within the quotes which is somewhat more likely
			closeQuoteIndex := nextOccurrence(statements, cursor, "\"")
			if closeQuoteIndex == -1 {
				return nil, errorMsg
			}
			value := statements[cursor:closeQuoteIndex]
			result[key] = value

			commaIndex := nextNoneSpace(statements, closeQuoteIndex+1)
			if commaIndex == -1 {
				// no more comma, done
				return &result, nil
			} else if statements[commaIndex] != ',' {
				// expect comma immediately after close quote
				return nil, errorMsg
			} else {
				cursor = commaIndex + 1
			}
		} else {
			commaIndex := nextOccurrence(statements, cursor, ",")
			endStatements := commaIndex == -1
			var untrimmed string
			if endStatements {
				untrimmed = statements[cursor:commaIndex]
			} else {
				untrimmed = statements[cursor:]
			}
			value := strings.TrimSpace(untrimmed)

			if len(value) == 0 {
				// disallow empty value without quote
				return nil, errorMsg
			}

			result[key] = value

			if endStatements {
				return &result, nil
			}
			cursor = commaIndex + 1
		}
	}
}
463727
func RunWithAssignedApp(runThis string) string {
	var openWith string
	if runtime.GOOS == "linux" {
		openWith = "xdg-open"
	} else if runtime.GOOS == "darwin" {
		openWith = "open"
	} else {
		return fmt.Sprintf("Error: %s is not supported as yet.", runtime.GOOS)
	}

	if !IsSystemCmd(openWith) {
		return fmt.Sprintf("Error: %s not found on this machine.", openWith)
	}

	cmdToRun := fmt.Sprintf("%s %s", openWith, runThis)
	return ExecOutput(cmdToRun)
}
342313
func Do(do DoFunc) Opt {
	return func(c *Client) Opt {
		old := c.do
		c.do = do
		return Do(old)
	}
}
968916
func (s *InstancesCount) SetAssigning(v int64) *InstancesCount {
	s.Assigning = &v
	return s
}
698761
func Compile(ctx context.Context, sctx sessionctx.Context, stmtNode ast.StmtNode) (sqlexec.Statement, error) {
	compiler := executor.Compiler{Ctx: sctx}
	stmt, err := compiler.Compile(ctx, stmtNode)
	return stmt, err
}
822393
func TaskAssigned(tx *bolt.Tx, id string) bool {
	bkt := getTaskBucket(tx, id)
	if bkt == nil {
		return false
	}

	return len(bkt.Get(bucketKeyAssigned)) > 0
}
1292683
func StatementCompatibilityHelper(statements dbplugin.Statements) dbplugin.Statements {
	switch {
	case len(statements.Creation) > 0 && len(statements.CreationStatements) == 0:
		statements.CreationStatements = strings.Join(statements.Creation, ";")
	case len(statements.CreationStatements) > 0:
		statements.Creation = []string{statements.CreationStatements}
	}
	switch {
	case len(statements.Revocation) > 0 && len(statements.RevocationStatements) == 0:
		statements.RevocationStatements = strings.Join(statements.Revocation, ";")
	case len(statements.RevocationStatements) > 0:
		statements.Revocation = []string{statements.RevocationStatements}
	}
	switch {
	case len(statements.Renewal) > 0 && len(statements.RenewStatements) == 0:
		statements.RenewStatements = strings.Join(statements.Renewal, ";")
	case len(statements.RenewStatements) > 0:
		statements.Renewal = []string{statements.RenewStatements}
	}
	switch {
	case len(statements.Rollback) > 0 && len(statements.RollbackStatements) == 0:
		statements.RollbackStatements = strings.Join(statements.Rollback, ";")
	case len(statements.RollbackStatements) > 0:
		statements.Rollback = []string{statements.RollbackStatements}
	}
	return statements
}
14293
func Statements(dst io.Writer, from, to model.Stmts, options ...Option) error {
	var txn bool
	for _, o := range options {
		switch o.Name() {
		case optkeyTransaction:
			txn = o.Value().(bool)
		}
	}

	ctx := newDiffCtx(from, to)

	var procs = []func(*diffCtx, io.Writer) (int64, error){
		dropTables,
		createTables,
		alterTables,
	}

	var buf bytes.Buffer
	if txn {
		buf.WriteString("\nBEGIN;\n\nSET FOREIGN_KEY_CHECKS = 0;")
	}

	for _, p := range procs {
		var pbuf bytes.Buffer
		n, err := p(ctx, &pbuf)
		if err != nil {
			return errors.Wrap(err, `failed to produce diff`)
		}
		if txn && n > 0 || !txn && buf.Len() > 0 && n > 0 {
			buf.WriteString("\n\n")
		}
		pbuf.WriteTo(&buf)
	}
	if txn {
		buf.WriteString("\n\nSET FOREIGN_KEY_CHECKS = 1;\n\nCOMMIT;")
	}

	if _, err := buf.WriteTo(dst); err != nil {
		return errors.Wrap(err, `failed to write diff`)
	}
	return nil
}
565848
func (stmt InsertStmt) Compile(d Dialect, params *Parameters) (string, error) {
	// Check for delayed errors
	if err := stmt.Error(); err != nil {
		return "", err
	}

	c := len(stmt.columns)
	// No columns? no statement!
	if c == 0 {
		return "", ErrNoColumns
	}

	columns := make([]string, len(stmt.columns))
	for i, column := range stmt.columns {
		columns[i] = fmt.Sprintf(`"%s"`, column.Name())
	}

	// Column length must divide args without remainder
	if len(stmt.args)%c != 0 {
		return "", fmt.Errorf(
			`aspect: size mismatch between arguments and columns: %d is not a multiple of %d`,
			len(stmt.args),
			c,
		)
	}

	g := len(stmt.args) / c
	// If there are no arguments, default to one group and create
	// placeholder values
	if g == 0 {
		g = 1
		stmt.args = make([]interface{}, c)
		for i, _ := range stmt.args {
			stmt.args[i] = nil
		}
	}
	parameters := make([]string, g)

	var param int
	for i := 0; i < g; i += 1 {
		group := make([]string, c)
		for j := 0; j < c; j += 1 {
			// Parameters are dialect specific
			// TODO errors
			p := &Parameter{stmt.args[param]}
			group[j], _ = p.Compile(d, params)
			param += 1
		}
		parameters[i] = fmt.Sprintf(`(%s)`, strings.Join(group, ", "))
	}

	// TODO Bulk insert syntax is dialect specific
	return fmt.Sprintf(
		`INSERT INTO "%s" (%s) VALUES %s`,
		stmt.table.Name(),
		strings.Join(columns, ", "),
		strings.Join(parameters, ", "),
	), nil
}
381629
func ConvertAssignDefault(src Value, dest interface{}, defaultValue interface{}) error {
	if src.IsNull() {
		// This is not the most efficient way of doing things, but it's certainly cleaner
		v, err := BuildValue(defaultValue)
		if err != nil {
			return err
		}
		return ConvertAssign(v, dest)
	}
	return ConvertAssign(src, dest)
}
781407
func (statement *Statement) join(joinOP string, tablename interface{}, condition string, args ...interface{}) *Statement {
	join := statement.joinTables.New(statement)
	join.Operator = joinOP
	join.Table = ``
	join.Alias = ``
	join.ONStr = condition
	join.Args = args

	switch t := tablename.(type) {
	case []string:
		if len(t) > 1 {
			join.Table = statement.withPrefix(t[0])
			join.Alias = t[1]
		} else if len(t) == 1 {
			join.Table = statement.withPrefix(t[0])
		}
	case []interface{}:
		l := len(t)
		var table string
		if l > 0 {
			f := t[0]
			v := rValue(f)
			t := v.Type()
			if t.Kind() == reflect.String {
				table = f.(string)
			} else if t.Kind() == reflect.Struct {
				r := statement.Engine.autoMapType(v)
				table = r.Name
			} else {
				table = fmt.Sprintf("%v", f)
			}
			join.Table = statement.withPrefix(table)
		}
		if l > 1 {
			join.Alias = fmt.Sprintf("%v", t[1])
		}
	case core.SQL:
		join.SQLStr = joinOP + ` JOIN ` + string(t)
	case string:
		join.Table = statement.withPrefix(t)
	default:
		v := rValue(tablename)
		typ := v.Type()
		if typ.Kind() == reflect.Struct {
			r := statement.Engine.autoMapType(v)
			join.Table = r.Name
		} else {
			join.Table = statement.withPrefix(fmt.Sprintf("%v", tablename))
		}
	}
	return statement
}
1133796
func (m *Dialect) Init() {
	if m.inited {
		return
	}
	m.inited = true
	for _, s := range m.Statements {
		s.init()
	}
}