-
Notifications
You must be signed in to change notification settings - Fork 3
/
uDBSetProvider.pas
201 lines (171 loc) · 4.94 KB
/
uDBSetProvider.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
unit uDBSetProvider;
{
Ìîäóëü ñîäåðæèò êëàññ ïðîâàéäåðà, êîòîðûé ñîäåðæèò íàáîð êîìïîíåíòîâ äëÿ
ðàáîòû ñ áàçîé áàííûõ êëèåíñêîãî ïîòîêà.}
interface
uses
IBDatabase, SysUtils, IBSQL, u_IBInfoDataWriter, uVehicles3C,
u_IBConfDataWriter, u_IBJournDataWriter;
type
TDBConnectParams = class(TObject)
private
FDBPath: String;
Fpass: String;
Flogin: String;
Frole: String;
procedure SetDBPath(const Value: String);
procedure Setlogin(const Value: String);
procedure Setpass(const Value: String);
procedure Setrole(const Value: String);
public
property DBPath: String read FDBPath write SetDBPath;
property login: String read Flogin write Setlogin;
property pass: String read Fpass write Setpass;
property role: String read Frole write Setrole;
end;
TDBSetProvider = class(TObject)
private
FDatabase: TIBDatabase;
FTransaction: TIBTransaction;
FSQL: TIBSQL;
FGenerator: TIBSQL;
FInfoWriter: TIBInfoDataWriter;
FConfWriter: TIBConfDataWriter;
FJournWriter: TIBJournDataWriter;
function GetActive: Boolean;
public
constructor Create;
destructor Destroy; override;
property Active: Boolean read GetActive;
function Connect(params: TDBConnectParams): Boolean;
procedure Disconnect;
property ibSQL: TIBSQL read FSQL;
function WriteInfoData(AData: PByte; vid: TVehIdent): Boolean;
function WriteConfData(AData: PByte; vid: TVehIdent): Boolean;
function WriteJournData(AData: PByte; vid: TVehIdent): Boolean;
function Gen_Id(const GeneratorName: string; Step: Int64): Int64;
end;
implementation
uses LogFileUnit;
{ TDBSetProvider }
function TDBSetProvider.Connect(params: TDBConnectParams): Boolean;
begin
FDatabase.DatabaseName := params.DBPath;
FDatabase.Params.Clear;
FDatabase.Params.Add('user_name=' + params.login);
FDatabase.Params.Add('password=' + params.pass);
if (params.role <> '') then
FDatabase.Params.Add('sql_role_name=' + params.role);
try
FDatabase.Connected := True;
except
on E: Exception do
LogError('Exception', E.Message);
end;
Result := FDatabase.Connected;
FJournWriter.ResetState;
end;
constructor TDBSetProvider.Create;
begin
FDatabase := TIBDatabase.Create(nil);
FTransaction := TIBTransaction.Create(nil);
FSQL := TIBSQL.Create(nil);
FGenerator := TIBSQL.Create(nil);
FDatabase.LoginPrompt := False;
FDatabase.SQLDialect := 3;
FDatabase.DefaultTransaction := FTransaction;
FTransaction.DefaultDatabase := FDatabase;
FTransaction.Params.Add('write');
FTransaction.Params.Add('read_committed');
FTransaction.Params.Add('rec_version');
FTransaction.Params.Add('nowait');
FTransaction.AllowAutoStart := True;
FTransaction.DefaultAction := TACommit;
FSQL.Database := FDatabase;
FSQL.Transaction := FTransaction;
FGenerator.Database := FDatabase;
FGenerator.Transaction := FTransaction;
FGenerator.SQL.Text := 'select gen_id(:GenName, :Step) from RDB$DATABASE';
FInfoWriter := TIBInfoDataWriter.Create(FDatabase);
FConfWriter := TIBConfDataWriter.Create(FDatabase);
FJournWriter := TIBJournDataWriter.Create(FDatabase);
end;
destructor TDBSetProvider.Destroy;
begin
FJournWriter.Free;
FConfWriter.Free;
FInfoWriter.Free;
FGenerator.Free;
FSQL.Free;
Disconnect;
FTransaction.Free;
FDatabase.Free;
inherited;
end;
procedure TDBSetProvider.Disconnect;
begin
try
if FTransaction.InTransaction then
FTransaction.Commit;
finally
if FTransaction.InTransaction then
FTransaction.Rollback;
end;
FDatabase.Connected := False;
end;
function TDBSetProvider.Gen_Id(const GeneratorName: string; Step: Int64): Int64;
begin
if not FTransaction.InTransaction then
FTransaction.StartTransaction;
FGenerator.Close;
FGenerator.ParamByName('GenName').AsString := GeneratorName;
FGenerator.ParamByName('Step').AsInt64 := Step;
try
FGenerator.ExecQuery;
Result := FGenerator.Fields[0].AsInt64;
if FTransaction.InTransaction then
FTransaction.Commit;
finally
if FTransaction.InTransaction then
FTransaction.Rollback;
end;
end;
function TDBSetProvider.GetActive: Boolean;
begin
Result := FDatabase.Connected;
end;
function TDBSetProvider.WriteConfData(AData: PByte;
vid: TVehIdent): Boolean;
begin
FConfWriter.VID := vid;
Result := FConfWriter.WriteData(AData);
end;
function TDBSetProvider.WriteInfoData(AData: PByte;
vid: TVehIdent): Boolean;
begin
FInfoWriter.VID := vid;
Result := FInfoWriter.WriteData(AData);
end;
function TDBSetProvider.WriteJournData(AData: PByte;
vid: TVehIdent): Boolean;
begin
Result := FJournWriter.WriteData(AData, vid);
end;
{ TDBConnectParams }
procedure TDBConnectParams.SetDBPath(const Value: String);
begin
FDBPath := Value;
end;
procedure TDBConnectParams.Setlogin(const Value: String);
begin
Flogin := Value;
end;
procedure TDBConnectParams.Setpass(const Value: String);
begin
Fpass := Value;
end;
procedure TDBConnectParams.Setrole(const Value: String);
begin
Frole := Value;
end;
end.