|
| 1 | +unit DataSetConverter4D.Util; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | + System.SysUtils, |
| 7 | + System.DateUtils, |
| 8 | + System.JSON, |
| 9 | + Data.DB, |
| 10 | + DataSetToJSON; |
| 11 | + |
| 12 | +function DateTimeToISOTimeStamp(const dateTime: TDateTime): string; |
| 13 | +function DateToISODate(const date: TDateTime): string; |
| 14 | +function TimeToISOTime(const time: TTime): string; |
| 15 | + |
| 16 | +function ISOTimeStampToDateTime(const dateTime: string): TDateTime; |
| 17 | +function ISODateToDate(const date: string): TDate; |
| 18 | +function ISOTimeToTime(const time: string): TTime; |
| 19 | + |
| 20 | +function NewDataSetField(dataSet: TDataSet; const fieldType: TFieldType; const fieldName: string; |
| 21 | + const size: Integer = 0; const origin: string = ''): TField; |
| 22 | + |
| 23 | +function BooleanToJSON(const value: Boolean): TJSONValue; |
| 24 | +function BooleanFieldToType(const booleanField: TBooleanField): TBooleanFieldType; |
| 25 | +function DataSetFieldToType(const dataSetField: TDataSetField): TDataSetFieldType; |
| 26 | + |
| 27 | +implementation |
| 28 | + |
| 29 | +function DateTimeToISOTimeStamp(const dateTime: TDateTime): string; |
| 30 | +var |
| 31 | + fs: TFormatSettings; |
| 32 | +begin |
| 33 | + fs.TimeSeparator := ':'; |
| 34 | + Result := FormatDateTime('yyyy-mm-dd hh:nn:ss', dateTime, fs); |
| 35 | +end; |
| 36 | + |
| 37 | +function DateToISODate(const date: TDateTime): string; |
| 38 | +begin |
| 39 | + Result := FormatDateTime('YYYY-MM-DD', date); |
| 40 | +end; |
| 41 | + |
| 42 | +function TimeToISOTime(const time: TTime): string; |
| 43 | +var |
| 44 | + fs: TFormatSettings; |
| 45 | +begin |
| 46 | + fs.TimeSeparator := ':'; |
| 47 | + Result := FormatDateTime('hh:nn:ss', time, fs); |
| 48 | +end; |
| 49 | + |
| 50 | +function ISOTimeStampToDateTime(const dateTime: string): TDateTime; |
| 51 | +begin |
| 52 | + Result := EncodeDateTime(StrToInt(Copy(dateTime, 1, 4)), StrToInt(Copy(dateTime, 6, 2)), StrToInt(Copy(dateTime, 9, 2)), |
| 53 | + StrToInt(Copy(dateTime, 12, 2)), StrToInt(Copy(dateTime, 15, 2)), StrToInt(Copy(dateTime, 18, 2)), 0); |
| 54 | +end; |
| 55 | + |
| 56 | +function ISODateToDate(const date: string): TDate; |
| 57 | +begin |
| 58 | + Result := EncodeDate(StrToInt(Copy(date, 1, 4)), StrToInt(Copy(date, 6, 2)), StrToInt(Copy(date, 9, 2))); |
| 59 | +end; |
| 60 | + |
| 61 | +function ISOTimeToTime(const time: string): TTime; |
| 62 | +begin |
| 63 | + Result := EncodeTime(StrToInt(Copy(time, 1, 2)), StrToInt(Copy(time, 4, 2)), StrToInt(Copy(time, 7, 2)), 0); |
| 64 | +end; |
| 65 | + |
| 66 | +function NewDataSetField(dataSet: TDataSet; const fieldType: TFieldType; const fieldName: string; |
| 67 | + const size: Integer = 0; const origin: string = ''): TField; |
| 68 | +begin |
| 69 | + Result := DefaultFieldClasses[fieldType].Create(dataSet); |
| 70 | + Result.FieldName := fieldName; |
| 71 | + |
| 72 | + if (Result.FieldName = '') then |
| 73 | + Result.FieldName := 'Field' + IntToStr(dataSet.FieldCount + 1); |
| 74 | + |
| 75 | + Result.FieldKind := fkData; |
| 76 | + Result.DataSet := dataSet; |
| 77 | + Result.Name := dataSet.Name + Result.FieldName; |
| 78 | + Result.Size := size; |
| 79 | + Result.Origin := origin; |
| 80 | + |
| 81 | + if (fieldType in [ftString, ftWideString]) and (size <= 0) then |
| 82 | + raise EDataSetToJSONException.CreateFmt('Size not defined for field "%s".', [fieldName]); |
| 83 | +end; |
| 84 | + |
| 85 | +function BooleanToJSON(const value: Boolean): TJSONValue; |
| 86 | +begin |
| 87 | + if value then |
| 88 | + Result := TJSONTrue.Create |
| 89 | + else |
| 90 | + Result := TJSONFalse.Create; |
| 91 | +end; |
| 92 | + |
| 93 | +function BooleanFieldToType(const booleanField: TBooleanField): TBooleanFieldType; |
| 94 | +const |
| 95 | + DESC_BOOLEAN_FIELD_TYPE: array [TBooleanFieldType] of string = ('Unknown', 'Boolean', 'Integer'); |
| 96 | +var |
| 97 | + index: Integer; |
| 98 | + origin: string; |
| 99 | +begin |
| 100 | + Result := bfUnknown; |
| 101 | + origin := Trim(booleanField.Origin); |
| 102 | + for index := Ord(Low(TBooleanFieldType)) to Ord(High(TBooleanFieldType)) do |
| 103 | + if (LowerCase(DESC_BOOLEAN_FIELD_TYPE[TBooleanFieldType(index)]) = LowerCase(origin)) then |
| 104 | + Exit(TBooleanFieldType(index)); |
| 105 | +end; |
| 106 | + |
| 107 | +function DataSetFieldToType(const dataSetField: TDataSetField): TDataSetFieldType; |
| 108 | +const |
| 109 | + DESC_DATASET_FIELD_TYPE: array [TDataSetFieldType] of string = ('Unknown', 'JSONObject', 'JSONArray'); |
| 110 | +var |
| 111 | + index: Integer; |
| 112 | + origin: string; |
| 113 | +begin |
| 114 | + Result := dfUnknown; |
| 115 | + origin := Trim(dataSetField.Origin); |
| 116 | + for index := Ord(Low(TDataSetFieldType)) to Ord(High(TDataSetFieldType)) do |
| 117 | + if (LowerCase(DESC_DATASET_FIELD_TYPE[TDataSetFieldType(index)]) = LowerCase(origin)) then |
| 118 | + Exit(TDataSetFieldType(index)); |
| 119 | +end; |
| 120 | + |
| 121 | +end. |
0 commit comments