Skip to content

Commit bf523d1

Browse files
fix(go/adbc/sqldriver): ignore NotImplemented errors for optional transaction options (apache#3759)
This change adds graceful handling for StatusNotImplemented errors when setting optional transaction features (isolation level and read-only mode), allowing transactions to work with drivers that don't implement these optional features.
1 parent 5ddebb3 commit bf523d1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

go/adbc/sqldriver/driver.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ func (c *conn) Begin() (driver.Tx, error) {
217217
return nil, &adbc.Error{Code: adbc.StatusNotImplemented}
218218
}
219219

220+
// setOptionIgnoreNotImplemented sets an option, but ignores StatusNotImplemented errors
221+
// since not all drivers support all optional features
222+
func setOptionIgnoreNotImplemented(setter interface{ SetOption(string, string) error }, key, value string) error {
223+
if err := setter.SetOption(key, value); err != nil {
224+
var adbcErr adbc.Error
225+
if errors.As(err, &adbcErr) && adbcErr.Code == adbc.StatusNotImplemented {
226+
// Driver doesn't support this option - continue without it
227+
return nil
228+
}
229+
return err
230+
}
231+
return nil
232+
}
233+
220234
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
221235
if postopt, ok := c.Conn.(adbc.PostInitOptions); ok {
222236
if err := postopt.SetOption(adbc.OptionKeyAutoCommit, adbc.OptionValueDisabled); err != nil {
@@ -227,12 +241,12 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
227241
return nil, &adbc.Error{Code: adbc.StatusNotImplemented}
228242
}
229243

230-
if err := postopt.SetOption(adbc.OptionKeyIsolationLevel, string(isolationLevel)); err != nil {
244+
if err := setOptionIgnoreNotImplemented(postopt, adbc.OptionKeyIsolationLevel, string(isolationLevel)); err != nil {
231245
return nil, err
232246
}
233247

234248
if opts.ReadOnly {
235-
if err := postopt.SetOption(adbc.OptionKeyReadOnly, adbc.OptionValueEnabled); err != nil {
249+
if err := setOptionIgnoreNotImplemented(postopt, adbc.OptionKeyReadOnly, adbc.OptionValueEnabled); err != nil {
236250
return nil, err
237251
}
238252
}

0 commit comments

Comments
 (0)