uses
mormot.core.base,
mormot.core.data,
mormot.core.text,
mormot.core.json,
mormot.core.rtti,
mormot.core.os;
type
TDBSettingDataBase =
class(TObject)
private
FDBServerName:
String;
FDBName:
String;
FDBUser:
String;
FDBUserPw: Integer;
FDBProvider: Integer;
protected
class procedure ReadDataBaseJson(
var pmvContext: TJsonParserContext; pmValue: TObject);
class procedure WriteDataBaseJson(pmWriter: TJsonWriter; pmValue: TObject; pmOptions: TTextWriterWriteObjectOptions);
public
property DBServerName:
String
read FDBServerName
write FDBServerName;
property DBName:
String
read FDBName
write FDBName;
property DBUser:
String
read FDBUser
write FDBUser;
property DBUserPw: Integer
read FDBUserPw
write FDBUserPw;
property DBProvider: Integer
read FDBProvider
write FDBProvider;
end;
TDBSettings =
class(TSynAutoCreateFields)
private
FDataBase: TDBSettingDataBase;
FShorthandSymbol: TStringDynArray;
public
function LoadFromFile(
const pmcFileName: TFileName): Boolean;
procedure SaveToFile(
const pmcFileName: TFileName);
published
property DataBase: TDBSettingDataBase
read FDataBase;
property ShorthandSymbol: TStringDynArray
read FShorthandSymbol;
end;
class procedure TDBSettingDataBase.ReadDataBaseJson(
var pmvContext: TJsonParserContext; pmValue: TObject);
var
values:
array[0..4]
of TValuePUtf8Char;
begin
if pmvContext.ParseObject(['
DB-ServerName', '
DB-Name', '
DB-User', '
DB-User-Pw', '
DB-Provider'], @values)
then
begin
with TDBSettingDataBase(pmValue)
do
begin
DBServerName := values[0].ToString;
DBName := values[1].ToString;
DBUser := values[2].ToString;
DBUserPw := values[3].ToInteger;
DBProvider := values[4].ToInteger;
end;
end;
end;
class procedure TDBSettingDataBase.WriteDataBaseJson(pmWriter: TJsonWriter; pmValue: TObject; pmOptions: TTextWriterWriteObjectOptions);
begin
with TDBSettingDataBase(pmValue)
do
pmWriter.AddJsonEscape(['
DB-ServerName', DBServerName, '
DB-Name', DBName, '
DB-User', DBUser, '
DB-User-Pw', DBUserPw, '
DB-Provider', DBProvider]);
end;
function TDBSettings.LoadFromFile(
const pmcFileName: TFileName): Boolean;
begin
Result := ObjectLoadJson(Self, StringFromFile(pmcFileName));
end;
procedure TDBSettings.SaveToFile(
const pmcFileName: TFileName);
begin
FileFromString(ObjectToJson(Self), pmcFileName);
end;
initialization
TRttiJson.RegisterCustomSerializerClass(TDBSettingDataBase, TDBSettingDataBase.ReadDataBaseJson, TDBSettingDataBase.WriteDataBaseJson);