-
Notifications
You must be signed in to change notification settings - Fork 19
/
ASGSQLiteData.pas
283 lines (263 loc) · 7.83 KB
/
ASGSQLiteData.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
{$I asqlite_def.inc}
unit ASGSQLiteData;
interface
uses
ActiveX,
Variants,
DB,
Windows,
sysutils,
FMTBcd;
procedure BufferToVar(FieldType: TFieldType; Buffer: Pointer; var Data: OleVariant);
procedure VarToBuffer(Field: TField; Data: OleVariant; Buffer: Pointer);
procedure UnpackBuffer(Buffer: PAnsiChar; FieldType: TFieldType; SQLiteDateFormat: boolean; var Data: OleVariant);
//function FieldNoToIndex(ADataset: TDataSet; AFieldNo: integer): integer;
function GetCalcFieldsCount(ADataset: TDataset): integer;
implementation
uses
{$IFDEF ASQLite_D2006PLUS}
WideStrUtils,
{$ENDIF}
ASGRout3,
ASGSQLite3;
function GetCalcFieldsCount(ADataset: TDataset): integer;
var i: integer;
begin
result:= 0;
for I := 0 to ADataset.FieldCount - 1 do
if ADataset.Fields[i].FieldKind <> fkData then
inc(result);
end;
//function FieldNoToIndex(ADataset: TDataSet; AFieldNo: integer): integer;
//var Field: TField;
//begin
// Assert(Assigned(ADataset));
// Assert(AFieldNo > 0);
//
//
// // if fieldcount is 0 then no fields defined yet so return field no -1
// if ADataset.FieldCount = 0 then
// result:= AFieldNo - 1
// else
// begin
// // otherwise find the matching field and return it's index
//
// Field:= ADataset.Fields.FieldByNumber(AFieldNo);
// if assigned(Field) then
// result:= Field.Index
// else
// result:= -1; // if no matching field found, return -1
// end;
//end;
procedure BufferToVar(FieldType: TFieldType; Buffer: Pointer; var Data: OleVariant);
begin
case FieldType of
ftString, ftFixedChar, ftGuid:
Data := string(PChar(Buffer));
ftWideString {$IFDEF ASQLite_D2006PLUS} ,ftFixedWideChar{$ENDIF}:
Data := WideString(PWideChar(Buffer));
ftAutoInc, ftInteger:
Data := LongInt(Buffer^);
ftSmallInt:
Data := SmallInt(Buffer^);
ftWord:
Data := Word(Buffer^);
ftBoolean:
Data := WordBool(Buffer^);
ftFloat, ftCurrency:
Data := Double(Buffer^);
ftBlob, ftMemo, ftGraphic, ftVariant {$IFDEF ASQLite_D2006PLUS} ,ftWideMemo{$ENDIF}:
Data := Variant(Buffer^);
ftInterface:
Data := IUnknown(Buffer^);
ftIDispatch:
Data := IDispatch(Buffer^);
ftDate, ftTime, ftDateTime:
Data := TDateTime(Buffer^);
ftBCD:
Data := Currency(Buffer^);
ftTimestamp:
Data := Variant(Buffer^);
ftBytes, ftVarBytes:
// if NativeFormat then
// DataConvert(Field, Buffer, @Data, False) else
Data := OleVariant(Buffer^);
ftLargeInt:
Data := LargeInt(Buffer^);
else
DatabaseErrorFmt('Unsupported field type "%s"', [FieldTypeNames[FieldType], '']);
end;
end;
function VarDataSize(const Value: OleVariant): Integer;
begin
if VarIsNull(Value) then
Result := -1
else if VarIsArray(Value) then
Result := VarArrayHighBound(Value, 1) + 1
else if TVarData(Value).VType = varOleStr then
begin
Result := Length(PWideString(@TVarData(Value).VOleStr)^);
if Result = 0 then
Result := -1;
end
else
Result := SizeOf(OleVariant);
end;
function WStrLCopy(Dest, Source: PWideChar; MaxLen: Cardinal): PWideChar;
var
Count: Cardinal;
begin
// copies a specified maximum number of characters from Source to Dest
Result := Dest;
Count := 0;
While (Count < MaxLen) and (Source^ <> #0) do begin
Dest^ := Source^;
Inc(Source);
Inc(Dest);
Inc(Count);
end;
Dest^ := #0;
end;
function WStrCopy(Dest, Source: PWideChar): PWideChar;
begin
Result := WStrLCopy(Dest, Source, MaxInt);
end;
procedure BCDConvert(Source, Dest: Pointer);
begin
TBcd(Dest^) := TBcd(Source^);
end;
procedure VarToBuffer(Field: TField; Data: OleVariant; Buffer: Pointer);
procedure CurrToBuffer(const C: Currency);
begin
BCDConvert(@C, Buffer);
end;
begin
with tagVariant(Data) do
case Field.DataType of
ftGuid, ftFixedChar, ftString:
begin
PChar(Buffer)[Field.Size] := #0;
WideCharToMultiByte(0, 0, bStrVal, SysStringLen(bStrVal)+1,
Buffer, Field.Size, nil, nil);
end;
{$IFDEF ASQLite_D2006PLUS} ftFixedWideChar, {$ENDIF}
ftWideString:
WStrCopy(Buffer, bstrVal);
ftSmallint:
if vt = VT_UI1 then
SmallInt(Buffer^) := Byte(cVal) else
SmallInt(Buffer^) := iVal;
ftWord:
if vt = VT_UI1 then
Word(Buffer^) := bVal else
Word(Buffer^) := uiVal;
ftAutoInc, ftInteger:
Integer(Buffer^) := Data;
ftFloat, ftCurrency:
if vt = VT_R8 then
Double(Buffer^) := dblVal else
Double(Buffer^) := Data;
ftBCD:
if vt = VT_CY then
CurrToBuffer(cyVal) else
CurrToBuffer(Data);
ftBoolean:
WordBool(Buffer^) := vbool;
ftDate, ftTime, ftDateTime:
// DataConvert(Field, @date, Buffer, True);
TOleDate(Buffer^) := date;
ftBytes, ftVarBytes:
// DataConvert(Field, @Data, Buffer, True);
OleVariant(Buffer^) := Data;
ftInterface: IUnknown(Buffer^) := Data;
ftIDispatch: IDispatch(Buffer^) := Data;
ftLargeInt:
if Decimal(Data).sign > 0 then
LargeInt(Buffer^):=-1*Decimal(Data).Lo64
else
LargeInt(Buffer^):=Decimal(Data).Lo64;
ftBlob..ftTypedBinary, ftVariant
{$IFDEF ASQLite_D2006PLUS}, ftWideMemo {$ENDIF} :
OleVariant(Buffer^) := Data;
else
DatabaseErrorFmt('Unsupported field type in "%s"', [FieldTypeNames[Field.DataType],
Field.DisplayName]);
end;
end;
procedure UnpackBuffer(Buffer: PAnsiChar; FieldType: TFieldType; SQLiteDateFormat: boolean; var Data: OleVariant);
var
TempInt : integer;
TempDouble : double;
TempBool : wordbool;
TempT : TDateTime;
SaveDateFormat : string;
begin
{$IFDEF DEBUG_VERY_LOUD}
DebugEnter('TASQLite3BaseQuery.UnpackBuffer: ' + Buffer);
{$ENDIF}
case FieldType of
ftString:
begin
{$IFDEF DEBUG_VERY_LOUD}
DebugLeave('TASQLite3BaseQuery.UnpackBuffer');
{$ENDIF}
exit;
end;
ftInteger, ftSmallInt:
begin
TempInt := StrToIntX(string(Buffer));
Data:= TempInt;
end;
ftTime:
begin
if SQLiteDateFormat then begin
shorttimeformat := 'hh":"nn":"ss"."zzz';
TempT := StrToTimeX(Buffer) // aducom 2006
end else begin
shorttimeformat := 'hh":"nn":"ss"';
TempT := StrToTimeX(string(Buffer)); // aducom 2006
end;
Data:= TempT;
end;
ftDate:
begin
if SQLiteDateFormat then begin
savedateformat := shortdateformat;
shortdateformat := 'yyyy-mm-dd';
TempT := (StrToDateX(Buffer)); // aducom 2006
shortdateformat := savedateformat;
end else
TempT := (StrToDateX(Buffer)); // aducom 2006
Data:= TempT;
end;
ftDateTime:
begin
if SQLiteDateFormat then // aducom
TempT := YYYYMMDDParser(Buffer) // jpierce
else
TempT := StrToDateTimeX(Buffer);
Data:= TempT;
end;
ftFloat, ftBCD, ftCurrency:
begin
TempDouble := StrToFloatX(FloatParser(Buffer));
Data:= TempDouble;
end;
{$IFDEF ASQLITE_D6PLUS}
ftBoolean:
begin
TempBool := StrToBool(Buffer);
Data:= TempBool;
end;
{$ENDIF}
ftMemo, ftGraphic, ftBlob, ftFMTMemo: // pointer to stream
begin
TempInt := StrToInt(Buffer);
Data:= TempInt;
end;
end;
{$IFDEF DEBUG_VERY_LOUD}
DebugLeave('TASQLite3BaseQuery.UnpackBuffer');
{$ENDIF}
end;
end.