From fe288c34a7e541bb3a7ed395225dcef03a50011f Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Thu, 9 Nov 2023 15:33:48 +0300 Subject: [PATCH] refactoring of xsql --- internal/xsql/conn.go | 49 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/internal/xsql/conn.go b/internal/xsql/conn.go index 85c99a746..a538a86fb 100644 --- a/internal/xsql/conn.go +++ b/internal/xsql/conn.go @@ -172,7 +172,7 @@ func (c *conn) sinceLastUsage() time.Duration { return time.Since(time.Unix(c.lastUsage.Load(), 0)) } -func (c *conn) execContext(ctx context.Context, query string, args []driver.NamedValue) ( +func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) ( _ driver.Result, finalErr error, ) { var ( @@ -181,11 +181,19 @@ func (c *conn) execContext(ctx context.Context, query string, args []driver.Name c.trace, &ctx, stack.FunctionID(0), query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(), ) ) - defer func() { c.lastUsage.Store(time.Now().Unix()) onDone(finalErr) }() + + if !c.isReady() { + return nil, badconn.Map(xerrors.WithStackTrace(errNotReadyConn)) + } + + if c.currentTx != nil { + return c.currentTx.ExecContext(ctx, query, args) + } + switch m { case DataQueryMode: normalizedQuery, params, err := c.normalize(query, args...) @@ -247,37 +255,28 @@ func (c *conn) execContext(ctx context.Context, query string, args []driver.Name } } -func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (_ driver.Result, _ error) { - if !c.isReady() { - return nil, badconn.Map(xerrors.WithStackTrace(errNotReadyConn)) - } - if c.currentTx != nil { - return c.currentTx.ExecContext(ctx, query, args) - } - return c.execContext(ctx, query, args) -} +func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) ( + _ driver.Rows, finalErr error, +) { + var ( + m = queryModeFromContext(ctx, c.defaultQueryMode) + onDone = trace.DatabaseSQLOnConnQuery( + c.trace, &ctx, stack.FunctionID(0), query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(), + ) + ) + defer func() { + c.lastUsage.Store(time.Now().Unix()) + onDone(finalErr) + }() -func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (_ driver.Rows, _ error) { if !c.isReady() { return nil, badconn.Map(xerrors.WithStackTrace(errNotReadyConn)) } + if c.currentTx != nil { return c.currentTx.QueryContext(ctx, query, args) } - return c.queryContext(ctx, query, args) -} -func (c *conn) queryContext(ctx context.Context, query string, args []driver.NamedValue) ( - _ driver.Rows, finalErr error, -) { - m := queryModeFromContext(ctx, c.defaultQueryMode) - onDone := trace.DatabaseSQLOnConnQuery( - c.trace, &ctx, stack.FunctionID(0), query, m.String(), xcontext.IsIdempotent(ctx), c.sinceLastUsage(), - ) - defer func() { - c.lastUsage.Store(time.Now().Unix()) - onDone(finalErr) - }() switch m { case DataQueryMode: normalizedQuery, params, err := c.normalize(query, args...)