add support for usage of autoinc ID-fields in WFS-tables

As you can see in your code (wsDB.pas), the name of workflow isn’t saved at the moment the record is created (see procedure WorkflowDefinitionInsert). Initially only the ID is save in the new record. Therefore name of workflow has initially value Null. Then after the record is created the procedure WorkflowDefinitionUpdate(WorkDef); will save other fields (name and diagram).

Anyhow, it’s not my intention to change working with the ID. I only would like the database to use autoinc fields for ID keyfields as an option. Using the approach I present you would make it a very simple adjustment to the current sourcecode.

Implementation:
• Add property “UseAutoIncInTables” to TWorkflowStudio
Based on this property the ID’s for records in all tables must be set by the database (autoinc) in stead of by determining it by code in function:
function TCustomWorkflowDB.FindNextID(ATableName, AFieldName: string): integer;
• Make ID fields optionally autoinc (Identity)
• Add string-field “GUID” to each WF-studio table.
This can easily be accomplished by added some additional sql-scripts for creating the databases

Field GUID will be used to store a unique string when adding a new record. As the table’s ID is set as autoinc (Indentity), the assigned ID can now be retrieved by function GetIDFromGUID. From that moment on it’s business as usual.

function TCustomWorkflowDB.GetIDFromGUID(ATableName, AFieldName, AGUIDField, AGUID: string) : Integer;
var
DS: TDataset;
begin
FParams.Clear;
DS := OpenQuery(
Format('Select %s from %s where %s = %s', [AFieldName, ATableName, AGUIDField, AGUID]),
FParams);
try
result := DS.Fields[0].AsInteger;
finally
DS.Close;
if DestroyQueries then
DS.Free;
end;
end;

procedure TCustomWorkflowDB.WorkflowDefinitionInsert(WorkDef: TWorkflowDefinition);
var SQL: string;
vGUID : String;
begin
With WorkDefBindary do
begin
if NOT FUseAutoIncInTables then
begin
{Insert workdefinition unique key}
WorkDef.Key := IntToStr(FindNextID(TableName, KeyField));
SQL := Format('INSERT INTO %s (%s) Values (:id)',
[TableName, KeyField]);
FParams.Clear;
FParams.CreateParam(ftInteger, 'id', ptInput).AsInteger := StrToInt(WorkDef.Key);
ExecuteQuery(SQL, FParams);
end else
begin
{Create workdefinition unique GUID, later used to retrieve assigned key in table}
vGUID := CreateClassID; // uses ComObj
SQL := Format('INSERT INTO %s (%s) Values (:GUID)',
[TableName, GUIDField]);
FParams.Clear;
FParams.CreateParam(ftString, 'GUID', ptInput).AsString := vGUID; // uses ComObj
ExecuteQuery(SQL, FParams);

  {Get workdefinition assigned key}
  WorkDef.Key := IntToStr(GetIDFromGUID(TableName, KeyField, GUIDField, vGUID));

end;
{Save other workflow definition fields}
WorkflowDefinitionUpdate(WorkDef);

end;
end;

I believe this approach is generic for every database and easy to implement.