-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOraUpLoadData.pas
293 lines (252 loc) · 6.65 KB
/
OraUpLoadData.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
unit OraUpLoadData;
interface
uses
Windows, SysUtils, Classes
, DB
, Oracle
, OracleData
, Contnrs
;
{$IFDEF Unicode}
type TData = array of array of AnsiString;
{$ELSE}
type TData = array of array of String;
{$ENDIF}
type TUpLoadField = class
private
fid: Integer;
fUserColumnNum: Integer;
fName: string;
fColumnType: TDirectPathColumnType;
fUsed: Boolean;
public
property Id: Integer read fId;
property UserColumnNum: Integer read fUserColumnNum write fUserColumnNum;
property Name: string read fName write fName;
property ColumnType: TDirectPathColumnType read fColumnType write fColumnType;
property Used: Boolean read fUsed write fUsed;
end;
type TOracleUpLoadData = class
private
fOraSession: TOracleSession;
fTableName: string;
fOraDirectPath: TOracleDirectPathLoader;
fBufferSize: Cardinal;
fUsedColumnNum: Boolean;
fArrIdColumns: array of array of Integer;
fUploadFields: tObjectList;
private
procedure SetId;
procedure SetFields;
procedure ODPPrepare;
procedure SetBufferSize(aValue: Cardinal);
function SetArrIdColumns: Boolean;
function GetUpLoadFields(index: Integer): TUpLoadField;
public
property TableName: string read fTableName;
property BufferSize: cardinal read fBufferSize write SetBufferSize;
property Fields[index: integer]: TUpLoadField read GetUpLoadFields; default;
property UsedColumnNum: Boolean read fUsedColumnNum write fUsedColumnNum; //Áóäóò èñïîëüçîâàòüñÿ TUpLoadField.UserColumnNum
public
function FieldsCount: Integer;
function FieldsUsedCount: Integer;
function UpLoad(aArData: TData): Boolean; overload; virtual; //[row, column]
function UpLoad(aDS: TDataSet): Boolean; overload; virtual;
public
constructor Create(aOraSession: TOracleSession; aTableName: string); overload;
constructor Create(aDataBase, aUserName, aPassword, aTableName: string); overload;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
end;
implementation
procedure TOracleUpLoadData.SetId;
var
i: Integer;
begin
for i := 0 to fUploadFields.Count - 1 do
Fields[i].fid := i;
end;
procedure TOracleUpLoadData.SetFields;
var
i: Integer;
t: TUpLoadField;
c: TDirectPathColumn;
begin
Assert(fOraSession.Connected, '{131ACFCE-C464-4477-A0A0-2770BFFC36CE}');
Assert(fTableName > '', '{97C996FF-6C58-4E3B-A46F-81561AB4BD69}');
for i := 0 to fOraDirectPath.Columns.Count - 1 do
begin
c := fOraDirectPath.Columns[i];
t := TUpLoadField.Create;
t.Name := c.Name;
t.fColumnType := c.DataType;
t.Used := true;
t.UserColumnNum := -1;
fUploadFields.Add(t);
end;
end;
procedure TOracleUpLoadData.ODPPrepare;
begin
fOraDirectPath.BufferSize := fBufferSize;
fOraDirectPath.Prepare;
end;
procedure TOracleUpLoadData.SetBufferSize(aValue: Cardinal);
begin
fBufferSize := aValue;
ODPPrepare;
end;
function TOracleUpLoadData.SetArrIdColumns: Boolean;
var
i, c: Integer;
begin
Result := True;
fArrIdColumns := nil;
c := 0;
for i := 0 to fUploadFields.Count - 1 do
begin
if (Fields[i].Used) then
begin
SetLength(fArrIdColumns, c + 1);
SetLength(fArrIdColumns[c], 2);
fArrIdColumns[c, 0] := Fields[i].Id;
fArrIdColumns[c, 1] := Fields[i].UserColumnNum;
if ((UsedColumnNum) and (fArrIdColumns[c, 1] < 0)) then
Result := False;
Inc(c);
end;
end;
end;
function TOracleUpLoadData.GetUpLoadFields(index: Integer): TUpLoadField;
begin
Result := TUpLoadField(fUploadFields[index]);
end;
function TOracleUpLoadData.FieldsCount: Integer;
begin
Result := fUploadFields.Count;
end;
function TOracleUpLoadData.FieldsUsedCount: Integer;
var
i: Integer;
begin
Result := 0;
for i := 0 to fUploadFields.Count - 1 do
if Fields[i].Used then
Inc(Result);
end;
function TOracleUpLoadData.UpLoad(aArData: TData): Boolean;
var
i, c, vRows: Integer;
procedure NotUseColumn;
var
j, k: Integer;
begin
for j := 0 to High(aArData[i]) do
begin
k := fArrIdColumns[j, 0];
if (aArData[i, j] > '') then
fOraDirectPath.Columns[k].SetData(vRows, @aArData[i, j][1], Length(aArData[i, j]));
end;
end;
procedure UseColumn;
var
j, k, n: Integer;
begin
for j := 0 to High(fArrIdColumns) do
begin
k := fArrIdColumns[j, 0];
n := fArrIdColumns[j, 1];
//OutputDebugString(PChar(Format('UseColumn:%d, %d, %d, %s', [vRows, j, Length(aArData[i, n]), aArData[i, n]])));
if (aArData[i, n] > '') then
fOraDirectPath.Columns[k].SetData(
vRows
, @aArData[i, n][1]
, Length(aArData[i, n]){*SizeOf(Char)}
);
end;
end;
begin
Result := False;
if (High(aArData) < 0) then
Exit;
if (not SetArrIdColumns) then
Exit;
c := High(aArData[0]) + 1;
if (c <> FieldsUsedCount) then
Exit;
vRows := 0;
for i := 0 to High(aArData) do
begin
if (fOraDirectPath.MaxRows = vRows) then
begin
fOraDirectPath.Load(vRows);
vRows := 0;
end;
if (UsedColumnNum) then
UseColumn
else
NotUseColumn;
Inc(vRows);
end;
fOraDirectPath.Load(vRows);
fOraDirectPath.Finish;
Result := True;
end;
function TOracleUpLoadData.UpLoad(aDS: TDataSet): Boolean;
var
r, i, c: Integer;
d: TData;
begin
Assert(aDS.Active, '{CF062155-3411-455E-9A38-C59ED4B8B556}');
r := 0;
c := aDS.FieldCount;
aDS.First;
while (not aDS.Eof) do
begin
SetLength(d, r + 1);
for i := 0 to c - 1 do
begin
SetLength(d[r], c);
d[r, i] := aDS.Fields[i].AsString;
end;
Inc(r);
aDS.Next;
end;
Result := UpLoad(d);
d := nil;
end;
constructor TOracleUpLoadData.Create(aOraSession: TOracleSession; aTableName: string);
begin
fOraSession := TOracleSession.Create(nil);
aOraSession.Share(fOraSession);
fTableName := aTableName;
end;
constructor TOracleUpLoadData.Create(aDataBase, aUserName, aPassword, aTableName: string);
begin
fOraSession := TOracleSession.Create(nil);
fOraSession.LogonDatabase := aDataBase;
fOraSession.LogonUsername := aUserName;
fOraSession.LogonPassword := aPassword;
fTableName := aTableName;
end;
procedure TOracleUpLoadData.AfterConstruction;
begin
inherited;
fOraSession.Connected := True;
fUsedColumnNum := False;
fBufferSize := 512 * 1024;
fUploadFields := TObjectList.Create;
fOraDirectPath := TOracleDirectPathLoader.Create(nil);
fOraDirectPath.Session := fOraSession;
fOraDirectPath.TableName := fTableName;
fOraDirectPath.GetDefaultColumns(True);
ODPPrepare;
SetFields;
SetId;
end;
procedure TOracleUpLoadData.BeforeDestruction;
begin
FreeAndNil(fUploadFields);
FreeAndNil(fOraDirectPath);
Inherited;
end;
end.