@@ -86,8 +86,11 @@ abstract class _PgSessionBase implements Session {
86
86
87
87
/// Sends a message to the server and waits for a response [T] , gracefully
88
88
/// handling error messages that might come in instead.
89
- Future <T > _sendAndWaitForQuery <T extends ServerMessage >(ClientMessage send) {
90
- final trace = StackTrace .current;
89
+ Future <T > _sendAndWaitForQuery <T extends ServerMessage >(
90
+ ClientMessage send, {
91
+ StackTrace ? stackTrace,
92
+ }) {
93
+ final trace = stackTrace ?? StackTrace .current;
91
94
92
95
return _withResource (() {
93
96
_connection._channel.sink.add (
@@ -193,7 +196,8 @@ abstract class _PgSessionBase implements Session {
193
196
}
194
197
195
198
Future <_PreparedStatement > _prepare (Object query) async {
196
- final trace = Trace .current ();
199
+ final stackTrace = StackTrace .current;
200
+ final trace = Trace .from (stackTrace);
197
201
final conn = _connection;
198
202
final name = 's/${conn ._statementCounter ++}' ;
199
203
final description = InternalQueryDescription .wrap (
@@ -207,6 +211,7 @@ abstract class _PgSessionBase implements Session {
207
211
statementName: name,
208
212
typeOids: description.parameterTypes? .map ((e) => e? .oid).toList (),
209
213
),
214
+ stackTrace: stackTrace,
210
215
);
211
216
212
217
return _PreparedStatement (description, name, this , trace);
@@ -710,18 +715,23 @@ class _PreparedStatement extends Statement {
710
715
711
716
@override
712
717
Future <Result > run (Object ? parameters, {Duration ? timeout}) async {
718
+ final stackTrace = StackTrace .current;
719
+ final trace = Trace .from (stackTrace);
713
720
_session._connection._queryCount++ ;
714
721
timeout ?? = _session._settings.queryTimeout;
715
722
final items = < ResultRow > [];
716
- final subscription = bind (parameters).listen (items.add);
723
+ final subscription = (bind (parameters) as _BoundStatement ).listen (
724
+ items.add,
725
+ callerTrace: trace,
726
+ );
717
727
try {
718
728
return await (subscription as _PgResultStreamSubscription )._waitForResult (
719
729
items: items,
720
730
timeout: timeout,
721
731
);
722
732
} finally {
723
733
await subscription.cancel ();
724
- await _closePendingPortals ();
734
+ await _closePendingPortals (stackTrace : stackTrace );
725
735
}
726
736
}
727
737
@@ -741,12 +751,13 @@ class _PreparedStatement extends Statement {
741
751
_portalsToClose! .add (portalName);
742
752
}
743
753
744
- Future <void > _closePendingPortals () async {
754
+ Future <void > _closePendingPortals ({ StackTrace ? stackTrace} ) async {
745
755
final list = _portalsToClose;
746
756
while (list != null && list.isNotEmpty) {
747
757
final portalName = list.removeFirst ();
748
758
await _session._sendAndWaitForQuery <CloseCompleteMessage >(
749
759
CloseMessage .portal (portalName),
760
+ stackTrace: stackTrace,
750
761
);
751
762
}
752
763
}
@@ -764,6 +775,7 @@ class _BoundStatement extends Stream<ResultRow> implements ResultStream {
764
775
Function ? onError,
765
776
void Function ()? onDone,
766
777
bool ? cancelOnError,
778
+ Trace ? callerTrace,
767
779
}) {
768
780
final controller = StreamController <ResultRow >();
769
781
@@ -774,7 +786,12 @@ class _BoundStatement extends Stream<ResultRow> implements ResultStream {
774
786
onDone: onDone,
775
787
cancelOnError: cancelOnError,
776
788
);
777
- return _PgResultStreamSubscription (this , controller, subscription);
789
+ return _PgResultStreamSubscription (
790
+ this ,
791
+ controller,
792
+ subscription,
793
+ callerTrace: callerTrace,
794
+ );
778
795
}
779
796
}
780
797
@@ -803,12 +820,13 @@ class _PgResultStreamSubscription
803
820
_PgResultStreamSubscription (
804
821
_BoundStatement statement,
805
822
this ._controller,
806
- this ._source,
807
- ) : session = statement.statement._session,
808
- ignoreRows = false ,
809
- _boundStatement = statement,
810
- _parentTrace = statement.statement._trace,
811
- _callerTrace = Trace .current () {
823
+ this ._source, {
824
+ Trace ? callerTrace,
825
+ }) : session = statement.statement._session,
826
+ ignoreRows = false ,
827
+ _boundStatement = statement,
828
+ _parentTrace = statement.statement._trace,
829
+ _callerTrace = callerTrace ?? Trace .current () {
812
830
_scheduleStatement (() async {
813
831
connection._pending = this ;
814
832
@@ -847,9 +865,10 @@ class _PgResultStreamSubscription
847
865
this ._controller,
848
866
this ._source,
849
867
this .ignoreRows, {
868
+ Trace ? callerTrace,
850
869
void Function ()? cleanup,
851
870
}) : _parentTrace = null ,
852
- _callerTrace = Trace .current () {
871
+ _callerTrace = callerTrace ?? Trace .current () {
853
872
_scheduleStatement (() async {
854
873
connection._pending = this ;
855
874
0 commit comments