Wednesday, March 18, 2009

Smarter Standalone Rowset - Part 2

We will continue with our exploration of enhancing the StandAlone Rowset class to include more functionality. We have seen how you utilize the population of a standalone rowset using a SQL statment.
In this post we will explore populating the standalone rowset from one source record and with a where clause + bind Variables.

The delivered Fill() method (or Select() for Standard rowset) accepts any number of Bind Variables for substitution in the where clause. PeopleCode API does not permit creation of methods/functions that accepts variable number of arguments. So in our approach we will explore the possibility of passing variable number of bind variables as elements of array of type any.

Class StandAloneRowset extends Rowset
    method
StandAloneRowset(&StandAloneRec as String);
    method FillFromRecord(&FillRecName As String,&WhereStr as String, &Params As Array of Any);
    method
FillUsingSQLObject(&SQLObj as SQL);
private
   instance String &RsRec;

end-class;


method StandAloneRowset
    &RsRec = &StandAloneRec;
    %Super = CreateRowset(@("Record."|&RsRec));
end-method;


method FillFromRecord
    Local Record &Rec;
    Local String &SQLStr;
    Local SQL &SQL;
    Local integer &Count;

   
&SQLStr = "%SelectAll(:1) Where "|&WhereStr;
    &SQL = CreateSQL(&SQLStr,CreateArrayAny(CreateRecord(@("Record."|&FillRecName)), &Params));
   
    &Rec = CreateRecord(@("Record."|
&FillRecName));
    While
&SQL.Fetch(&Rec)
        if &Count > 0 then
             %This.InsertRow(%This.ActiveRowCount);
        end-if;
        &Rec.CopyFieldsTo(%This.GetRow(%This.ActiveRowCount).GetRecord(@("Record."|&RsRec)));

        &Count = &Count + 1;
    end-while;
  
end-method;

method FillUsingSQLObject
    Local Record &Rec;
    Local integer &Count;

    &Rec = CreateRecord(@("Record."|&RsRec));
    While &SqlObj.Fetch(&Rec)
        if &Count > 0 then
           %This.InsertRow(%This.ActiveRowCount);
       end-if;
   &Rec.CopyFieldsTo(%this.GetRow(%this.ActiveRowCount).GetRecord(@("Record."|&RsRec));
    &Count = &Count + 1;
    End-While;
end-method;



Since in last post we have already seen how to use the StandAloneRowset class with SQL object for data population in the rowset. Here we will use our "latest" FillFromRecord method to simulate a Rowset.Select functionality.

Local StandAloneRowset &MyRs;

&MyRs = Create
StandAloneRowset
("REC1");

&MyRs.FillFromRecord("REC2","WHERE FIELD1=:2 AND FIELD2=:3 AND FIELD3=:4",CreateArrayAny("Val1","Val2","val3");


Note that the Bind variables are specified starting from :2. This is because the first bind parameter to be passed is the record object. Remember to number your bind parameters from :2.
Thus we have an identical functionality like the Rowset.Select method where it is possible to populate the rowset from another record.

We will keep on exploring our standalone rowset class....adding new method for saving data etc..but later...







No comments:

Post a Comment