LinqList.dpr 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. program LinqList;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils,
  6. Quick.Commons,
  7. Quick.Console,
  8. Quick.Chrono,
  9. Quick.Lists,
  10. Quick.Linq;
  11. type
  12. TLoginInfo = record
  13. Username : string;
  14. UserPassword : string;
  15. Locked : Boolean;
  16. end;
  17. TUser = class
  18. private
  19. fId : Int64;
  20. fName : string;
  21. fSurName : string;
  22. fAge : Integer;
  23. fLoginInfo : TLoginInfo;
  24. published
  25. property Id : Int64 read fId write fId;
  26. property Name : string read fName write fName;
  27. property SurName : string read fSurName write fSurName;
  28. property Age : Integer read fAge write fAge;
  29. property LoginInfo : TLoginInfo read fLoginInfo write fLoginInfo;
  30. end;
  31. const
  32. numusers = 100000;
  33. UserNames : array of string = ['Cliff','Alan','Anna','Phil','John','Michel','Jennifer','Peter','Brandon','Joe','Steve','Lorraine','Bill','Tom','Norma','Martin','Steffan','Wilma','Derek','Lewis','Paul',
  34. 'Erik','Robert','Nicolas','Frederik','Rose'];
  35. UserSurnames : array of string = ['Gordon','Summer','Huan','Paterson','Johnson','Michelson','Smith','Peterson','Miller','McCarney','Roller','Gonzalez','Thomson','Muller','Jefferson','Volkov','Matheu','Morrison','Newman','Lover','Sunday',
  36. 'Roberts','Landon','Yuri','Paris','Levis'];
  37. var
  38. users : TIndexedObjectList<TUser>;
  39. users2 : TSearchObjectList<TUser>;
  40. user : TUser;
  41. i : Integer;
  42. n : Integer;
  43. crono : TChronometer;
  44. login : TLoginInfo;
  45. begin
  46. try
  47. ReportMemoryLeaksOnShutdown := True;
  48. users := TIndexedObjectList<TUser>.Create(True);
  49. users.Indexes.Add('Name','Name');
  50. users.Indexes.Add('Surname','fSurname',TClassField.cfField);
  51. users.Indexes.Add('id','Id');
  52. users2 := TSearchObjectList<TUser>.Create(False);
  53. cout('Generating list...',etInfo);
  54. //generate first dummy entries
  55. for i := 1 to numusers - high(UserNames) do
  56. begin
  57. user := TUser.Create;
  58. user.Id := Random(999999999999999);
  59. user.Name := 'Name' + i.ToString;
  60. user.SurName := 'SurName' + i.ToString;
  61. user.Age := 18 + Random(20);
  62. users.Add(user);
  63. users2.Add(user);
  64. end;
  65. //generate real entries to search
  66. for i := 0 to high(UserNames) do
  67. begin
  68. user := TUser.Create;
  69. user.Id := Random(999999999999999);
  70. user.Name := UserNames[i];
  71. user.SurName := UserSurnames[i];
  72. user.Age := 18 + Random(20);
  73. login.Username := user.Name;
  74. login.UserPassword := RandomPassword(8,[pfIncludeNumbers,pfIncludeSigns]);
  75. login.Locked := False;
  76. user.LoginInfo := login;
  77. users.Add(user);
  78. users2.Add(user);
  79. end;
  80. crono := TChronometer.Create;
  81. //test search by index
  82. crono.Start;
  83. user := users.Get('Name','Peter');
  84. crono.Stop;
  85. if user <> nil then cout('Found by Index: %s %s in %s',[user.Name,user.SurName,crono.ElapsedTime],etSuccess)
  86. else cout('Not found!',etError);
  87. //test search by normal iteration
  88. user := nil;
  89. crono.Start;
  90. for i := 0 to users.Count - 1 do
  91. begin
  92. //if (users[i].Name = 'Anus') or (users[i].SurName = 'Smith') then
  93. if users[i].Name = 'Peter' then
  94. begin
  95. crono.Stop;
  96. user := users[i];
  97. Break;
  98. end;
  99. end;
  100. if user <> nil then cout('Found by Iteration: %s %s at %d position in %s',[user.Name,user.SurName,i,crono.ElapsedTime],etSuccess)
  101. else cout('Not found by Iteration!',etError);
  102. //test search by Linq iteration
  103. crono.Start;
  104. //user := TLinq.From<TUser>(users2).Where('(Name = ?) OR (SurName = ?)',['Anus','Smith']).OrderBy('Name').SelectFirst;
  105. user := TLinq<TUser>.From(users2).Where('Name = ?',['Peter']).SelectFirst;
  106. crono.Stop;
  107. if user <> nil then cout('Found by Linq: %s %s in %s',[user.Name,user.SurName,crono.ElapsedTime],etSuccess)
  108. else cout('Not found by Linq! (%s)',[crono.ElapsedTime],etError);
  109. //test search by embeded iteration
  110. crono.Start;
  111. user := users2.Get('Name','Peter');
  112. crono.Stop;
  113. if user <> nil then cout('Found by Search: %s %s in %s',[user.Name,user.SurName,crono.ElapsedTime],etSuccess)
  114. else cout('Not found!',etError);
  115. //ConsoleWaitForEnterKey;
  116. cout('Multi results:',etInfo);
  117. //test search by normal iteration
  118. user := nil;
  119. n := 0;
  120. cout('Found by Iteration:',etInfo);
  121. for i := 0 to users.Count - 1 do
  122. begin
  123. //if ((users[i].Name = 'Anna') or (users[i].Age > 30)) or (users[i].SurName = 'Smith') then
  124. //if users[i].Age > 18 then
  125. if (users[i].Name = 'Anna') then
  126. begin
  127. Inc(n);
  128. user := users[i];
  129. cout('%d. %s %s',[n,user.Name,user.SurName],etSuccess)
  130. end;
  131. end;
  132. if user = nil then cout('Not found by Iteration!',etError);
  133. //test search by Linq iteration
  134. user := nil;
  135. n := 0;
  136. cout('Found by Linq:',etInfo);
  137. //TLinq<TUser>.From(users2).Where('SurName Like ?',['p%']).Delete;
  138. TLinq<TUser>.From(users2).Where('Name = ?',['Peter']).Update(['Name'],['Poter']);
  139. //for user in TLinq<TUser>.From(users2).Where('(Name = ?) OR (SurName = ?) OR (SurName = ?)',['Peter','Smith','Huan']).Select do
  140. //for user in TLinq<TUser>.From(users2).Where('(Name = ?) OR (Age > ?) OR (SurName = ?)',['Anna',30,'Smith']).Select do
  141. //for user in TLinq<TUser>.From(users2).Where('Age > ?',[18]).Select do
  142. //for user in TLinq<TUser>.From(users2).Where('SurName Like ?',['%son']).Select do
  143. //for user in TLinq<TUser>.From(users2).Where('SurName Like ?',['p%']).Select do
  144. //for user in TLinq<TUser>.From(users2).Where('1 = 1',[]).Select do
  145. for user in TLinq<TUser>.From(users2).Where('(LoginInfo.UserName Like ?) OR (LoginInfo.UserName Like ?)',['p%','a%'])
  146. .OrderBy('Name')
  147. .Select do
  148. begin
  149. Inc(n);
  150. cout('%d. %s %s',[n,user.Name,user.SurName],etSuccess);
  151. end;
  152. if user = nil then cout('Not found by Linq!',etError);
  153. cout('Press a key to Exit',etInfo);
  154. Readln;
  155. users.Free;
  156. users2.Free;
  157. crono.Free;
  158. except
  159. on E: Exception do
  160. cout('%s : %s',[E.ClassName,E.Message],etError);
  161. end;
  162. end.