Guest User

Untitled

a guest
Jan 12th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. unit UMisc;
  2.  
  3. interface
  4.  
  5. uses IBCustomDataSet, StrUtils, SysUtils, Classes, IBDataBase, DBCtrls;
  6.  
  7. type
  8. TSQLQueryIndex = (SelectIndex, DeleteIndex, InsertIndex, ModifyIndex,
  9. RefreshIndex);
  10. TSQLElements = (FormatString, FirstQueryPart, LastQueryPart);
  11. TSQLComplexQueryIndex = (ComplexSelectIndex, ComplexRefreshIndex);
  12.  
  13. procedure DeleteAmp(var AString: string);
  14. procedure DeleteEndComma(var AString: string);
  15. function ReplaceDash(AString: string): string;
  16. function NormalizeStr(AString: string): string;
  17. procedure InitStrings(const AStrings: TStrings);
  18.  
  19. function MakeCaptionFromTable(const ATableName: string): string;
  20. function MakeCaptionForSchedule(const ATableName: string): string;
  21.  
  22. function GetRelatedRefIndex(ARefIndex: integer): integer;
  23. function MakeQueryStr(
  24. AQueryIndex: TSQLQueryIndex; ARefIndex: integer): string;
  25. function MakeComplexQueryStr(
  26. AQueryIndex: TSQLComplexQueryIndex; ARefIndex: integer): string;
  27.  
  28. function GetNeededRefIndex(AField: string; ARefIndex: integer): integer;
  29. function MakeCustomSelect(AField: string; ARefIndex: integer): string;
  30. procedure SetLookupFields(var ABox: TDBLookupCombobox;
  31. const AFieldName: string);
  32.  
  33. procedure ReOpenDataSet(DSet: TIBDataSet);
  34.  
  35. function MakeIdField(const AIndex: integer): string;
  36. function GetRefName(const AId: integer): string;
  37.  
  38. const
  39. EM_DASH = #151;
  40.  
  41. const
  42. REF_COUNT = 9;
  43. REFERENCES: array[1..REF_COUNT] of string =
  44. ('auditoriums', 'days', 'disciplines', 'groups', 'lecturers', 'lessons',
  45. 'lecturers_disciplines', 'groups_disciplines', 'schedule');
  46.  
  47. SCHEDULE_INDEX = 9;
  48.  
  49. CM_REF_COUNT = 3;
  50. COMPLEX_REFERENCES = [7, 8, 9];
  51.  
  52. REF_INDEX: array[boolean, 1..REF_COUNT] of byte =
  53. ((1, 2, 3, 4, 5, 6, 0, 0, 0),
  54. (7, 8, 9, 0, 0, 0, 0, 0, 0));
  55.  
  56. MAX_FIELD_COUNT = 6;
  57. REF_FIELDS: array[1..REF_COUNT, 1..MAX_FIELD_COUNT] of string =
  58. (('name', '', '', '', '', ''),
  59. ('number', 'name', '', '', '', ''),
  60. ('name', '', '', '', '', ''),
  61. ('name', 'quantity', '', '', '', ''),
  62. ('name', '', '', '', '', ''),
  63. ('number', 'name', '', '', '', ''),
  64. ('lecturer_id', 'discipline_id', '', '', '', ''),
  65. ('group_id', 'discipline_id', '', '', '', ''),
  66. ('day_id', 'lesson_id', 'group_id', 'discipline_id', 'lecturer_id',
  67. 'auditorium_id'));
  68.  
  69. CM_REF_FK_ALIAS: array[1..CM_REF_COUNT, 1..REF_COUNT] of string =
  70. (('', '', 'discipline', '', 'lecturer', '', '', '', ''),
  71. ('', '', 'discipline', 'group', '', '', '', '', ''),
  72. ('auditorium', 'day', 'discipline', 'group', 'lecturer', 'lesson', '', '', ''));
  73.  
  74. CM_REF_FK_IDX: array[1..CM_REF_COUNT, 1..REF_COUNT] of byte =
  75. ((0, 0, 2, 0, 1, 0, 0, 0, 0),
  76. (0, 0, 2, 1, 0, 0, 0, 0, 0),
  77. (6, 1, 4, 3, 5, 2, 0, 0, 0));
  78.  
  79. CM_REF_FK_NAMES: array[0..5] of string =
  80. ('Day', 'Lesson', 'Group', 'Discipline', 'Lecturer', 'Auditorium');
  81.  
  82. const
  83. SELECT_TEMPLATE_START = 'select id, ';
  84. SELECT_TEMPLATE_END = ' from %s';
  85. DELETE_TEMPLATE = 'delete from %s where id = :id';
  86. INSERT_TEMPLATE_START = 'insert into %s values((gen_id(%0:s_id_gen, 1)),';
  87. INSERT_TEMPLATE_END = ')';
  88. MODIFY_TEMPLATE_START = 'update %s set ';
  89. MODIFY_TEMPLATE_END = ' where id = :old_id';
  90. REFRESH_TEMPLATE_START = SELECT_TEMPLATE_START;
  91. REFRESH_TEMPLATE_END = SELECT_TEMPLATE_END + ' where id = :id';
  92.  
  93. TEMPLATES: array[TSQLQueryIndex, TSQLElements] of string =
  94. (('%s,', SELECT_TEMPLATE_START, SELECT_TEMPLATE_END),
  95. ('', DELETE_TEMPLATE, ''),
  96. (':%s,', INSERT_TEMPLATE_START, INSERT_TEMPLATE_END),
  97. ('%s = :%0:s,', MODIFY_TEMPLATE_START, MODIFY_TEMPLATE_END),
  98. ('%s,', REFRESH_TEMPLATE_START, REFRESH_TEMPLATE_END));
  99.  
  100. const
  101. FK_SELECT = 'select %s.id, %s from %0:s %2:s';
  102. FK_REFRESH = FK_SELECT + ' where %0:s.id = :id';
  103.  
  104. FK_TEMPLATES: array[TSQLComplexQueryIndex] of string =
  105. (FK_SELECT, FK_REFRESH);
  106.  
  107. START_FMT_TEMPLATE = '%s.name as "%s",';
  108. END_FMT_TEMPLATE = 'inner join %s on %0:s.id = %s.%s_id ';
  109.  
  110. implementation
  111.  
  112. procedure DeleteAmp(var AString: string);
  113. begin
  114. Delete(AString, Pos('&', AString), 1);
  115. end;
  116.  
  117. procedure DeleteEndComma(var AString: string);
  118. begin
  119. if AnsiEndsStr(',', AString) then Delete(AString, Length(AString), 1);
  120. end;
  121.  
  122. function ReplaceDash(AString: string): string;
  123. begin
  124. Result := AnsiReplaceStr(AString, '-', EM_DASH);
  125. end;
  126.  
  127. function NormalizeStr(AString: string): string;
  128. var
  129. i: integer;
  130. begin
  131. Result := AnsiReplaceStr(AnsiLowerCase(AString), '_', ' ');
  132. Result := AnsiReplaceStr(Result, '"', '');
  133. for i := 1 to Length(Result) do
  134. if (i = 1) or (Result[i - 1] = ' ') then Result[i] := UpCase(Result[i]);
  135. end;
  136.  
  137. procedure InitStrings(const AStrings: TStrings);
  138. begin
  139. AStrings.Clear;
  140. AStrings.Add('');
  141. end;
  142.  
  143. function MakeCaptionFromTable(const ATableName: string): string;
  144. begin
  145. Result := NormalizeStr(ATableName);
  146. Result := ReplaceDash(AnsiReplaceStr(Result, ' ', ' - '));
  147. end;
  148.  
  149. function MakeCaptionForSchedule(const ATableName: string): string;
  150. begin
  151. Result := MakeCaptionFromTable(ATableName);
  152. Delete(Result, Length(Result), 1);
  153. end;
  154.  
  155. function GetRelatedRefIndex(ARefIndex: integer): integer;
  156. var
  157. i: integer;
  158. isComplexReference: boolean;
  159. begin
  160. Result := 0;
  161. isComplexReference := ARefIndex in COMPLEX_REFERENCES;
  162. for i := 1 to REF_COUNT do
  163. if REF_INDEX[isComplexReference, i] = ARefIndex then begin
  164. Result := i;
  165. Exit;
  166. end;
  167. end;
  168.  
  169. function MakeQueryStr(AQueryIndex: TSQLQueryIndex; ARefIndex: integer): string;
  170. var
  171. i: integer;
  172. fieldName: string;
  173. begin
  174. for i := 1 to MAX_FIELD_COUNT do begin
  175. fieldName := REF_FIELDS[ARefIndex, i];
  176. if fieldName <> '' then
  177. Result := Result + Format(TEMPLATES[AQueryIndex, FormatString],
  178. [fieldName]);
  179. end;
  180. DeleteEndComma(Result);
  181. Result :=
  182. Format(TEMPLATES[AQueryIndex, FirstQueryPart], [REFERENCES[ARefIndex]]) +
  183. Result +
  184. Format(TEMPLATES[AQueryIndex, LastQueryPart], [REFERENCES[ARefIndex]]);
  185. end;
  186.  
  187. function MakeComplexQueryStr(
  188. AQueryIndex: TSQLComplexQueryIndex; ARefIndex: integer): string;
  189. var
  190. endStr: string;
  191. i, foreignIndex, fieldIndex: integer;
  192. fieldList: array[1..REF_COUNT] of string;
  193. begin
  194. Result := '';
  195. endStr := '';
  196. foreignIndex := GetRelatedRefIndex(ARefIndex);
  197. for i := 1 to REF_COUNT do begin
  198. if CM_REF_FK_ALIAS[foreignIndex, i] <> '' then begin
  199. fieldIndex := CM_REF_FK_IDX[foreignIndex, i];
  200. fieldList[fieldIndex] := Format(START_FMT_TEMPLATE,
  201. [REFERENCES[i], CM_REF_FK_ALIAS[foreignIndex, i]]);
  202. endStr := endStr + Format(END_FMT_TEMPLATE,
  203. [REFERENCES[i], REFERENCES[ARefIndex], CM_REF_FK_ALIAS[foreignIndex, i]]);
  204. end;
  205. end;
  206. for i := 1 to REF_COUNT do
  207. if fieldList[i] <> '' then Result := Result + fieldList[i];
  208. DeleteEndComma(Result);
  209. Result := Format(FK_TEMPLATES[AQueryIndex],
  210. [REFERENCES[ARefIndex], Result, endStr]);
  211. end;
  212.  
  213. function GetNeededRefIndex(AField: string; ARefIndex: integer): integer;
  214. begin
  215. Result := AnsiIndexText(
  216. AField, CM_REF_FK_ALIAS[GetRelatedRefIndex(ARefIndex)]) + 1;
  217. end;
  218.  
  219. function MakeCustomSelect(AField: string; ARefIndex: integer): string;
  220. var
  221. neededRefIndex: integer;
  222. begin
  223. neededRefIndex := GetNeededRefIndex(AField, ARefIndex);
  224. Result := MakeQueryStr(SelectIndex, neededRefIndex);
  225. end;
  226.  
  227. procedure SetLookupFields(
  228. var ABox: TDBLookupCombobox; const AFieldName: string);
  229. const
  230. NAME = 'name';
  231. KEY = 'id';
  232. begin
  233. ABox.DataField := Format('%s_%s', [AFieldName, KEY]);
  234. ABox.ListField := NAME;
  235. ABox.KeyField := KEY;
  236. end;
  237.  
  238. procedure ReOpenDataSet(DSet: TIBDataSet);
  239. begin
  240. DSet.Close;
  241. DSet.Open;
  242. end;
  243.  
  244. function MakeIdField(const AIndex: integer): string;
  245. begin
  246. Result := REFERENCES[AIndex];
  247. Delete(Result, Length(Result), 1);
  248. Result := Format('%s_id', [Result]);
  249. end;
  250.  
  251. function GetRefName(const AId: integer): string;
  252. begin
  253. Result := NormalizeStr(CM_REF_FK_ALIAS[3, AId]);
  254. end;
  255.  
  256. end.
Advertisement
Add Comment
Please, Sign In to add comment