package pgxtraces

import (
	

	

	
)

type (
	querySpanKey    struct{}
	batchSpanKey    struct{}
	copyFromSpanKey struct{}
	prepareSpanKey  struct{}
)

// TraceQueryStart implements the [pgx.QueryTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TraceQueryStartData,
) context.Context {
	 := sqlOperation(.SQL)
	if !.allow(, ) {
		return 
	}
	var  *pgx.ConnConfig
	if  != nil {
		 = .Config()
	}
	 := .connAttrs(, , )
	if  := sqlCollection(, .SQL);  != "" {
		 = append(, traces.String(attrDBCollectionName, ))
	}
	if ,  := .queryText(.SQL);  {
		 = append(, traces.String(attrDBQueryText, ))
	}
	 := .tracer.Start(
		,
		traces.FromContext(),
		traces.SpanKindClient,
		.spanName(, dbName()),
		...,
	)
	return context.WithValue(, querySpanKey{}, )
}

// TraceQueryEnd implements the [pgx.QueryTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TraceQueryEndData,
) {
	,  := .Value(querySpanKey{}).(*traces.Span)
	if  == nil {
		return
	}
	if .Err != nil {
		setError(, .Err)
	} else {
		.SetAttrs(traces.Int64(attrDBReturnedRows, .CommandTag.RowsAffected()))
	}
	.End()
}

// TraceBatchStart implements the [pgx.BatchTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TraceBatchStartData,
) context.Context {
	if !.allow(, opBatch) {
		return 
	}
	var  *pgx.ConnConfig
	if  != nil {
		 = .Config()
	}
	 := .connAttrs(, , opBatch)
	if .Batch != nil {
		 = append(, traces.Int(attrDBBatchSize, .Batch.Len()))
	}
	 := .tracer.Start(
		,
		traces.FromContext(),
		traces.SpanKindClient,
		.spanName(opBatch, dbName()),
		...,
	)
	return context.WithValue(, batchSpanKey{}, )
}

// TraceBatchQuery implements the [pgx.BatchTracer] interface. It records a
// child span under the active batch span using data.ReadStartTime when
// available.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TraceBatchQueryData,
) {
	 := sqlOperation(.SQL)
	if !.allow(, ) {
		return
	}
	 := traces.FromContext()
	if ,  := .Value(batchSpanKey{}).(*traces.Span);  &&  != nil {
		 = .Ref()
	}
	var  *pgx.ConnConfig
	if  != nil {
		 = .Config()
	}
	 := .connAttrs(, , )
	if  := sqlCollection(, .SQL);  != "" {
		 = append(, traces.String(attrDBCollectionName, ))
	}
	if ,  := .queryText(.SQL);  {
		 = append(, traces.String(attrDBQueryText, ))
	}
	 := .tracer.Start(
		,
		,
		traces.SpanKindClient,
		.spanName(, dbName()),
		...,
	)
	if !.ReadStartTime.IsZero() {
		.SetStart(.ReadStartTime)
	}
	if .Err != nil {
		setError(, .Err)
	} else {
		.SetAttrs(traces.Int64(attrDBReturnedRows, .CommandTag.RowsAffected()))
	}
	.End()
}

// TraceBatchEnd implements the [pgx.BatchTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TraceBatchEndData,
) {
	,  := .Value(batchSpanKey{}).(*traces.Span)
	if  == nil {
		return
	}
	setError(, .Err)
	.End()
}

// TraceCopyFromStart implements the [pgx.CopyFromTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TraceCopyFromStartData,
) context.Context {
	if !.allow(, opCopy) {
		return 
	}
	var  *pgx.ConnConfig
	if  != nil {
		 = .Config()
	}
	 := .connAttrs(, , opCopy)
	if len(.TableName) > 0 {
		 = append(, traces.String(attrDBCollectionName, .TableName.Sanitize()))
	}
	 := .tracer.Start(
		,
		traces.FromContext(),
		traces.SpanKindClient,
		.spanName(opCopy, dbName()),
		...,
	)
	return context.WithValue(, copyFromSpanKey{}, )
}

// TraceCopyFromEnd implements the [pgx.CopyFromTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TraceCopyFromEndData,
) {
	,  := .Value(copyFromSpanKey{}).(*traces.Span)
	if  == nil {
		return
	}
	if .Err != nil {
		setError(, .Err)
	} else {
		.SetAttrs(traces.Int64(attrDBReturnedRows, .CommandTag.RowsAffected()))
	}
	.End()
}

// TracePrepareStart implements the [pgx.PrepareTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TracePrepareStartData,
) context.Context {
	if !.allow(, opPrepare) {
		return 
	}
	var  *pgx.ConnConfig
	if  != nil {
		 = .Config()
	}
	 := .connAttrs(, , opPrepare)
	if ,  := .queryText(.SQL);  {
		 = append(, traces.String(attrDBQueryText, ))
	}
	 := .tracer.Start(
		,
		traces.FromContext(),
		traces.SpanKindClient,
		.spanName(opPrepare, dbName()),
		...,
	)
	return context.WithValue(, prepareSpanKey{}, )
}

// TracePrepareEnd implements the [pgx.PrepareTracer] interface.
func ( *Tracer) (
	 context.Context,
	 *pgx.Conn,
	 pgx.TracePrepareEndData,
) {
	,  := .Value(prepareSpanKey{}).(*traces.Span)
	if  == nil {
		return
	}
	setError(, .Err)
	.End()
}