@@ -23,10 +23,15 @@ implementation // compile a void unit if NOSYNDBZEOS conditional is set
23
23
24
24
{ $I Zeos.inc} // define conditionals like ZEOS72UP and ENABLE_*
25
25
26
+ { $I ..\mormot.defines.inc}
27
+
26
28
// for best performance: tune your project options or Zeos.inc
27
29
// by defining MORMOT2 and leverage best of mORMot and ZEOS !
28
30
29
- { $I ..\mormot.defines.inc}
31
+ { $if defined(ZEOS73UP) and defined(MORMOT2)}
32
+ // use direct IZResultSet.ColumnsToJson() export
33
+ { $define ZDBC_COLUMNSTOJSON}
34
+ { $ifend}
30
35
31
36
uses
32
37
types,
@@ -251,15 +256,15 @@ TSqlDBZeosConnection = class(TSqlDBConnectionThreadSafe)
251
256
TSqlDBZeosStatement = class (TSqlDBStatementWithParamsAndColumns)
252
257
protected
253
258
fStatement: IZPreparedStatement;
254
- fResultSet: IZResultSet;
259
+ fResultSet: IZResultSet;
255
260
fResultInfo: IZResultSetMetaData;
256
- { $if defined(ZEOS73UP) and defined(MORMOT2) }
261
+ { $ifdef ZDBC_COLUMNSTOJSON }
257
262
fJSONComposeOptions: TZJSONComposeOptions;
258
- { $ifend }
263
+ { $endif ZDBC_COLUMNSTOJSON }
259
264
public
260
- { $if defined(ZEOS73UP) and defined(MORMOT2) }
265
+ { $ifdef ZDBC_COLUMNSTOJSON }
261
266
procedure AfterConstruction ; override;
262
- { $ifend }
267
+ { $endif ZDBC_COLUMNSTOJSON }
263
268
// / Prepare an UTF-8 encoded SQL statement
264
269
// - parameters marked as ? will be bound later, before ExecutePrepared call
265
270
// - if ExpectResults is TRUE, then Step() and Column*() methods are available
@@ -312,7 +317,7 @@ TSqlDBZeosStatement = class(TSqlDBStatementWithParamsAndColumns)
312
317
function ColumnUtf8 (Col: integer): RawUtf8; override;
313
318
// / return a Column as a blob value of the current Row, first Col is 0
314
319
function ColumnBlob (Col: integer): RawByteString; override;
315
- { $if defined(ZEOS73UP) and defined(MORMOT2) }
320
+ { $ifdef ZDBC_COLUMNSTOJSON }
316
321
public
317
322
// / the ColumnsToJson options provided by ZDBC
318
323
// - jcoEndJsonObject:
@@ -337,7 +342,7 @@ TSqlDBZeosStatement = class(TSqlDBStatementWithParamsAndColumns)
337
342
property JSONComposeOptions: TZJSONComposeOptions
338
343
read fJSONComposeOptions write fJSONComposeOptions
339
344
default [jcoEndJsonObject];
340
- { $ifend }
345
+ { $endif ZDBC_COLUMNSTOJSON }
341
346
end ;
342
347
343
348
var
@@ -711,11 +716,22 @@ procedure TSqlDBZeosConnectionProperties.GetFields(const aTableName: RawUtf8;
711
716
F.ColumnTypeNative := SynUnicodeToUtf8(
712
717
res.GetUnicodeString(TableColColumnTypeNameIndex));
713
718
{ $endif ZEOS72UP}
714
- F.ColumnType := TZSQLTypeToTSqlDBFieldType(
715
- TZSQLType(res.GetInt(TableColColumnTypeIndex)));
716
- F.ColumnLength := res.GetInt(TableColColumnSizeIndex); // for char or date types this is the maximum number of characters
717
- F.ColumnPrecision := res.GetInt(TableColColumnSizeIndex); // for numeric or decimal types this is precision
718
- F.ColumnScale := res.GetInt(TableColColumnDecimalDigitsIndex); // the number of fractional digits
719
+ // the number of fractional digits
720
+ if res.IsNull(TableColColumnDecimalDigitsIndex) then
721
+ F.ColumnScale := -1
722
+ else
723
+ F.ColumnScale := res.GetInt(TableColColumnDecimalDigitsIndex);
724
+ if PosEx(' /*' , F.ColumnTypeNative) > 0 then
725
+ // circumvent https://synopse.info/forum/viewtopic.php?id=6353
726
+ F.ColumnType := ColumnTypeNativeToDB(F.ColumnTypeNative, F.ColumnScale)
727
+ else
728
+ // use Zeos/ZDBC type recognition
729
+ F.ColumnType := TZSQLTypeToTSqlDBFieldType(
730
+ TZSQLType(res.GetInt(TableColColumnTypeIndex)));
731
+ // for char or date types this is the maximum number of characters
732
+ F.ColumnLength := res.GetInt(TableColColumnSizeIndex);
733
+ // for numeric or decimal types this is precision
734
+ F.ColumnPrecision := res.GetInt(TableColColumnSizeIndex);
719
735
FA.Add(F);
720
736
end ;
721
737
if n > 0 then
@@ -742,20 +758,40 @@ function TSqlDBZeosConnectionProperties.TZSQLTypeToTSqlDBFieldType(
742
758
aNativeType: TZSQLType): TSqlDBFieldType;
743
759
begin
744
760
case aNativeType of
745
- stBoolean, stByte, stShort, stInteger, stLong
746
- { $ifdef ZEOS72UP} , stSmall, stWord, stLongWord, stULong { $endif ZEOS72UP} :
761
+ stBoolean,
762
+ stByte,
763
+ stShort,
764
+ stInteger,
765
+ stLong
766
+ { $ifdef ZEOS72UP} ,
767
+ stSmall,
768
+ stWord,
769
+ stLongWord,
770
+ stULong
771
+ { $endif ZEOS72UP} :
747
772
result := ftInt64;
748
- stFloat, stDouble:
773
+ stFloat,
774
+ stDouble:
749
775
result := ftDouble;
750
776
stBigDecimal
751
- { $ifdef ZEOS72UP} , stCurrency { $endif ZEOS72UP} :
777
+ { $ifdef ZEOS72UP} ,
778
+ stCurrency
779
+ { $endif ZEOS72UP} :
752
780
result := ftCurrency;
753
- stDate, stTime, stTimestamp:
781
+ stDate,
782
+ stTime,
783
+ stTimestamp:
754
784
result := ftDate;
755
- { $ifdef ZEOS72UP} stGUID, { $endif ZEOS72UP}
756
- stString, stUnicodeString, stAsciiStream, stUnicodeStream:
785
+ { $ifdef ZEOS72UP}
786
+ stGUID,
787
+ { $endif ZEOS72UP}
788
+ stString,
789
+ stUnicodeString,
790
+ stAsciiStream,
791
+ stUnicodeStream:
757
792
result := ftUtf8;
758
- stBytes, stBinaryStream:
793
+ stBytes,
794
+ stBinaryStream:
759
795
result := ftBlob;
760
796
else
761
797
raise ESqlDBZeos.CreateUtf8(' %: unexpected TZSQLType "%"' , [self,
@@ -775,9 +811,9 @@ class function TSqlDBZeosConnectionProperties.URI(aServer: TSqlDBDefinition;
775
811
aLibraryLocationAppendExePath);
776
812
end ;
777
813
778
- class function TSqlDBZeosConnectionProperties.URI (const aProtocol, aServerName:
779
- RawUtf8; const aLibraryLocation: TFileName; aLibraryLocationAppendExePath:
780
- boolean): RawUtf8;
814
+ class function TSqlDBZeosConnectionProperties.URI (
815
+ const aProtocol, aServerName: RawUtf8; const aLibraryLocation: TFileName;
816
+ aLibraryLocationAppendExePath: boolean): RawUtf8;
781
817
begin
782
818
// return e.g. mysql://192.168.2.60:3306/world?username=root;password=dev
783
819
result := TrimU(aProtocol);
@@ -792,7 +828,6 @@ class function TSqlDBZeosConnectionProperties.URI(const aProtocol, aServerName:
792
828
result := result + ' ;LibLocation='
793
829
else
794
830
result := result + ' ?LibLocation=' ;
795
-
796
831
if aLibraryLocationAppendExePath then
797
832
result := result + StringToUtf8(Executable.ProgramFilePath);
798
833
result := result + StringToUtf8(aLibraryLocation);
@@ -842,6 +877,7 @@ procedure TSqlDBZeosConnection.Connect;
842
877
end ;
843
878
end ;
844
879
end ;
880
+
845
881
procedure TSqlDBZeosConnection.Disconnect ;
846
882
begin
847
883
try
@@ -878,6 +914,7 @@ procedure TSqlDBZeosConnection.StartTransaction;
878
914
fDatabase.SetAutoCommit(false);
879
915
{ $endif ZEOS73UP}
880
916
end ;
917
+
881
918
procedure TSqlDBZeosConnection.Commit ;
882
919
begin
883
920
inherited Commit;
@@ -1215,13 +1252,13 @@ function TSqlDBZeosStatement.Step(SeekFirst: boolean): boolean;
1215
1252
end ;
1216
1253
end ;
1217
1254
1218
- { $if defined(ZEOS73UP) and defined(MORMOT2) }
1255
+ { $ifdef ZDBC_COLUMNSTOJSON }
1219
1256
procedure TSqlDBZeosStatement.AfterConstruction ;
1220
1257
begin
1221
1258
inherited ;
1222
1259
fJSONComposeOptions := [jcoEndJsonObject];
1223
1260
end ;
1224
- { $ifend }
1261
+ { $endif ZDBC_COLUMNSTOJSON }
1225
1262
1226
1263
function TSqlDBZeosStatement.ColumnBlob (Col: integer): RawByteString;
1227
1264
var
@@ -1313,16 +1350,23 @@ function TSqlDBZeosStatement.UpdateCount: integer;
1313
1350
end ;
1314
1351
1315
1352
{ $ifdef ZEOS72UP}
1353
+ { $ifdef ZDBC_COLUMNSTOJSON}
1316
1354
1317
1355
procedure TSqlDBZeosStatement.ColumnsToJson (WR: TResultsWriter);
1356
+ begin
1357
+ // direct export into JSON from internal ZDBC buffers for each provider
1358
+ fResultSet.ColumnsToJson(WR, fJSONComposeOptions);
1359
+ end ;
1318
1360
1319
- { $if not (defined(ZEOS73UP) and defined(MORMOT2))}
1361
+ { $else}
1362
+
1363
+ procedure TSqlDBZeosStatement.ColumnsToJson (WR: TResultsWriter);
1320
1364
var
1321
1365
col: integer;
1322
1366
P: PAnsiChar;
1323
1367
Len: NativeUInt; // required by Zeos for GetPAnsiChar out param (not PtrUInt)
1324
1368
1325
- procedure WriteIZBlob ;
1369
+ procedure WriteIZBlob ; // sub-function to avoid local stack temp variables
1326
1370
var
1327
1371
blob: IZBlob;
1328
1372
raw: RawByteString;
@@ -1331,13 +1375,9 @@ procedure TSqlDBZeosStatement.ColumnsToJson(WR: TResultsWriter);
1331
1375
raw := blob.GetString;
1332
1376
WR.WrBase64(pointer(raw), length(raw), { withmagic=} true); // withMagic=true
1333
1377
end ;
1334
- { $ifend}
1335
1378
1336
1379
begin
1337
- // take care of the layout of internal ZDBC buffers for each provider
1338
- { $if defined(ZEOS73UP) and defined(MORMOT2)}
1339
- fResultSet.ColumnsToJson(WR, fJSONComposeOptions);
1340
- { $else}
1380
+ // manual care of the layout of internal ZDBC buffers for each provider
1341
1381
if WR.Expand then
1342
1382
WR.Add(' {' );
1343
1383
for col := 0 to fColumnCount - 1 do
@@ -1409,17 +1449,17 @@ procedure TSqlDBZeosStatement.ColumnsToJson(WR: TResultsWriter);
1409
1449
else
1410
1450
raise ESqlDBException.CreateUtf8(
1411
1451
' %.ColumnsToJson: invalid ColumnType(#% "%")=%' ,
1412
- [self, col, fColumns [col].ColumnName , ord(fColumns[col].ColumnType)]);
1452
+ [self, col, WR.ColNames [col], ord(fColumns[col].ColumnType)]);
1413
1453
end ;
1414
1454
end ;
1415
1455
WR.AddComma;
1416
1456
end ;
1417
1457
WR.CancelLastComma; // cancel last ','
1418
1458
if WR.Expand then
1419
1459
WR.Add(' }' );
1420
- { $ifend}
1421
1460
end ;
1422
1461
1462
+ { $endif ZDBC_COLUMNSTOJSON}
1423
1463
{ $endif ZEOS72UP}
1424
1464
1425
1465
initialization
0 commit comments