This is an automated email from the ASF dual-hosted git repository.

francischuang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite-avatica-go.git


The following commit(s) were added to refs/heads/master by this push:
     new a3c8c7f  [CALCITE-5077] ResetSession implements driver.SessionResetter
a3c8c7f is described below

commit a3c8c7f29976ce3d37920a7def6e7ae99693f0d9
Author: fuling <[email protected]>
AuthorDate: Fri Apr 1 12:11:31 2022 +0800

    [CALCITE-5077] ResetSession implements driver.SessionResetter
---
 connection.go | 22 ++++++++++++++----
 driver.go     | 75 +++++++++++++++++++++++++++++------------------------------
 2 files changed, 55 insertions(+), 42 deletions(-)

diff --git a/connection.go b/connection.go
index 2307c5c..28bb374 100644
--- a/connection.go
+++ b/connection.go
@@ -27,10 +27,11 @@ import (
 )
 
 type conn struct {
-       connectionId string
-       config       *Config
-       httpClient   *httpClient
-       adapter      Adapter
+       connectionId  string
+       config        *Config
+       httpClient    *httpClient
+       adapter       Adapter
+       connectorInfo map[string]string
 }
 
 // Prepare returns a prepared statement, bound to this connection.
@@ -229,3 +230,16 @@ func (c *conn) avaticaErrorToResponseErrorOrError(err 
error) error {
                },
        }
 }
+
+// ResetSession implements driver.SessionResetter.
+// (From Go 1.10)
+func (c *conn) ResetSession(ctx context.Context) error {
+       if c.connectionId == "" {
+               return driver.ErrBadConn
+       }
+       err := registerConn(c)
+       if err != nil {
+               return err
+       }
+       return nil
+}
diff --git a/driver.go b/driver.go
index e5c3388..4a2d342 100644
--- a/driver.go
+++ b/driver.go
@@ -22,10 +22,10 @@ Quickstart
 
 Import the database/sql package along with the avatica driver.
 
-       import "database/sql"
-       import _ "github.com/apache/calcite-avatica-go/v5"
+  import "database/sql"
+  import _ "github.com/apache/calcite-avatica-go/v5"
 
-       db, err := sql.Open("avatica", "http://phoenix-query-server:8765";)
+  db, err := sql.Open("avatica", "http://phoenix-query-server:8765";)
 
 See https://calcite.apache.org/avatica/docs/go_client_reference.html for more 
details
 */
@@ -68,51 +68,27 @@ func (c *Connector) Connect(context.Context) (driver.Conn, 
error) {
        if err != nil {
                return nil, fmt.Errorf("unable to open connection: %w", err)
        }
-
-       httpClient, err := NewHTTPClient(config.endpoint, c.Client, config)
-
-       if err != nil {
-               return nil, fmt.Errorf("unable to create HTTP client: %w", err)
-       }
-
        connectionId, err := uuid.GenerateUUID()
        if err != nil {
                return nil, fmt.Errorf("error generating connection id: %w", 
err)
        }
+       httpClient, err := NewHTTPClient(config.endpoint, c.Client, config)
 
-       info := map[string]string{
-               "AutoCommit":  "true",
-               "Consistency": "8",
-       }
-
-       for k, v := range c.Info {
-               info[k] = v
+       if err != nil {
+               return nil, fmt.Errorf("unable to create HTTP client: %w", err)
        }
-
        conn := &conn{
-               connectionId: connectionId,
-               httpClient:   httpClient,
-               config:       config,
-       }
-
-       // Open a connection to the server
-       req := &message.OpenConnectionRequest{
-               ConnectionId: connectionId,
-               Info:         info,
+               connectionId:  connectionId,
+               httpClient:    httpClient,
+               config:        config,
+               connectorInfo: c.Info,
        }
-
-       if config.schema != "" {
-               req.Info["schema"] = config.schema
-       }
-
-       _, err = httpClient.post(context.Background(), req)
-
+       err = registerConn(conn)
        if err != nil {
-               return nil, conn.avaticaErrorToResponseErrorOrError(err)
+               return nil, err
        }
-
-       response, err := httpClient.post(context.Background(), 
&message.DatabasePropertyRequest{
-               ConnectionId: connectionId,
+       response, err := conn.httpClient.post(context.Background(), 
&message.DatabasePropertyRequest{
+               ConnectionId: conn.connectionId,
        })
 
        if err != nil {
@@ -134,6 +110,29 @@ func (c *Connector) Connect(context.Context) (driver.Conn, 
error) {
        return conn, nil
 }
 
+func registerConn(conn *conn) error {
+       info := map[string]string{
+               "AutoCommit":  "true",
+               "Consistency": "8",
+       }
+       for k, v := range conn.connectorInfo {
+               info[k] = v
+       }
+       // Open a connection to the server
+       req := &message.OpenConnectionRequest{
+               ConnectionId: conn.connectionId,
+               Info:         info,
+       }
+       if conn.config.schema != "" {
+               req.Info["schema"] = conn.config.schema
+       }
+       _, err := conn.httpClient.post(context.Background(), req)
+       if err != nil {
+               return conn.avaticaErrorToResponseErrorOrError(err)
+       }
+       return nil
+}
+
 // Driver returns the underlying driver
 func (c *Connector) Driver() driver.Driver {
        return &Driver{}

Reply via email to