贴一些主要的代码
这是核心代码
unit DBGW;
interface
uses
Windows, Messages, SysUtils, Classes, DB, DBTables, Forms, Contnrs, ADODB, Entity,
EtyList, EntityFactory, UDF, XMLDoc, XMLIntf, DBClient;
const
//stored proc
LOAD_TYPE_TABLE = 'Table';
LOAD_TYPE_STOREDPROC = 'StoredProc';
MAX_DB_CONNECTION = 10;
//field type
ID_FIELD_TYPE_STRING = 'string';
ID_AND = ' AND ';
AND_LENGTH = 5;
type
TDB = class
public
strAliasName : String;
strDatabaseName : String;
strDriverName : String;
bKeepConnection : Boolean;
end;
type
//-----------------------------------------------------------------------------
// Class TdmDBGW
//-----------------------------------------------------------------------------
TdmDBGW = class(TDataModule)
m_db: TDatabase;
procedure DataModuleCreate(Sender: TObject);
procedure m_dbBeforeConnect(Sender: TObject);
procedure m_dbAfterConnect(Sender: TObject);
private
m_strEntityMapPath : string;
m_EntityFactory : IEntityFactory;
m_listAllEntityMapField : TEtyList;
m_StackConnections : TStack;
procedure Init();
procedure InitConnectionPool;
function GetConnection() : TQuery;
procedure ReleaseConnection(connection : TQuery);
protected
function LoadEntityByTable(pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
function LoadEntityByStoredProc(pety : PIEntity; const listCondition : TEtyList = nil) : Boolean; virtual;
function GetAllEntityMapField() : TEtyList;
function GetLoadType(const strEntityName : String) : String;
function GetEntityMapField(const strEntityName : String = '') : TEtyList;
function GetTableName(const strEntityName : String) : String;
function GetTableFieldName(const strTableName : String; const strEntityFieldName : String) : String;
function GetTableFieldType(const strTableName : String; const strFieldName : String) : String;
function FormatValueAppToDB(const varFieldValue : Variant; const strFieldType : String) : String;
function FormatValueDBToApp(const varFieldValue : Variant; const strFieldType : String) : Variant;
function GetCondition(const strEntityName : String; const listCondition : TEtyList) : String;
function GetRule(const strEntityName : String; const listRule : TEtyList) : String;
function GetPKCondition(const pety : PIEntity) : String;
public
procedure SetEntityFactory(const factory : IEntityFactory);
function LoadEntity(pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
function LoadEntityList(list : TEtyList; const listCondition : TEtyList = nil; const listRule : TEtyList = nil) : Boolean;
function InsertEntity(const pety : PIEntity) : Boolean;
function UpdateEntity(const pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
function DeleteEntity(const pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
// procedure BeginTrans;
procedure Refresh(etyList : TEtyList);
end;
var
dmDBGW: TdmDBGW;
implementation
uses Variants, EtyEntityMapField, EtyCondition;
{$R *.dfm}
//-----------------------------------------------------------------------------
// Init
//-----------------------------------------------------------------------------
procedure TdmDBGW.Init();
begin
m_strEntityMapPath := GetAppPath() + ID_ENTITY_MAP_PATH + '/';
m_listAllEntityMapField := GetAllEntityMapField;
end;
//-----------------------------------------------------------------------------
// SetEntityFactory
//-----------------------------------------------------------------------------
procedure TdmDBGW.SetEntityFactory(const factory : IEntityFactory);
begin
m_EntityFactory := factory;
end;
//-----------------------------------------------------------------------------
// LoadEntity
//-----------------------------------------------------------------------------
function TdmDBGW.LoadEntity(pety : PIEntity; const listCondition : TEtyList) : Boolean;
var
strLoadType : String;
begin
strLoadType := GetLoadType(pety^.EntityName);
strLoadType := LowerCase(strLoadType);
if strLoadType = LowerCase(LOAD_TYPE_TABLE) then
Result := LoadEntityByTable(pety, listCondition)
else if strLoadType = LowerCase(LOAD_TYPE_STOREDPROC) then
Result := LoadEntityByStoredProc(pety, listCondition)
else
Result := LoadEntityByTable(pety, listCondition);
end;
//-----------------------------------------------------------------------------
// LoadEntityByTable
//-----------------------------------------------------------------------------
function TdmDBGW.LoadEntityByTable(pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
var
strTableName : String;
listEntityMapField : TEtyList;
etyEntityMapField : IEntity;
strSQL, strSQLFields, strSQLWhere : String;
i : Integer;
nColumnCounts : Integer;
connection : TQuery;
strFieldType : String;
strFieldName : String;
varFieldValue : Variant;
begin
connection := GetConnection;
try
//get table name
strTableName := GetTableName(pety.GetEntityName);
if strTableName = '' then
begin
Result := false;
exit;
end;
//get table field
listEntityMapField := GetEntityMapField(pety.GetEntityName);
//set SQL's title
strSQLFields := '';
for i := 0 to listEntityMapField.GetEntityCount - 1 do
begin
etyEntityMapField := listEntityMapField.GetEntity(i);
strTableName := etyEntityMapField.GetAttributeValue(ID_TABLE_NAME);
strFieldName := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_NAME);
strSQLFields := strSQLFields + '[' + strTableName + '].[' + strFieldName + '], ';
end;
//get rid of the last ',' in strSQLFields
strSQLFields := Copy(strSQLFields, 0, Length(strSQLFields) - 2);
//get where
if listCondition <> nil then
strSQLWhere := GetCondition(pety.EntityName, listCondition)
else //if listCondition = nil then only load this pety
strSQLWhere := GetPKCondition(pety);
//it must have strSQLWhere
if strSQLWhere = '' then
begin
result := false;
exit;
end;
//set the sql
strSQL := 'SELECT ' + strSQLFields + ' FROM ' + strTableName + ' WHERE ' + strSQLWhere;
connection.Close;
connection.SQL.Clear;
connection.SQL.Text := strSQL;
connection.Open;
//the result must only be one record
if connection.RecordCount <> 1 then
begin
Result := false;
exit;
end;
//save the data to entity
nColumnCounts := connection.FieldCount;
if nColumnCounts = 0 then
begin
Result := false;
exit
end;
//save data to pety
for i := 0 to nColumnCounts - 1 do
begin
strFieldName := connection.Fields.FieldName;
strFieldType := GetTableFieldType(strTableName, strFieldName);
varFieldValue := FormatValueDBToApp(connection.Fields.AsVariant, strFieldType);
if not VarIsNull(varFieldValue) then
pety.SetAttributeValue(strFieldName, varFieldValue);
end;
Result := true;
finally
ReleaseConnection(connection);
end
end;
//-----------------------------------------------------------------------------
// LoadEntityByStoredProc
//-----------------------------------------------------------------------------
function TdmDBGW.LoadEntityByStoredProc(pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
begin
Result := true;
end;
//-----------------------------------------------------------------------------
// LoadEntityList
//-----------------------------------------------------------------------------
function TdmDBGW.LoadEntityList(list : TEtyList; const listCondition : TEtyList = nil; const listRule : TEtyList = nil) : Boolean;
var
connection : TQuery;
strSQL, strSQLFields, strSQLWhere, strSQLRule : String;
listEntityMapField : TEtyList;
etyEntityMapField : IEntity;
i : integer;
strTableName : String;
nColumnCounts : Integer;
strFieldType : String;
strFieldName : String;
varFieldValue : Variant;
ety : IEntity;
begin
connection := GetConnection;
try
list.Clear;
//get table field
listEntityMapField := GetEntityMapField(list.GetEntityName);
strTableName := '';
strSQLFields := '';
strFieldName := '';
for i := 0 to listEntityMapField.GetEntityCount - 1 do
begin
etyEntityMapField := listEntityMapField.GetEntity(i);
strTableName := etyEntityMapField.GetAttributeValue(ID_TABLE_NAME);
strFieldName := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_NAME);
if (strTableName = '') or (strFieldName = '') then
begin
Result := false;
exit;
end;
strSQLFields := strSQLFields + '[' + strTableName + '].[' + strFieldName + '], ';
end;
//get rid of the last ',' in strSQLFields and strSQLValues
strSQLFields := Copy(strSQLFields, 0, Length(strSQLFields) - 2);
//get table name
strTableName := GetTableName(list.GetEntityName);
//get where
if listCondition <> nil then
strSQLWhere := GetCondition(list.GetEntityName, listCondition);
if strSQLWhere = '' then
strSQL := 'SELECT ' + strSQLFields + ' FROM ' + strTableName
else
strSQL := 'SELECT ' + strSQLFields + ' FROM ' + strTableName + ' WHERE ' + strSQLWhere;
//get rule
if listRule <> nil then
strSQLRule := GetRule(list.GetEntityName, listRule);
if strSQLRule <> '' then
strSQL := strSQL + ' ' + strSQLRule;
//set the sql
connection.Close;
connection.SQL.Clear;
connection.SQL.Text := strSQL;
connection.Open;
//save the data to entity
nColumnCounts := connection.FieldCount;
connection.First;
while not connection.Eof do
begin
//这就是动态创建Entity Class,在Control Class定义DBGW并初始化时给m_EntityFactory赋值
//create new pety from EntityFactory
ety := m_EntityFactory.CreateEntity(list.GetEntityName);
for i := 0 to nColumnCounts - 1 do
begin
strFieldName := connection.Fields.FieldName;
strFieldType := GetTableFieldType(strTableName, strFieldName);
varFieldValue := FormatValueDBToApp(connection.Fields.AsVariant, strFieldType);
if not VarIsNull(varFieldValue) then
ety.SetAttributeValue(strFieldName, varFieldValue);
end;
list.AddEntity(ety);
connection.Next;
end;
Result := true;
finally
ReleaseConnection(connection);
end
end;
//-----------------------------------------------------------------------------
// InsertEntity
//-----------------------------------------------------------------------------
function TdmDBGW.InsertEntity(const pety : PIEntity) : Boolean;
var
strTableName : String;
etyEntityMapField : IEntity;
listEntityMapField : TEtyList;
strSQL, strSQLFields, strSQLValues : String;
strFieldName, strFieldType, strFieldValue : String;
varFieldValue : Variant;
bIsFieldChanged : Boolean;
i : Integer;
connection : TQuery;
begin
connection := GetConnection;
try
//get table name
strTableName := GetTableName(pety.GetEntityName);
//get table field
listEntityMapField := GetEntityMapField(pety.GetEntityName);
//set the insert sql
strSQLFields := '';
strSQLValues := '';
strFieldName := '';
strFieldType := '';
for i := 0 to listEntityMapField.GetEntityCount - 1 do
begin
etyEntityMapField := listEntityMapField.GetEntity(i);
strFieldName := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_NAME);
bIsFieldChanged := pety.IsAttributeChanged(strFieldName);
//在Insert时候,有些字段的值有值,有些没有,以下的代码是把有值的字段的Name
//写到SQL语句里,没有值的就不需要写. 但是PK必须要有值
if bIsFieldChanged then
begin
strFieldType := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_TYPE);
varFieldValue := pety.GetAttributeValue(strFieldName);
if not VarIsNull(varFieldValue) then
begin
strSQLFields := strSQLFields + '[' + strFieldName + '], ';
strFieldValue := FormatValueAppToDB(varFieldValue, strFieldType);
if strFieldType = ID_FIELD_TYPE_STRING then
strSQLValues := strSQLValues + '''' + strFieldValue + '''' + ', '
else
strSQLValues := strSQLValues + strFieldValue + ', ';
end;
end;
end;
if (strSQLValues = '') then
begin
InsertEntity := false;
exit;
end;
//get rid of the last ',' in strSQLFields and strSQLValues
strSQLFields := Copy(strSQLFields, 0, Length(strSQLFields) - 2);
strSQLValues := Copy(strSQLValues, 0, Length(strSQLValues) - 2);
strSQL := 'INSERT INTO ' + strTableName + '(' + strSQLFields + ') ' + 'VALUES( ' + strSQLValues + ')';
connection.Close;
connection.SQL.Clear;
connection.SQL.Text := strSQL;
connection.ExecSQL;
Result := true;
finally
ReleaseConnection(connection);
end
end;
//-----------------------------------------------------------------------------
// UpdateEntity
//-----------------------------------------------------------------------------
function TdmDBGW.UpdateEntity(const pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
var
strTableName : String;
etyEntityMapField : IEntity;
listEntityMapField : TEtyList;
strSQL, strSQLSet, strSQLWhere : String;
strFieldName, strFieldType, strFieldValue : String;
varFieldValue : Variant;
bIsFieldChanged, bIsPK : Boolean;
i : Integer;
connection : TQuery;
begin
connection := GetConnection;
try
//get table name
strTableName := GetTableName(pety.GetEntityName);
//get table field
listEntityMapField := GetEntityMapField(pety.GetEntityName);
//set the insert sql
strSQLSet := '';
strSQLWhere := '';
strFieldName := '';
strFieldType := '';
for i := 0 to listEntityMapField.GetEntityCount - 1 do
begin
etyEntityMapField := listEntityMapField.GetEntity(i);
strFieldName := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_NAME);
bIsFieldChanged := pety.IsAttributeChanged(strFieldName);
//在Update时候,有些字段的值改变了,有些没有,以下的代码是把改变的字段的Name
//写到SQL语句里,没有改变的就不需要写
if bIsFieldChanged then
begin
strFieldType := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_TYPE);
bIsPK := etyEntityMapField.GetAttributeValue(ID_ISPK);
varFieldValue := pety.GetAttributeValue(strFieldName);
if (not bIsPK) then
begin
strFieldValue := FormatValueAppToDB(varFieldValue, strFieldType);
if strFieldType = ID_FIELD_TYPE_STRING then
strSQLSet := strSQLSet + '[' + strFieldName + '] = ' + ''''+ strFieldValue + '''' + ', '
else
strSQLSet := strSQLSet + '[' + strFieldName + '] = ' + strFieldValue + ', ';
end;
end;
end;
//get rid of the last ',' in strSQLSet
if (strSQLSet = '') then
begin
UpdateEntity := false;
exit;
end;
strSQLSet := Copy(strSQLSet, 0, Length(strSQLSet) - 2);
//get strSQLWhere
if listCondition <> nil then
strSQLWhere := GetCondition(pety.GetEntityName, listCondition)
else //if listCondition = nil then only update this pety
strSQLWhere := GetPKCondition(pety);
//set strSQL
if strSQLWhere = '' then
strSQL := 'UPDATE ' + strTableName + ' SET ' + strSQLSet
else
strSQL := 'UPDATE ' + strTableName + ' SET ' + strSQLSet + ' WHERE ' + strSQLWhere;
connection.Close;
connection.SQL.Clear;
connection.SQL.Text := strSQL;
connection.ExecSQL;
Result := true;
finally
ReleaseConnection(connection);
end
end;
//-----------------------------------------------------------------------------
// DeleteEntity
//-----------------------------------------------------------------------------
function TdmDBGW.DeleteEntity(const pety : PIEntity; const listCondition : TEtyList = nil) : Boolean;
var
strTableName : String;
strSQL, strSQLWhere: String;
connection : TQuery;
begin
connection := GetConnection;
try
//get table name
strTableName := GetTableName(pety.GetEntityName);
if listCondition <> nil then
strSQLWhere := GetCondition(pety.GetEntityName, listCondition)
else //if listCondition = nil then only delete this pety
strSQLWhere := GetPKCondition(pety);
//set strSQL
if strSQLWhere = '' then
strSQL := 'DELETE FROM ' + strTableName
else
strSQL := 'DELETE FROM ' + strTableName + ' WHERE ' + strSQLWhere;
connection.Close;
connection.SQL.Clear;
connection.SQL.Text := strSQL;
connection.ExecSQL;
Result := true;
finally
ReleaseConnection(connection);
end
end;
//-----------------------------------------------------------------------------
// GetCondition
//-----------------------------------------------------------------------------
function TdmDBGW.GetCondition(const strEntityName : String; const listCondition : TEtyList) : String;
var
i : Integer;
strTableName : String;
strSQLWhere : String;
etyCondition : IEntity;
strEntityFieldName, strOperation, strValue : String;
strTableFieldName, strTableFieldType : String;
begin
try
if listCondition = nil then
begin
Result := '';
exit;
end;
strTableName := GetTableName(strEntityName);
strSQLWhere := '';
for i := 0 to listCondition.GetEntityCount - 1 do
begin
etyCondition := listCondition.GetEntity(i);
strEntityFieldName := etyCondition.GetAttributeValue(FD_ENTITY_FIELD_NAME);
strTableFieldName := GetTableFieldName(strTableName, strEntityFieldName);
strTableFieldType := GetTableFieldType(strTableName, strTableFieldName);
strOperation := etyCondition.GetAttributeValue(FD_OPERATION);
strValue := etyCondition.GetAttributeValue(FD_VALUE);
if strTableFieldType = ID_FIELD_TYPE_STRING then
strSQLWhere := strSQLWhere + '[' + strTableName + '].[' + strTableFieldName +']' + strOperation + '''' + strValue + ''''+ ID_AND
else
strSQLWhere := strSQLWhere + '[' + strTableName + '].[' + strTableFieldName +']' + strOperation + strValue + ID_AND;
end;
//get rid of the last ID_AND in strSQLWhere
strSQLWhere := Copy(strSQLWhere, 0, Length(strSQLWhere) - AND_LENGTH);
Result := strSQLWhere;
except
Result := '';
end
end;
//-----------------------------------------------------------------------------
// GetRule
//-----------------------------------------------------------------------------
function TdmDBGW.GetRule(const strEntityName : String; const listRule : TEtyList) : String;
var
i : Integer;
strTableName : String;
strSQLRule : String;
etyRule : IEntity;
strEntityFieldName, strOperation : String;
strTableFieldName : String;
begin
try
if listRule = nil then
begin
Result := '';
exit;
end;
strTableName := GetTableName(strEntityName);
strSQLRule := '';
for i := 0 to listRule.GetEntityCount - 1 do
begin
etyRule := listRule.GetEntity(i);
strEntityFieldName := etyRule.GetAttributeValue(FD_ENTITY_FIELD_NAME);
strTableFieldName := GetTableFieldName(strTableName, strEntityFieldName);
strOperation := etyRule.GetAttributeValue(FD_OPERATION);
strSQLRule := strSQLRule + strOperation + ' [' + strTableName + '].[' + strTableFieldName +']' + ID_AND;
end;
//get rid of the last ID_AND in strSQLRule
strSQLRule := Copy(strSQLRule, 0, Length(strSQLRule) - AND_LENGTH);
Result := strSQLRule;
except
Result := '';
end
end;
//-----------------------------------------------------------------------------
// GetPKCondition
//-----------------------------------------------------------------------------
function TdmDBGW.GetPKCondition(const pety : PIEntity) : String;
var
strCondition : String;
listEntityMapField : TEtyList;
etyEntityMapField : IEntity;
i, nFieldCount : Integer;
strFieldName, strFieldType, strFieldValue : String;
bIsPK : Boolean;
begin
try
//get Table Field
listEntityMapField := GetEntityMapField(pety.EntityName);
nFieldCount := listEntityMapField.GetEntityCount;
if (nFieldCount = 0) then
begin
Result := '';
exit;
end;
strCondition := '';
strFieldName := '';
strFieldType := '';
strFieldValue := '';
for i := 0 to nFieldCount - 1 do
begin
etyEntityMapField := listEntityMapField.GetEntity(i);
strFieldName := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_NAME);
strFieldType := etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_TYPE);
bIsPK := etyEntityMapField.GetAttributeValue(ID_ISPK);
if bIsPK then
begin
strFieldValue := pety.GetAttributeValue(strFieldName);
//if pk is '', then we can not get pk
if (strFieldValue = '') then
begin
result := '';
exit;
end;
strFieldValue := FormatValueDBToApp(strFieldValue, strFieldType);
if strFieldType = ID_FIELD_TYPE_STRING then
strCondition := strCondition + strFieldName + '=' + '''' + strFieldValue + '''' + ID_AND
else
strCondition := strCondition + strFieldName + '=' + strFieldValue + ID_AND;
end;
end;
//get rid of the last ID_AND in strCondition
strCondition := Copy(strCondition, 0, Length(strCondition) - AND_LENGTH);
Result := strCondition;
except
Result := '';
end
end;
//-----------------------------------------------------------------------------
// FormatValueAppToDB和FormatValueDBToApp方法暂时还没有完善
// 这两个方法是用来格式化值,因为UI层的数据显示可能和数据库的值不一样
// 所以需要转换一下
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// FormatValueAppToDB
//-----------------------------------------------------------------------------
function TdmDBGW.FormatValueAppToDB(const varFieldValue : Variant; const strFieldType : String) : String;
begin
try
if VarIsNull(varFieldValue) then
Result := 'NULL'
else
Result := varFieldValue;
except
Result := '';
end
end;
//-----------------------------------------------------------------------------
// FormatValueDBToApp
//-----------------------------------------------------------------------------
function TdmDBGW.FormatValueDBToApp(const varFieldValue : Variant; const strFieldType : String) : Variant;
begin
try
if VarIsNull(varFieldValue) then
begin
if strFieldType = 'string' then
Result := ''
else if strFieldType = 'int' then
Result := 0
else
//This is set to 'NULL', it is will used in many places. so, we should be careful it
// Result := 'NULL';
Result := varFieldValue;
end
else
Result := varFieldValue;
except
Result := '';
end
end;
//-----------------------------------------------------------------------------
// InitConnectionPool
//-----------------------------------------------------------------------------
procedure TdmDBGW.InitConnectionPool;
var
i : Integer;
connection : TQuery;
begin
try
m_StackConnections := TStack.Create;
for i := 1 to MAX_DB_CONNECTION do
begin
connection := TQuery.Create(self);
connection.AutoCalcFields := true;
connection.DatabaseName := m_db.DatabaseName;
m_StackConnections.Push(connection);
end;
except
end
end;
//-----------------------------------------------------------------------------
// GetConnection
//-----------------------------------------------------------------------------
function TdmDBGW.GetConnection() : TQuery;
begin
Result := m_StackConnections.Pop;
end;
//-----------------------------------------------------------------------------
// GetConnection
//-----------------------------------------------------------------------------
procedure TdmDBGW.ReleaseConnection(connection : TQuery);
begin
m_StackConnections.Push(connection);
end;
//-----------------------------------------------------------------------------
// Refresh
//-----------------------------------------------------------------------------
procedure TdmDBGW.Refresh(etyList : TEtyList);
begin
LoadEntityList(etyList);
end;
//-----------------------------------------------------------------------------
// GetAllEntityMapField
// 该方法获得all Entity Class对应的Field
//-----------------------------------------------------------------------------
function TdmDBGW.GetAllEntityMapField() : TEtyList;
var
xmlFile : TXMLDocument;
list : IXMLNodeList;
i : Integer;
etyEntityMapField : IEntity;
listEntityMapField : TEtyList;
strXMLFileName, strEntityName, strTableName, strValue : String;
handle : Cardinal;
win32FindData : WIN32_FIND_DATA;
strFileName : string;
label Label_Continue;
label Label_Exit;
begin
xmlFile := TXMLDocument.Create(self);
listEntityMapField := TEtyList.Create(TEtyEntityMapField.Create);
try
strFileName := m_strEntityMapPath + '*.xml';
handle := FindFirstFile(PChar(strFileName), win32FindData);
if (handle = INVALID_HANDLE_VALUE) then
begin
result := nil;
exit;
end;
while (true) do
begin
//scan xml files
if (win32FindData.cFileName[0] = '.') or (win32FindData.cFileName = '..') then
goto Label_Continue;
strXMLFileName := m_strEntityMapPath + win32FindData.cFileName;
//get entity map file name
xmlFile.LoadFromFile(strXMLFileName);
xmlFile.Active := true;
list := xmlFile.DocumentElement.ChildNodes[ID_FIELDS].ChildNodes;
strEntityName := xmlFile.DocumentElement.ChildNodes[ID_ENTITY_NAME].NodeValue;
strTableName := xmlFile.DocumentElement.ChildNodes[ID_TABLE_NAME].NodeValue;
//save the data to entity
for i := 0 to list.Count - 1 do
begin
etyEntityMapField := TEtyEntityMapField.Create;
//entity name
etyEntityMapField.SetAttributeValue(ID_ENTITY_NAME, strEntityName);
//entity field name
strValue := list.ChildNodes[FD_ENTITY_FIELD_NAME].NodeValue;
etyEntityMapField.SetAttributeValue(FD_ENTITY_FIELD_NAME, strValue);
//table name
etyEntityMapField.SetAttributeValue(ID_TABLE_NAME, strTableName);
//table field name
strValue := list.ChildNodes[ID_TABLE_FIELD_NAME].NodeValue;
etyEntityMapField.SetAttributeValue(ID_TABLE_FIELD_NAME, strValue);
//field type
strValue := list.ChildNodes[ID_TABLE_FIELD_TYPE].NodeValue;
etyEntityMapField.SetAttributeValue(ID_TABLE_FIELD_TYPE, strValue);
//is pk
strValue := list.ChildNodes[ID_ISPK].NodeValue;
etyEntityMapField.SetAttributeValue(ID_ISPK, strValue);
//add etyEntityMapField to listEntityMapField
listEntityMapField.AddEntity(etyEntityMapField);
end;
Label_Continue:
if not FindNextFile(handle, win32FindData) then
goto Label_Exit;
end;
Label_Exit:
Result := listEntityMapField;
finally
xmlFile.Free;
end
end;
//-----------------------------------------------------------------------------
// GetEntityMapField
// 该方法获得Entity Class对应的Field
//-----------------------------------------------------------------------------
function TdmDBGW.GetEntityMapField(const strEntityName : String = '') : TEtyList;
var
i : Integer;
etyEntityMapField : IEntity;
listEntityMapField : TEtyList;
begin
listEntityMapField := TEtyList.Create(TEtyEntityMapField.Create);
for i := 0 to m_listAllEntityMapField.GetEntityCount - 1 do
begin
etyEntityMapField := m_listAllEntityMapField.GetEntity(i);
if (Trim(LowerCase(etyEntityMapField.GetAttributeValue(ID_ENTITY_NAME))) = Trim(LowerCase(strEntityName))) then
begin
//add etyEntityMapField to listEntityMapField
listEntityMapField.AddEntity(etyEntityMapField);
end;
end;
Result := listEntityMapField;
end;
//-----------------------------------------------------------------------------
// GetTableName
//-----------------------------------------------------------------------------
function TdmDBGW.GetTableName(const strEntityName : String) : String;
var
xmlFile : TXMLDocument;
begin
xmlFile := TXMLDocument.Create(self);
try
//get entity map file name
xmlFile.LoadFromFile(GetAppPath() + ID_ENTITY_MAP_PATH + '/' + strEntityName + '.xml');
xmlFile.Active := true;
Result := xmlFile.DocumentElement.ChildNodes[ID_TABLE_NAME].NodeValue;
finally
xmlFile.Free;
end
end;
//-----------------------------------------------------------------------------
// GetTableFieldName
//-----------------------------------------------------------------------------
function TdmDBGW.GetTableFieldName(const strTableName : String; const strEntityFieldName : String) : String;
var
i : Integer;
etyEntityMapField : IEntity;
begin
try
for i := 0 to m_listAllEntityMapField.GetEntityCount - 1 do
begin
etyEntityMapField := m_listAllEntityMapField.GetEntity(i);
if (Trim(LowerCase(etyEntityMapField.GetAttributeValue(ID_TABLE_NAME))) = Trim(LowerCase(strTableName))) and
(Trim(LowerCase(etyEntityMapField.GetAttributeValue(ID_ENTITY_FIELD_NAME))) = Trim(LowerCase(strEntityFieldName))) then
begin
Result := Trim(etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_NAME));
exit;
end;
end;
Result := '';
except
Result := '';
end;
end;
//-----------------------------------------------------------------------------
// GetLoadType
//-----------------------------------------------------------------------------
function TdmDBGW.GetLoadType(const strEntityName : String) : String;
var
xmlFile : TXMLDocument ;
begin
//get entity map file name
xmlFile := TXMLDocument.Create(self);
xmlFile.LoadFromFile(GetAppPath() + ID_ENTITY_MAP_PATH + '/' + strEntityName + '.xml');
xmlFile.Active := true;
result := xmlFile.DocumentElement.ChildNodes.Nodes[ID_LOAD_TYPE].NodeValue;
xmlFile.Free;
end;
//-----------------------------------------------------------------------------
// GetTableFieldType
//-----------------------------------------------------------------------------
function TdmDBGW.GetTableFieldType(const strTableName : String; const strFieldName : String) : String;
var
i : Integer;
etyEntityMapField : IEntity;
begin
try
for i := 0 to m_listAllEntityMapField.GetEntityCount - 1 do
begin
etyEntityMapField := m_listAllEntityMapField.GetEntity(i);
if (Trim(LowerCase(etyEntityMapField.GetAttributeValue(ID_TABLE_NAME))) = Trim(LowerCase(strTableName))) and
(Trim(LowerCase(etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_NAME))) = Trim(LowerCase(strFieldName))) then
begin
Result := Trim(etyEntityMapField.GetAttributeValue(ID_TABLE_FIELD_TYPE));
exit;
end;
end;
Result := '';
except
Result := '';
end
end;
//-----------------------------------------------------------------------------
// DataModuleCreate
//-----------------------------------------------------------------------------
procedure TdmDBGW.DataModuleCreate(Sender: TObject);
begin
//it is implemented by derived classes
end;
//-----------------------------------------------------------------------------
// m_dbBeforeConnect
//-----------------------------------------------------------------------------
procedure TdmDBGW.m_dbBeforeConnect(Sender: TObject);
begin
//it is implemented by derived classes
end;
//-----------------------------------------------------------------------------
// m_dbAfterConnect
//-----------------------------------------------------------------------------
procedure TdmDBGW.m_dbAfterConnect(Sender: TObject);
begin
InitConnectionPool;
Init;
end;
end.