forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlCommand.cs
More file actions
1195 lines (1043 loc) · 44 KB
/
NpgsqlCommand.cs
File metadata and controls
1195 lines (1043 loc) · 44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// created on 21/5/2002 at 20:03
// Npgsql.NpgsqlCommand.cs
//
// Author:
// Francisco Jr. (fxjrlists@yahoo.com.br)
//
// Copyright (C) 2002 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics.Contracts;
using AsyncRewriter;
using Npgsql.BackendMessages;
using Npgsql.FrontendMessages;
using Npgsql.Logging;
namespace Npgsql
{
/// <summary>
/// Represents a SQL statement or function (stored procedure) to execute
/// against a PostgreSQL database. This class cannot be inherited.
/// </summary>
#if WITHDESIGN
[System.Drawing.ToolboxBitmapAttribute(typeof(NpgsqlCommand)), ToolboxItem(true)]
#endif
#if DNXCORE50
public sealed partial class NpgsqlCommand : DbCommand
#else
[System.ComponentModel.DesignerCategory("")]
public sealed partial class NpgsqlCommand : DbCommand, ICloneable
#endif
{
#region Fields
NpgsqlConnection _connection;
/// <summary>
/// Cached version of _connection.Connector, for performance
/// </summary>
NpgsqlConnector _connector;
NpgsqlTransaction _transaction;
String _commandText;
int? _timeout;
readonly NpgsqlParameterCollection _parameters = new NpgsqlParameterCollection();
List<QueryDetails> _queries;
int _queryIndex;
UpdateRowSource _updateRowSource = UpdateRowSource.Both;
/// <summary>
/// Indicates whether this command has been prepared.
/// Never access this field directly, use <see cref="IsPrepared"/> instead.
/// </summary>
bool _isPrepared;
/// <summary>
/// For prepared commands, captures the connection's <see cref="NpgsqlConnection.OpenCounter"/>
/// at the time the command was prepared. This allows us to know whether the connection was
/// closed since the command was prepared.
/// </summary>
int _prepareConnectionOpenId;
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
#endregion Fields
#region Constants
internal const int DefaultTimeout = 30;
/// <summary>
/// Specifies the maximum number of queries we allow in a multiquery, separated by semicolons.
/// We limit this because of deadlocks: as we send Parse and Bind messages to the backend, the backend
/// replies with ParseComplete and BindComplete messages which we do not read until we finished sending
/// all messages. Once our buffer gets full the backend will get stuck writing, and then so will we.
/// </summary>
internal const int MaxQueriesInMultiquery = 5000;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlCommand">NpgsqlCommand</see> class.
/// </summary>
public NpgsqlCommand() : this(String.Empty, null, null) {}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlCommand">NpgsqlCommand</see> class with the text of the query.
/// </summary>
/// <param name="cmdText">The text of the query.</param>
public NpgsqlCommand(String cmdText) : this(cmdText, null, null) {}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlCommand">NpgsqlCommand</see> class with the text of the query and a <see cref="NpgsqlConnection">NpgsqlConnection</see>.
/// </summary>
/// <param name="cmdText">The text of the query.</param>
/// <param name="connection">A <see cref="NpgsqlConnection">NpgsqlConnection</see> that represents the connection to a PostgreSQL server.</param>
public NpgsqlCommand(String cmdText, NpgsqlConnection connection) : this(cmdText, connection, null) {}
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlCommand">NpgsqlCommand</see> class with the text of the query, a <see cref="NpgsqlConnection">NpgsqlConnection</see>, and the <see cref="NpgsqlTransaction">NpgsqlTransaction</see>.
/// </summary>
/// <param name="cmdText">The text of the query.</param>
/// <param name="connection">A <see cref="NpgsqlConnection">NpgsqlConnection</see> that represents the connection to a PostgreSQL server.</param>
/// <param name="transaction">The <see cref="NpgsqlTransaction">NpgsqlTransaction</see> in which the <see cref="NpgsqlCommand">NpgsqlCommand</see> executes.</param>
public NpgsqlCommand(string cmdText, NpgsqlConnection connection, NpgsqlTransaction transaction)
{
Init(cmdText);
Connection = connection;
Transaction = transaction;
}
void Init(string cmdText)
{
_commandText = cmdText;
CommandType = CommandType.Text;
_queries = new List<QueryDetails>();
}
#endregion Constructors
#region Public properties
/// <summary>
/// Gets or sets the SQL statement or function (stored procedure) to execute at the data source.
/// </summary>
/// <value>The Transact-SQL statement or stored procedure to execute. The default is an empty string.</value>
[DefaultValue("")]
#if !DNXCORE50
[Category("Data")]
#endif
public override String CommandText
{
get { return _commandText; }
set
{
// [TODO] Validate commandtext.
_commandText = value;
DeallocatePrepared();
}
}
/// <summary>
/// Gets or sets the wait time before terminating the attempt to execute a command and generating an error.
/// </summary>
/// <value>The time (in seconds) to wait for the command to execute. The default value is 30 seconds.</value>
[DefaultValue(DefaultTimeout)]
public override int CommandTimeout
{
get
{
return _timeout ?? (
_connection != null
? _connection.CommandTimeout
: DefaultTimeout
);
}
set
{
if (value < 0) {
throw new ArgumentOutOfRangeException("value", value, "CommandTimeout can't be less than zero.");
}
_timeout = value;
}
}
/// <summary>
/// Gets or sets a value indicating how the
/// <see cref="NpgsqlCommand.CommandText">CommandText</see> property is to be interpreted.
/// </summary>
/// <value>One of the <see cref="System.Data.CommandType">CommandType</see> values. The default is <see cref="System.Data.CommandType">CommandType.Text</see>.</value>
[DefaultValue(CommandType.Text)]
#if !DNXCORE50
[Category("Data")]
#endif
public override CommandType CommandType { get; set; }
/// <summary>
/// DB connection.
/// </summary>
protected override DbConnection DbConnection
{
get { return Connection; }
set { Connection = (NpgsqlConnection)value; }
}
/// <summary>
/// Gets or sets the <see cref="NpgsqlConnection">NpgsqlConnection</see>
/// used by this instance of the <see cref="NpgsqlCommand">NpgsqlCommand</see>.
/// </summary>
/// <value>The connection to a data source. The default value is a null reference.</value>
[DefaultValue(null)]
#if !DNXCORE50
[Category("Behavior")]
#endif
public new NpgsqlConnection Connection
{
get { return _connection; }
set
{
if (_connection == value)
{
return;
}
//if (this._transaction != null && this._transaction.Connection == null)
// this._transaction = null;
// All this checking needs revising. It should be simpler.
// This this.Connector != null check was added to remove the nullreferenceexception in case
// of the previous connection has been closed which makes Connector null and so the last check would fail.
// See bug 1000581 for more details.
if (_transaction != null && _connection != null && _connection.Connector != null && _connection.Connector.InTransaction)
{
throw new InvalidOperationException("The Connection property can't be changed with an uncommited transaction.");
}
IsPrepared = false;
_connection = value;
Transaction = null;
}
}
/// <summary>
/// Design time visible.
/// </summary>
public override bool DesignTimeVisible { get; set; }
/// <summary>
/// Gets or sets how command results are applied to the <see cref="System.Data.DataRow">DataRow</see>
/// when used by the <see cref="System.Data.Common.DbDataAdapter.Update(DataSet)">Update</see>
/// method of the <see cref="System.Data.Common.DbDataAdapter">DbDataAdapter</see>.
/// </summary>
/// <value>One of the <see cref="System.Data.UpdateRowSource">UpdateRowSource</see> values.</value>
#if WITHDESIGN
[Category("Behavior"), DefaultValue(UpdateRowSource.Both)]
#endif
public override UpdateRowSource UpdatedRowSource
{
get { return _updateRowSource; }
set
{
switch (value)
{
// validate value (required based on base type contract)
case UpdateRowSource.None:
case UpdateRowSource.OutputParameters:
case UpdateRowSource.FirstReturnedRecord:
case UpdateRowSource.Both:
_updateRowSource = value;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// Returns whether this query will execute as a prepared (compiled) query.
/// </summary>
public bool IsPrepared
{
get
{
if (_isPrepared)
{
Contract.Assert(Connection != null);
if (Connection.State != ConnectionState.Open || _prepareConnectionOpenId != Connection.OpenCounter) {
_isPrepared = false;
}
}
return _isPrepared;
}
private set
{
Contract.Requires(!value || Connection != null);
_isPrepared = value;
if (value) {
_prepareConnectionOpenId = Connection.OpenCounter;
}
}
}
#endregion Public properties
#region Known/unknown Result Types Management
/// <summary>
/// Marks all of the query's result columns as either known or unknown.
/// Unknown results column are requested them from PostgreSQL in text format, and Npgsql makes no
/// attempt to parse them. They will be accessible as strings only.
/// </summary>
public bool AllResultTypesAreUnknown
{
get { return _allResultTypesAreUnknown; }
set
{
// TODO: Check that this isn't modified after calling prepare
_unknownResultTypeList = null;
_allResultTypesAreUnknown = value;
}
}
bool _allResultTypesAreUnknown;
/// <summary>
/// Marks the query's result columns as known or unknown, on a column-by-column basis.
/// Unknown results column are requested them from PostgreSQL in text format, and Npgsql makes no
/// attempt to parse them. They will be accessible as strings only.
/// </summary>
/// <remarks>
/// If the query includes several queries (e.g. SELECT 1; SELECT 2), this will only apply to the first
/// one. The rest of the queries will be fetched and parsed as usual.
///
/// The array size must correspond exactly to the number of result columns the query returns, or an
/// error will be raised.
/// </remarks>
public bool[] UnknownResultTypeList
{
get { return _unknownResultTypeList; }
set
{
// TODO: Check that this isn't modified after calling prepare
_allResultTypesAreUnknown = false;
_unknownResultTypeList = value;
}
}
bool[] _unknownResultTypeList;
#endregion
#region State management
int _state;
/// <summary>
/// Gets the current state of the connector
/// </summary>
internal CommandState State
{
get { return (CommandState)_state; }
set
{
var newState = (int)value;
if (newState == _state)
return;
Interlocked.Exchange(ref _state, newState);
}
}
#endregion State management
#region Parameters
/// <summary>
/// Creates a new instance of an <see cref="System.Data.Common.DbParameter">DbParameter</see> object.
/// </summary>
/// <returns>An <see cref="System.Data.Common.DbParameter">DbParameter</see> object.</returns>
protected override DbParameter CreateDbParameter()
{
return CreateParameter();
}
/// <summary>
/// Creates a new instance of a <see cref="NpgsqlParameter">NpgsqlParameter</see> object.
/// </summary>
/// <returns>A <see cref="NpgsqlParameter">NpgsqlParameter</see> object.</returns>
public new NpgsqlParameter CreateParameter()
{
return new NpgsqlParameter();
}
/// <summary>
/// DB parameter collection.
/// </summary>
protected override DbParameterCollection DbParameterCollection
{
get { return Parameters; }
}
/// <summary>
/// Gets the <see cref="NpgsqlParameterCollection">NpgsqlParameterCollection</see>.
/// </summary>
/// <value>The parameters of the SQL statement or function (stored procedure). The default is an empty collection.</value>
#if WITHDESIGN
[Category("Data"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
#endif
public new NpgsqlParameterCollection Parameters { get { return _parameters; } }
#endregion
#region Prepare
/// <summary>
/// Creates a prepared version of the command on a PostgreSQL server.
/// </summary>
public override void Prepare()
{
Prechecks();
if (Parameters.Any(p => !p.IsTypeExplicitlySet)) {
throw new InvalidOperationException("NpgsqlCommand.Prepare method requires all parameters to have an explicitly set type.");
}
_connector = Connection.Connector;
Log.Debug("Prepare command", _connector.Id);
using (_connector.StartUserAction())
{
DeallocatePrepared();
ProcessRawQuery();
for (var i = 0; i < _queries.Count; i++)
{
var query = _queries[i];
ParseMessage parseMessage;
DescribeMessage describeMessage;
if (i == 0)
{
parseMessage = _connector.ParseMessage;
describeMessage = _connector.DescribeMessage;
}
else
{
parseMessage = new ParseMessage();
describeMessage = new DescribeMessage();
}
query.PreparedStatementName = _connector.NextPreparedStatementName();
_connector.AddMessage(parseMessage.Populate(query, _connector.TypeHandlerRegistry));
_connector.AddMessage(describeMessage.Populate(StatementOrPortal.Statement,
query.PreparedStatementName));
}
_connector.AddMessage(SyncMessage.Instance);
_connector.SendAllMessages();
_queryIndex = 0;
while (true)
{
var msg = _connector.ReadSingleMessage();
switch (msg.Code)
{
case BackendMessageCode.CompletedResponse: // prepended messages, e.g. begin transaction
case BackendMessageCode.ParseComplete:
case BackendMessageCode.ParameterDescription:
continue;
case BackendMessageCode.RowDescription:
var description = (RowDescriptionMessage) msg;
FixupRowDescription(description, _queryIndex == 0);
_queries[_queryIndex++].Description = description;
continue;
case BackendMessageCode.NoData:
_queries[_queryIndex++].Description = null;
continue;
case BackendMessageCode.ReadyForQuery:
Contract.Assume(_queryIndex == _queries.Count);
IsPrepared = true;
return;
default:
throw _connector.UnexpectedMessageReceived(msg.Code);
}
}
}
}
void DeallocatePrepared()
{
if (!IsPrepared) { return; }
foreach (var query in _queries) {
_connector.PrependInternalMessage(new CloseMessage(StatementOrPortal.Statement, query.PreparedStatementName));
}
_connector.PrependInternalMessage(SyncMessage.Instance);
IsPrepared = false;
}
#endregion Prepare
#region Query analysis
void ProcessRawQuery()
{
_queries.Clear();
switch (CommandType) {
case CommandType.Text:
SqlQueryParser.ParseRawQuery(CommandText, _connection == null || _connection.UseConformantStrings, _parameters, _queries);
if (_queries.Count > 1 && _parameters.Any(p => p.IsOutputDirection)) {
throw new NotSupportedException("Commands with multiple queries cannot have out parameters");
}
break;
case CommandType.TableDirect:
_queries.Add(new QueryDetails("SELECT * FROM " + CommandText, new List<NpgsqlParameter>()));
break;
case CommandType.StoredProcedure:
var numInput = _parameters.Count(p => p.IsInputDirection);
var sb = new StringBuilder();
sb.Append("SELECT * FROM ");
sb.Append(CommandText);
sb.Append('(');
for (var i = 1; i <= numInput; i++) {
sb.Append('$');
sb.Append(i);
if (i < numInput) {
sb.Append(',');
}
}
sb.Append(')');
_queries.Add(new QueryDetails(sb.ToString(), _parameters.Where(p => p.IsInputDirection).ToList()));
break;
default:
throw PGUtil.ThrowIfReached();
}
}
#endregion
#region Frontend message creation
internal void ValidateAndCreateMessages(CommandBehavior behavior = CommandBehavior.Default)
{
_connector = Connection.Connector;
foreach (NpgsqlParameter p in Parameters.Where(p => p.IsInputDirection)) {
p.Bind(_connector.TypeHandlerRegistry);
if (p.LengthCache != null) {
p.LengthCache.Clear();
}
p.ValidateAndGetLength();
}
// For prepared SchemaOnly queries, we already have the RowDescriptions from the Prepare phase.
// No need to send anything
if (IsPrepared && (behavior & CommandBehavior.SchemaOnly) != 0) {
return;
}
// Set the frontend timeout
_connector.UserCommandFrontendTimeout = CommandTimeout;
// If needed, prepend a "SET statement_timeout" message to set the backend timeout
_connector.PrependBackendTimeoutMessage(CommandTimeout);
// Create actual messages depending on scenario
if (IsPrepared) {
CreateMessagesPrepared(behavior);
} else {
if ((behavior & CommandBehavior.SchemaOnly) == 0) {
CreateMessagesNonPrepared(behavior);
} else {
CreateMessagesSchemaOnly(behavior);
}
}
}
void CreateMessagesNonPrepared(CommandBehavior behavior)
{
Contract.Requires((behavior & CommandBehavior.SchemaOnly) == 0);
ProcessRawQuery();
var portalNames = _queries.Count > 1
? Enumerable.Range(0, _queries.Count).Select(i => "MQ" + i).ToArray()
: null;
for (var i = 0; i < _queries.Count; i++)
{
var query = _queries[i];
ParseMessage parseMessage;
DescribeMessage describeMessage;
BindMessage bindMessage;
if (i == 0)
{
parseMessage = _connector.ParseMessage;
describeMessage = _connector.DescribeMessage;
bindMessage = _connector.BindMessage;
}
else
{
parseMessage = new ParseMessage();
describeMessage = new DescribeMessage();
bindMessage = new BindMessage();
}
_connector.AddMessage(parseMessage.Populate(query, _connector.TypeHandlerRegistry));
_connector.AddMessage(describeMessage.Populate(StatementOrPortal.Statement));
bindMessage.Populate(
_connector.TypeHandlerRegistry,
query.InputParameters,
_queries.Count == 1 ? "" : portalNames[i]
);
if (AllResultTypesAreUnknown) {
bindMessage.AllResultTypesAreUnknown = AllResultTypesAreUnknown;
} else if (i == 0 && UnknownResultTypeList != null) {
bindMessage.UnknownResultTypeList = UnknownResultTypeList;
}
_connector.AddMessage(bindMessage);
}
if (_queries.Count == 1) {
_connector.AddMessage(_connector.ExecuteMessage.Populate("", (behavior & CommandBehavior.SingleRow) != 0 ? 1 : 0));
} else
for (var i = 0; i < _queries.Count; i++) {
// TODO: Verify SingleRow behavior for multiqueries
_connector.AddMessage(new ExecuteMessage(portalNames[i], (behavior & CommandBehavior.SingleRow) != 0 ? 1 : 0));
_connector.AddMessage(new CloseMessage(StatementOrPortal.Portal, portalNames[i]));
}
_connector.AddMessage(SyncMessage.Instance);
}
void CreateMessagesPrepared(CommandBehavior behavior)
{
for (var i = 0; i < _queries.Count; i++)
{
BindMessage bindMessage;
ExecuteMessage executeMessage;
if (i == 0)
{
bindMessage = _connector.BindMessage;
executeMessage = _connector.ExecuteMessage;
}
else
{
bindMessage = new BindMessage();
executeMessage = new ExecuteMessage();
}
var query = _queries[i];
bindMessage.Populate(_connector.TypeHandlerRegistry, query.InputParameters, "", query.PreparedStatementName);
if (AllResultTypesAreUnknown) {
bindMessage.AllResultTypesAreUnknown = AllResultTypesAreUnknown;
} else if (i == 0 && UnknownResultTypeList != null) {
bindMessage.UnknownResultTypeList = UnknownResultTypeList;
}
_connector.AddMessage(bindMessage);
_connector.AddMessage(executeMessage.Populate("", (behavior & CommandBehavior.SingleRow) != 0 ? 1 : 0));
}
_connector.AddMessage(SyncMessage.Instance);
}
void CreateMessagesSchemaOnly(CommandBehavior behavior)
{
Contract.Requires((behavior & CommandBehavior.SchemaOnly) != 0);
ProcessRawQuery();
for (var i = 0; i < _queries.Count; i++)
{
ParseMessage parseMessage;
DescribeMessage describeMessage;
if (i == 0) {
parseMessage = _connector.ParseMessage;
describeMessage = _connector.DescribeMessage;
} else {
parseMessage = new ParseMessage();
describeMessage = new DescribeMessage();
}
_connector.AddMessage(parseMessage.Populate(_queries[i], _connector.TypeHandlerRegistry));
_connector.AddMessage(describeMessage.Populate(StatementOrPortal.Statement));
}
_connector.AddMessage(SyncMessage.Instance);
}
#endregion
#region Execute
[RewriteAsync]
internal NpgsqlDataReader Execute(CommandBehavior behavior = CommandBehavior.Default)
{
State = CommandState.InProgress;
try
{
_queryIndex = 0;
_connector.SendAllMessages();
if (!IsPrepared)
{
IBackendMessage msg;
do
{
msg = _connector.ReadSingleMessage();
} while (!ProcessMessageForUnprepared(msg, behavior));
}
var reader = new NpgsqlDataReader(this, behavior, _queries);
reader.Init();
_connector.CurrentReader = reader;
return reader;
}
catch
{
State = CommandState.Idle;
throw;
}
}
bool ProcessMessageForUnprepared(IBackendMessage msg, CommandBehavior behavior)
{
Contract.Requires(!IsPrepared);
switch (msg.Code) {
case BackendMessageCode.CompletedResponse: // e.g. begin transaction
case BackendMessageCode.ParseComplete:
case BackendMessageCode.ParameterDescription:
return false;
case BackendMessageCode.RowDescription:
Contract.Assert(_queryIndex < _queries.Count);
var description = (RowDescriptionMessage)msg;
FixupRowDescription(description, _queryIndex == 0);
_queries[_queryIndex].Description = description;
if ((behavior & CommandBehavior.SchemaOnly) != 0) {
_queryIndex++;
}
return false;
case BackendMessageCode.NoData:
Contract.Assert(_queryIndex < _queries.Count);
_queries[_queryIndex].Description = null;
return false;
case BackendMessageCode.BindComplete:
Contract.Assume((behavior & CommandBehavior.SchemaOnly) == 0);
return ++_queryIndex == _queries.Count;
case BackendMessageCode.ReadyForQuery:
Contract.Assume((behavior & CommandBehavior.SchemaOnly) != 0);
return true; // End of a SchemaOnly command
default:
throw _connector.UnexpectedMessageReceived(msg.Code);
}
}
#endregion
#region Execute Non Query
/// <summary>
/// Executes a SQL statement against the connection and returns the number of rows affected.
/// </summary>
/// <returns>The number of rows affected if known; -1 otherwise.</returns>
public override int ExecuteNonQuery()
{
return ExecuteNonQueryInternal();
}
/// <summary>
/// Asynchronous version of <see cref="ExecuteNonQuery"/>
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation, with the number of rows affected if known; -1 otherwise.</returns>
public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.Register(Cancel);
try
{
return await ExecuteNonQueryInternalAsync().ConfigureAwait(false);
}
catch (NpgsqlException e)
{
if (e.Code == "57014")
throw new TaskCanceledException(e.Message);
throw;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[RewriteAsync]
int ExecuteNonQueryInternal()
{
Prechecks();
Log.Debug("ExecuteNonQuery", Connection.Connector.Id);
using (Connection.Connector.StartUserAction())
{
ValidateAndCreateMessages();
NpgsqlDataReader reader;
using (reader = Execute())
{
while (reader.NextResult()) ;
}
return reader.RecordsAffected;
}
}
#endregion Execute Non Query
#region Execute Scalar
/// <summary>
/// Executes the query, and returns the first column of the first row
/// in the result set returned by the query. Extra columns or rows are ignored.
/// </summary>
/// <returns>The first column of the first row in the result set,
/// or a null reference if the result set is empty.</returns>
public override object ExecuteScalar()
{
return ExecuteScalarInternal();
}
/// <summary>
/// Asynchronous version of <see cref="ExecuteScalar"/>
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation, with the first column of the
/// first row in the result set, or a null reference if the result set is empty.</returns>
public override async Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.Register(Cancel);
try
{
return await ExecuteScalarInternalAsync().ConfigureAwait(false);
}
catch (NpgsqlException e)
{
if (e.Code == "57014")
throw new TaskCanceledException(e.Message);
throw;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[RewriteAsync]
object ExecuteScalarInternal()
{
Prechecks();
Log.Debug("ExecuteNonScalar", Connection.Connector.Id);
using (Connection.Connector.StartUserAction())
{
var behavior = CommandBehavior.SequentialAccess | CommandBehavior.SingleRow;
ValidateAndCreateMessages(behavior);
using (var reader = Execute(behavior))
{
return reader.Read() && reader.FieldCount != 0 ? reader.GetValue(0) : null;
}
}
}
#endregion Execute Scalar
#region Execute Reader
/// <summary>
/// Executes the CommandText against the Connection, and returns an DbDataReader.
/// </summary>
/// <remarks>
/// Unlike the ADO.NET method which it replaces, this method returns a Npgsql-specific
/// DataReader.
/// </remarks>
/// <returns>A DbDataReader object.</returns>
public new NpgsqlDataReader ExecuteReader()
{
return (NpgsqlDataReader)base.ExecuteReader();
}
/// <summary>
/// Executes the CommandText against the Connection, and returns an DbDataReader using one
/// of the CommandBehavior values.
/// </summary>
/// <remarks>
/// Unlike the ADO.NET method which it replaces, this method returns a Npgsql-specific
/// DataReader.
/// </remarks>
/// <returns>A DbDataReader object.</returns>
public new NpgsqlDataReader ExecuteReader(CommandBehavior behavior)
{
return (NpgsqlDataReader)base.ExecuteReader(behavior);
}
/// <summary>
/// Executes the command text against the connection.
/// </summary>
/// <param name="behavior">An instance of <see cref="CommandBehavior"/>.</param>
/// <param name="cancellationToken">A task representing the operation.</param>
/// <returns></returns>
protected async override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.Register(Cancel);
try
{
return await ExecuteDbDataReaderInternalAsync(behavior).ConfigureAwait(false);
}
catch (NpgsqlException e)
{
if (e.Code == "57014")
throw new TaskCanceledException(e.Message);
throw;
}
}
/// <summary>
/// Executes the command text against the connection.
/// </summary>
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
Log.Debug("ExecuteReader with CommandBehavior=" + behavior);
return ExecuteDbDataReaderInternal(behavior);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[RewriteAsync]
NpgsqlDataReader ExecuteDbDataReaderInternal(CommandBehavior behavior)
{
Prechecks();
Log.Debug("ExecuteReader", Connection.Connector.Id);
Connection.Connector.StartUserAction();
try
{
ValidateAndCreateMessages(behavior);
var reader = Execute(behavior);
// Transparently dereference cursors returned from functions
if (CommandType == CommandType.StoredProcedure &&
reader.FieldCount == 1 &&
reader.GetDataTypeName(0) == "refcursor")
{
var sb = new StringBuilder();
while (reader.Read()) {
sb.AppendFormat(@"FETCH ALL FROM ""{0}"";", reader.GetString(0));
}
reader.Dispose();
var dereferenceCmd = new NpgsqlCommand(sb.ToString(), Connection);
return dereferenceCmd.ExecuteReader(behavior);
}
return reader;
}
catch
{
if (Connection.Connector != null) {
Connection.Connector.EndUserAction();
}
// Close connection if requested even when there is an error.
if ((behavior & CommandBehavior.CloseConnection) == CommandBehavior.CloseConnection)
{
_connection.Close();
}
throw;
}
}
#endregion
#region Transactions
/// <summary>
/// DB transaction.
/// </summary>
protected override DbTransaction DbTransaction
{
get { return Transaction; }
set { Transaction = (NpgsqlTransaction)value; }
}
/// <summary>
/// Gets or sets the <see cref="NpgsqlTransaction">NpgsqlTransaction</see>
/// within which the <see cref="NpgsqlCommand">NpgsqlCommand</see> executes.
/// </summary>
/// <value>The <see cref="NpgsqlTransaction">NpgsqlTransaction</see>.