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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
|
//
// System.Data.ProviderBase.AbstractDbCommand
//
// Authors:
// Konstantin Triger <kostat@mainsoft.com>
// Boris Kirzner <borisk@mainsoft.com>
//
// (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Data;
using System.Data.Common;
using java.sql;
using java.io;
#if !USE_DOTNET_REGEXP
using java.util.regex;
#endif
namespace System.Data.ProviderBase
{
public abstract class AbstractDbCommand : DbCommand, ICloneable
{
#region ProcedureColumnCache
internal sealed class ProcedureColumnCache : AbstractDbMetaDataCache
{
internal ArrayList GetProcedureColumns(AbstractDBConnection connection, String commandText,AbstractDbCommand command)
{
string connectionCatalog = connection.JdbcConnection.getCatalog();
string key = String.Concat(connection.ConnectionString, connectionCatalog, commandText);
System.Collections.Hashtable cache = Cache;
ArrayList col = cache[key] as ArrayList;
if (null != col) {
return col;
}
col = connection.GetProcedureColumns(commandText,command);
if (col != null)
cache[key] = col;
return col;
}
}
#endregion
#region SqlStatementsHelper
internal sealed class SqlStatementsHelper
{
#region Fields
#if USE_DOTNET_REGEXP
internal static readonly Regex NamedParameterStoredProcedureRegExp = new Regex(@"^\s*{?\s*((?<RETVAL>@\w+)\s*=\s*)?call\s+(?<PROCNAME>(((\[[^\]]*\])|([^\.\(])*)\s*\.\s*){0,2}(\[[^\]]*\]|((\s*[^\.\(\)\{\}\s])+)))\s*(\(\s*(?<USERPARAM>((""([^""]|(""""))*"")|('([^']|(''))*')|[^,])*)?\s*(,\s*(?<USERPARAM>((""([^""]|(""""))*"")|('([^']|(''))*')|[^,])*)\s*)*\))?\s*}?\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
internal static readonly Regex SimpleParameterStoredProcedureRegExp = new Regex(@"^\s*{?\s*((?<RETVAL>\?)\s*=\s*)?call\s+(?<PROCNAME>(((\[[^\]]*\])|([^\.\(])*)\s*\.\s*){0,2}(\[[^\]]*\]|((\s*[^\.\(\)\{\}\s])+)))\s*(\(\s*(?<USERPARAM>((""([^""]|(""""))*"")|('([^']|(''))*')|[^,])*)?\s*(,\s*(?<USERPARAM>((""([^""]|(""""))*"")|('([^']|(''))*')|[^,])*)\s*)*\))?\s*}?\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
internal static readonly Regex ForBrowseStatementReqExp = new Regex(@"\s+FOR\s+BROWSE\s*$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
#else
internal static readonly Pattern NamedParameterStoredProcedureRegExp = Pattern.compile(@"^\s*\{?\s*(?:(@\w+)\s*=\s*)?call\s+((?:(?:(?:\[[^\]]*\])|(?:[^\.\(\)\{\}\[\]])*)\s*\.\s*){0,2}(?:\[[^\]]*\]|(?:(?:\s*[^\.\(\)\{\}\[\]])+)))\s*(?:\((.*)\))?\s*\}?\s*$", Pattern.CASE_INSENSITIVE);
internal static readonly Pattern SimpleParameterStoredProcedureRegExp = Pattern.compile(@"^\s*\{?\s*(?:(\?)\s*=\s*)?call\s+((?:(?:(?:\[[^\]]*\])|(?:[^\.\(\)\{\}\[\]])*)\s*\.\s*){0,2}(?:\[[^\]]*\]|(?:(?:\s*[^\.\(\)\{\}\[\]])+)))\s*(?:\((.*)\))?\s*\}?\s*$", Pattern.CASE_INSENSITIVE);
internal static readonly Pattern ForBrowseStatementReqExp = Pattern.compile(@"\s+FOR\s+BROWSE\s*$", Pattern.CASE_INSENSITIVE);
#endif
internal static readonly SimpleRegex NamedParameterRegExp = new SqlParamsRegex();
internal static readonly SimpleRegex SimpleParameterRegExp = new OleDbParamsRegex();
internal static readonly SimpleRegex CompoundStatementSplitterReqExp = new CharacterSplitterRegex(';');
internal static readonly SimpleRegex ProcedureParameterSplitterReqExp = new CharacterSplitterRegex(',');
#endregion // Fields
}
#endregion // SqlStatementsHelper
#region Fields
string _commandText;
int _commandTimeout;
CommandType _commandType;
bool _designTimeVisible;
UpdateRowSource _updatedRowSource;
private DbParameterCollection _parameters;
private java.sql.Statement _statement;
private AbstractDBConnection _connection;
private AbstractTransaction _transaction;
private bool _isCommandPrepared;
private CommandBehavior _behavior;
private ArrayList _internalParameters;
string _javaCommandText;
private int _recordsAffected;
private ResultSet _currentResultSet;
private DbDataReader _currentReader;
private bool _nullParametersInPrepare;
private bool _hasResultSet;
private bool _explicitPrepare;
static ProcedureColumnCache _procedureColumnCache = new ProcedureColumnCache();
#endregion // Fields
#region Constructors
public AbstractDbCommand(
String cmdText,
AbstractDBConnection connection,
AbstractTransaction transaction)
{
_connection = connection;
_commandText = cmdText;
_transaction = transaction;
_commandTimeout = 30;
_commandType = CommandType.Text;
_designTimeVisible = true;
_updatedRowSource = UpdateRowSource.Both;
_isCommandPrepared = false;
_explicitPrepare = false;
_recordsAffected = -1;
if (connection != null) {
connection.AddReference(this);
}
}
#endregion // Constructors
#region Properties
public override int CommandTimeout {
get { return _commandTimeout; }
set { _commandTimeout = value; }
}
public override CommandType CommandType {
get { return _commandType; }
set { _commandType = value; }
}
public override bool DesignTimeVisible {
get { return _designTimeVisible; }
set { _designTimeVisible = value; }
}
public override UpdateRowSource UpdatedRowSource {
get { return _updatedRowSource; }
set { _updatedRowSource = value; }
}
protected override DbParameterCollection DbParameterCollection
{
get {
if (_parameters == null) {
_parameters = CreateParameterCollection(this);
}
return _parameters;
}
}
protected override DbConnection DbConnection
{
get { return (DbConnection)_connection; }
set {
if (value == _connection) {
return;
}
if (_currentReader != null && !_currentReader.IsClosed) {
throw ExceptionHelper.ConnectionIsBusy(this.GetType().Name,((AbstractDBConnection)_connection).InternalState);
}
if (_connection != null) {
_connection.RemoveReference(this);
}
_connection = (AbstractDBConnection) value;
if (_connection != null) {
_connection.AddReference(this);
}
}
}
protected override DbTransaction DbTransaction
{
get { return _transaction != null ? _transaction.ActiveTransaction : null; }
set { _transaction = (AbstractTransaction)value; }
}
public override string CommandText
{
get { return _commandText; }
set {
if (CommandText == null || String.Compare(CommandText, value, true) != 0) {
_commandText = value;
_isCommandPrepared = false;
_explicitPrepare = false;
}
}
}
protected virtual string InternalCommandText {
get { return CommandText; }
//set { CommandText = value; }
}
internal CommandBehavior Behavior
{
get { return _behavior; }
set { _behavior = value; }
}
bool IsCommandPrepared
{
get { return _isCommandPrepared; }
set { _isCommandPrepared = value; }
}
bool NullParametersInPrepare
{
get { return _nullParametersInPrepare; }
set { _nullParametersInPrepare = value; }
}
protected ArrayList InternalParameters
{
get {
if (_internalParameters == null) {
_internalParameters = new ArrayList();
}
return _internalParameters;
}
}
// Number of records affected by execution of batch statement
// -1 for SELECT statements.
internal int RecordsAffected
{
get {
return _recordsAffected;
}
}
// AbstractDbCommand acts as IEnumerator over JDBC statement
// AbstractDbCommand.CurrentResultSet corresponds to IEnumerator.Current
protected internal virtual ResultSet CurrentResultSet
{
get {
try {
if (_currentResultSet == null && _hasResultSet) {
_currentResultSet = _statement.getResultSet();
}
return _currentResultSet;
}
catch(SQLException e) {
throw new Exception(e.Message, e);
}
}
}
protected internal java.sql.Statement Statement
{
get { return _statement; }
}
#if USE_DOTNET_REGEX
protected virtual Regex StoredProcedureRegExp
#else
protected virtual Pattern StoredProcedureRegExp
#endif
{
get { return SqlStatementsHelper.SimpleParameterStoredProcedureRegExp; }
}
protected virtual SimpleRegex ParameterRegExp
{
get { return SqlStatementsHelper.SimpleParameterRegExp; }
}
#endregion // Properties
#region Methods
protected abstract DbParameter CreateParameterInternal();
protected abstract void CheckParameters();
protected abstract DbDataReader CreateReader();
protected abstract DbParameterCollection CreateParameterCollection(AbstractDbCommand parent);
protected internal abstract SystemException CreateException(SQLException e);
public override int ExecuteNonQuery ()
{
IDataReader reader = null;
try {
reader = ExecuteReader ();
}
finally {
if (reader != null)
reader.Close ();
}
return reader.RecordsAffected;
}
public override object ExecuteScalar ()
{
IDataReader reader = ExecuteReader(CommandBehavior.SequentialAccess);
try {
do {
if (reader.FieldCount > 0 && reader.Read ())
return reader.GetValue (0);
}
while (reader.NextResult ());
return null;
} finally {
reader.Close();
}
}
public virtual void ResetCommandTimeout ()
{
_commandTimeout = 30;
}
public override void Cancel()
{
try {
if (_statement != null)
_statement.cancel();
}
catch {
// MSDN says : "If there is nothing to cancel, nothing happens.
// However, if there is a command in process, and the attempt to cancel fails,
// no exception is generated."
}
}
protected virtual bool SkipParameter(DbParameter parameter)
{
return false;
}
protected sealed override DbParameter CreateDbParameter()
{
return CreateParameterInternal();
}
internal ArrayList DeriveParameters(string procedureName, bool throwIfNotExist)
{
try {
ArrayList col = _procedureColumnCache.GetProcedureColumns((AbstractDBConnection)Connection, procedureName, this);
if (col == null) {
if (throwIfNotExist)
throw ExceptionHelper.NoStoredProcedureExists(procedureName);
col = new ArrayList();
}
return col;
}
catch(SQLException e) {
throw CreateException(e);
}
}
string CreateTableDirectCommandText(string tableNames) {
string forBrowse = String.Empty;
if ((Behavior & CommandBehavior.KeyInfo) != 0) {
AbstractDBConnection connection = (AbstractDBConnection)Connection;
if (connection != null) {
string dbname = connection.JdbcConnection.getMetaData().getDatabaseProductName();
if (dbname == "Microsoft SQL Server") //must add "FOR BROWSE" for selects
forBrowse = " FOR BROWSE";
}
}
string[] names = tableNames.Split(',');
StringBuilder sb = new StringBuilder();
for(int i = 0; i < names.Length; i++) {
sb.Append("SELECT * FROM ");
sb.Append(names[i]);
sb.Append(forBrowse);
sb.Append(';');
}
if(names.Length <= 1) {
sb.Remove(sb.Length - 1,1);
}
return sb.ToString();
}
private string PrepareCommandTextAndParameters()
{
NullParametersInPrepare = false;
switch (CommandType) {
case CommandType.TableDirect :
return CreateTableDirectCommandText(CommandText);
case CommandType.StoredProcedure :
return CreateStoredProcedureCommandTextSimple (InternalCommandText, Parameters, DeriveParameters (InternalCommandText, false));
case CommandType.Text :
int userParametersPosition = 0;
int charsConsumed = 0;
StringBuilder sb = new StringBuilder(CommandText.Length);
for (SimpleMatch match = SqlStatementsHelper.CompoundStatementSplitterReqExp.Match(CommandText);
match.Success;
match = match.NextMatch()) {
int length = match.Length;
if (length == 0)
continue;
int start = match.Index;
string value = match.Value;
sb.Append(CommandText, charsConsumed, start-charsConsumed);
charsConsumed = start + length;
#if USE_DOTNET_REGEX
Match storedProcMatch = StoredProcedureRegExp.Match(value);
// count parameters for all kinds of simple statements
userParametersPosition +=
(storedProcMatch.Success) ?
// statement is stored procedure call
CreateStoredProcedureCommandText(sb, value, storedProcMatch, Parameters, userParametersPosition) :
// statement is a simple SQL query
PrepareSimpleQuery(sb, value, Parameters, userParametersPosition);
#else
Matcher storedProcMatch = StoredProcedureRegExp.matcher((java.lang.CharSequence)(object)value);
userParametersPosition +=
(storedProcMatch.find()) ?
// statement is stored procedure call
CreateStoredProcedureCommandText(sb, value, storedProcMatch, Parameters, userParametersPosition) :
// statement is a simple SQL query
PrepareSimpleQuery(sb, value, Parameters, userParametersPosition);
#endif
}
sb.Append(CommandText, charsConsumed, CommandText.Length-charsConsumed);
return sb.ToString();
}
return null;
}
string CreateStoredProcedureCommandTextSimple(string procedureName, IDataParameterCollection userParams, IList derivedParams) {
StringBuilder sb = new StringBuilder();
int curUserPos = 0;
int curDerivedPos = 0;
bool addParas = true;
string trimedProcedureName = (procedureName != null) ? procedureName.TrimEnd() : String.Empty;
if (trimedProcedureName.Length > 0 && trimedProcedureName[trimedProcedureName.Length-1] == ')')
addParas = false;
AbstractDbParameter derivedParam = (derivedParams.Count > 0) ? (AbstractDbParameter)derivedParams[curDerivedPos] : null;
if (derivedParam != null) {
if (derivedParam.Direction == ParameterDirection.ReturnValue)
curDerivedPos++;
else
derivedParam = null; //play as if there is no retval parameter
}
AbstractDbParameter returnValueParameter = GetReturnParameter (userParams);
if (returnValueParameter != null) {
curUserPos++;
InternalParameters.Add(returnValueParameter);
sb.Append("{? = call ");
if (derivedParam != null && !returnValueParameter.IsDbTypeSet) {
returnValueParameter.JdbcType = derivedParam.JdbcType;
}
}
else {
sb.Append("{call ");
}
sb.Append(procedureName);
if (addParas)
sb.Append('(');
bool needComma = false;
for (int i = curDerivedPos; i < derivedParams.Count; i++) {
AbstractDbParameter derivedParameter = (AbstractDbParameter)derivedParams[curDerivedPos++];
bool addParam = false;
if (derivedParameter.IsSpecial) {
// derived parameter is special - never appears in user parameters or user values
InternalParameters.Add((AbstractDbParameter)derivedParameter.Clone());
addParam = true;
}
else {
AbstractDbParameter userParameter = GetUserParameter(derivedParameter.Placeholder, userParams, curUserPos);
if (userParameter != null) {
curUserPos++;
InternalParameters.Add(userParameter);
addParam = true;
if (derivedParameter != null && !userParameter.IsDbTypeSet) {
userParameter.JdbcType = derivedParameter.JdbcType;
}
}
}
if (addParam) {
if (needComma)
sb.Append(',');
else
needComma = true;
sb.Append('?');
}
}
for (int i = curUserPos; i < userParams.Count; i++) {
if (needComma)
sb.Append(',');
else
needComma = true;
AbstractDbParameter userParameter = (AbstractDbParameter)userParams[curUserPos++];
InternalParameters.Add(userParameter);
sb.Append('?');
}
if (addParas)
sb.Append(')');
sb.Append('}');
return sb.ToString();
}
/// <summary>
/// We suppose that user parameters are in the same order as devived parameters except the special cases
/// (return value, oracle ref cursors etc.)
/// </summary>
//protected virtual string CreateStoredProcedureCommandText(string procedureName, IList userParametersList, int userParametersListStart/*, int userParametersListCount*/, string[] userValuesList, ArrayList derivedParametersList)
#if USE_DOTNET_REGEX
int CreateStoredProcedureCommandText(StringBuilder sb, string sql, Match match, IDataParameterCollection userParams, int userParamsStartPosition)
#else
int CreateStoredProcedureCommandText(StringBuilder sb, string sql, Matcher match, IDataParameterCollection userParams, int userParamsStartPosition)
#endif
{
int curUserPos = userParamsStartPosition;
#if USE_DOTNET_REGEX
Group procNameGroup = null;
for (Match procNameMatch = match; procNameMatch.Success; procNameMatch = procNameMatch.NextMatch()){
procNameGroup = match.Groups["PROCNAME"];
if (!procNameGroup.Success) {
continue;
}
}
if (procNameGroup == null || !procNameGroup.Success)
throw new ArgumentException("Not a stored procedure call: '{0}'", sql);
ArrayList derivedParameters = DeriveParameters(procNameGroup.Value, false);
#else
ArrayList derivedParameters = DeriveParameters(match.group(2).Trim(), false);
#endif
int curDerivedPos = 0;
AbstractDbParameter retValderivedParameter = curDerivedPos < derivedParameters.Count ?
(AbstractDbParameter)derivedParameters[curDerivedPos] : null;
if (retValderivedParameter != null && retValderivedParameter.Direction == ParameterDirection.ReturnValue)
curDerivedPos++;
int queryCurrentPosition = 0;
#if USE_DOTNET_REGEX
for (Match retValMatch = match; retValMatch.Success; retValMatch = retValMatch.NextMatch()){
Group retval = retValMatch.Groups["RETVAL"];
if (!retval.Success) {
continue;
}
int retvalIndex = retval.Index;
string retvalValue = retval.Value;
int retvalLength = retval.Length;
#else
int retvalIndex = match.start(1);
for (;retvalIndex >= 0;) {
string retvalValue = match.group(1);
int retvalLength = retvalValue.Length;
#endif
sb.Append(sql, queryCurrentPosition, retvalIndex);
AbstractDbParameter userParameter = GetUserParameter(retvalValue, userParams, curUserPos);
if (userParameter != null) {
sb.Append('?');
InternalParameters.Add(userParameter);
if (retValderivedParameter != null && !userParameter.IsDbTypeSet) {
userParameter.JdbcType = retValderivedParameter.JdbcType;
}
curUserPos++;
}
else {
sb.Append(retvalValue);
}
queryCurrentPosition = (retvalIndex + retvalLength);
break;
}
#if USE_DOTNET_REGEX
sb.Append(sql, queryCurrentPosition, procNameGroup.Index + procNameGroup.Length - queryCurrentPosition);
queryCurrentPosition = procNameGroup.Index + procNameGroup.Length;
#else
sb.Append(sql, queryCurrentPosition, match.end(2) - queryCurrentPosition);
queryCurrentPosition = match.end(2);
#endif
bool hasUserParams = false;
#if USE_DOTNET_REGEX
must rewrite the regex to not parse params to have single code with java regex
#else
int paramsStart = match.start(3);
if (paramsStart >= 0) {
#endif
hasUserParams = true;
sb.Append(sql,queryCurrentPosition,paramsStart - queryCurrentPosition);
queryCurrentPosition = paramsStart;
for (SimpleMatch m = SqlStatementsHelper.ProcedureParameterSplitterReqExp.Match(match.group(3));
m.Success;m = m.NextMatch()) {
SimpleCapture parameterCapture = m;
sb.Append(sql,queryCurrentPosition,paramsStart + parameterCapture.Index - queryCurrentPosition);
// advance in query
queryCurrentPosition = paramsStart + parameterCapture.Index + parameterCapture.Length;
AbstractDbParameter derivedParameter = curDerivedPos < derivedParameters.Count ?
(AbstractDbParameter)derivedParameters[curDerivedPos++] : null;
//check for special params
while (derivedParameter != null && derivedParameter.IsSpecial) {
// derived parameter is special - never appears in user parameters or user values
InternalParameters.Add((AbstractDbParameter)derivedParameter.Clone());
sb.Append('?');
sb.Append(',');
derivedParameter = curDerivedPos < derivedParameters.Count ?
(AbstractDbParameter)derivedParameters[curDerivedPos++] : null;
}
AbstractDbParameter userParameter = GetUserParameter(parameterCapture.Value.Trim(), userParams, curUserPos);
if (userParameter != null) {
sb.Append('?');
InternalParameters.Add(userParameter);
if (derivedParameter != null && !userParameter.IsDbTypeSet) {
userParameter.JdbcType = derivedParameter.JdbcType;
}
// advance in user parameters
curUserPos++;
}
else {
sb.Append(parameterCapture.Value);
}
}
}
bool addedSpecialParams = false;
for (int i = curDerivedPos; i < derivedParameters.Count;) {
AbstractDbParameter derivedParameter = (AbstractDbParameter)derivedParameters[i++];
if (derivedParameter.IsSpecial) {
// derived parameter is special - never appears in user parameters or user values
if (!hasUserParams && !addedSpecialParams) {
addedSpecialParams = true;
curDerivedPos++;
sb.Append('(');
}
for (;curDerivedPos < i;curDerivedPos++)
sb.Append(',');
InternalParameters.Add((AbstractDbParameter)derivedParameter.Clone());
sb.Append('?');
}
}
if (!hasUserParams && addedSpecialParams)
sb.Append(')');
sb.Append(sql,queryCurrentPosition,sql.Length - queryCurrentPosition);
return curUserPos - userParamsStartPosition;
}
protected virtual AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition)
{
if (userParametersListPosition < userParametersList.Count) {
AbstractDbParameter param = (AbstractDbParameter)userParametersList[userParametersListPosition];
if (param.Placeholder == parameterName)
return param;
}
return null;
}
protected virtual AbstractDbParameter GetReturnParameter (IList userParametersList)
{
AbstractDbParameter param = GetUserParameter ("?", userParametersList, 0);
if (param != null && param.Direction == ParameterDirection.ReturnValue)
return param;
return null;
}
int PrepareSimpleQuery(StringBuilder sb, string query, IList userParametersList, int userParametersListStart)
{
int queryCurrentPosition = 0;
int userParametersListPosition = userParametersListStart;
if (userParametersList.Count > 0) {
for (SimpleMatch m = ParameterRegExp.Match(query);
m.Success;m = m.NextMatch()) {
SimpleCapture parameterCapture = m;
sb.Append(query,queryCurrentPosition,parameterCapture.Index - queryCurrentPosition);
// advance in query
queryCurrentPosition = parameterCapture.Index + parameterCapture.Length;
AbstractDbParameter userParameter = GetUserParameter(parameterCapture.Value, userParametersList, userParametersListPosition);
if (userParameter != null) {
if (IsNullParameter(userParameter)) {
sb.Append("null");
NullParametersInPrepare = true;
}
else {
sb.Append('?');
InternalParameters.Add(userParameter);
}
// advance in user parameters
userParametersListPosition++;
}
else {
sb.Append(parameterCapture.Value);
}
}
}
sb.Append(query,queryCurrentPosition,query.Length - queryCurrentPosition);
int userParamsConsumed = userParametersListPosition - userParametersListStart;
if ((Behavior & CommandBehavior.KeyInfo) == 0)
return userParamsConsumed;
AbstractDBConnection connection = (AbstractDBConnection)Connection;
if (connection == null)
return userParamsConsumed;
string dbname = connection.JdbcConnection.getMetaData().getDatabaseProductName();
if (dbname == "Microsoft SQL Server") { //must add "FOR BROWSE" for selects
#if USE_DOTNET_REGEX
if (!SqlStatementsHelper.ForBrowseStatementReqExp.IsMatch(query))
sb.Append(" FOR BROWSE");
#else
if (!SqlStatementsHelper.ForBrowseStatementReqExp.matcher ((java.lang.CharSequence)(object)query).find ())
sb.Append (" FOR BROWSE");
#endif
}
return userParamsConsumed;
}
protected virtual bool IsNullParameter(AbstractDbParameter parameter)
{
return ((parameter.Value == null || parameter.Value == DBNull.Value) && !parameter.IsDbTypeSet);
}
protected virtual void PrepareInternalParameters()
{
InternalParameters.Clear();
}
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
AbstractDBConnection connection = (AbstractDBConnection)Connection;
if (connection == null) {
throw ExceptionHelper.ConnectionNotInitialized("ExecuteReader");
}
connection.IsExecuting = true;
try {
IDbTransaction transaction = Transaction;
if ((transaction != null && transaction.Connection != connection) ||
(transaction == null && !connection.JdbcConnection.getAutoCommit ())) {
throw ExceptionHelper.TransactionNotInitialized ();
}
Behavior = behavior;
PrepareInternalParameters();
PrepareInternal();
// For SchemaOnly there is no need for statement execution
if (Behavior != CommandBehavior.SchemaOnly) {
_recordsAffected = -1;
// FIXME: this causes SP in MS Sql Server to create no mor than one row.
if ((Behavior & CommandBehavior.SingleRow) != 0) {
_statement.setMaxRows (1);
}
if(_statement is PreparedStatement) {
BindParameters(InternalParameters);
_hasResultSet = ((PreparedStatement)_statement).execute();
}
else {
_hasResultSet =_statement.execute(_javaCommandText);
}
if (!_hasResultSet) {
int updateCount = _statement.getUpdateCount();
if (updateCount >= 0) {
AccumulateRecordsAffected(updateCount);
_hasResultSet = true; //play as if we have resultset
NextResultSet();
}
}
}
connection.IsFetching = true;
try {
_currentReader = CreateReader();
}
catch(Exception e) {
connection.IsFetching = false;
throw e;
}
return _currentReader;
}
catch(SQLException e) {
throw CreateException(e);
}
finally {
connection.IsExecuting = false;
NullParametersInPrepare = false;
}
}
public override void Prepare()
{
((AbstractDBConnection)Connection).IsExecuting = true;
try {
CheckParameters();
_explicitPrepare = true;
}
finally {
((AbstractDBConnection)Connection).IsExecuting = false;
}
}
private void PrepareInternal()
{
if ((Connection == null) || (Connection.State != ConnectionState.Open)) {
throw ExceptionHelper.ConnectionNotOpened("Prepare",(Connection != null) ? Connection.State.ToString() : "");
}
if (IsCommandPrepared) {
// maybe we have to prepare the command again
bool hasNullParameters = false;
for(int i = 0; (i < Parameters.Count) && !hasNullParameters; i++) {
AbstractDbParameter parameter = (AbstractDbParameter)Parameters[i];
if (IsNullParameter(parameter)) {
// if we still have null parameters - have to prepare agail
IsCommandPrepared = false;
hasNullParameters = true;
}
}
if (!NullParametersInPrepare && hasNullParameters) {
// if we prepeared using null parameters and now there is no null parameters - need to prepare again
IsCommandPrepared = false;
}
}
if (!IsCommandPrepared) {
_javaCommandText = PrepareCommandTextAndParameters();
java.sql.Connection jdbcCon = _connection.JdbcConnection;
// For SchemaOnly we just prepare statement (for future use in GetSchemaTable)
if (Behavior == CommandBehavior.SchemaOnly) {
if (CommandType == CommandType.StoredProcedure)
_statement = jdbcCon.prepareCall(_javaCommandText);
else
_statement = jdbcCon.prepareStatement(_javaCommandText);
return;
}
if (CommandType == CommandType.StoredProcedure)
_statement = jdbcCon.prepareCall(_javaCommandText);
else {
int internalParametersCount = InternalParameters.Count;
if ( internalParametersCount > 0) {
bool hasOnlyInputParameters = true;
for(int i=0; i < internalParametersCount; i++) {
AbstractDbParameter internalParameter = (AbstractDbParameter)InternalParameters[i];
if (IsNullParameter(internalParameter)) {
NullParametersInPrepare = true;
}
if ((internalParameter.Direction & ParameterDirection.Output) != 0){
hasOnlyInputParameters = false;
}
}
if (hasOnlyInputParameters) {
_statement = jdbcCon.prepareStatement(_javaCommandText);
}
else {
_statement = jdbcCon.prepareCall(_javaCommandText);
}
}
else {
if (_explicitPrepare) {
_statement = jdbcCon.prepareStatement(_javaCommandText);
}
else {
_statement = jdbcCon.createStatement();
}
}
}
IsCommandPrepared = true;
}
}
protected void BindParameters(ArrayList parameters)
{
for(int parameterIndex = 0; parameterIndex < parameters.Count; parameterIndex++) {
AbstractDbParameter parameter = (AbstractDbParameter)parameters[parameterIndex];
switch (parameter.Direction) {
case ParameterDirection.Input :
BindInputParameter(parameter,parameterIndex);
break;
case ParameterDirection.InputOutput:
BindInputParameter(parameter,parameterIndex);
BindOutputParameter(parameter,parameterIndex);
break;
case ParameterDirection.Output :
BindOutputParameter(parameter,parameterIndex);
break;
case ParameterDirection.ReturnValue :
BindOutputParameter(parameter,parameterIndex);
break;
}
}
}
protected virtual void BindInputParameter(AbstractDbParameter parameter, int parameterIndex)
{
object value = parameter.ConvertedValue;
// java parameters are 1 based, while .net are 0 based
parameterIndex++;
PreparedStatement preparedStatement = ((PreparedStatement)_statement);
switch ((DbConvert.JavaSqlTypes)parameter.JdbcType) {
case DbConvert.JavaSqlTypes.DATALINK:
case DbConvert.JavaSqlTypes.DISTINCT:
case DbConvert.JavaSqlTypes.JAVA_OBJECT:
case DbConvert.JavaSqlTypes.OTHER:
case DbConvert.JavaSqlTypes.REF:
case DbConvert.JavaSqlTypes.STRUCT: {
preparedStatement.setObject(parameterIndex, value, (int)parameter.JdbcType);
return;
}
}
if ((value is DBNull) || (value == null)) {
preparedStatement.setNull(parameterIndex, (int)((AbstractDbParameter)parameter).JdbcType);
}
else if (value is long) {
preparedStatement.setLong(parameterIndex, (long)value);
}
else if (value is byte[]) {
if (((byte[])value).Length <= 4000) {
preparedStatement.setBytes(parameterIndex, vmw.common.TypeUtils.ToSByteArray((byte[]) value));
}
else {
InputStream iStream=new ByteArrayInputStream(vmw.common.TypeUtils.ToSByteArray((byte[]) value));
preparedStatement.setBinaryStream(parameterIndex,iStream,((byte[])value).Length);
}
}
else if (value is byte) {
preparedStatement.setByte(parameterIndex, (sbyte)(byte)value);
}
else if (value is char[]) {
Reader reader = new CharArrayReader((char[])value);
preparedStatement.setCharacterStream(parameterIndex,reader,((char[])value).Length);
}
else if (value is bool) {
preparedStatement.setBoolean(parameterIndex, (bool) value);
}
else if (value is char) {
preparedStatement.setString(parameterIndex, ((char)value).ToString());
}
else if (value is DateTime) {
switch ((DbConvert.JavaSqlTypes)parameter.JdbcType) {
default:
case DbConvert.JavaSqlTypes.TIMESTAMP:
preparedStatement.setTimestamp(parameterIndex,DbConvert.ClrTicksToJavaTimestamp(((DateTime)value).Ticks));
break;
case DbConvert.JavaSqlTypes.TIME:
preparedStatement.setTime(parameterIndex,DbConvert.ClrTicksToJavaTime(((DateTime)value).Ticks));
break;
case DbConvert.JavaSqlTypes.DATE:
preparedStatement.setDate(parameterIndex,DbConvert.ClrTicksToJavaDate(((DateTime)value).Ticks));
break;
}
}
else if (value is TimeSpan) {
if (parameter.JdbcType == (int)DbConvert.JavaSqlTypes.TIMESTAMP)
preparedStatement.setTimestamp(parameterIndex,DbConvert.ClrTicksToJavaTimestamp(((TimeSpan)value).Ticks));
else
preparedStatement.setTime(parameterIndex,DbConvert.ClrTicksToJavaTime(((TimeSpan)value).Ticks));
}
else if (value is Decimal) {
preparedStatement.setBigDecimal(parameterIndex, vmw.common.PrimitiveTypeUtils.DecimalToBigDecimal((Decimal) value));
}
else if (value is double) {
preparedStatement.setDouble(parameterIndex, (double)value);
}
else if (value is float) {
preparedStatement.setFloat(parameterIndex, (float)value);
}
else if (value is int) {
preparedStatement.setInt(parameterIndex, (int)value);
}
else if (value is string) {
//can not be done for inout params, due to Oracle problem with FIXED_CHAR out param fetching
if (parameter.Direction == ParameterDirection.Input &&
preparedStatement is Mainsoft.Data.Jdbc.Providers.IPreparedStatement &&
(DbConvert.JavaSqlTypes)parameter.JdbcType == DbConvert.JavaSqlTypes.CHAR) {
((Mainsoft.Data.Jdbc.Providers.IPreparedStatement)preparedStatement)
.setChar(parameterIndex, (string)value);
}
else
preparedStatement.setString(parameterIndex, (string)value);
}
else if (value is Guid) {
preparedStatement.setString(parameterIndex, value.ToString());
}
else if (value is short) {
preparedStatement.setShort(parameterIndex, (short)value);
}
else if (value is sbyte) {
preparedStatement.setByte(parameterIndex, (sbyte)value);
}
else {
preparedStatement.setObject(parameterIndex, value);
}
}
protected virtual void BindOutputParameter(AbstractDbParameter parameter, int parameterIndex)
{
parameter.Validate();
int jdbcType = (int)parameter.JdbcType;
// java parameters are 1 based, while .net are 0 based
parameterIndex++;
CallableStatement callableStatement = ((CallableStatement)_statement);
// the scale has a meening only in DECIMAL and NUMERIC parameters
if (jdbcType == Types.DECIMAL || jdbcType == Types.NUMERIC) {
if(parameter.DbType == DbType.Currency) {
callableStatement.registerOutParameter(parameterIndex, jdbcType, 4);
}
else {
callableStatement.registerOutParameter(parameterIndex, jdbcType, parameter.Scale);
}
}
else {
callableStatement.registerOutParameter(parameterIndex, jdbcType);
}
}
private void FillOutputParameters()
{
if (!(_statement is CallableStatement)) {
return;
}
for(int i = 0; i < InternalParameters.Count; i++) {
AbstractDbParameter parameter = (AbstractDbParameter)InternalParameters[i];
ParameterDirection direction = parameter.Direction;
if (((direction & ParameterDirection.Output) != 0) && !SkipParameter(parameter)) {
FillOutputParameter(parameter, i);
}
// drop jdbc type of out parameter, since it possibly was updated in ExecuteReader
parameter.IsJdbcTypeSet = false;
}
}
protected virtual void FillOutputParameter(DbParameter parameter, int index)
{
CallableStatement callableStatement = (CallableStatement)_statement;
ParameterMetadataWrapper parameterMetadataWrapper = null;
// FIXME wait for other drivers to implement
// try {
// parameterMetadataWrapper = new ParameterMetadataWrapper(callableStatement.getParameterMetaData());
// }
// catch {
// // suppress error : ms driver for sql server does not implement getParameterMetaData
// // suppress exception : ms driver for sql server does not implement getParameterMetaData
// }
DbConvert.JavaSqlTypes javaSqlType = (DbConvert.JavaSqlTypes)((AbstractDbParameter)parameter).JdbcType;
try {
parameter.Value = DbConvert.JavaResultSetToClrWrapper(callableStatement,index,javaSqlType,parameter.Size,parameterMetadataWrapper);
}
catch(java.sql.SQLException e) {
throw CreateException(e);
}
}
// AbstractDbCommand acts as IEnumerator over JDBC statement
// AbstractDbCommand.NextResultSet corresponds to IEnumerator.MoveNext
protected internal virtual bool NextResultSet()
{
if (!_hasResultSet)
return false;
try {
for(;;) {
_hasResultSet = _statement.getMoreResults();
if (_hasResultSet)
return true;
int updateCount = _statement.getUpdateCount();
if (updateCount < 0)
return false;
AccumulateRecordsAffected(updateCount);
}
}
catch (SQLException e) {
throw CreateException(e);
}
finally {
_currentResultSet = null;
}
}
private void AccumulateRecordsAffected(int updateCount)
{
if (_recordsAffected < 0) {
_recordsAffected = updateCount;
}
else {
_recordsAffected += updateCount;
}
}
internal void OnReaderClosed(object reader)
{
CloseInternal();
if (Connection != null) {
((AbstractDBConnection)Connection).RemoveReference(reader);
((AbstractDBConnection)Connection).IsFetching = false;
if ((Behavior & CommandBehavior.CloseConnection) != 0) {
Connection.Close();
}
}
}
internal void CloseInternal()
{
if (Behavior != CommandBehavior.SchemaOnly) {
if (_statement != null) {
while (NextResultSet()) {
}
FillOutputParameters();
}
}
_currentReader = null;
CleanUp();
}
protected override void Dispose(bool disposing)
{
if (disposing) {
CleanUp();
}
base.Dispose(disposing);
}
private void CleanUp()
{
if (_currentReader != null) {
// we must preserve statement object until we have an associated reader object that might access it.
return;
}
if (Connection != null) {
((AbstractDBConnection)Connection).RemoveReference(this);
}
if (_statement != null) {
_statement.close();
_statement = null;
}
IsCommandPrepared = false;
_internalParameters = null;
_currentResultSet = null;
}
internal void OnSchemaChanging()
{
}
#endregion // Methods
#region ICloneable Members
public virtual object Clone() {
AbstractDbCommand target = (AbstractDbCommand)MemberwiseClone();
target._statement = null;
target._isCommandPrepared = false;
target._internalParameters = null;
target._javaCommandText = null;
target._recordsAffected = -1;
target._currentResultSet = null;
target._currentReader = null;
target._nullParametersInPrepare = false;
target._hasResultSet = false;
target._explicitPrepare = false;
if (Parameters != null && Parameters.Count > 0) {
target._parameters = CreateParameterCollection(target);
for(int i=0 ; i < Parameters.Count; i++) {
target.Parameters.Add(((AbstractDbParameter)Parameters[i]).Clone());
}
}
return target;
}
#endregion
}
}
|