Skip to content

Commit

Permalink
refactoring of xsql
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Nov 9, 2023
1 parent 6373d76 commit fe288c3
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions internal/xsql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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...)
Expand Down Expand Up @@ -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...)
Expand Down

0 comments on commit fe288c3

Please sign in to comment.