Icon View Thread

The following is the text of the current message along with any replies.
Messages 11 to 12 of 12 total
Thread How to update/delete records
Fri, Aug 3 2012 4:08 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Uli,

<< I am just working on a JSON parser to for the update- and delete stuff.
Would you like to share some code snippets that you are using in your
integrated webserver? >>

Sure, here you go:

procedure ParseOperationsJSON(const Value: String;
                             const User: String='';
                             const Password: String='');
var
  TempParser: TEWBJSONParser;
  TempDataSetName: String;
  TempDataSet: TIDEDataSet;
  TempInstances: TObjectList;
  TempInstance: TIDEDataSetInstance;
  TempOperationType: Integer;
  TempBeforeRow: TStrings;
  TempAfterRow: TStrings;

  function FindInstance(DataSet: TIDEDataSet): TIDEDataSetInstance;
  var
     I: Integer;
  begin
     Result:=nil;
     for I:=0 to TempInstances.Count-1 do
        begin
        if (TIDEDataSetInstance(TempInstances[I]).DataSet=DataSet) then
           begin
           Result:=TIDEDataSetInstance(TempInstances[I]);
           Break;
           end;
        end;
     if (Result=nil) then
        begin
        Result:=CreateDataSetInstance(DataSet);
        TempInstances.Add(Result);
        Result.Connect(User,Password,True);
        Result.StartTransaction;
        Result.OpenDataSet;
        end;
  end;

  procedure CommitInstances;
  var
     I: Integer;
  begin
     for I:=0 to TempInstances.Count-1 do
        TIDEDataSetInstance(TempInstances[I]).Commit;
  end;

  procedure RollbackInstances;
  var
     I: Integer;
  begin
     for I:=0 to TempInstances.Count-1 do
        TIDEDataSetInstance(TempInstances[I]).Rollback;
  end;

  procedure ParseRow(RowValues: TStrings);
  var
     TempColumnName: String;
     TempColumnValue: String;
  begin
     with TempParser do
        begin
        if (not SkipSymbol('null')) then
           begin
           ErrorIfNotSkipToken('{');
           if (not CheckToken('}')) then
              begin
              while True do
                 begin
                 ErrorIfNotToken(ptString);
                 TempColumnName:=TokenString;
                 NextToken;
                 ErrorIfNotSkipToken(':');
                 TempColumnValue:=TokenString;
                 NextToken;
                 RowValues.Add(TempColumnName+'='+TempColumnValue);
                 if (not SkipToken(',')) then
                    Break;
                 end;
              end;
           ErrorIfNotSkipToken('}');
           end;
        end;
  end;

begin
  TempParser:=TEWBJSONParser.Create;
  try
     TempInstances:=TObjectList.Create;
     TempBeforeRow:=TStringList.Create;
     TempAfterRow:=TStringList.Create;
     try
        TempInstance:=nil;
        try
           with TempParser do
              begin
              Initialize(Value);
              ErrorIfNotSkipToken('{');
              ErrorIfNotSkipString('operations');
              ErrorIfNotSkipToken(':');
              ErrorIfNotSkipToken('[');
              if CheckToken('{') then
                 begin
                 while True do
                    begin
                    ErrorIfNotSkipToken('{');
                    ErrorIfNotSkipString('dataset');
                    ErrorIfNotSkipToken(':');
                    ErrorIfNotToken(ptString);
                    TempDataSetName:=TokenString;
                    TempDataSet:=GetDataSetByName(TempDataSetName);
                    if (TempDataSet <> nil) then
                       TempInstance:=FindInstance(TempDataSet)
                    else
                       raise Exception.Create('Invalid dataset name
'+TempDataSetName+
                                              ' specified in the
transaction operations');

                    NextToken;
                    ErrorIfNotSkipToken(',');
                    ErrorIfNotSkipString('operation');
                    ErrorIfNotSkipToken(':');
                    ErrorIfNotToken(ptInteger);
                    TempOperationType:=StrToInt(TokenString);
                    if (TempOperationType <= OPERATION_UNKNOWN) or
(TempOperationType > OPERATION_DELETE) then
                       raise Exception.Create('Invalid or unknown operation
type '+IntToStr(TempOperationType)+
                                              ' specified in the
transaction operations');
                    NextToken;
                    TempBeforeRow.Clear;
                    TempAfterRow.Clear;
                    ErrorIfNotSkipToken(',');
                    ErrorIfNotSkipString('beforerow');
                    ErrorIfNotSkipToken(':');
                    ParseRow(TempBeforeRow);
                    ErrorIfNotSkipToken(',');
                    ErrorIfNotSkipString('afterrow');
                    ErrorIfNotSkipToken(':');
                    ParseRow(TempAfterRow);
                    { Now execute the actual operation }
                    case TempOperationType of
                       OPERATION_INSERT:
                          TempInstance.InsertRow(TempAfterRow);
                       OPERATION_UPDATE:
                          TempInstance.UpdateRow(TempBeforeRow,TempAfterRow);
                       OPERATION_DELETE:
                          TempInstance.DeleteRow(TempBeforeRow);
                       end;
                    ErrorIfNotSkipToken('}');
                    if (not SkipToken(',')) then
                       Break;
                    end;
                 end;
              ErrorIfNotSkipToken(']');
              ErrorIfNotSkipToken('}');
              end;
           CommitInstances;
        except
           RollbackInstances;
           raise;
        end;
     finally
        FreeAndNil(TempBeforeRow);
        FreeAndNil(TempAfterRow);
        FreeAndNil(TempInstances);
     end;
  finally
     FreeAndNil(TempParser);
  end;
end;

If you have any other questions, please let me know.

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Aug 3 2012 5:29 PMPermanent Link

Uli Becker

Tim,

> Sure, here you go:

thanks a lot for this code!

Uli
Previous PagePage 2 of 2
Jump to Page:  1 2
Image