Thursday 12 February 2015

Copy table buffer between two table with similar structure

Just a quick snippet for those who need it.
The standard buf2Buf() method works if the field Id is identical, but in case the field name is same but have different Id, the code snippet below does the job. It is modified from the buf2Buf() to cater to two tables with similar structure and field name but different field Id.

static void buf2BufByName(
    Common  _from,
    Common  _to
    )
{
    DictTable   dictTableFrom   = new DictTable(_from.TableId);
    DictTable   dictTableTo     = new DictTable(_to.TableId);
    DictField   dictFieldFrom;
    FieldId     fieldIdFrom     = dictTableFrom.fieldNext(0);
    FieldId     fieldIdTo
    ;

    while (fieldIdFrom && ! isSysId(fieldIdFrom))
    {
        dictFieldFrom   = new DictField(_from.TableId, fieldIdFrom);

        if(dictFieldFrom)
        {
            fieldIdTo = dictTableTo.fieldName2Id(dictFieldFrom.name());

            if(fieldIdTo)
                _to.(fieldIdTo) = _from.(fieldIdFrom);
        }

        fieldIdFrom = dictTableFrom.fieldNext(fieldIdFrom);
    }
}

No comments:

Post a Comment