xo / usql

Universal command-line interface for SQL databases
MIT License
8.82k stars 346 forks source link

Add ON_ERROR_STOP: halt execution when a statement fails #378

Closed sfc-gh-pbennes closed 1 year ago

sfc-gh-pbennes commented 1 year ago

This PR adds a variable, ON_ERROR_STOP, disabled by default to maintain status quo, that allows usql to stop execution in the case where it's being requested to execute multiple statements, either by file input or multiple statements on the command line.

Here are some examples:

SQL file execution with file:

select 1;
select asdfa;
select 2;
[pbennes@localhost usql]$ ./usql -v ON_ERROR_STOP=0 "snowflake://redacted" -f test.sql
Connected with driver snowflake (<unknown>)
 1 
---
 1 
(1 row)

error: snowflake: 904: SQL compilation error: error line 1 at position 7
invalid identifier 'ASDFA'
 2 
---
 2 
(1 row)

[pbennes@localhost usql]$ ./usql -v ON_ERROR_STOP=1 "snowflake://redacted" -f test.sql
Connected with driver snowflake (<unknown>)
 1 
---
 1 
(1 row)

error: snowflake: 904: SQL compilation error: error line 1 at position 7
invalid identifier 'ASDFA'

-c:

[pbennes@localhost usql]$ ./usql -v ON_ERROR_STOP=0 "snowflake://redacted" -c 'select 1; select asdf; select 2;'
Connected with driver snowflake (<unknown>)
 1 
---
 1 
(1 row)

error: snowflake: 904: SQL compilation error: error line 1 at position 7
invalid identifier 'ASDF'
 2 
---
 2 
(1 row)

[pbennes@localhost usql]$ ./usql -v ON_ERROR_STOP=1 "snowflake://redacted" -c 'select 1; select asdf; select 2;'
Connected with driver snowflake (<unknown>)
 1 
---
 1 
(1 row)

error: snowflake: 904: SQL compilation error: error line 1 at position 7
invalid identifier 'ASDF'

And then a garbage value for the parameter:

[pbennes@localhost usql]$ ./usql -v ON_ERROR_STOP=asdf "snowflake://redacted" -c 'select 1; select asdf; select 2;'
Connected with driver snowflake (<unknown>)
 1 
---
 1 
(1 row)

error parsing value for ON_ERROR_STOP: strconv.ParseBool: parsing "asdf": invalid syntax
error: snowflake: 904: SQL compilation error: error line 1 at position 7
invalid identifier 'ASDF'
 2 
---
 2 
(1 row)

Interactive mode:

[pbennes@localhost usql]$ ./usql "snowflake://redacted"
Connected with driver snowflake (<unknown>)
Type "help" for help.

sf:pbennes@127=> select 1; select asdf; select 2;
 1 
---
 1 
(1 row)

error: snowflake: 904: SQL compilation error: error line 1 at position 7
invalid identifier 'ASDF'
 2 
---
 2 
(1 row)

sf:pbennes@127=> \set ON_ERROR_STOP 1
sf:pbennes@127=> select 1; select asdf; select 2;
 1 
---
 1 
(1 row)

error: snowflake: 904: SQL compilation error: error line 1 at position 7
invalid identifier 'ASDF'
sf:pbennes@127=>  
sfc-gh-pbennes commented 1 year ago

Ok, I think this is ready for another review.

sfc-gh-pbennes commented 1 year ago

Ok take a look at this most recent commit. I use types.go's ParseBool for ON_ERROR_STOP as well, which makes it consistent with the set behavior of QUIET. I push the case of \set QUIET with the empty value being interpreted as "on" in to ParseBool. This simplifies Set() nicely.

sfc-gh-pbennes commented 1 year ago

Squashed! Was going to ask about documentation as well but you read my mind and answered haha. Thanks @nineinchnick

nineinchnick commented 1 year ago

Docs issue: https://github.com/xo/usql/issues/379

kenshaw commented 1 year ago

@sfc-gh-pbennes I didn't like the addition of a top level exported func in the env package that is not used anywhere else. I've cleaned up the logic, and fixed some other superficial errors in the latest commit. Can you please check that this logic still works as you expected? Thanks.

sfc-gh-pbennes commented 1 year ago

@kenshaw makes sense, thanks for the FYI. Just checked and the logic still works as expected!

kenshaw commented 1 year ago

Ok, thanks. I'll be tagging and pushing a point release shortly with this.