delphi-orm是否支持枚举类型或假设支持,如何使用枚举类型?
在“ Helloworld”示例中,我添加了一个简单的TGender类型:
type
TGender = (gtMale, gtFemale);
[Entity('CUSTOMERS')]
TCustomer = class
private
FName: String;
FEMail: String;
FID: Integer;
FCreatedAt: TDate;
FADDRESS: String;
FGender: TGender;
procedure SetADDRESS(const Value: String);
procedure SetCreatedAt(const Value: TDate);
procedure SetEMail(const Value: String);
procedure SetGender(const Value: TGender);
procedure SetID(const Value: Integer);
procedure SetName(const Value: String);
public
property ID: Integer read FID write SetID;
property Name: String read FName write SetName;
property Address: String read FADDRESS write SetADDRESS;
property EMail: String read FEMail write SetEMail;
[Column('CREATED_AT')]
property CreatedAt: TDate read FCreatedAt write SetCreatedAt;
property Gender: TGender read FGender write SetGender;
end;
简单插入:
procedure SimpleInsert;
var
dormSession: TSession;
Customer: TCustomer;
begin
dormSession := TSession.CreateConfigured(
TStringReader.Create(Form1.Memo1.Lines.Text), TdormEnvironment.deDevelopment);
try
Customer := TCustomer.Create;
Customer.Name := 'Daniele Teti Inc.';
Customer.Address := 'Via Roma, 16';
Customer.EMail := 'daniele@danieleteti.it';
Customer.CreatedAt := Now;
Customer.Gender := gtMale;
dormSession.Insert(Customer);
Customer.Free;
finally
dormSession.Free;
end;
end;
结果是GENDER为空:
(性别定义为INTEGER)
加载记录时,宿舍报告错误:
“性别的未知字段类型”
答案 0 :(得分:0)
浏览源似乎没有。 (至少对于SQLite适配器而言。)
无论如何,该部分的源代码非常容易理解和修改。
以下是我为添加枚举支持所做的更改:
在dorm.utils
中:
function TdormUtils.GetFieldType
...
else if (_PropInfo.Kind = tkEnumeration) and (_PropInfo.Name = 'Boolean') then
Result := 'boolean'
else if _PropInfo.Kind = tkEnumeration then
Result := 'integer'
...
在dorm.adapter.Sqlite3
中:
procedure TSqlite3PersistStrategy.LoadObjectFromSqliteTable(...);
var
PropTypeInfo: PTypeInfo;
...
begin
...
else if CompareText(field.FieldType, 'integer') = 0 then
begin
if (Field.RTTICache.RTTIProp <> nil) and
(Field.RTTICache.RTTIProp.PropertyType.TypeKind = tkEnumeration) then
begin
PropTypeInfo := Field.RTTICache.RTTIProp.PropertyType.Handle;
v := TValue.FromOrdinal(PropTypeInfo,
AReader.FieldAsInteger(AReader.FieldIndex[field.FieldName]));
end
else
v := AReader.FieldAsInteger(AReader.FieldIndex[field.FieldName]);
S := field.FieldName + ' as integer';
end
...
end
procedure TSqlite3PersistStrategy.SetSqlite3ParameterValue(...);
begin
...
else if CompareText(aFieldType, 'integer') = 0 then
begin
if aValue.Kind = tkEnumeration then
begin
ADB.AddParamInt(aParameterName, aValue.AsOrdinal);
GetLogger.Debug(aParameterName + ' = ' + IntToStr(aValue.AsOrdinal));
end
else
begin
ADB.AddParamInt(aParameterName, aValue.AsInteger);
GetLogger.Debug(aParameterName + ' = ' + IntToStr(aValue.AsInteger));
end;
end
...
end
在数据库中,该字段应声明为INTEGER。