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
|
{
This file is part of the Free Pascal Class Library SDO Implementation
Copyright (c) 2012 by Inoussa OUEDRAOGO
Free Pascal development team
This unit implements some SDO DAS utilities
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$INCLUDE sdo_global.inc}
unit sdo_das_utils;
interface
uses
SysUtils, Classes, DB,
sdo, sdo_das, data_acces_intf;
const
BOOLEAN_TRUE_CHARS : set of AnsiChar = ['T','t', 'Y', 'y', 'O', 'o', '1'];
DefaultSdoType = StringType;
{$IFNDEF HAS_SDO_FLOAT}
FloatType = DefaultSdoType;
{$ENDIF HAS_SDO_FLOAT}
{$IFNDEF HAS_SDO_DOUBLE}
DoubleType = DefaultSdoType;
{$ENDIF HAS_SDO_DOUBLE}
{$IFNDEF HAS_SDO_TIME}
TimeType = DefaultSdoType;
{$ENDIF HAS_SDO_TIME}
FieldTypeToSDOTypeMAP : array[TFieldType] of TSDOTypeKind = (
StringType, StringType, ShortType, IntType, IntType,
//ftUnknown, ftString, ftSmallint, ftInteger, ftWord,
BooleanType, FloatType, CurrencyType, DoubleType, DateType, TimeType, DateTimeType,
//ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,
BytesType, BytesType, IntType, BytesType, BytesType, BytesType, BytesType,
//ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo,
BytesType, BytesType, BytesType, BytesType, StringType, StringType,
//ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar, ftWideString,
LongType, BytesType, BytesType, BytesType, ObjectType, BytesType, BytesType,
//ftLargeint, ftADT, ftArray, ftReference, ftDataSet, ftOraBlob, ftOraClob,
BytesType, BytesType, BytesType, StringType, TimeType, DoubleType
//ftVariant, ftInterface, ftIDispatch, ftGuid, ftTimeStamp, ftFMTBcd
{$IF Declared(ftFixedWideChar)}
, StringType, StringType
//ftFixedWideChar, ftWideMemo
{$IFEND }
{$IF Declared(ftOraTimeStamp)}
, StringType, StringType
//ftOraTimeStamp, ftOraInterval
{$IFEND }
{$IF Declared(ftLongWord)}
, StringType, StringType, StringType, StringType
//ftLongWord, ftShortint, ftByte, ftExtended,
{$IFNDEF FPC}
, StringType, StringType, StringType,
// ftConnection, ftParams, ftStream,
StringType, StringType, StringType
//ftTimeStampOffset, ftObject, ftSingle
{$ENDIF}
{$IFEND }
);
type
TSDODASHandlerItem = class
private
FHandler: ISDODASObjectHandler;
FTargetType: ISDOType;
public
property TargetType : ISDOType read FTargetType;
property Handler : ISDODASObjectHandler read FHandler;
end;
procedure AssignValue(
AObject : ISDODataObject;
AProp : ISDOProperty;
AField : TField
);
function GenerateInsertQuery(
const ARowType : ISDOType;
const ATableName : string
) : string;
function CopyToTable(
const ARows : ISDODataObjectList;
const ATableName : string;
const ADac : TDataAccessInterface
) : Integer;overload;
procedure CopyToTable(
const AObject : ISDODataObject;
const ATableName : string;
const ADac : TDataAccessInterface
);overload;
implementation
uses
sdo_date_utils, sdo_imp_utils, sdo_linked_list;
procedure AssignValue(AObject : ISDODataObject; AProp : ISDOProperty; AField : TField);
procedure AssignChar();
var
s : string;
begin
s := AField.AsString;
if ( Length(s) = 0 ) then
AObject.setCharacter(AProp,#0)
else
AObject.setCharacter(AProp,s[1]);
end;
procedure AssignBoolean();
var
s : string;
begin
s := AField.AsString;
AObject.setBoolean(AProp,(Length(s) > 0) and (s[1] in BOOLEAN_TRUE_CHARS));
end;
procedure AssignBlobField();
var
locStream : TMemoryStream;
locBlobField : TBlobField;
locBytes : TSDOBytes;
{$IFDEF DELPHI}
locBytes2 : TBytes;
{$ENDIF DELPHI}
begin
locBlobField := TBlobField(AField);
if (locBlobField.BlobSize = 0) then begin
AObject.setBytes(AProp,nil);
end else begin
{$IFDEF DELPHI}
if (locBlobField.BlobType = ftWideMemo) then
locBytes2 := TEncoding.UTF8.GetBytes(locBlobField.AsWideString)
else
locBytes2 := locBlobField.AsBytes;
SetLength(locBytes,Length(locBytes2));
if (Length(locBytes) > 0) then
Move(locBytes2[0],locBytes[0],Length(locBytes));
AObject.setBytes(AProp,locBytes);
{$ENDIF DELPHI}
{$IFDEF FPC}
locStream := TMemoryStream.Create();
try
locBlobField.SaveToStream(locStream);
locBytes := StreamToVarBytes(locStream);
finally
locStream.Free();
end;
AObject.setBytes(AProp,locBytes);
{$ENDIF FPC}
end;
end;
begin
if AField.IsNull then begin
AObject.setNull(AProp);
end else begin
case AProp.getTypeEnum() of
BooleanType :
begin
if AField.InheritsFrom(TBooleanField) then
AObject.setBoolean(AProp,AField.AsBoolean)
else
AssignBoolean();
end;
ByteType : AObject.setByte(AProp,AField.AsInteger);
BytesType :
begin
if AField.InheritsFrom(TBlobField) then
AssignBlobField()
else
AObject.setBytes(AProp, StringToVarBytes(AField.AsString));
end;
CharacterType : AssignChar();
CurrencyType : AObject.setCurrency(AProp,AField.AsCurrency);
DateTimeType : AObject.setDate(AProp,DateTimeToDateTimeRec(AField.AsDateTime));
DoubleType : AObject.setDouble(AProp,AField.AsFloat);
FloatType : AObject.setFloat(AProp,AField.AsFloat);
IntegerType : AObject.setInteger(AProp,AField.AsInteger);
LongType :
begin
if AField.InheritsFrom(TLargeintField) then
AObject.setLong(AProp,TLargeintField(AField).AsLargeInt)
else
AObject.setLong(AProp,AField.AsInteger);
end;
ShortType : AObject.setShort(AProp,AField.AsInteger);
StringType :
begin
{$IFDEF DEFAULT_UNICODE}
AObject.setString(AProp,AField.AsString);
{$ELSE DEFAULT_UNICODE}
if (AField.DataType in [ftWideString,ftWideMemo,ftFixedWideChar]) then
AObject.setString(AProp,AField.AsWideString)
else
AObject.setString(AProp,UTF8Decode(AField.AsString));
{$ENDIF DEFAULT_UNICODE}
end
else
raise ESDONotImplementedException.Create('TSDODAS.ExecuteQuery()>AssignValue()');
end;
end;
end;
function GenerateInsertQuery(
const ARowType : ISDOType;
const ATableName : string
) : string;
var
locRowType : ISDOType;
pl : ISDOPropertyList;
p : ISDOProperty;
locInsertSQLCol, locInsertSQLVal: string;
k, c : Integer;
begin
locRowType := ARowType;
pl := locRowType.getProperties();
locInsertSQLCol := '';
locInsertSQLVal := '';
c := 0;
for k := 0 to pl.getCount() - 1 do begin
p := pl.getItem(k);
if p.getType().isDataType() then begin
locInsertSQLCol := Format('%s, %s',[locInsertSQLCol,p.getName()]);
locInsertSQLVal := Format('%s, :P%d',[locInsertSQLVal,c]);
Inc(c);
end;
end;
Delete(locInsertSQLCol,1,1);
Delete(locInsertSQLVal,1,1);
Result :=
Format(
'INSERT INTO %s(%s) VALUES(%s)',
[Trim(ATableName),locInsertSQLCol,locInsertSQLVal]
);
end;
function CopyToTable(
const ARows : ISDODataObjectList;
const ATableName : string;
const ADac : TDataAccessInterface
) : Integer;
var
locRowType, locPropType : ISDOType;
locInsertSQLQuery: string;
k, kc, kp : Integer;
locCursor : ILinkedListCursor;
locRow : ISDODataObject;
locParams : array of Variant;
locRowPropList : ISDOPropertyList;
locIsDataType : array of record IsDataType : Boolean; Index : Integer; end;
begin
Result := ARows.size();
if (Result = 0) then
exit;
locRowType := ARows.getDataObject(0).getType();
locRowPropList := locRowType.getProperties();
locInsertSQLQuery := GenerateInsertQuery(locRowType,ATableName);
kc := locRowPropList.getCount();
SetLength(locIsDataType,kc);
kp := 0;
for k := 0 to kc - 1 do begin
locPropType := locRowPropList.getItem(k).getType();
locIsDataType[k].IsDataType := locPropType.isDataType();
if locIsDataType[k].IsDataType then begin
locIsDataType[k].Index := kp;
Inc(kp);
end;
end;
SetLength(locParams,kp);
locCursor := ARows.getCursor();
locCursor.Reset();
while locCursor.MoveNext() do begin
locRow := ARows.getDataObject();
for k := 0 to kc - 1 do begin
if locIsDataType[k].IsDataType then
locParams[locIsDataType[k].Index] := locRow.getVariant(k);
end;
ADac.ExecuteNonDataset(locInsertSQLQuery,locParams);
end;
end;
procedure CopyToTable(
const AObject : ISDODataObject;
const ATableName : string;
const ADac : TDataAccessInterface
);
var
locRowType, locPropType : ISDOType;
locInsertSQLQuery: string;
k, kc, kp : Integer;
locParams : array of Variant;
locRowPropList : ISDOPropertyList;
locIsDataType : array of record IsDataType : Boolean; Index : Integer; end;
begin
if (AObject = nil) then
exit;
locRowType := AObject.getType();
locRowPropList := locRowType.getProperties();
locInsertSQLQuery := GenerateInsertQuery(locRowType,ATableName);
kc := locRowPropList.getCount();
SetLength(locIsDataType,kc);
kp := 0;
for k := 0 to kc - 1 do begin
locPropType := locRowPropList.getItem(k).getType();
locIsDataType[k].IsDataType := locPropType.isDataType();
if locIsDataType[k].IsDataType then begin
locIsDataType[k].Index := kp;
Inc(kp);
end;
end;
SetLength(locParams,kp);
for k := 0 to kc - 1 do begin
if locIsDataType[k].IsDataType then
locParams[locIsDataType[k].Index] := AObject.getVariant(k);
end;
ADac.ExecuteNonDataset(locInsertSQLQuery,locParams);
end;
end.
|