Repository: dolthub/dolt
File: go/cmd/dolt/commands/diff.go (Go)

Code before:
		return status
	}

	verr := cmd.validateArgs(apr)
	if verr != nil {
		return HandleVErrAndExitCode(verr, usage)
	}

	queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
	if err != nil {
		return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
	}
	if closeFunc != nil {
		defer closeFunc()
	}

	updateSystemVar, systemVarVal, err := setSystemVar(queryist, sqlCtx, apr.Contains(cli.SystemFlag))

	dArgs, err := parseDiffArgs(queryist, sqlCtx, apr)
	if err != nil {
		return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
	}

	verr = diffUserTables(queryist, sqlCtx, dArgs)

	if updateSystemVar {
		query := fmt.Sprintf("SET @@dolt_show_system_tables = %t", systemVarVal)
		_, _, _, err = queryist.Query(sqlCtx, query)
		if err != nil {
			return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
		}
	}

	return HandleVErrAndExitCode(verr, usage)
}

// setSystemVar sets the @@dolt_show_system_tables variable if necessary, and returns the value it must be
// set to after the commands completion, if necessary.
func setSystemVar(queryist cli.Queryist, sqlCtx *sql.Context, showSystem bool) (bool, bool, error) {
	_, rowIter, _, err := queryist.Query(sqlCtx, "SHOW VARIABLES WHERE VARIABLE_NAME='dolt_show_system_tables'")
	updateSystemVar := false
	if err != nil {
		return false, false, err
	}

	row, err := sql.RowIterToRows(sqlCtx, rowIter)
	if err != nil {
		return false, false, err
	}
	prevVal, err := GetInt8ColAsBool(row[0][1])
	if err != nil {

Review comment (suggestion):
It would be cleaner to have the setSystemVar method return a "cleanup" method, which sets the variable to the right thing. That way it keeps all the logic in that one function, and all this method needs to do is `defer cleanup()` or something like that.

Code after:
		return status
	}

	verr := cmd.validateArgs(apr)
	if verr != nil {
		return HandleVErrAndExitCode(verr, usage)
	}

	queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
	if err != nil {
		return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
	}
	if closeFunc != nil {
		defer closeFunc()
	}

	updateSystemVar, err := setSystemVar(queryist, sqlCtx, apr.Contains(cli.SystemFlag))
	if err != nil {
		return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
	}

	dArgs, err := parseDiffArgs(queryist, sqlCtx, apr)
	if err != nil {
		return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
	}

	verr = diffUserTables(queryist, sqlCtx, dArgs)
	if verr != nil {
		return HandleVErrAndExitCode(verr, usage)
	}

	if updateSystemVar != nil {
		err = updateSystemVar()
	}

	return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}

// setSystemVar sets the @@dolt_show_system_tables variable if necessary, and returns the value it must be
// set to after the commands completion, if necessary.
func setSystemVar(queryist cli.Queryist, sqlCtx *sql.Context, showSystem bool) (func() error, error) {
	_, rowIter, _, err := queryist.Query(sqlCtx, "SHOW VARIABLES WHERE VARIABLE_NAME='dolt_show_system_tables'")
	if err != nil {
		return nil, err
	}

	row, err := sql.RowIterToRows(sqlCtx, rowIter)
	if err != nil {
		return nil, err
	}
	prevVal, err := GetInt8ColAsBool(row[0][1])