-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWork.DB.pas
142 lines (125 loc) · 2.86 KB
/
Work.DB.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
unit Work.DB;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections, Vcl.Grids,
HGM.Controls.VirtualTable, SQLLang, SQLiteTable3;
type
TUID = record
ID:Integer;
Caption:string;
class function Create(AID:Integer; ACaption:string):TUID; static;
end;
TDatabaseCore = class
private
FDBFileName:string;
FSQL:TSQLiteDatabase;
FWork:Boolean;
FQCount:Integer;
procedure OnQuery(Sender:TObject; SQL:string);
procedure CreateSysTable;
public
function GetNextVal(Index:string):Integer;
constructor Create(AFileName:string);
destructor Destroy;
property Work:Boolean read FWork;
property SQL:TSQLiteDatabase read FSQL;
property QCount:Integer read FQCount;
end;
function CreateFIO(F, I, O:string):string;
function CreateFullFIO(F, I, O:string):string;
implementation
function CreateFIO(F, I, O:string):string;
begin
Result:=F;
if Length(I) > 0 then
begin
Result:=Result + ' ' + I[1]+'.';
if Length(O) > 0 then Result:=Result + ' ' + O[1] + '.';
end;
end;
function CreateFullFIO(F, I, O:string):string;
begin
Result:=F;
if Length(I) > 0 then
begin
Result:=Result + ' ' + I;
if Length(O) > 0 then Result:=Result + ' ' + O;
end;
end;
{ TDatabaseCore }
constructor TDatabaseCore.Create(AFileName: string);
begin
FDBFileName:=AFileName;
FWork:=False;
FQCount:=0;
try
FSQL:=TSQLiteDatabase.Create(FDBFileName);
FSQL.OnQuery:=OnQuery;
CreateSysTable;
FWork:=True;
except
FWork:=False;
end;
end;
procedure TDatabaseCore.CreateSysTable;
begin
if not FSQL.TableExists('CRM_SYS_INDEX') then
begin
with SQLLang.SQL.CreateTable('CRM_SYS_INDEX') do
begin
AddField('SEC_INDEX', ftString);
AddField('VALUE', ftInteger);
SQL.ExecSQL(GetSQL);
EndCreate;
end;
end;
end;
destructor TDatabaseCore.Destroy;
begin
FSQL.Free;
end;
function TDatabaseCore.GetNextVal(Index: string):Integer;
var Value:Integer;
begin
with SQLLang.SQL.Select('CRM_SYS_INDEX') do
begin
AddField('VALUE');
WhereFieldEqual('SEC_INDEX', Index);
Value:=SQL.GetTableValue(GetSQL);
EndCreate;
end;
if Value < 0 then
begin
with SQLLang.SQL.InsertInto('CRM_SYS_INDEX') do
begin
AddValue('SEC_INDEX', Index);
AddValue('VALUE', 10001);
Result:=10001;
SQL.ExecSQL(GetSQL);
EndCreate;
end;
end
else
begin
Result:=Value+1;
with SQLLang.SQL.Update('CRM_SYS_INDEX') do
begin
AddValue('VALUE', Result);
WhereFieldEqual('SEC_INDEX', Index);
SQL.ExecSQL(GetSQL);
EndCreate;
end;
end;
end;
procedure TDatabaseCore.OnQuery(Sender: TObject; SQL: string);
begin
Inc(FQCount);
end;
{ TUID }
class function TUID.Create(AID:Integer; ACaption:string): TUID;
begin
Result.ID:=AID;
Result.Caption:=ACaption;
end;
end.