-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGXDataCommon.cs
More file actions
4723 lines (4348 loc) · 131 KB
/
GXDataCommon.cs
File metadata and controls
4723 lines (4348 loc) · 131 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
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Text;
using System.Threading;
using GeneXus.Application;
using GeneXus.Cache;
using GeneXus.Configuration;
using GeneXus.Utils;
#if !NETCORE
using ConnectionBuilder;
using System.Data.SqlClient;
#else
using Microsoft.Data.SqlClient;
#endif
using TZ4Net;
using System.Collections.Generic;
using System.Data.SqlTypes;
using GeneXus.Services;
using GeneXus.Data.NTier.ADO;
using System.Net;
using GeneXus.Http;
using System.Globalization;
using GeneXus.Metadata;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
namespace GeneXus.Data
{
public enum GXMetaType
{
LVChar=0,
VChar=1,
MMedia=2,
Blob=3
}
public enum GXType
{
Number = 0,
Int16 = 1,
Int32 = 2,
Int64 = 3,
Date = 4,
DateTime = 5,
DateTime2 = 17,
Byte = 6,
NChar = 7,
NClob = 8,
NVarChar = 9,
Char = 10,
LongVarChar = 11,
Clob = 12,
VarChar = 13,
Raw = 14,
Blob = 15,
Undefined = 16,
Boolean = 18,
Decimal = 19,
NText = 20,
Text = 21,
Image = 22,
UniqueIdentifier = 23,
Xml = 24,
Geography=25,
Geopoint=26,
Geoline=27,
Geopolygon=28,
DateAsChar=29
}
public interface IGxDataRecord
{
byte GetByte(IGxDbCommand cmd, IDataRecord DR, int i);
long GetBytes(IGxDbCommand cmd, IDataRecord DR, int i, long fieldOffset, byte[] buffer, int bufferOffset, int length);
short GetShort(IGxDbCommand cmd, IDataRecord DR, int i);
int GetInt(IGxDbCommand cmd, IDataRecord DR, int i);
long GetLong(IGxDbCommand cmd, IDataRecord DR, int i);
double GetDouble(IGxDbCommand cmd, IDataRecord DR, int i);
string GetString(IGxDbCommand cmd, IDataRecord DR, int i);
string GetString(IGxDbCommand cmd, IDataRecord DR, int i, int size);
string GetBinary(IGxDbCommand cmd, IDataRecord DR, int i);
DateTime GetDateTimeMs(IGxDbCommand cmd, IDataRecord DR, int i);
DateTime GetDateTime(IGxDbCommand cmd, IDataRecord DR, int i);
DateTime GetDate(IGxDbCommand cmd, IDataRecord DR, int i);
Boolean GetBoolean(IGxDbCommand cmd, IDataRecord DR, int i);
Guid GetGuid(IGxDbCommand cmd, IDataRecord DR, int i);
decimal GetDecimal(IGxDbCommand cmd, IDataRecord DR, int i);
IGeographicNative GetGeospatial(IGxDbCommand cmd, IDataRecord DR, int i);
DateTime Dbms2NetDateTime(DateTime dt, Boolean precision);
DateTime Dbms2NetDate(IGxDbCommand cmd, IDataRecord DR, int i);
Object Net2DbmsDateTime(IDbDataParameter parm, DateTime dt);
Object Net2DbmsGeo(GXType type, IGeographicNative parm);
String DbmsTToC(DateTime dt);
void GetValues(IDataReader reader, ref object[] values);
[Obsolete("ProcessError with 6 arguments is deprecated", false)]
bool ProcessError(uint e, string emsg, GxErrorMask errMask, ref int status, ref bool retry, int retryCount);
bool ProcessError(int e, string emsg, GxErrorMask errMask, IGxConnection con, ref int status, ref bool retry, int retryCount);
bool IsDBNull(IGxDbCommand cmd, IDataRecord DR, int i);
string FalseCondition();
string ToDbmsConstant(short Value);
string ToDbmsConstant(int Value);
string ToDbmsConstant(long Value);
string ToDbmsConstant(decimal Value);
string ToDbmsConstant(float Value);
string ToDbmsConstant(double Value);
string ToDbmsConstant(string Value);
string ToDbmsConstant(DateTime Value);
string ToDbmsConstant(Boolean Value);
int GetCommandTimeout();
IDbCommand GetCommand(IGxConnection con, string stmt, GxParameterCollection parameters);
IDbCommand GetCommand(IGxConnection con, string stmt, GxParameterCollection parameters, bool isCursor, bool forFirst, bool isRpc);
IDataReader GetDataReader(IGxConnectionManager connManager, IGxConnection connection,
GxParameterCollection parameters, string stmt, ushort fetchSize, bool forFirst,
int handle, bool cached, SlidingTime expiration, bool hasNested, bool dynStmt);
IDataReader GetCacheDataReader(CacheItem item, bool computeSize, string keyCache);
IDbDataParameter CreateParameter(string name, Object dbtype, int gxlength, int gxdec);
IDbDataParameter CreateParameter();
void SetParameter(IDbDataParameter parameter, Object value);
object SetParameterValue(IDbDataParameter parameter, Object value);
void SetParameterLVChar(IDbDataParameter parameter, string value, IGxDataStore datastore);
void SetParameterBlob(IDbDataParameter parameter, string value, bool dbBlob);
void SetParameterChar(IDbDataParameter parameter, string value);
void SetParameterVChar(IDbDataParameter parameter, string value);
string DataSource { get; }
void CreateDataBase(string dbname, IGxConnection con);
DbDataAdapterElem GetDataAdapter(IGxConnection con, string stmt, GxParameterCollection parameters);
DbDataAdapterElem GetDataAdapter(IGxConnection con, string stmt, int batchSize, string stmtId);
int BatchUpdate(DbDataAdapterElem da);
int LockTimeout { get; set; }
int LockRetryCount { set; get; }
bool Retry(GxErrorMask errMask, int retryCount);
void SetParameterDir(GxParameterCollection parms, int num, ParameterDirection dir);
object[] ExecuteStoredProcedure(IDbCommand cmd);
bool AllowsDuplicateParameters { get; }
void SetAdapterInsertCommand(DbDataAdapterElem da, IGxConnection con, string stmt, GxParameterCollection parameters);
void SetCursorDef(CursorDef cursorDef);
string ConcatOp(int pos);
string AfterCreateCommand(string stmt, GxParameterCollection parmBinds);
int MaxNumberOfValuesInList { get; }
}
public interface IGxDbCommand : IDbCommand
{
IGxDataRecord Db{get; set;}
Boolean NoError{get; }
bool HasMoreRows{get; set;}
GxErrorMask ErrorMask{get;set;}
void FetchData(out IDataReader DR);
void ExecuteStmt();
void SetParameter( int num, Object value);
IGxConnection Conn { get; }
}
public interface IGxDataStore
{
string Id { get; set; }
int Handle { get; set; }
IGxConnection Connection {get;}
IGxContext Context { get; }
IGxDataRecord Db {get; set;}
string Schema { get; }
string UserId {get;}
short ErrCode {get;}
string ErrDescription {get;}
[Obsolete("IGxDataStore.SmartCacheProvider is deprecated, use IDataStoreProvider.SmartCacheProvider instead", false)]
GxSmartCacheProvider SmartCacheProvider {get;}
[Obsolete("ClientTimeZone is deprecated. Use GxContext.GetTimeZone() instead.", false)]
OlsonTimeZone ClientTimeZone { get; }
void CloseConnections();
void Release();
IDbTransaction BeginTransaction();
void Commit();
void Rollback();
void Close();
void Disconnect();
DateTime DateTime { get;}
DateTime DateTimeMs { get; }
string Version { get;}
bool BeforeConnect();
bool AfterConnect();
}
public interface IGxConnectionManager
{
IGxConnection IncOpenHandles(int handle, string dataSource);
void DecOpenHandles(int handle, string dataSource);
void RefreshTimeStamp(int handle, string dataSource);
IGxConnection SetAvailable(int handle, string dataSource, bool available);
IGxConnection NewConnection(int handle, string dataSource);
void RemoveAllConnections(int handle);
void RemoveConnection(int handle, string dataSource);
bool CloseUserConnections(int handle, string dataSource);
object GetUserInformation(int handle, string dataSource);
void run();
}
public interface IGxConnection: IDbConnection
{
string Data {get ;set ;}
short Method { get;}
new string Database { get; set; }
string DatabaseName { get;}
[Obsolete("ClientTimeZone is deprecated. Use GxContext.GetTimeZone() instead.", false)]
OlsonTimeZone ClientTimeZone { get;}
string DataSourceName {get ;set ;}
string DriverName {get ;set ;}
string FileDataSourceName {get ;set ;}
short ShowPrompt {get ; set ;}
string UserId { get ; set ; }
string UserPassword { get ; set ;}
string Port { get ; set ;}
string CurrentSchema { get; set; }
DateTime ServerDateTime {get;}
DateTime ServerDateTimeMs { get; }
string ServerVersion { get;}
short ErrCode {get ;}
string ErrDescription { get ;}
short FullConnect();
short Disconnect();
IDbTransaction rollbackTransaction();
IDbTransaction commitTransaction();
void SetTransactionalIntegrity( short trnInt);
GxAbstractConnectionWrapper InternalConnection{get; }
bool Available{ get;set;}
void RefreshTimeStamp();
int OpenHandles{ get;}
void IncOpenHandles();
void DecOpenHandles();
void MonitorEnter();
void MonitorExit();
string BlobPath{ get; set;}
string MultimediaPath { get; }
bool Cache{get; set;}
bool Opened{get; set;}
GxConnectionCache ConnectionCache{get;set;}
string CurrentStmt{get;set;}
GxDataRecord DataRecord{get;set;}
IGxDataStore DataStore{get;set;}
void SetIsolationLevel( ushort isolationLevel);
bool UncommitedChanges{get;set;}
void FlushBatchCursors(GXBaseObject obj=null);
void BeforeRollback();
string LastObject
{
get;set;
}
bool LastSQLStatementEnded
{
get;set;
}
string DSVersion
{
get;
set;
}
}
public static class GxCacheFrequency
{
public static int OFF = 0;
public static int TIME_TO_TIME = 1;
public static int HARDLY_EVER = 2;
public static int ALMOST_NEVER = 3;
}
[Flags]
public enum GxErrorMask
{
GX_NOMASK = 1,
GX_MASKLOCKERR = 2,
GX_MASKNOTFOUND = 4,
GX_MASKDUPKEY = 8,
GX_MASKOBJEXIST = 16,
GX_MASKLOOPLOCK = 32,
GX_MASKFOREIGNKEY = 64,
GX_ROLLBACKSAVEPOINT= 128
}
public abstract class GxDataRecord : IGxDataRecord
{
static readonly IGXLogger log = GXLoggerFactory.GetLogger<GxDataRecord>();
public static int RETRY_SLEEP = 500; //500 milliseconds.
protected string m_connectionString;
protected string m_datasource;
protected IsolationLevel isolationLevel;
protected int m_lockRetryCount;
protected int m_lockTimeout;
protected string m_dataBaseName;
protected bool m_avoidDataTruncationError;
public GxDataRecord()
{
if (Config.GetValueOf("AvoidDataTruncationError", out string str))
m_avoidDataTruncationError = (str.ToLower() == "y");
}
public abstract IDataReader GetDataReader(IGxConnectionManager connManager,IGxConnection connection,
GxParameterCollection parameters, string stmt, ushort fetchSize, bool forFirst, int handle,
bool cached, SlidingTime expiration, bool hasNested, bool dynStmt);
#if !NETCORE
protected void GetConnectionDialogString(int dbmsType)
{
if (Dialogs.DialogFactory != null)
{
IConnectionDialog dlg = Dialogs.DialogFactory.GetConnectionDialog(dbmsType);
if (dlg.Show(" "))
{
m_connectionString = dlg.ConnectionString;
}
else
{
GXLogging.Error(log, "GxConnection.Open No se puedo obtener informacion de string de conexion");
throw (new GxADODataException("Cancel"));
}
}
}
#endif
public virtual IDataReader GetCacheDataReader(CacheItem item, bool computeSize, string keyCache)
{
return new GxCacheDataReader (item, computeSize, keyCache);
}
public virtual IsolationLevel IsolationLevelTrn
{
get{ return isolationLevel;}
set { isolationLevel=value;}
}
public abstract GxAbstractConnectionWrapper GetConnection(
bool showPrompt, string datasourceName, string userId,
string userPassword,string databaseName, string port, string schema, string extra,
GxConnectionCache connectionCache);
public abstract IDbDataParameter CreateParameter();
public abstract IDbDataParameter CreateParameter(string name, Object dbtype, int gxlength, int gxdec);
public string BuildConnectionStringImpl(string datasourceName, string userId,
string userPassword, string databaseName, string port, string schema, string extra)
{
return BuildConnectionString(datasourceName, userId, userPassword, databaseName, port, schema, extra);
}
protected abstract string BuildConnectionString(string datasourceName, string userId,
string userPassword,string databaseName, string port, string schema, string extra);
protected virtual string BuildConnectionStringForLog(string datasourceName, string userId,
string userPassword, string databaseName, string port, string schema, string extra)
{
return BuildConnectionString(datasourceName, userId, userPassword, databaseName, port, schema, extra);
}
public virtual object[] ExecuteStoredProcedure(IDbCommand cmd)
{
cmd.ExecuteNonQuery();
int count=cmd.Parameters!=null ? cmd.Parameters.Count:0;
if (count>0)
{
object[] values = new object[count];
for (int i=0; i<count; i++)
{
values[i]=((IDataParameter)cmd.Parameters[i]).Value;
}
return values;
}
else
{
return null;
}
}
public virtual bool AllowsDuplicateParameters
{
get{return true;}
}
public virtual bool MultiThreadSafe
{
get{ return true;}
}
public virtual void SetParameterDir(GxParameterCollection parameters, int num, ParameterDirection dir)
{
parameters[num].Direction=dir;
}
public virtual void SetParameter(IDbDataParameter parameter, Object value)
{
if (value==null || value==DBNull.Value)
{
parameter.Value = DBNull.Value;
}
#if !NETCORE
else if ( value.GetType().ToString().Equals(SQLGeographyWrapper.SqlGeographyClass) &&
parameter.GetType() == typeof(SqlParameter))
{
parameter.Value = value;
((SqlParameter)parameter).UdtTypeName = "Geography";
}
#endif
else if (!IsBlobType(parameter))
{
parameter.Value = CheckDataLength(value, parameter);
}
else
{
SetBinary(parameter, GetBinary((string)value, false));
}
}
protected object CheckDataLength(object value, IDbDataParameter parameter)
{
if (m_avoidDataTruncationError)
{
string svalue = value.ToString();
if (svalue != null && svalue.Length > parameter.Size)
{
return svalue.Substring(0, parameter.Size);
}
}
return value;
}
public virtual void SetParameterBlob(IDbDataParameter parameter, string value, bool dbBlob)
{
byte[] binary = GetBinary(value, dbBlob);
SetBinary(parameter, binary);
}
public virtual void SetBinary(IDbDataParameter parameter, byte[] binary) {
parameter.Value = binary;
if (binary != null)
parameter.Size = binary.Length;
}
public virtual object SetParameterValue(IDbDataParameter parameter, Object value)
{
if (value == null || value == DBNull.Value)
{
return DBNull.Value;
}
else if (!IsBlobType(parameter))
{
return value;
}
else
{
byte[] binary = GetBinary((string)value);
return binary;
}
}
public virtual void SetParameterChar(IDbDataParameter parameter, string value)
{
SetParameter(parameter, value);
}
public virtual void SetParameterLVChar(IDbDataParameter parameter, string value, IGxDataStore datastore)
{
SetParameter(parameter, value);
}
public virtual void SetParameterVChar(IDbDataParameter parameter, string value)
{
SetParameter(parameter, value);
}
public abstract string GetServerDateTimeStmt(IGxConnection connection);
public abstract string GetServerDateTimeStmtMs(IGxConnection connection);
public abstract string GetServerUserIdStmt();
public abstract string GetServerVersionStmt();
public virtual void SetTimeout(IGxConnectionManager connManager, IGxConnection connection, int handle)
{
}
public virtual string SetTimeoutSentence(long milliseconds){ return null;}
public virtual int LockTimeout
{
get{ return m_lockTimeout;}
set{ m_lockTimeout=value;}
}
public virtual int LockRetryCount
{
get{ return m_lockRetryCount;}
set{ m_lockRetryCount=value;}
}
public virtual string DataBaseName
{
get { return m_dataBaseName; }
set { m_dataBaseName = value; }
}
internal virtual void CalculateRetryCount(out int maxRetryCount, out int sleepTimeout)
{
maxRetryCount = m_lockRetryCount;
sleepTimeout = m_lockTimeout;
}
public bool Retry(GxErrorMask errMask, int retryCount)
{
GXLogging.Debug(log, "Lock Retry, retry:" + retryCount + ", maxRetryCount:" + m_lockRetryCount + ", lockTimeout: " + m_lockTimeout + ",GX_MASKLOOPLOCK:" + ((errMask & GxErrorMask.GX_MASKLOOPLOCK) > 0));
bool retry=false;
int maxRetryCount, sleepTimeout;
CalculateRetryCount( out maxRetryCount, out sleepTimeout);
retry = ((errMask & GxErrorMask.GX_MASKLOOPLOCK) > 0) && ((maxRetryCount==0) || retryCount < maxRetryCount);
if (retry)
{
Thread.Sleep(sleepTimeout);
}
return retry;
}
public virtual void CreateDataBase(string dbname, IGxConnection con)
{
}
public virtual byte GetByte( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return 0;
else
return DR.GetByte( i);
}
public virtual long GetBytes(IGxDbCommand cmd, IDataRecord DR, int i, long fieldOffset, byte[] buffer , int bufferOffset, int length )
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return 0;
else
return DR.GetBytes( i, fieldOffset, buffer , bufferOffset, length);
}
public virtual short GetShort( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return 0;
else
return DR.GetInt16( i);
}
public virtual int GetInt( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return 0;
else
return DR.GetInt32( i);
}
public virtual long GetLong( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return 0;
else
return DR.GetInt64( i);
}
public virtual double GetDouble( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return 0;
else
return DR.GetDouble( i);
}
public virtual string GetString( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return string.Empty;
else
return DR.GetString( i);
}
public virtual string GetBinary( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return string.Empty;
else
return DR.GetString( i).TrimEnd();
}
public virtual DateTime GetDateTimeMs(IGxDbCommand cmd, IDataRecord DR, int i)
{
if (!cmd.HasMoreRows || DR == null || DR.IsDBNull(i))
return DateTimeUtil.NullDate();
else
return Dbms2NetDateTime(DR.GetDateTime(i), true);
}
public virtual DateTime GetDateTime( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return DateTimeUtil.NullDate();
else
return Dbms2NetDateTime( DR.GetDateTime( i) , false);
}
public virtual DateTime GetDate( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return DateTimeUtil.NullDate();
else
return Dbms2NetDate(cmd, DR, i);
}
public virtual Boolean GetBoolean( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return false;
else
return DR.GetBoolean( i);
}
public virtual Guid GetGuid(IGxDbCommand cmd, IDataRecord DR, int i)
{
if (!cmd.HasMoreRows || DR == null || DR.IsDBNull(i))
return Guid.Empty;
else
return DR.GetGuid(i);
}
public virtual IGeographicNative GetGeospatial(IGxDbCommand cmd, IDataRecord DR, int i)
{
throw (new GxNotImplementedException());
}
public virtual decimal GetDecimal(IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return 0;
else
return DR.GetDecimal( i);
}
public virtual DateTime Dbms2NetDateTime( DateTime dt, Boolean precision)
{
return dt;
}
public virtual Object Net2DbmsDateTime(IDbDataParameter parm, DateTime dt)
{
return dt;
}
public virtual IGeographicNative Dbms2NetGeo(IGxDbCommand cmd, IDataRecord DR, int i)
{
throw (new GxNotImplementedException());
}
public virtual Object Net2DbmsGeo(GXType type, IGeographicNative geo)
{
throw (new GxNotImplementedException());
}
public virtual DateTime Dbms2NetDate(IGxDbCommand cmd, IDataRecord DR, int i)
{
return DR.GetDateTime(i);
}
public virtual String DbmsTToC( DateTime dt)
{
return dt.ToString( "yyyy-MM-dd HH:mm:ss");
}
public bool ProcessError( uint dbmsErrorCode, string emsg, GxErrorMask errMask, ref int status, ref bool retry, int retryCount)
{
return ProcessError((int)dbmsErrorCode, emsg, errMask, null, ref status, ref retry, 0);
}
public virtual bool ProcessError( int dbmsErrorCode, string emsg, GxErrorMask errMask, IGxConnection con, ref int status, ref bool retry, int retryCount)
{
return false;
}
public virtual Boolean IsDBNull( IGxDbCommand cmd, IDataRecord DR, int i)
{
if ( !cmd.HasMoreRows || DR == null || DR.IsDBNull( i))
return true;
else
return false;
}
public virtual string ToDbmsConstant(short Value)
{
return Value.ToString();
}
public virtual string ToDbmsConstant(int Value)
{
return Value.ToString();
}
public virtual string ToDbmsConstant(long Value)
{
return Value.ToString();
}
public virtual string ToDbmsConstant(decimal Value)
{
return Value.ToString(CultureInfo.InvariantCulture);
}
public virtual string ToDbmsConstant(float Value)
{
return Value.ToString(CultureInfo.InvariantCulture);
}
public virtual string ToDbmsConstant(double Value)
{
return Value.ToString(CultureInfo.InvariantCulture);
}
public virtual string ToDbmsConstant(string Value)
{
return "'" + Value.Replace("'","''") + "'";
}
public virtual string ToDbmsConstant(DateTime Value)
{
return "'" + Value.ToString("yyyy-MM-dd HH\\:mm\\:ss").Replace("'","''") + "'";
}
public virtual string ToDbmsConstant(Boolean Value)
{
return (Value)?"1":"0";
}
public virtual string FalseCondition()
{
return " 1=0 ";
}
internal virtual object CloneParameter(IDbDataParameter parameter)
{
return parameter;
}
public abstract DbDataAdapter CreateDataAdapeter();
public virtual bool SupportUpdateBatchSize
{
get { return true; }
}
public virtual void SetAdapterInsertCommand(DbDataAdapterElem da, IGxConnection con, string stmt, GxParameterCollection parameters)
{
da.Adapter.InsertCommand = (DbCommand) GetCommand(con, stmt, parameters);
da.Adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
}
public virtual DbDataAdapterElem GetDataAdapter(IGxConnection con, string stmt, int batchSize, string stmtId)
{
DbDataAdapterElem adapter = GetCachedDataAdapter(con, stmtId);
if (adapter == null)
{
DbDataAdapter dbadapter = CreateDataAdapeter();
if (SupportUpdateBatchSize)
{
dbadapter.UpdateBatchSize = batchSize;
}
DataTable dt = new DataTable();
adapter = new DbDataAdapterElem(dbadapter, dt, batchSize);
con.ConnectionCache.AddDataAdapter(stmtId, adapter);
}
return adapter;
}
public virtual DbDataAdapterElem GetDataAdapter(IGxConnection con, string stmt, GxParameterCollection parameters)
{
DbDataAdapterElem adapter = GetCachedDataAdapter(con, stmt);
if (adapter == null)
{
DbDataAdapter dbadapter = con.InternalConnection.CreateDataAdapter();
DataTable dt = new DataTable();
adapter = new DbDataAdapterElem(dbadapter, dt, 0);
con.ConnectionCache.AddDataAdapter(stmt, adapter);
}
return adapter;
}
public virtual DbDataAdapterElem GetCachedDataAdapter(IGxConnection con, string stmt)
{
return con.ConnectionCache.GetDataAdapter(stmt);
}
public virtual int BatchUpdate(DbDataAdapterElem da)
{
return da.Adapter.Update(da.DataTable);
}
public virtual bool IsBlobType(IDbDataParameter idbparameter)
{
return idbparameter.DbType == DbType.Binary;
}
public virtual void AddParameters(IDbCommand cmd, GxParameterCollection parameters)
{
for (int j=0; j< parameters.Count; j++)
{
cmd.Parameters.Add(CloneParameter(parameters[j]));
}
}
public virtual IDbCommand GetCommand(IGxConnection con, string stmt, GxParameterCollection parameters, bool isCursor, bool forFirst, bool isRpc)
{
return GetCommand(con, stmt, parameters);
}
public virtual void PrepareCommand(IDbCommand cmd)
{
}
public virtual int GetCommandTimeout()
{
return 0;
}
public virtual IDbCommand GetCommand(IGxConnection con, string stmt, GxParameterCollection parameters)
{
IDbCommand cmd = GetCachedCommand(con, stmt);
if (cmd==null)
{
cmd = con.InternalConnection.CreateCommand();
cmd.CommandText=stmt;
cmd.Connection=con.InternalConnection.InternalConnection;
cmd.CommandTimeout = GetCommandTimeout();
AddParameters(cmd, parameters);
cmd.Transaction=con.BeginTransaction();
PrepareCommand(cmd);
con.ConnectionCache.AddPreparedCommand(stmt, cmd);
}
else
{
if (parameters.Count==cmd.Parameters.Count)
{
if (parameters.Count>0)
{
for (int j = 0; j< parameters.Count; j++)
{
IDbDataParameter idbparameter = (IDbDataParameter)cmd.Parameters[j];
object value = parameters[j].Value;
idbparameter.Value=value;
if (value!=null && IsBlobType(idbparameter))
{
try
{
idbparameter.Size = ((byte[])idbparameter.Value).Length;
}
catch (Exception ex)
{
GXLogging.Error(log, "Set Binary parameter length in cached command error", ex.Message, ex);
}
}
}
}
}
else
{
foreach (object oldParam in cmd.Parameters)
{
if (oldParam is IDisposable disposable) disposable.Dispose();
}
cmd.Parameters.Clear();
AddParameters(cmd, parameters);
}
cmd.Connection=con.InternalConnection.InternalConnection;
cmd.Transaction=con.BeginTransaction();
}
return cmd;
}
public virtual void DisposeCommand(IDbCommand command)
{
command.Dispose();
}
public virtual IDbCommand GetCachedCommand(IGxConnection con, string stmt)
{
return con.ConnectionCache.GetPreparedCommand(stmt);
}
public virtual string ConnectionString
{
get{ return m_connectionString;}
set{ m_connectionString=value;}
}
protected static byte[] GetBinary(string fileName)
{
return GetBinary(fileName, false);
}
protected static byte[] GetBinary(string fileNameParm, bool dbBlob)
{
Uri uri;
string fileName = fileNameParm;
bool inLocalStorage = dbBlob || ServiceFactory.GetExternalProvider() == null;
bool validFileName = !String.IsNullOrEmpty(fileName) && !String.IsNullOrEmpty(fileName.Trim()) && String.Compare(fileName, "about:blank", false) != 0;
byte[] binary = Array.Empty<byte>();
if (inLocalStorage && validFileName)
{
if (GxUploadHelper.IsUpload(fileName))
fileName = GxUploadHelper.UploadPath(fileName);
bool ok = PathUtil.AbsoluteUri(fileName, out uri);
if (ok && uri != null)
{
switch (uri.Scheme)
{
case "http":
case "https":
HttpStatusCode statusCode;
binary = HttpHelper.DownloadFile(uri.AbsoluteUri, out statusCode);
if (statusCode != HttpStatusCode.OK)
{
if (statusCode == HttpStatusCode.NotFound) //Error 404 Not found.
{
GXLogging.Error(log, "GxCommand. The filename does not exists in url: " + uri.AbsoluteUri);
throw new GxADODataException("GxCommand. The filename does not exists in url: " + uri.AbsoluteUri);
}
else
{
GXLogging.Error(log, "GxCommand. An error occurred while downloading data from url " + uri.AbsoluteUri);
throw new GxADODataException("GxCommand. An error occurred while downloading data from url " + uri.AbsoluteUri);
}
}
break;
case "file":
try
{
#pragma warning disable SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}'
using (FileStream fs = new FileStream(uri.LocalPath, FileMode.Open, FileAccess.Read))
#pragma warning restore SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}'
{
using (BinaryReader br = new BinaryReader(fs))
{
binary = br.ReadBytes((int)fs.Length);
}
}
}
catch (Exception e)
{
if (ServiceFactory.GetExternalProvider() != null)
{
GxFile file = new GxFile(string.Empty, fileNameParm, GxFileType.PrivateAttribute);
if (file.Exists())
{
binary = file.ToByteArray();
return binary;
}
}
GXLogging.Debug(log, "GxCommand. An error occurred while getting data from file path ", uri.AbsolutePath, e);
throw e;
}
break;
default:
GXLogging.Error(log, "Schema not supported: ", fileName);
break;
}
GXLogging.Debug(log, "GetBinary fileName ", uri.AbsolutePath, ",ReadBytes:", binary != null ? binary.Length.ToString() : "0");
}
else
{
GXLogging.Error(log, "Not a valid URI: ", fileName);
throw new GxADODataException("GxCommand. Not a valid uri: " + fileName);
}
return binary;
}
else
{
return Array.Empty<byte>();
}
}
public virtual string DataSource
{
get {return m_datasource;}
set{m_datasource=value;}
}
public virtual int MaxNumberOfValuesInList => int.MaxValue;
protected const string NaV = "xxxxx";
public string ConnectionStringForLog()
{
string result="";
if (m_connectionString!=null)
{
int i1 = m_connectionString.IndexOf("Password");
int i=0;
if (i1>0) i=i1;
int j = m_connectionString.IndexOf( ";", i)!=-1 ? m_connectionString.IndexOf( ";", i): m_connectionString.Length;
result = m_connectionString.Substring(0,i);
result += (i1>=0 ? "Password=xxxxx" : "") + m_connectionString.Substring(j);
}
return result;
}
public virtual bool HasKey(string data, string key)
{
if (!string.IsNullOrEmpty(data) && data.IndexOf(key, StringComparison.OrdinalIgnoreCase) >= 0)
{
string[] props = data.Split(';');
foreach (string s in props)
{
string[] parts = s.Split('=');
if (parts[0].Equals(key, StringComparison.OrdinalIgnoreCase))
return true;
}
}
return false;
}
public virtual string ParseAdditionalData(string data, string extractWord)
{
char[] sep = {';'};
StringBuilder res=new StringBuilder("");
string [] props = data.Split(sep);
foreach (string s in props)
{
if ( s!=null && s.Length>0 && !s.ToLower().StartsWith(extractWord))