From fea6b0c273c8cce85e3c5ae30b63df0326e1638f Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Tue, 19 Mar 2024 16:05:17 +0100 Subject: [PATCH] Added support for TSQLTimeStampOffset in dataset derialization and deserialization --- samples/activerecord_showcase/EntitiesU.pas | 3 +++ samples/activerecord_showcase/MainFormU.dfm | 1 - samples/activerecord_showcase/MainFormU.pas | 6 ++++++ samples/renders/WebModuleU.pas | 5 ++--- sources/MVCFramework.Serializer.JsonDataObjects.pas | 9 ++++++++- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/samples/activerecord_showcase/EntitiesU.pas b/samples/activerecord_showcase/EntitiesU.pas index a4d767dd..aad33c65 100644 --- a/samples/activerecord_showcase/EntitiesU.pas +++ b/samples/activerecord_showcase/EntitiesU.pas @@ -129,6 +129,8 @@ type fCompanyName: NullableString; [MVCTableField('city')] fCity: string; + [MVCTableField('last_contact_timestamp')] + fLastContact: NullableTDateTime; [MVCTableField('rating')] fRating: NullableInt32; [MVCTableField('note')] @@ -141,6 +143,7 @@ type property Code: NullableString read fCode write fCode; property CompanyName: NullableString read fCompanyName write fCompanyName; property City: string read fCity write fCity; + property LastContact: NullableTDateTime read fLastContact write fLastContact; property Rating: NullableInt32 read fRating write fRating; property Note: string read fNote write fNote; end; diff --git a/samples/activerecord_showcase/MainFormU.dfm b/samples/activerecord_showcase/MainFormU.dfm index d4e738a0..a5054ed1 100644 --- a/samples/activerecord_showcase/MainFormU.dfm +++ b/samples/activerecord_showcase/MainFormU.dfm @@ -55,7 +55,6 @@ object MainForm: TMainForm TabOrder = 2 WantReturns = False WordWrap = False - ExplicitHeight = 610 end object btnRelations: TButton Left = 8 diff --git a/samples/activerecord_showcase/MainFormU.pas b/samples/activerecord_showcase/MainFormU.pas index ffa7561d..95c372b3 100644 --- a/samples/activerecord_showcase/MainFormU.pas +++ b/samples/activerecord_showcase/MainFormU.pas @@ -238,6 +238,7 @@ begin lCustomer.CompanyName := 'Google Inc.'; lCustomer.City := 'Montain View, CA'; lCustomer.Note := 'Μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος οὐλομένην 😁'; + lCustomer.LastContact := Now(); lCustomer.Insert; lID := lCustomer.ID; Log('Just inserted Customer ' + lID.ToString); @@ -250,6 +251,7 @@ begin Assert(not lCustomer.Code.HasValue); lCustomer.Code.Value := '5678'; lCustomer.Note := lCustomer.Note + sLineBreak + 'Code changed to 5678 🙂'; + lCustomer.LastContact.Clear; lTestNote := lCustomer.Note; lCustomer.Update; Log('Just updated Customer ' + lID.ToString); @@ -261,6 +263,8 @@ begin try lCustomer.LoadByPK(lID); lCustomer.Code.Value := '😉9012🙂'; + Assert(lCustomer.LastContact.IsNull); + lCustomer.LastContact := Now(); lCustomer.Update; finally lCustomer.Free; @@ -271,6 +275,8 @@ begin lCustomer.LoadByPK(lID); Assert(lCustomer.Code.Value = '😉9012🙂'); Assert(lCustomer.Note = lTestNote); + Assert(lCustomer.LastContact.HasValue); + lCustomer.LastContact := Now(); lCustomer.Update; finally lCustomer.Free; diff --git a/samples/renders/WebModuleU.pas b/samples/renders/WebModuleU.pas index f218dcdd..2137c0fb 100644 --- a/samples/renders/WebModuleU.pas +++ b/samples/renders/WebModuleU.pas @@ -109,12 +109,11 @@ begin // calling http://localhost:8080/customserializationtype DMVC - .Serializers - .Items[TMVCMediaType.APPLICATION_JSON] + .Serializer(TMVCMediaType.APPLICATION_JSON) .RegisterTypeSerializer(TypeInfo(TNullableRecordAlias), TNullableAliasSerializer.Create); // This line registers custom serializers for TBitmap, TPngImage (Only MSWindows) and TJPEGImage (Only MSWindows) - RegisterOptionalCustomTypesSerializersForJSON(DMVC.Serializers); + RegisterOptionalCustomTypesSerializers(DMVC.Serializer(TMVCMediaType.APPLICATION_JSON)); end; end. diff --git a/sources/MVCFramework.Serializer.JsonDataObjects.pas b/sources/MVCFramework.Serializer.JsonDataObjects.pas index 34a6e563..f85b9eb6 100644 --- a/sources/MVCFramework.Serializer.JsonDataObjects.pas +++ b/sources/MVCFramework.Serializer.JsonDataObjects.pas @@ -1014,6 +1014,10 @@ begin AJSONObject.S[lFName] := DateTimeToISOTimeStamp (SQLTimeStampToDateTime(ADataSet.Fields[lField.I].AsSQLTimeStamp)); + ftTimeStampOffset: + AJSONObject.S[lFName] := DateTimeToISOTimeStamp + (SQLTimeStampOffsetToDateTime(ADataSet.Fields[lField.I].AsSQLTimeStampOffset)); + ftCurrency: AJSONObject.F[lFName] := ADataSet.Fields[lField.I].AsCurrency; @@ -1069,7 +1073,7 @@ begin end; end; else - raise EMVCSerializationException.CreateFmt('Cannot find type for field "%s"', [lField.FieldName]); + raise EMVCSerializationException.CreateFmt('Cannot find type for field "%s" - TFieldType = %s', [lField.FieldName, GetEnumName(TypeInfo(TFieldType), Ord(lField.DataType))]); end; end; end; @@ -2246,6 +2250,9 @@ begin TFieldType.ftDateTime, TFieldType.ftTimeStamp: Field.AsDateTime := ISOTimeStampToDateTime(AJSONObject.S[lName]); + TFieldType.ftTimeStampOffset: + Field.AsSQLTimeStampOffset := StrToSQLTimeStampOffset(AJSONObject.S[lName]); + TFieldType.ftTime: Field.AsDateTime := ISOTimeToTime(AJSONObject.S[lName]);