dummzeuch 1517 Posted December 10, 2018 From docwiki.embarcadero.com/Libraries/en/Data.DB.TDataSet.CopyFields ---------- schnipp --------- Description CopyFields copies the field definition structure of another dataset. CopyFields copies the matching fields in the current records and returns the number of fields copied. For more information, see http://qc.embarcadero.com/wc/qcmain.aspx?d=7768. For another example, see http://community.devexpress.com/forums/p/47463/160009.aspx. ---------- schnapp --------- So, which is it? Or does it do both? This is no great help, especially since the first link results in an timeout error while the second says authorization required. (The Delphi 2007 help was even less helpful: "This is CopyFields, a member of TDataset.") OK, so I checked the sources and wasn't any wiser. From the context in the sources I am currently looking at I guess it does the first, if necessary and then the second: DestDs.Append; DestDs.CopyFields(SrcDs); DestDs.Post; But so far I am only guessing. Share this post Link to post
Lars Fosdal 1793 Posted December 10, 2018 Intuitively (i.e. also guessing 😛), I would tend to believe that DestDs tries to look up fields that exists in it's own structure , by name from SrcDs, and copy the value of such fields? Share this post Link to post
Kryvich 165 Posted December 10, 2018 Looking at the function code, I would say that it copies the field values of the current record from the source data set to another data set. It also copies records of all nested data sets. But I think there is a bug in their code: instead of CopyFields(TDataSetField(DestField).NestedDataSet); should be TDataSetField(DestField).NestedDataSet.CopyFields(TDataSetField(SourceField).NestedDataSet); Not tested yet. Share this post Link to post
dummzeuch 1517 Posted December 10, 2018 OK, I'm pretty sure that it only copies the field values and then only for those fields that exist in the target dataset. It does not create new fields, so the description in the OLH is wrong: * CopyFields copies the field definition structure of another dataset. WRONG * CopyFields copies the matching fields in the current records and returns the number of fields copied. Marco Cantu says the same in his Mastering Delphi Update for Delphi 2006 post. Share this post Link to post
Guest Posted December 11, 2018 (edited) @Kryvich, i think that when that line is called the recursion will treat the NestedDataSet as a DataSet. So it will actually work. What i mean is that for each nested record the function is using its own code in the else section. However, to be 100% sure i would have to trace and check, and i never used nested datasets. @dummzeuch, no structure is altered in that function, i agree. Field values existing in both sets will be copied. The docs should say copies matching field values and... Edited December 11, 2018 by Guest Clarified the doc suggestion, last line Share this post Link to post
Guest Posted December 11, 2018 Also it will copy values "regardless" of field type matching or not, implicitly converting types, matching only on field names. An EDataBaseError will be raised when types cannot be implicitly converted. Share this post Link to post
Kryvich 165 Posted December 11, 2018 @Dany Marmur I would say the else section is for fields that are not inherited from TDataSetField. Share this post Link to post
Guest Posted December 11, 2018 @Kryvich, exactly. And that is the case for the fields inside NestedDataSet. Because of recursion it will work. Property NestedDataSet is of type TDataSet and so on. Agree? Share this post Link to post
Kryvich 165 Posted December 11, 2018 @Dany Marmur No, it does not work. You can see for yourself: I have prepared a test application with 2 in-memory datasets and 2 nested datasets (see TestCopyFields.zip in the attachment). Values of fields of the nested dataset are not copied: But if you copy the Data.DB.pas to the application's folder and make the modification I suggested, it works: I think Embarcadero never tested this feature. Honestly, I myself learned about nested data sets only today. TestCopyFields.zip Share this post Link to post
Uwe Raabe 2064 Posted December 11, 2018 10 minutes ago, Kryvich said: No, it does not work. Please report that bug to QP and add your example program. Share this post Link to post
Kryvich 165 Posted December 11, 2018 @Uwe Raabe Of course, https://quality.embarcadero.com/browse/RSP-22946 1 Share this post Link to post