Skip to content

Commit 4008bea

Browse files
committed
StatementBase refactoring.
1 parent 2342e79 commit 4008bea

File tree

5 files changed

+64
-161
lines changed

5 files changed

+64
-161
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsStatement.cs

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ internal class GdsStatement : StatementBase
3535
private GdsTransaction _transaction;
3636
protected Descriptor _parameters;
3737
protected Descriptor _fields;
38-
protected StatementState _state;
39-
protected DbStatementType _statementType;
4038
protected bool _allRowsFetched;
4139
private Queue<DbValue[]> _rows;
4240
private Queue<DbValue[]> _outputParams;
4341
private int _fetchSize;
44-
private bool _returnRecordsAffected;
4542

4643
#endregion
4744

@@ -90,47 +87,12 @@ public override Descriptor Fields
9087
get { return _fields; }
9188
}
9289

93-
public override int RecordsAffected { get; protected set; }
94-
95-
public override bool IsPrepared
96-
{
97-
get
98-
{
99-
if (_state == StatementState.Deallocated || _state == StatementState.Error)
100-
{
101-
return false;
102-
}
103-
else
104-
{
105-
return true;
106-
}
107-
}
108-
}
109-
110-
public override DbStatementType StatementType
111-
{
112-
get { return _statementType; }
113-
protected set { _statementType = value; }
114-
}
115-
116-
public override StatementState State
117-
{
118-
get { return _state; }
119-
protected set { _state = value; }
120-
}
121-
12290
public override int FetchSize
12391
{
12492
get { return _fetchSize; }
12593
set { _fetchSize = value; }
12694
}
12795

128-
public override bool ReturnRecordsAffected
129-
{
130-
get { return _returnRecordsAffected; }
131-
set { _returnRecordsAffected = value; }
132-
}
133-
13496
#endregion
13597

13698
#region Constructors
@@ -152,7 +114,6 @@ public GdsStatement(IDatabase db, TransactionBase transaction)
152114
}
153115

154116
_handle = IscCodes.INVALID_OBJECT;
155-
RecordsAffected = -1;
156117
_fetchSize = 200;
157118
_rows = new Queue<DbValue[]>();
158119
_outputParams = new Queue<DbValue[]>();
@@ -183,11 +144,8 @@ public override void Dispose()
183144
_parameters = null;
184145
_transaction = null;
185146
_allRowsFetched = false;
186-
_state = StatementState.Deallocated;
187-
_statementType = DbStatementType.None;
188147
_handle = 0;
189148
_fetchSize = 0;
190-
RecordsAffected = 0;
191149
base.Dispose();
192150
}
193151
}
@@ -235,7 +193,7 @@ public override void Prepare(string commandText)
235193

236194
try
237195
{
238-
if (_state == StatementState.Deallocated)
196+
if (State == StatementState.Deallocated)
239197
{
240198
SendAllocateToBuffer();
241199
_database.XdrStream.Flush();
@@ -248,22 +206,20 @@ public override void Prepare(string commandText)
248206

249207
SendInfoSqlToBuffer(StatementTypeInfoItems, IscCodes.STATEMENT_TYPE_BUFFER_SIZE);
250208
_database.XdrStream.Flush();
251-
_statementType = ProcessStatementTypeInfoBuffer(ProcessInfoSqlResponse(_database.ReadResponse<GenericResponse>()));
252-
209+
StatementType = ProcessStatementTypeInfoBuffer(ProcessInfoSqlResponse(_database.ReadResponse<GenericResponse>()));
253210

254-
_state = StatementState.Prepared;
211+
State = StatementState.Prepared;
255212
}
256213
catch (IOException ex)
257214
{
258-
if (_state == StatementState.Allocated)
259-
_state = StatementState.Error;
215+
State = State == StatementState.Allocated ? StatementState.Error : State;
260216
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
261217
}
262218
}
263219

264220
public override void Execute()
265221
{
266-
if (_state == StatementState.Deallocated)
222+
if (State == StatementState.Deallocated)
267223
{
268224
throw new InvalidOperationException("Statement is not correctly created.");
269225
}
@@ -272,13 +228,11 @@ public override void Execute()
272228

273229
try
274230
{
275-
RecordsAffected = -1;
276-
277231
SendExecuteToBuffer();
278232

279233
_database.XdrStream.Flush();
280234

281-
if (_statementType == DbStatementType.StoredProcedure)
235+
if (StatementType == DbStatementType.StoredProcedure)
282236
{
283237
ProcessStoredProcedureExecuteResponse(_database.ReadResponse<SqlResponse>());
284238
}
@@ -292,32 +246,36 @@ public override void Execute()
292246
_database.XdrStream.Flush();
293247
RecordsAffected = ProcessRecordsAffectedBuffer(ProcessInfoSqlResponse(_database.ReadResponse<GenericResponse>()));
294248
}
249+
else
250+
{
251+
RecordsAffected = -1;
252+
}
295253

296-
_state = StatementState.Executed;
254+
State = StatementState.Executed;
297255
}
298256
catch (IOException ex)
299257
{
300-
_state = StatementState.Error;
258+
State = StatementState.Error;
301259
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
302260
}
303261
}
304262

305263
public override DbValue[] Fetch()
306264
{
307-
if (_state == StatementState.Deallocated)
265+
if (State == StatementState.Deallocated)
308266
{
309267
throw new InvalidOperationException("Statement is not correctly created.");
310268
}
311-
if (_statementType == DbStatementType.StoredProcedure && !_allRowsFetched)
269+
if (StatementType == DbStatementType.StoredProcedure && !_allRowsFetched)
312270
{
313271
_allRowsFetched = true;
314272
return GetOutputParameters();
315273
}
316-
else if (_statementType == DbStatementType.Insert && _allRowsFetched)
274+
else if (StatementType == DbStatementType.Insert && _allRowsFetched)
317275
{
318276
return null;
319277
}
320-
else if (_statementType != DbStatementType.Select && _statementType != DbStatementType.SelectForUpdate)
278+
else if (StatementType != DbStatementType.Select && StatementType != DbStatementType.SelectForUpdate)
321279
{
322280
return null;
323281
}
@@ -478,8 +436,7 @@ protected override void Free(int option)
478436

479437
protected bool FreeNotNeeded(int option)
480438
{
481-
// Does not seem to be possible or necessary to close
482-
// an execute procedure statement.
439+
// does not seem to be possible or necessary to close an execute procedure statement
483440
if (StatementType == DbStatementType.StoredProcedure && option == IscCodes.DSQL_close)
484441
{
485442
return true;
@@ -509,7 +466,7 @@ protected void DoFreePacket(int option)
509466
}
510467
catch (IOException ex)
511468
{
512-
_state = StatementState.Error;
469+
State = StatementState.Error;
513470
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
514471
}
515472
}
@@ -531,8 +488,8 @@ protected void ProcessAllocateResponce(GenericResponse response)
531488
{
532489
_handle = response.ObjectHandle;
533490
_allRowsFetched = false;
534-
_state = StatementState.Allocated;
535-
_statementType = DbStatementType.None;
491+
State = StatementState.Allocated;
492+
StatementType = DbStatementType.None;
536493
}
537494
#endregion
538495

@@ -542,7 +499,7 @@ protected void SendExecuteToBuffer()
542499
// this may throw error, so it needs to be before any writing
543500
var descriptor = WriteParameters();
544501

545-
if (_statementType == DbStatementType.StoredProcedure)
502+
if (StatementType == DbStatementType.StoredProcedure)
546503
{
547504
_database.XdrStream.Write(IscCodes.op_execute2);
548505
}
@@ -568,7 +525,7 @@ protected void SendExecuteToBuffer()
568525
_database.XdrStream.Write(0);
569526
}
570527

571-
if (_statementType == DbStatementType.StoredProcedure)
528+
if (StatementType == DbStatementType.StoredProcedure)
572529
{
573530
_database.XdrStream.WriteBuffer(_fields?.ToBlrArray());
574531
_database.XdrStream.Write(0); // Output message number
@@ -603,7 +560,7 @@ protected override void TransactionUpdated(object sender, EventArgs e)
603560
Transaction.Update -= TransactionUpdate;
604561
}
605562

606-
_state = StatementState.Closed;
563+
State = StatementState.Closed;
607564
TransactionUpdate = null;
608565
_allRowsFetched = false;
609566
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version11/GdsStatement.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override void Prepare(string commandText)
4747
try
4848
{
4949
var numberOfResponses = 0;
50-
if (_state == StatementState.Deallocated)
50+
if (State == StatementState.Deallocated)
5151
{
5252
SendAllocateToBuffer();
5353
numberOfResponses++;
@@ -64,7 +64,7 @@ public override void Prepare(string commandText)
6464
try
6565
{
6666
GenericResponse allocateResponse = null;
67-
if (_state == StatementState.Deallocated)
67+
if (State == StatementState.Deallocated)
6868
{
6969
numberOfResponses--;
7070
allocateResponse = _database.ReadResponse<GenericResponse>();
@@ -82,26 +82,25 @@ public override void Prepare(string commandText)
8282
ProcessAllocateResponce(allocateResponse);
8383
}
8484
ProcessPrepareResponse(prepareResponse);
85-
_statementType = ProcessStatementTypeInfoBuffer(ProcessInfoSqlResponse(statementTypeResponse));
85+
StatementType = ProcessStatementTypeInfoBuffer(ProcessInfoSqlResponse(statementTypeResponse));
8686
}
8787
finally
8888
{
8989
SafeFinishFetching(ref numberOfResponses);
9090
}
9191

92-
_state = StatementState.Prepared;
92+
State = StatementState.Prepared;
9393
}
9494
catch (IOException ex)
9595
{
96-
if (_state == StatementState.Allocated)
97-
_state = StatementState.Error;
96+
State = State == StatementState.Allocated ? StatementState.Error : State;
9897
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
9998
}
10099
}
101100

102101
public override void Execute()
103102
{
104-
if (_state == StatementState.Deallocated)
103+
if (State == StatementState.Deallocated)
105104
{
106105
throw new InvalidOperationException("Statement is not correctly created.");
107106
}
@@ -124,8 +123,7 @@ public override void Execute()
124123

125124
_database.XdrStream.Flush();
126125

127-
var numberOfResponses =
128-
(StatementType == DbStatementType.StoredProcedure ? 1 : 0) + 1 + (readRowsAffectedResponse ? 1 : 0);
126+
var numberOfResponses = (StatementType == DbStatementType.StoredProcedure ? 1 : 0) + 1 + (readRowsAffectedResponse ? 1 : 0);
129127
try
130128
{
131129
SqlResponse sqlStoredProcedureResponse = null;
@@ -155,11 +153,11 @@ public override void Execute()
155153
SafeFinishFetching(ref numberOfResponses);
156154
}
157155

158-
_state = StatementState.Executed;
156+
State = StatementState.Executed;
159157
}
160158
catch (IOException ex)
161159
{
162-
_state = StatementState.Error;
160+
State = StatementState.Error;
163161
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
164162
}
165163
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version12/GdsStatement.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public GdsStatement(IDatabase db, TransactionBase transaction)
4242

4343
public override void Execute()
4444
{
45-
if (_state == StatementState.Deallocated)
45+
if (State == StatementState.Deallocated)
4646
{
4747
throw new InvalidOperationException("Statement is not correctly created.");
4848
}
@@ -57,8 +57,7 @@ public override void Execute()
5757

5858
_database.XdrStream.Flush();
5959

60-
var numberOfResponses =
61-
(StatementType == DbStatementType.StoredProcedure ? 1 : 0) + 1;
60+
var numberOfResponses = (StatementType == DbStatementType.StoredProcedure ? 1 : 0) + 1;
6261
try
6362
{
6463
SqlResponse sqlStoredProcedureResponse = null;
@@ -99,11 +98,11 @@ public override void Execute()
9998
}
10099
}
101100

102-
_state = StatementState.Executed;
101+
State = StatementState.Executed;
103102
}
104103
catch (IOException ex)
105104
{
106-
_state = StatementState.Error;
105+
State = StatementState.Error;
107106
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
108107
}
109108
}

0 commit comments

Comments
 (0)