Skip to content

Commit 8fb8ac4

Browse files
author
Arnaud Bouchez
committed
several Zeos/ZDBC fixes
- try to circumvent https://synopse.info/forum/viewtopic.php?id=6353 - code reformatting and conditionals leveraging
1 parent 34fb0e2 commit 8fb8ac4

File tree

2 files changed

+77
-37
lines changed

2 files changed

+77
-37
lines changed

src/db/mormot.db.sql.zeos.pas

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ implementation // compile a void unit if NOSYNDBZEOS conditional is set
2323

2424
{$I Zeos.inc} // define conditionals like ZEOS72UP and ENABLE_*
2525

26+
{$I ..\mormot.defines.inc}
27+
2628
// for best performance: tune your project options or Zeos.inc
2729
// by defining MORMOT2 and leverage best of mORMot and ZEOS !
2830

29-
{$I ..\mormot.defines.inc}
31+
{$if defined(ZEOS73UP) and defined(MORMOT2)}
32+
// use direct IZResultSet.ColumnsToJson() export
33+
{$define ZDBC_COLUMNSTOJSON}
34+
{$ifend}
3035

3136
uses
3237
types,
@@ -251,15 +256,15 @@ TSqlDBZeosConnection = class(TSqlDBConnectionThreadSafe)
251256
TSqlDBZeosStatement = class(TSqlDBStatementWithParamsAndColumns)
252257
protected
253258
fStatement: IZPreparedStatement;
254-
fResultSet: IZResultSet;
259+
fResultSet: IZResultSet;
255260
fResultInfo: IZResultSetMetaData;
256-
{$if defined(ZEOS73UP) and defined(MORMOT2)}
261+
{$ifdef ZDBC_COLUMNSTOJSON}
257262
fJSONComposeOptions: TZJSONComposeOptions;
258-
{$ifend}
263+
{$endif ZDBC_COLUMNSTOJSON}
259264
public
260-
{$if defined(ZEOS73UP) and defined(MORMOT2)}
265+
{$ifdef ZDBC_COLUMNSTOJSON}
261266
procedure AfterConstruction; override;
262-
{$ifend}
267+
{$endif ZDBC_COLUMNSTOJSON}
263268
/// Prepare an UTF-8 encoded SQL statement
264269
// - parameters marked as ? will be bound later, before ExecutePrepared call
265270
// - if ExpectResults is TRUE, then Step() and Column*() methods are available
@@ -312,7 +317,7 @@ TSqlDBZeosStatement = class(TSqlDBStatementWithParamsAndColumns)
312317
function ColumnUtf8(Col: integer): RawUtf8; override;
313318
/// return a Column as a blob value of the current Row, first Col is 0
314319
function ColumnBlob(Col: integer): RawByteString; override;
315-
{$if defined(ZEOS73UP) and defined(MORMOT2)}
320+
{$ifdef ZDBC_COLUMNSTOJSON}
316321
public
317322
/// the ColumnsToJson options provided by ZDBC
318323
// - jcoEndJsonObject:
@@ -337,7 +342,7 @@ TSqlDBZeosStatement = class(TSqlDBStatementWithParamsAndColumns)
337342
property JSONComposeOptions: TZJSONComposeOptions
338343
read fJSONComposeOptions write fJSONComposeOptions
339344
default [jcoEndJsonObject];
340-
{$ifend}
345+
{$endif ZDBC_COLUMNSTOJSON}
341346
end;
342347

343348
var
@@ -711,11 +716,22 @@ procedure TSqlDBZeosConnectionProperties.GetFields(const aTableName: RawUtf8;
711716
F.ColumnTypeNative := SynUnicodeToUtf8(
712717
res.GetUnicodeString(TableColColumnTypeNameIndex));
713718
{$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);
719735
FA.Add(F);
720736
end;
721737
if n > 0 then
@@ -742,20 +758,40 @@ function TSqlDBZeosConnectionProperties.TZSQLTypeToTSqlDBFieldType(
742758
aNativeType: TZSQLType): TSqlDBFieldType;
743759
begin
744760
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}:
747772
result := ftInt64;
748-
stFloat, stDouble:
773+
stFloat,
774+
stDouble:
749775
result := ftDouble;
750776
stBigDecimal
751-
{$ifdef ZEOS72UP}, stCurrency {$endif ZEOS72UP}:
777+
{$ifdef ZEOS72UP},
778+
stCurrency
779+
{$endif ZEOS72UP}:
752780
result := ftCurrency;
753-
stDate, stTime, stTimestamp:
781+
stDate,
782+
stTime,
783+
stTimestamp:
754784
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:
757792
result := ftUtf8;
758-
stBytes, stBinaryStream:
793+
stBytes,
794+
stBinaryStream:
759795
result := ftBlob;
760796
else
761797
raise ESqlDBZeos.CreateUtf8('%: unexpected TZSQLType "%"', [self,
@@ -775,9 +811,9 @@ class function TSqlDBZeosConnectionProperties.URI(aServer: TSqlDBDefinition;
775811
aLibraryLocationAppendExePath);
776812
end;
777813

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;
781817
begin
782818
// return e.g. mysql://192.168.2.60:3306/world?username=root;password=dev
783819
result := TrimU(aProtocol);
@@ -792,7 +828,6 @@ class function TSqlDBZeosConnectionProperties.URI(const aProtocol, aServerName:
792828
result := result + ';LibLocation='
793829
else
794830
result := result + '?LibLocation=';
795-
796831
if aLibraryLocationAppendExePath then
797832
result := result + StringToUtf8(Executable.ProgramFilePath);
798833
result := result + StringToUtf8(aLibraryLocation);
@@ -842,6 +877,7 @@ procedure TSqlDBZeosConnection.Connect;
842877
end;
843878
end;
844879
end;
880+
845881
procedure TSqlDBZeosConnection.Disconnect;
846882
begin
847883
try
@@ -878,6 +914,7 @@ procedure TSqlDBZeosConnection.StartTransaction;
878914
fDatabase.SetAutoCommit(false);
879915
{$endif ZEOS73UP}
880916
end;
917+
881918
procedure TSqlDBZeosConnection.Commit;
882919
begin
883920
inherited Commit;
@@ -1215,13 +1252,13 @@ function TSqlDBZeosStatement.Step(SeekFirst: boolean): boolean;
12151252
end;
12161253
end;
12171254

1218-
{$if defined(ZEOS73UP) and defined(MORMOT2)}
1255+
{$ifdef ZDBC_COLUMNSTOJSON}
12191256
procedure TSqlDBZeosStatement.AfterConstruction;
12201257
begin
12211258
inherited;
12221259
fJSONComposeOptions := [jcoEndJsonObject];
12231260
end;
1224-
{$ifend}
1261+
{$endif ZDBC_COLUMNSTOJSON}
12251262

12261263
function TSqlDBZeosStatement.ColumnBlob(Col: integer): RawByteString;
12271264
var
@@ -1313,16 +1350,23 @@ function TSqlDBZeosStatement.UpdateCount: integer;
13131350
end;
13141351

13151352
{$ifdef ZEOS72UP}
1353+
{$ifdef ZDBC_COLUMNSTOJSON}
13161354

13171355
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;
13181360

1319-
{$if not (defined(ZEOS73UP) and defined(MORMOT2))}
1361+
{$else}
1362+
1363+
procedure TSqlDBZeosStatement.ColumnsToJson(WR: TResultsWriter);
13201364
var
13211365
col: integer;
13221366
P: PAnsiChar;
13231367
Len: NativeUInt; // required by Zeos for GetPAnsiChar out param (not PtrUInt)
13241368

1325-
procedure WriteIZBlob;
1369+
procedure WriteIZBlob; // sub-function to avoid local stack temp variables
13261370
var
13271371
blob: IZBlob;
13281372
raw: RawByteString;
@@ -1331,13 +1375,9 @@ procedure TSqlDBZeosStatement.ColumnsToJson(WR: TResultsWriter);
13311375
raw := blob.GetString;
13321376
WR.WrBase64(pointer(raw), length(raw), {withmagic=}true); // withMagic=true
13331377
end;
1334-
{$ifend}
13351378

13361379
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
13411381
if WR.Expand then
13421382
WR.Add('{');
13431383
for col := 0 to fColumnCount - 1 do
@@ -1409,17 +1449,17 @@ procedure TSqlDBZeosStatement.ColumnsToJson(WR: TResultsWriter);
14091449
else
14101450
raise ESqlDBException.CreateUtf8(
14111451
'%.ColumnsToJson: invalid ColumnType(#% "%")=%',
1412-
[self, col, fColumns[col].ColumnName, ord(fColumns[col].ColumnType)]);
1452+
[self, col, WR.ColNames[col], ord(fColumns[col].ColumnType)]);
14131453
end;
14141454
end;
14151455
WR.AddComma;
14161456
end;
14171457
WR.CancelLastComma; // cancel last ','
14181458
if WR.Expand then
14191459
WR.Add('}');
1420-
{$ifend}
14211460
end;
14221461

1462+
{$endif ZDBC_COLUMNSTOJSON}
14231463
{$endif ZEOS72UP}
14241464

14251465
initialization

src/mormot.commit.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
'2.0.4092'
1+
'2.0.4093'

0 commit comments

Comments
 (0)