Created
September 3, 2018 14:39
-
-
Save ComingNine/d9ff5b0a55213b71744ebf5b0c289d18 to your computer and use it in GitHub Desktop.
Float in a variant field of TSQLRecord becomes string when got back from DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program TestFloatInVariant; | |
{$APPTYPE CONSOLE} | |
uses | |
FastMM4, | |
SysUtils, | |
Variants, | |
SynCommons, | |
SynLog, | |
SynTests, | |
SynSQLite3, | |
SynSQLite3Static, | |
mORMot, | |
mORMotSQLite3; | |
type | |
{$M+} | |
TSQLSpeed = class(TSQLRecord) | |
private | |
FPlainObj: Variant; | |
public | |
constructor Create; override; | |
published | |
property PlainObj: Variant read FPlainObj write FPlainObj; | |
end; | |
TSpeed = class | |
private | |
FVal: Double; | |
published | |
property Val: Double read FVal write FVal; | |
end; | |
constructor TSQLSpeed.Create; | |
begin | |
inherited Create; | |
TDocVariant.New(FPlainObj); | |
end; | |
var | |
Model: TSQLModel; | |
Rest: TSQLRest; | |
SQLiteDBPath: string; | |
SqlS: TSQLSpeed; | |
S: TSpeed; | |
ID: TID; | |
begin | |
SQLiteDBPath := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + SQLiteDBName; | |
Model := TSQLModel.Create([TSQLSpeed]); | |
Rest := TSQLRestClientDB.Create(Model, nil, SQLiteDBPath, TSQLRestServerDB, False); | |
TSQLRestClientDB(Rest).Server.DB.Synchronous := smFull; | |
TSQLRestClientDB(Rest).Server.DB.LockingMode := lmExclusive; | |
TSQLRestClientDB(Rest).Server.CreateMissingTables; | |
S := TSpeed.Create; | |
S.Val := 1234.567890; | |
SqlS := TSQLSpeed.Create; | |
Writeln(VarType(SqlS.PlainObj)); // 271 | |
SqlS.PlainObj := ObjectToJSON(S); | |
Writeln(VarType(SqlS.PlainObj)); // 256 | |
Writeln(SqlS.PlainObj); // {"Val":1234.56789} | |
Writeln; | |
ID := Rest.Add(SqlS, True); | |
SqlS.Free; | |
S.Free; | |
SqlS := TSQLSpeed.Create(Rest, ID); | |
Writeln(VarType(SqlS.PlainObj)); // 271 | |
Writeln(SqlS.PlainObj); // {"Val":"1234.56789"} | |
Writeln; | |
S := TSpeed.Create; | |
ObjectLoadJSON(S, VariantToUTF8(SqlS.PlainObj)); | |
Writeln(S.Val); // 0.00000000000000E+0000 | |
Rest.Free; | |
Model.Free; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment