Problems sorting a DataSet lazarus / 16-Databases ---------------------------------------------------------------------- jciberta · 2021-05-11 15:35 I have problems sorting a DataSet with IndexFieldNames. I don't know if it's a bug, or I'm doing something wrong. The code is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   //SQLQuery1.IndexFieldNames:='Title DESC'; // Works fine!  //SQLQuery1.IndexFieldNames:='Title; LastName'; // Works fine!  SQLQuery1.IndexFieldNames:='Title; LastName DESC'; // WRONG: Order Title by DESC!   I'm using Ubuntu 18.04 with Lazarus 2.0.12 (the newest at this moment). The database engine is SQLite with Chinook database (https://github.com/lerocha/chinook-database). A sample program is attached. I need to know if IndexFieldNames works properly or if there is a bug. Change SQL is not an option. Thanks in advance ---------------------------------------------------------------------- jciberta · 2021-05-12 15:29 I just find out if you set the value of IndexFieldNames twice, it works. That is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   //SQLQuery1.IndexFieldNames:='Title DESC'; // Works fine!  //SQLQuery1.IndexFieldNames:='Title; LastName'; // Works fine!  SQLQuery1.IndexFieldNames:='Title; LastName DESC'; // WRONG: Order Title by DESC!  SQLQuery1.IndexFieldNames:='Title; LastName DESC'; // Do it twice -> Works properly!  So, it seems to me a bug. I do not know how to report it, any help would be appreciated ---------------------------------------------------------------------- Lutz Mändle · 2021-05-12 17:17 The string for IndexFieldNames should not contain any other spaces then before DESC, like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SQLQuery1.IndexFieldNames:='Title;LastName DESC'; ---------------------------------------------------------------------- jciberta · 2021-05-12 18:20 Still not working. It persists in order Title by DESC. I tried now on a Windows 10, Lazarus 2.0.10. The same behaviour ---------------------------------------------------------------------- MarkMLl · 2021-05-12 18:29 Quote from: jciberta on May 12, 2021, 05:29:41 pmSo, it seems to me a bug. I do not know how to report it, any help would be appreciated You'll find https://bugs.freepascal.org/bug_report_page.php fairly painless. Sorry I've not been able to help any more than that. MarkMLl ---------------------------------------------------------------------- GAN · 2021-05-12 19:51 Did you try something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  SQLQuery1.IndexFieldNames:='Title ASC ; LastName DESC'; ---------------------------------------------------------------------- dseligo · 2021-05-12 20:31 Quote from: jciberta on May 12, 2021, 05:29:41 pmI just find out if you set the value of IndexFieldNames twice, it works. That is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   //SQLQuery1.IndexFieldNames:='Title DESC'; // Works fine!  //SQLQuery1.IndexFieldNames:='Title; LastName'; // Works fine!  SQLQuery1.IndexFieldNames:='Title; LastName DESC'; // WRONG: Order Title by DESC!  SQLQuery1.IndexFieldNames:='Title; LastName DESC'; // Do it twice -> Works properly!  So, it seems to me a bug. I do not know how to report it, any help would be appreciated I think it's a bug too. I tried with ZeosDBO components and it seems to work fine there. ---------------------------------------------------------------------- dseligo · 2021-05-12 20:33 Quote from: GAN on May 12, 2021, 09:51:02 pmDid you try something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  SQLQuery1.IndexFieldNames:='Title ASC ; LastName DESC'; There is an error if you use ASC ('Field not found: "Title ASC"'). ---------------------------------------------------------------------- jciberta · 2021-05-13 11:44 Thank you to you all. I'm gonna report the bug. Temporary I fixed it doing it twice. I tried to debug unit BufDataset, but I don't know how to do it. My guess is something is not right in function SetIndexFieldNames and BuildCustomIndex is not called the first time Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomBufDataset.SetIndexFieldNames(const AValue: String);begin  FIndexFieldNames:=AValue;  if (AValue='') then    begin    FCurrentIndexDef:=FIndexes.FindIndex(SDefaultIndex);    Exit;    end;  if Active then    BuildCustomIndex;end; ---------------------------------------------------------------------- MarkMLl · 2021-05-13 12:32 Quote from: jciberta on May 13, 2021, 01:44:51 pmTemporary I fixed it doing it twice. I tried to debug unit BufDataset, but I don't know how to do it. My guess is something is not right in function SetIndexFieldNames and BuildCustomIndex is not called the first time I'm surprised that IndexFieldNames is cumulative... it's not documented as such at https://www.freepascal.org/docs- html/fcl/bufdataset/tcustombufdataset.indexfieldnames.html and the only hint is that '' is suggested as removing all file names not just one. I suggest that you point that out in your bug report. MarkMLl ---------------------------------------------------------------------- jciberta · 2021-05-13 14:32 Thanks MarkMLl. I took a look deeper and I think the first guess was wrong because it does some order with the data the fisrt time (wrong order though), so BuildCustomIndex is called as well ---------------------------------------------------------------------- MarkMLl · 2021-05-13 15:12 Just report it. You've done exactly the right thing raising it here first so that if you were doing something obviously wrong people could correct you, but the maintainer of that part might not use the forum... there's also mailing lists etc. MarkMLl ---------------------------------------------------------------------- Lutz Mändle · 2021-05-13 15:26 The problem is in TCustomBufDataset.BuildCustomIndex in the unit .../fpcsrc/3.2.0/packages/fcl-db/src/base/bufdataset.pas Changing the following sequence Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ..  F.Fields:=SortFields;  F.Options:=[];  F.DescFields:=DescFields;..  to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ..  F.Fields:=SortFields;  F.DescFields:=DescFields;  F.Options:=[];..  solves the problem, but maybe arises some other inconsistencies. That a second assignment to the IndexFieldNames seems to solve the problem is a side effect of how TIndexDef.SetDescFields in the unit .../fpcsrc/3.2.0/packages/fcl-db/src/base/db.pas works. It adds the value ixDescending to the index options, but only if the new assigned value is different from the former value and not empty. Edit: It is possible to debug bufdataset.pas by including the path in project options->compiler options->paths>other unit files (in my case /usr/share/fpcsrc/3.2.0/packages/fcl-db/src/base/). ---------------------------------------------------------------------- jciberta · 2021-05-13 16:10 Quote from: MarkMLl on May 13, 2021, 05:12:48 pmJust report it. You've done exactly the right thing raising it here first so that if you were doing something obviously wrong people could correct you, but the maintainer of that part might not use the forum... there's also mailing lists etc. MarkMLl I reported earlier. https://bugs.freepascal.org/view.php?id=38884 Quote from: Lutz Mändle on May 13, 2021, 05:26:22 pmThe problem is in TCustomBufDataset.BuildCustomIndex in the unit .../fpcsrc/3.2.0/packages/fcl-db/src/base/bufdataset.pas Changing the following sequence Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ..  F.Fields:=SortFields;  F.Options:=[];  F.DescFields:=DescFields;..  to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ..  F.Fields:=SortFields;  F.DescFields:=DescFields;  F.Options:=[];..  solves the problem, but maybe arises some other inconsistencies. That a second assignment to the IndexFieldNames seems to solve the problem is a side effect of how TIndexDef.SetDescFields in the unit .../fpcsrc/3.2.0/packages/fcl-db/src/base/db.pas works. It adds the value ixDescending to the index options, but only if the new assigned value is different from the former value and not empty. Great! I'm gonna add the issue this additional information to help the mantainer