-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(go/adbc/driver/snowflake): fix setting database and schema context after initial connection #2169
fix(go/adbc/driver/snowflake): fix setting database and schema context after initial connection #2169
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,11 @@ public abstract class AdbcConnection11 : IDisposable | |
, IAsyncDisposable | ||
#endif | ||
{ | ||
public static string CurrentCatalogOption = "adbc.connection.catalog"; | ||
public static string CurrentDbSchemaOption = "adbc.connection.db_schema"; | ||
public static string ReadOnlyOption = "adbc.connection.readonly"; | ||
public static string AutoCommitOption = "adbc.connection.autocommit"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider removing added whitespace |
||
~AdbcConnection11() => Dispose(false); | ||
|
||
/// <summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,24 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth, | |
sql.Named("UNIQUE_QUERY_ID", uniqueQueryID), | ||
} | ||
|
||
// the connection that is used is not the same connection context where the database may have been set | ||
// if the caller called SetCurrentCatalog() so need to ensure the database context is appropriate | ||
if !isNilOrEmpty(catalog) { | ||
_, e := conn.ExecContext(context.Background(), fmt.Sprintf("USE DATABASE %s;", *catalog), nil) | ||
if e != nil { | ||
return nil, errToAdbcErr(adbc.StatusIO, e) | ||
} | ||
} | ||
|
||
// the connection that is used is not the same connection context where the schema may have been set | ||
// if the caller called SetCurrentDbSchema() so need to ensure the schema context is appropriate | ||
if !isNilOrEmpty(dbSchema) { | ||
_, e2 := conn.ExecContext(context.Background(), fmt.Sprintf("USE SCHEMA %s;", *dbSchema), nil) | ||
if e2 != nil { | ||
return nil, errToAdbcErr(adbc.StatusIO, e2) | ||
} | ||
} | ||
|
||
query := bldr.String() | ||
rows, err := conn.QueryContext(ctx, query, args...) | ||
if err != nil { | ||
|
@@ -214,6 +232,10 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth, | |
} | ||
} | ||
|
||
func isNilOrEmpty(str *string) bool { | ||
return str == nil || *str == "" | ||
} | ||
|
||
// PrepareDriverInfo implements driverbase.DriverInfoPreparer. | ||
func (c *connectionImpl) PrepareDriverInfo(ctx context.Context, infoCodes []adbc.InfoCode) error { | ||
if err := c.ConnectionImplBase.DriverInfo.RegisterInfoCode(adbc.InfoVendorSql, true); err != nil { | ||
|
@@ -239,13 +261,13 @@ func (c *connectionImpl) GetCurrentDbSchema() (string, error) { | |
|
||
// SetCurrentCatalog implements driverbase.CurrentNamespacer. | ||
func (c *connectionImpl) SetCurrentCatalog(value string) error { | ||
_, err := c.cn.ExecContext(context.Background(), "USE DATABASE ?", []driver.NamedValue{{Value: value}}) | ||
_, err := c.cn.ExecContext(context.Background(), fmt.Sprintf("USE DATABASE %s", value), nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bobby Tables says never to forget about quoting SQL identifiers properly. Snowflake identifiers can contain spaces and double-quotes and semicolons, and are quoted by using delimiting with double-quotes and doubling any contained double-quotes. That is, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bah, I've succeeded in confusing the "code" feature of Markdown. That last substitution should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use snowflake's e.g. That should eliminate possible injection scenarios, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't work, unfortunately. (I tried it specifically, and it's also not usually how SQL grammars work: you can't usually supply an expression here, only an identifier.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I also feel obliged to point out that any single quotes in the identifier would need to be escaped in that variant, if it did happen to work.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. went with Matt's suggestion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we had a proper quote function? Can we use that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have quoteTblName(...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
return err | ||
} | ||
|
||
// SetCurrentDbSchema implements driverbase.CurrentNamespacer. | ||
func (c *connectionImpl) SetCurrentDbSchema(value string) error { | ||
_, err := c.cn.ExecContext(context.Background(), "USE SCHEMA ?", []driver.NamedValue{{Value: value}}) | ||
_, err := c.cn.ExecContext(context.Background(), fmt.Sprintf("USE SCHEMA %s", value), nil) | ||
davidhcoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are currently in the AdbcOptions class e.g.
AdbcOptions.Connection.CurrentCatalog
. Are you thinking that that class should be deprecated in favor of distributing them into the connection/statement/etc. classes? I seem to recall that there were some cross-cutting concerns that made it desirable to keep them under AdbcOptions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
honestly, just didn't realize those were even there :/