Merge pull request #980 from dolthub/zachmu/shell-bugs

Bug fixes for SQL shell: 
* Properly close connection in the case of errors in UPDATE or INSERT statements
* Save query history before execution, not after
This commit is contained in:
Zach Musgrave
2020-11-03 14:05:35 -08:00
committed by GitHub

View File

@@ -665,16 +665,6 @@ func runShell(ctx *sql.Context, se *sqlEngine, mrEnv env.MultiRepoEnv) error {
return
}
if sqlSch, rowIter, err := processQuery(ctx, query, se); err != nil {
verr := formatQueryError("", err)
shell.Println(verr.Verbose())
} else if rowIter != nil {
err = PrettyPrintResults(ctx, se.resultFormat, sqlSch, rowIter)
if err != nil {
shell.Println(color.RedString(err.Error()))
}
}
// TODO: there's a bug in the readline library when editing multi-line history entries.
// Longer term we need to switch to a new readline library, like in this bug:
// https://github.com/cockroachdb/cockroach/issues/15460
@@ -685,6 +675,16 @@ func runShell(ctx *sql.Context, se *sqlEngine, mrEnv env.MultiRepoEnv) error {
shell.Println(color.RedString(err.Error()))
}
if sqlSch, rowIter, err := processQuery(ctx, query, se); err != nil {
verr := formatQueryError("", err)
shell.Println(verr.Verbose())
} else if rowIter != nil {
err = PrettyPrintResults(ctx, se.resultFormat, sqlSch, rowIter)
if err != nil {
shell.Println(color.RedString(err.Error()))
}
}
currPrompt := fmt.Sprintf("%s> ", ctx.GetCurrentDatabase())
shell.SetPrompt(currPrompt)
shell.SetMultiPrompt(fmt.Sprintf(fmt.Sprintf("%%%ds", len(currPrompt)), "-> "))
@@ -1217,10 +1217,6 @@ func (se *sqlEngine) query(ctx *sql.Context, query string) (sql.Schema, sql.RowI
// Pretty prints the output of the new SQL engine
func PrettyPrintResults(ctx context.Context, resultFormat resultFormat, sqlSch sql.Schema, rowIter sql.RowIter) (rerr error) {
if isOkResult(sqlSch) {
return printOKResult(ctx, rowIter)
}
defer func() {
closeErr := rowIter.Close()
if rerr == nil && closeErr != nil {
@@ -1228,6 +1224,10 @@ func PrettyPrintResults(ctx context.Context, resultFormat resultFormat, sqlSch s
}
}()
if isOkResult(sqlSch) {
return printOKResult(rowIter)
}
nbf := types.Format_Default
doltSch, err := sqleSchema.ToDoltResultSchema(sqlSch)
@@ -1347,17 +1347,12 @@ func PrettyPrintResults(ctx context.Context, resultFormat resultFormat, sqlSch s
return nil
}
func printOKResult(ctx context.Context, iter sql.RowIter) (returnErr error) {
func printOKResult(iter sql.RowIter) (returnErr error) {
row, err := iter.Next()
if err != nil {
return err
}
err = iter.Close()
if err != nil {
return err
}
if okResult, ok := row[0].(sql.OkResult); ok {
rowNoun := "row"
if okResult.RowsAffected != 1 {