44 "context"
55 "database/sql/driver"
66 "fmt"
7+ "strings"
8+ "time"
79
810 "github.com/databricks/databricks-sql-go/driverctx"
911 "github.com/databricks/databricks-sql-go/internal/cli_service"
@@ -48,7 +50,7 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
4850 },
4951 }
5052 // default timeout in here in addition to potential context timeout
51- _ , res , err := sentinel .Watch (ctx , c .cfg .PollInterval , c .cfg .DefaultTimeout )
53+ _ , res , err := sentinel .Watch (ctx , c .cfg .PollInterval , c .cfg .ConnectTimeout )
5254 if err != nil {
5355 return nil , wrapErrf (err , "error connecting: host=%s port=%d, httpPath=%s" , c .cfg .Host , c .cfg .Port , c .cfg .HTTPPath )
5456 }
@@ -86,6 +88,8 @@ var _ driver.Connector = (*connector)(nil)
8688
8789type connOption func (* config.Config )
8890
91+ // NewConnector creates a connection that can be used with sql.OpenDB().
92+ // This is an easier way to set up the DB instead of having to construct a DSN string.
8993func NewConnector (options ... connOption ) (driver.Connector , error ) {
9094 // config with default options
9195 cfg := config .WithDefaults ()
@@ -98,30 +102,38 @@ func NewConnector(options ...connOption) (driver.Connector, error) {
98102 return & connector {cfg }, nil
99103}
100104
105+ // WithServerHostname sets up the server hostname. Mandatory.
101106func WithServerHostname (host string ) connOption {
102107 return func (c * config.Config ) {
108+ if host == "localhost" {
109+ c .Protocol = "http"
110+ }
103111 c .Host = host
104112 }
105113}
106114
115+ // WithPort sets up the server port. Mandatory.
107116func WithPort (port int ) connOption {
108117 return func (c * config.Config ) {
109118 c .Port = port
110119 }
111120}
112121
122+ // WithAccessToken sets up the Personal Access Token. Mandatory for now.
113123func WithAccessToken (token string ) connOption {
114124 return func (c * config.Config ) {
115125 c .AccessToken = token
116126 }
117127}
118128
129+ // WithHTTPPath sets up the endpoint to the warehouse. Mandatory.
119130func WithHTTPPath (path string ) connOption {
120131 return func (c * config.Config ) {
121132 c .HTTPPath = path
122133 }
123134}
124135
136+ // WithMaxRows sets up the max rows fetched per request. Default is 10000
125137func WithMaxRows (n int ) connOption {
126138 return func (c * config.Config ) {
127139 if n != 0 {
@@ -130,32 +142,43 @@ func WithMaxRows(n int) connOption {
130142 }
131143}
132144
133- // This will add a timeout for the server execution.
134- // In seconds.
135- func WithTimeout (n int ) connOption {
145+ // WithTimeout adds timeout for the server query execution. Default is no timeout.
146+ func WithTimeout (n time.Duration ) connOption {
136147 return func (c * config.Config ) {
137- c .QueryTimeoutSeconds = n
148+ c .QueryTimeout = n
138149 }
139150}
140151
152+ // Sets the initial catalog name and schema name in the session.
153+ // Use <select * from foo> instead of <select * from catalog.schema.foo>
141154func WithInitialNamespace (catalog , schema string ) connOption {
142155 return func (c * config.Config ) {
143156 c .Catalog = catalog
144157 c .Schema = schema
145158 }
146159}
147160
161+ // Used to identify partners. Set as a string with format <isv-name+product-name>.
148162func WithUserAgentEntry (entry string ) connOption {
149163 return func (c * config.Config ) {
150164 c .UserAgentEntry = entry
151165 }
152-
153166}
154167
155- // Sessions params will be set upon opening the session
168+ // Sessions params will be set upon opening the session by calling SET function.
156169// If using connection pool, session params can avoid successive calls of "SET ..."
157170func WithSessionParams (params map [string ]string ) connOption {
158171 return func (c * config.Config ) {
172+ for k , v := range params {
173+ if strings .ToLower (k ) == "timezone" {
174+ if loc , err := time .LoadLocation (v ); err != nil {
175+ logger .Error ().Msgf ("timezone %s is not valid" , v )
176+ } else {
177+ c .Location = loc
178+ }
179+
180+ }
181+ }
159182 c .SessionParams = params
160183 }
161184}
0 commit comments