-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminiscriptcontroller.pas
3340 lines (3040 loc) · 98.3 KB
/
miniscriptcontroller.pas
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
//////////////////////////////////////////////////////////////////////////////////
/// ///
/// miniframe开源Web框架技术群:821855479 如果加不了群,请联系QQ:3123827806 ///
/// 网页制作器网址:https://wyeditor.com ///
/// 源码及demo下载:https://wyeditor.com/miniframe/ ///
/// ///
//////////////////////////////////////////////////////////////////////////////////
unit miniscriptcontroller;
//{$mode objfpc}{$H+}
{$IFDEF FPC}{$MODE DELPHI}{$H+}{$ENDIF}
{.$define nonecachescript} {nonecachescript 不允许缓存脚本}
{$define nonedmmaxcount} {nonedmmaxcount 不控制对象池总数}
interface
uses
Classes, SysUtils, IdGlobal, Controls, DataPackageUnit,
minidb, miniPub, db, MyDataTypeMapUni,
Uni, UniScript, Generics.Collections, uPSRuntime,
uPSComponent_DB,
uPSCompiler, uPSC_std, uPSC_classes,
uPSC_Controls, uPSComponent, IdGlobalProtocols, DateUtils,
uPSC_graphics, uPSR_graphics,
uPSR_std,
//这种方法fpc不能用,另外注册 uPSR_dateutils, uPSC_dateutils,
uPSR_controls,
uPSR_stdctrls,
uPSR_classes,
///uPSC_forms, //服务端不需要
///uPSR_forms,
uPSR_DB,
uPSC_DB, uPSUtils,
minihttp, miniregister,
IdHttp, {$IFDEF FPC}FileUtil,Process,{$else}{$endif}PubPWDUnit{,
uPSDisassembly}, {$ifdef MSWINDOWS}ShellApi, Windows,{$endif}MSDataTypeMapUni,
SQLServerUniProvider, minicomponents;
type
TminiPooler = class;
TMiniPSScript = class;
TPubbase = class;
TDMInfo = class
HadUsed: boolean;
Start: TDateTime;
PSScript1: TMiniPSScript;
LastHtml: string;
TmpSl: TStringList;
Request: TminiHTTPRequest;
Response: TminiHTTPResponse;
Pub: TPubbase;
end;
{ TMiniPSScript }
TMiniPSScript = class(TPSScriptDebugger) //class(TPSScript)
DMInfo: TDMInfo;
private
FMyIsComiled: boolean;
procedure SetMyIsComiled(AValue: boolean);
public
constructor Create(AOwner: TComponent); override;
property MyIsComiled: boolean read FMyIsComiled write SetMyIsComiled;
end;
TLogInfo = record
FRunLog_File, FServiceLog_File: string;
FIsLog: boolean;
FTF, FTF_Run: TextFile;
FTF_IsIni, FTF_IsIni_Run: boolean;
MaxCount, AllCount, AllCount_Run: integer;
end;
TSSLFileInfo = record
SSL_Domain: TStringlist;
SSL_Key: TStringlist;
SSL_Cer: TStringlist;
//SSL_Obj: TList<TSslContext>;
end;
TScriptInfo = class
FileName: string;
MS: tbtstring;
Used: boolean;
Start: TDateTime;
noparent: string; //2022-07-12 add
AllFileNameList: TStringlist;
AllFileTimeList: TList<TDatetime>;
end;
{ TPubbase }
TPubbase = class
private
FAZTCode: string;
FConnPooler: TminiPooler;
FZTCode: string;
procedure SetZTCode(AValue: string);
protected
FList, FPoolerList: TList;
public
constructor Create;
destructor Destroy; override;
procedure Last;
function TestConnect(dbhost, DbType, DBName, User, Pwd: string): string;
class procedure SetConnParam(Conn: TminiConnection; dbhost, DbType, DBName, User, Pwd: string); static;
//这样获取连接池实例,将不受是否停用影响
function GetConnPooler(AZTCode: string): TminiPooler;
//这样获取连接池实例,将受是否停用影响
function GetConnPoolerEx(AZTCode: string; var ErrStr: string): TminiPooler;
//数据库操作 start
//连接到数据源
function DB_C(AZTCode: string; var ErrStr: string): boolean;
//执行一条SQL
function DB_E(SQLText: string; var ErrStr: string; AZTCode: string=''): boolean;
//执行一条SQL,打开数据集,数据返回到Query中
function DB_O(SQLText: string; var ErrStr: string; AZTCode: string=''): boolean;
function O(ZTCode: string; var PooledInfo: TminiPooler; var Query: TminiQuery; var ErrStr: string): boolean;
//执行一条SQL,打开数据集,数据返回到MemTable中
function DB_OMT(SQLText: string; var ErrStr: string; Mt1: TminiMemTable; AZTCode: string=''): boolean;
//执行一条SQL,打开数据集,数据返回到THJHMemoryDataSet中
function DB_ODS(SQLText: string; var ErrStr: string; ds: THjhMemoryDataSet; AZTCode: string=''): boolean;
//执行一条SQL,把SQL语句返回的数据集生成到临时或固定表中,考虑了兼容不同的数据库问题
function DB_ETEx(TableName, SQLText: string; IsTmpTBL: boolean; var ErrStr: string; Query1: TminiQuery): boolean;
function DB_OT(SQLText, TableName: string; IsTmpTBL: boolean; var ErrStr: string; AZTCode: string=''): boolean;
//数据库操作 end;
property ZTCode: string read FZTCode write SetZTCode; //数据源
function TmpSl: TStringList;
function Conn: TminiConnection;
function Query: TminiQuery;
function mds: THjhMemoryDataSet;
function DBName: string;
function DBType: string;
function Pooler: TminiPooler;
procedure SetStartTime;
//以下这些创建的对象,脚本中调用后不需要使用者释放,系统自动释放
function CreateConn: TminiConnection;
function CreateQuery: TminiQuery;
function CreateStringlist: TStringlist;
function CreateMemTable: TminiMemTable;
function CreateMemoryDataSet: THjhMemoryDataSet;
function CreateJson: TminiJson;
function CreateMemoryStream: TMemoryStream;
function CreateStoredProc: TminiStoredProc;
function CreateFileStream(FileName: string; Mode: Integer; Var ErrStr: String): TFileStream;
published
end;
{ TConnPooledInfo }
{ TminiPooler }
TminiPooler = class
AZTCode, FDBName, FDBType: string;
Start: TDateTime;
aConnection1: TminiConnection;
aQuery1: TminiQuery;
aTmpSl: TStringList;
aHJHMemodataset: THjhMemoryDataSet;
private
FHadUsed: boolean;
Fztstop: boolean;
procedure SetHadUsed(AValue: boolean);
procedure Setztstop(const Value: boolean);
public
constructor Create();
function GetTminiConnection: TminiConnection;
function Conn: TminiConnection;
function Query: TminiQuery;
//function MemTable: TminiMemTable;
function TmpSl: TStringList;
function mds: THjhMemoryDataSet;
function DBName: string;
function DBType: string;
published
property HadUsed: boolean read FHadUsed write SetHadUsed;
property ztstop: boolean read Fztstop write Setztstop;
//property FDConnection1: TFDConnection read GetTminiConnection;
end;
TConnPooledInfo = class(TminiPooler)
end;
{ TRunner }
TRunner = class
private
public
class procedure PPSScriptExecute(Sender: TPSScript);
class procedure PPSScript1ExecImport(Sender: TObject; se: TPSExec;
x: TPSRuntimeClassImporter);
class procedure PPSScript1CompImport(Sender: TObject; x: TPSPascalCompiler);
class procedure RunOnePspFile(DMInfo: TDMInfo; Session: TObject; Service_Path, FileName: string; Port: integer;
Request: TminiHTTPRequest; Response: TminiHTTPResponse);
class procedure PSScript1Compile(Sender: TPSScript);
class procedure GoToErrPage(DMInfo: TDMInfo; Session: TObject; Service_Path, FileName: string;
Port: integer; Request: TminiHTTPRequest; Response: TminiHTTPResponse; addstr: string = '');
class procedure LoadYSFile(Port: integer; //Host, RemoteIP, RemotePort, Document, RawHTTPCommand: string;
YsHost, FileName: string; ARequestInfo: TminiHTTPRequest; Response: TminiHTTPResponse);
class procedure LoadOtherFile(OldLDoc, FileName: string; Port: integer; Request: TminiHTTPRequest; Response: TminiHTTPResponse);
end;
TServerInfo = record
serviceext, YsHost, clientcachefile, hosttodir, oldhosttodir, ApachePath, mainservicepath,
errorpage, autopcandmobileext: string;
ProgramPath: string;
crossdomain, NeedStartSession, AutoZip, autopcandmobile, servernocachefile: boolean;
Poolermaxcount, poolermincount, poolertimeout, threadcount, quequelength: integer;
DirSL, hosttodirSL, CacheName, exttoserver, CacheText: TStringList;
sessiontimeout, maxsessioncount: integer;
httpport, httpsport: string;
starthttps: boolean;
rootcert, cert, secretkey, secretkeypwd: string;
openby127_0_0_1: boolean; //2023-05-22 add
Starttime: TDateTime;
blacklist, whitelist: TStringlist;
Usedblacklist, Usedwhitelist: TStringlist;
end;
TGarbageCollector = class(TThread)
private
procedure RefInfo;
protected
procedure Execute; override;
public
constructor Create();
destructor Destroy; override;
end;
//写日志start
//访问日志,开启记录选项才记录
procedure WriteServiceLogToFile(Port: integer; Request: TminiHTTPRequest; ClientIP: string = ''; Document: string = ''; UserAgent: string = ''; Host: string = '');
//运行日志, 必须记录(如意外出错情况,服务器运行信息)
procedure WriteRunLogToFile(Text: string);
//内部用,定时线程日志
procedure WriteInnerLog(Text: string);
//写日志end
function FindNoUseConn(AZTCode: string; ConnList: Classes.Tlist): integer;
function FindNoUseDm(DMList: Classes.Tlist): integer;
function GetService_Path(Host, Document: string; var IsSetting: boolean): string;
function GetYs_From_EXT(Ext: string; var Host: string): boolean;
function GetFileContentType(FileName: string): string;
function MyRedirect(Headers, OldLDoc: string): string;
function GetOthInfo(Port: integer; Request: TminiHTTPRequest): string;
function GetDMInfo(Request: TObject): TDMInfo;
var
FLogInfo: TLogInfo;
ServerInfo: TServerInfo;
DMList, ScriptList, ConnList: Classes.TList;
implementation
uses mormothttps, PubFileUnit, SelfDefine;
var
GarbageCollector: TGarbageCollector;
QueList: TList<TObject>;
FIsFreeAllObj: boolean = false;
MIMEMap: TIdMIMETable;
CriticalSection, CriticalSection_Log, CriticalSection_RunLog, CriticalSection_Conn, CriticalSection_Script, CriticalSection_Sqe: TMiniCriticalSection;
function MyRedirect(Headers, OldLDoc: string): string;
var
SS: TStringStream;
cache, Contentext: string;
begin
//MeCriticalSection2.Enter; //2022-04-08 add
try
cache :=
'Location: ' + OldLDoc + #13#10 +
'Date: ' + LocalDateTimeToHttpStr(Now) + #13#10 +
'Expires: '+ LocalDateTimeToHttpStr(IncDay(Now, -30));
//Headers := 'Location: "http://huo"';
Headers := cache + #13#10 + Headers;
//Headers := Headers + #13#10'Location: ' + OldLDoc + #13#10;
//2022-11-21 Client.AnswerString(Flags, '302', '', Headers, Contentext);
Result := Headers;
finally
//MeCriticalSection2.Leave; //2022-04-08 add
end;
end;
function GetFileContentType(FileName: string): string;
var
Ext: string;
begin
Ext := LowerCase(ExtractFileExt(FileName));
if Ext = '.css' then
Result := 'text/css'
else
if Ext = '.js' then
Result := 'text/javascript'
else
if Ext = '.jpg' then
Result := 'image/jpeg'
else
if Ext = '.jpeg' then
Result := 'image/jpeg'
else
if Ext = '.png' then
Result := 'image/x-png'
else
if Ext = '.gif' then
Result := 'image/gif'
else
if Ext = '.svg' then
Result := 'image/svg+xml'
else
if Ext = '.rar' then
Result := 'application/rar'
else
if Ext = '.dll' then
Result := 'application/dll'
else
if Ext = '.exe' then
Result := 'application/x-msdos-program'
else
if Ext = '.pdf' then
Result := 'application/pdf'
else
if Ext = '.apk' then
Result := 'application/vnd.android'
else
if (Ext = '.doc') or (Ext = '.dot') or (Ext = '.docx') then
Result := 'application/msword'
else
if (Ext = '.xls') or (Ext = '.xlsx') then
Result := 'application/x-msexcel'
else
if Ext = '.ico' then
Result := 'image/x-icon'
else
if Ext = '.bmp' then
Result := 'image/bmp'
else
if Ext = '.txt' then
Result := 'text/plain'
else
if Ext = '.mp2' then
Result := 'video/mpeg'
else
if Ext = '.mp3' then
Result := 'video/mpeg'
else
if Ext = '.mp4' then
Result := 'video/mpeg'
else
if Ext = '.mpeg' then
Result := 'video/x-mpeg2a'
else
if Ext = '.ico' then
Result := 'image/x-icon'
else
if Ext = '.7z' then
Result := 'application/x-7z-compressed'
else
Result := MIMEMap.GetFileMIMEType(FileName);
end;
function GetService_Path(Host, Document: string; var IsSetting: boolean): string;
var
index: integer;
begin
if (Pos('/$/', Document) = 1) or SameText('/$', Document) then
begin
Result := ServerInfo.ProgramPath;
IsSetting := true;
end else
begin
IsSetting := false;
//if Request.Session <> nil then
// Result := Request.Session.Values['ServicePath'];
if trim(Result) = '' then
begin //host: '127.0.0.1:899'
Host := LowerCase(GetDeliPri(Host, ':'));
Host := GetDeliBack(Host, 'www.');
Index := ServerInfo.hosttodirSL.IndexOf(Host);
if Index > -1 then
Result := ServerInfo.DirSL[Index]
else
Result := ServerInfo.mainservicepath;
Result := PathWithSlash(Result);
//if Request.Session <> nil then
//Request.Session.Values['ServicePath'] := Result;
end;
end;
end;
function FindNoUseConn(AZTCode: string; ConnList: Classes.Tlist): integer;
var
lp: integer;
begin
//Result := Connlist.IndexOf(Key); HadUsed
Result := -1;
for lp := 0 to ConnList.Count - 1 do
begin
if (not TminiPooler(ConnList[lp]).HadUsed) and (LowerCase(TminiPooler(ConnList[lp]).AZTCode) = LowerCase(AZTCode)) then
begin
Result := lp;
break;
end;
end;
end;
function FindNoUseDm(DMList: Classes.Tlist): integer;
var
lp: integer;
begin
Result := -1;
for lp := 0 to DMList.Count - 1 do
if (not TDMInfo(DMList[lp]).HadUsed) then
begin
Result := lp;
break;
end;
end;
{ TminiPooler }
procedure TminiPooler.SetHadUsed(AValue: boolean);
begin
if FHadUsed=AValue then Exit;
FHadUsed:=AValue;
end;
procedure TminiPooler.Setztstop(const Value: boolean);
begin
Fztstop := Value;
end;
constructor TminiPooler.Create;
begin
aHJHMemodataset := nil;
end;
function TminiPooler.DBName: string;
begin
Result := FDBName;
end;
function TminiPooler.DBType: string;
begin
Result := FDBType;
end;
function TminiPooler.GetTminiConnection: TminiConnection;
begin
Result := Conn;
end;
function TminiPooler.mds: THjhMemoryDataSet;
begin
if aHJHMemodataset = nil then
aHJHMemodataset := THjhMemoryDataSet.Create(nil);
Result := aHJHMemodataset;
end;
function TminiPooler.Conn: TminiConnection;
begin
Result := aConnection1;
end;
function TminiPooler.Query: TminiQuery;
begin
Result := aQuery1;
end;
function TminiPooler.TmpSl: TStringList;
begin
Result := aTmpSl;
end;
{ TMiniPSScript }
procedure TMiniPSScript.SetMyIsComiled(AValue: boolean);
begin
if FMyIsComiled=AValue then Exit;
FMyIsComiled:=AValue;
end;
constructor TMiniPSScript.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMyIsComiled := false;
end;
{ TPubbase }
procedure TPubbase.SetZTCode(AValue: string);
begin
if FZTCode=AValue then Exit;
FZTCode:=AValue;
end;
procedure TPubbase.SetStartTime;
begin
if FConnPooler <> nil then
FConnPooler.Start := Now;
end;
constructor TPubbase.Create;
begin
FConnPooler := nil;
FList := TList.Create;
FList.Clear;
FPoolerList := TList.Create;
end;
procedure TPubbase.Last;
var
lp: integer;
begin
try
if FConnPooler <> nil then
FConnPooler.HadUsed := false;
except
end;
for lp := 0 to FPoolerList.Count - 1 do
try
TminiPooler(FPoolerList.Items[lp]).HadUsed := false;
except
end;
for lp := 0 to FList.Count - 1 do
begin
try
if Assigned(FList.Items[lp]) and (TObject(FList.Items[lp]) is TObject) then
TObject(FList.Items[lp]).Free;
except
end;
end;
FList.Clear;
end;
destructor TPubbase.Destroy;
begin
Last;
FList.Free;
FPoolerList.Free;
inherited;
end;
function TPubbase.GetConnPoolerEx(AZTCode: string; var ErrStr: string): TminiPooler;
begin
ErrStr := '';
if trim(AZTCode) = '' then AZTCode := FAZTCode;
if AZTCode = '' then
begin
ErrStr := '数据源名称为空,不能继续(67999123)!';
exit;
end;
try
if (FConnPooler <> nil) and (AZTCode = FConnPooler.AZTCode) and (FConnPooler.HadUsed) then
begin
FConnPooler.Start := Now;
Result := FConnPooler;
exit;
end;
except
end;
try
if (FConnPooler <> nil) then
FConnPooler.HadUsed := false;
Result := GetConnPooler(AZTCode);
FConnPooler := Result;
FAZTCode := AZTCode;
except
on e: exception do
begin
ErrStr := e.Message;
exit;
end;
end;
if FConnPooler.ztstop then
begin
ErrStr := '该账套已被停用,不能访问!';
exit;
end;
end;
function TPubbase.CreateMemoryStream: TMemoryStream;
begin
Result := TMemoryStream.Create;
FList.Add(Result);
end;
class procedure TPubbase.SetConnParam(Conn: TminiConnection; dbhost, DbType, DBName, User, Pwd: string);
var
TmpStr, Port: string;
begin
Port := '';
//dbhost := ConnInfo.TmpSl[2];
if Pos(':', dbhost) > 0 then
begin
Port := GetDeliBack(dbhost, ':');
dbhost := GetDeliPri(dbhost, ':');
end;
if Pos(',', dbhost) > 0 then
begin
Port := GetDeliBack(dbhost, ':');
dbhost := GetDeliPri(dbhost, ':');
end;
if SameText(DbType, 'MySQL') then
begin
Conn.DbType := 'mysql';
Conn.DataTypeMap.AddDBTypeRule(myDecimal, ftFMTBCD);
if trim(Port) = '' then Port := '3306';
TmpStr := 'Provider Name=MySQL;Data Source=' +
dbhost + ';database=' + DBName +
';User ID=' + User + ';Password=' +
pwd + ';Login Prompt=False' + ';port=' + Port + ';CharacterSet=UTF8';
Conn.ConnectString := TmpStr;
Conn.SpecificOptions.Clear;
Conn.SpecificOptions.Add('SQL Server.Provider=prDirect'); //解决乱码
end else
if SameText(DbType, 'SQLite') then
begin
Conn.DbType := 'sqlite';
Conn.ConnectString := 'Provider Name=SQLite;Database=' + DBName;
end else
if SameText(DbType, 'Oracle') then
begin
Conn.DbType := 'oracle';
Conn.ConnectString := 'Provider Name=Oracle;Data Source=' + dbhost + ';User ID=' + user + ';Password=' + Pwd;
//Conn.DataTypeMap.AddDBTypeRule(oraVarchar2, ftString);
//Conn.DataTypeMap.AddDBTypeRule(oraNVarchar2, ftWideString);
//Conn.SpecificOptions.Values['Unicode'] := 'True';
end else
if SameText(DbType, 'ODBC') then
begin
Conn.DbType := DbType;
Conn.ConnectString := 'Provider Name=ODBC;Server=' + dbhost + ';User ID=' + User + ';Password=' + Pwd;
end else
if SameText(DbType, 'MSSQL_OS') then
begin
Conn.BeforeConnect := Conn.ConnBeforeConnect;
Conn.AfterConnect := Conn.COnnConnAfterDisconnect;
Conn.DbType := 'mssql';
Conn.DataTypeMap.AddDBTypeRule(msDecimal, ftFMTBCD);
Conn.ConnectString :=
'Provider Name=SQL Server;Data Source=' +
dbhost + ';Initial Catalog=' + DBName +
';Authentication=Windows;Login Prompt=False'
//2023-07-03
+ ';CharacterSet=UTF8';
;
Conn.SpecificOptions.Clear;
Conn.SpecificOptions.Add('SQL Server.Provider=prDirect'); //解决乱码
end else
begin
Conn.DbType := DbType;
Conn.DataTypeMap.AddDBTypeRule(msDecimal, ftFMTBCD);
Conn.ConnectString := 'Provider Name=SQL Server;Data Source=' +
dbhost + ';Initial Catalog=' + DBName +
';User ID=' + User + ';Password=' +
pwd + ';Login Prompt=False';
Conn.SpecificOptions.Clear;
Conn.SpecificOptions.Add('SQL Server.Provider=prDirect'); //解决乱码
end;
end;
function TPubbase.CreateStoredProc: TminiStoredProc;
begin
Result := TminiStoredProc.Create(nil);
Result.Connection := TUniConnection(TminiConnection);
FList.Add(Result);
end;
function TPubbase.CreateFileStream(FileName: string; Mode: Integer;
var ErrStr: String): TFileStream;
begin
try
ErrStr := '';
Result := TFileStream.Create(FileName, Mode);
FList.Add(Result);
except
on e: Exception do
ErrStr := e.Message;
end;
end;
function TPubbase.GetConnPooler(AZTCode: string): TminiPooler;
var
Index, lp: integer;
ConnInfo: TminiPooler;
Flag: boolean;
TmpStr, dbhost, DbType, DBName, User, Pwd: string;
mds: THjhMemoryDataSet;
procedure CreatePooler;
begin
ConnInfo := TminiPooler.Create;
ConnInfo.aTmpSl := TStringList.Create;
ConnInfo.AZTCode := AZTCode;
ConnInfo.HadUsed := true;
ConnInfo.Start := Now;
ConnList.Add(TObject(ConnInfo));
end;
procedure CreateObj;
begin
ConnInfo.aConnection1 := TminiConnection.Create(nil);
ConnInfo.aQuery1 := TminiQuery.Create(nil);
ConnInfo.aQuery1.Connection := ConnInfo.aConnection1;
ConnInfo.aHJHMemodataset := nil;
end;
procedure nilObj;
begin
ConnInfo.aConnection1 := nil;
ConnInfo.aQuery1 := nil;
end;
begin
ConnInfo := nil;
CriticalSection_Conn.Enter;
try
Index := FindNoUseConn(AZTCode, ConnList);
if Index > -1 then
begin
ConnInfo := TminiPooler(ConnList[Index]);
ConnInfo.HadUsed := true;
ConnInfo.Start := Now;
end else
if SameText('confmx', AZTCode) or SameText('conf', AZTCode) or SameText('syslogin', AZTCode) then //内置账套
begin
CreatePooler;
nilObj;
ConnInfo.FDBName := ExtractFilePath(ParamStr(0)) + 'setting\' + AZTCode + '.json';
ConnInfo.FDBType := 'memorytable';
end else
begin
TmpStr := ExtractFilePath(ParamStr(0)) + 'setting\confmx.json';
if FileExists(TmpStr) then
begin
CreatePooler;
mds := THjhMemoryDataSet.Create(nil);
try
DBMemory_LoadJson(mds, TmpStr);
Decrypt(mds, 'dbpwd=ftString@0#数据库密码@1#@2#0@3#0@4#1@5#@6#0@7#0@8#0@9#1@10#');
Flag := false;
mds.First;
while not mds.Eof do
begin
if SameText(mds.V('ztcode'), Trim(AZTCode)) then
begin
Flag := true;
break;
end;
mds.Next;
end;
if Flag then
begin
DbType := mds.V('dbclass');
if (trim(DbType) = '') or (trim(DbType) = '自动') then
DbType := mds.V('drivername');
DBName := mds.V('dbname');
ConnInfo.ztstop := mds.V('ztstop') = 'on';//2023-05-19 add
ConnInfo.FDBName := DBName;
ConnInfo.FDBType := DbType;
if DbType = 'memorytable' then
begin
nilObj;
end else
begin
CreateObj;
dbhost := mds.V('dbhost');
User := mds.V('dbuser');
Pwd := mds.V('dbpwd');
SetConnParam(ConnInfo.Conn, dbhost, ConnInfo.FDBType, DBName,
User, Pwd);
end;
end else //创建是为了不报nil错误
CreateObj;
finally
mds.Free;
end;
end;
end;
Result := ConnInfo;
FPoolerList.Add(Result);
finally
CriticalSection_Conn.Leave;
end;
end;
function TPubbase.CreateMemoryDataSet: THjhMemoryDataSet;
begin
Result := THjhMemoryDataSet.Create(nil);
FList.Add(Result);
end;
function TPubbase.CreateJson: TminiJson;
begin
Result := TminiJson.Create;
FList.Add(Result);
end;
function TPubbase.mds: THjhMemoryDataSet;
begin
if FConnPooler = nil then
Result := nil
else
Result := FConnPooler.mds;
end;
function TPubbase.DBName: string;
begin
if FConnPooler = nil then
Result := ''
else
Result := FConnPooler.DBName;
end;
function TPubbase.DBType: string;
begin
if FConnPooler = nil then
Result := ''
else
Result := FConnPooler.DBType;
end;
function TPubbase.DB_C(AZTCode: string; var ErrStr: string): boolean;
begin
Result := false;
FConnPooler := GetConnPoolerEx(AZTCode, ErrStr);
if trim(ErrStr) <> '' then exit;
if FConnPooler = nil then
begin
ErrStr := '没能从连接池中取到实例(Pool=nil)!';
exit;
end;
//连接数据库 start
try
if not Conn.Connected then
Conn.Connected := true;
except
on e: exception do
begin
FConnPooler.HadUsed := false;
ErrStr := e.Message + '-2>' + AZTCode {+ ' ->DriverName:' + FDConnection1.DriverName + ' DriverID:' + FDConnection1.Params.DriverID +
' ConnectionString:' + FDConnection1.ConnectionString};
exit;
end;
end;
//连接数据库 end;
Result := true;
end;
function TPubbase.DB_E(SQLText: string; var ErrStr: string; AZTCode: string
): boolean;
begin
Result := false;
try
//连接数据库 start
if not DB_C(AZTCode, ErrStr) then exit;
//连接数据库 end;
//开始执行
if not Query.ExecSQL(SQLText, ErrStr) then exit;
finally
//FConnPooler.HadUsed := false; //注意: 一定要有这行,把实例还回连接池, 现在不需要
end;
Result := true;
end;
function TPubbase.DB_O(SQLText: string; var ErrStr: string; AZTCode: string
): boolean;
begin
Result := false;
try
//连接数据库 start
if not DB_C(AZTCode, ErrStr) then exit;
//连接数据库 end;
//开始执行
if not Query.Open(SQLText, ErrStr) then exit;
finally
//FConnPooler.HadUsed := false; //注意: 一定要有这行,把实例还回连接池, 现在不需要
end;
Result := true;
end;
function TPubbase.O(ZTCode: string; var PooledInfo: TminiPooler;
var Query: TminiQuery; var ErrStr: string): boolean;
var
aHJHMemodataset: THjhMemoryDataSet;
begin
Result := false;
PooledInfo := GetConnPoolerEx(ZTCode, ErrStr);
if trim(ErrStr) <> '' then exit;
if PooledInfo = nil then
begin
ErrStr := '没能从连接池中取到实例(Pool=nil)!';
exit;
end;
if PooledInfo.DBType = 'memorytable' then
begin
aHJHMemodataset := PooledInfo.mds;
{try
if FileExists(PooledInfo.DBName) then
aHJHMemodataset.LoadFromFile(PooledInfo.DBName);
except
on e: exception do
begin
PooledInfo.HadUsed := false;
ErrStr := e.Message + '-1>' + 'ZTCode ->DBType:' + PooledInfo.DbType + ' DBName: ' + PooledInfo.DBName;//' DriverID:' + PooledInfo.FDConnection1.Params.DriverID +
exit;
end;
end;//}
end else
begin
Query := PooledInfo.Query;
Query.Connection := PooledInfo.Conn;
//连接数据库 start
try
if not PooledInfo.Conn.Connected then
PooledInfo.Conn.Connected := true;
except
on e: exception do
begin
PooledInfo.HadUsed := false;
{$ifdef debug}
ErrStr := e.Message + '-1>' + 'ZTCode ->DriverName:' + PooledInfo.Conn.DriverName + //' DriverID:' + PooledInfo.FDConnection1.Params.DriverID +
' ConnectionString:' + PooledInfo.Conn.ConnectString;
{$else}
ErrStr := e.Message + '-1>' + 'ZTCode ->DriverName:' + PooledInfo.Conn.DriverName {//+ ' DriverID:' + PooledInfo.FDConnection1.Params.DriverID +
' ConnectionString:' + PooledInfo.FDConnection1.ConnectionString};
{$endif}
exit;
end;
end;
//连接数据库 end;
end;
Result := true;
end;
function TPubbase.DB_OMT(SQLText: string; var ErrStr: string; Mt1: TminiMemTable;
AZTCode: string): boolean;
begin
Result := false;
if not DB_O(SQLText, ErrStr, AZTCode) then exit;
Mt1.Close;
Mt1.Assign(Query);
if not Mt1.Open(ErrStr) then exit;
Result := true;
end;
function TPubbase.DB_ODS(SQLText: string; var ErrStr: string;
ds: THjhMemoryDataSet; AZTCode: string): boolean;
begin
Result := false;
if not DB_O(SQLText, ErrStr, AZTCode) then exit;