rptmasterdetaildataset.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. unit rptmasterdetaildataset;
  2. {$mode objfpc}{$H+}
  3. {$I demos.inc}
  4. interface
  5. uses
  6. Classes,
  7. SysUtils,
  8. fpreport,
  9. fpreportdb,
  10. db,
  11. sqldb,
  12. IBConnection,
  13. udapp;
  14. type
  15. { TMasterDetailDatasetDemo }
  16. TMasterDetailDatasetDemo = class(TReportDemoApp)
  17. private
  18. IBConnection1: TIBConnection;
  19. SQLTransaction1: TSQLTransaction;
  20. ProjectDS: TDataSource;
  21. qryProject: TSQLQuery;
  22. qryEmployee: TSQLQuery;
  23. qryProjBudget: TSQLQuery;
  24. ReportMasterData: TFPReportDatasetData;
  25. ReportDetailData: TFPReportDatasetData;
  26. ReportBudgetData: TFPReportDatasetData;
  27. Protected
  28. procedure CreateReportDesign; override;
  29. procedure InitialiseData; override;
  30. public
  31. constructor Create(AOwner : TComponent); override;
  32. destructor Destroy; override;
  33. Class function Description : string; override;
  34. end;
  35. implementation
  36. uses
  37. fpTTF;
  38. const
  39. // use alias, it is pre-defined in Firebird installations
  40. cDatabase = 'localhost:employee';
  41. // cDatabase = 'localhost:/usr/share/doc/firebird2.5-common-doc/examples/empbuild/employee.fdb';
  42. // cDatabase = '/opt/firebird/examples/empbuild/employee.fdb';
  43. { TMasterDetailDatasetDemo }
  44. procedure TMasterDetailDatasetDemo.CreateReportDesign;
  45. var
  46. p: TFPReportPage;
  47. TitleBand: TFPReportTitleBand;
  48. MasterDataBand: TFPReportDataBand;
  49. DetailDataBand: TFPReportDataBand;
  50. ProjBudgetBand: TFPReportDataBand;
  51. Memo: TFPReportMemo;
  52. DataHeader: TFPReportDataHeaderBand;
  53. BudgetDataHeader: TFPReportDataHeaderBand;
  54. begin
  55. Inherited;
  56. rpt.Author := 'Graeme Geldenhuys';
  57. rpt.Title := 'FPReport Demo 11 - Master/Detail using datasets';
  58. p := TFPReportPage.Create(rpt);
  59. p.Orientation := poPortrait;
  60. p.PageSize.PaperName := 'A4';
  61. { page margins }
  62. p.Margins.Left := 30;
  63. p.Margins.Top := 20;
  64. p.Margins.Right := 30;
  65. p.Margins.Bottom := 20;
  66. p.Data := ReportMasterData;
  67. p.Font.Name := 'LiberationSans';
  68. // ======== ReportTitle band ===========
  69. TitleBand := TFPReportTitleBand.Create(p);
  70. TitleBand.Layout.Height := 20;
  71. {$ifdef ColorBands}
  72. TitleBand.Frame.Shape := fsRectangle;
  73. TitleBand.Frame.BackgroundColor := clReportTitleSummary;
  74. {$endif}
  75. Memo := TFPReportMemo.Create(TitleBand);
  76. Memo.Layout.Left := 0;
  77. Memo.Layout.Top := 0;
  78. Memo.Layout.Width := TitleBand.Layout.Width;
  79. Memo.Layout.Height := 15;
  80. Memo.UseParentFont := False;
  81. Memo.Font.Name := 'LiberationSans-Bold';
  82. Memo.Font.Size := 18;
  83. Memo.Text := 'FPReport Demo 11' + LineEnding + 'Master/Detail using datasets';
  84. Memo.TextAlignment.Vertical := tlCenter;
  85. Memo.TextAlignment.Horizontal := taCentered;
  86. // ======== MasterData band ===========
  87. MasterDataBand := TFPReportDataBand.Create(p);
  88. MasterDataBand.Layout.Height := 8;
  89. {$ifdef ColorBands}
  90. MasterDataBand.Frame.Shape := fsRectangle;
  91. MasterDataBand.Frame.BackgroundColor := clDataBand;
  92. {$endif}
  93. MasterDataBand.Data:=ReportMasterData;
  94. Memo := TFPReportMemo.Create(MasterDataBand);
  95. Memo.Layout.Left := 5;
  96. Memo.Layout.Top := 2;
  97. Memo.Layout.Width := 50;
  98. Memo.Layout.Height := 5;
  99. Memo.Text := '[reportmasterdata.proj_id] - [reportmasterdata.proj_name]';
  100. Memo.TextAlignment.Vertical := tlCenter;
  101. Memo.Frame.Shape := fsRectangle;
  102. Memo.Frame.BackgroundColor := clLtGray;
  103. // ======== DataHeader band for DetailData ===========
  104. DataHeader := TFPReportDataHeaderBand.Create(p);
  105. DataHeader.Layout.Height := 8;
  106. {$ifdef ColorBands}
  107. DataHeader.Frame.Shape := fsRectangle;
  108. DataHeader.Frame.BackgroundColor := clDataHeaderFooter;
  109. {$endif}
  110. DataHeader.Data:=ReportDetailData;
  111. Memo := TFPReportMemo.Create(DataHeader);
  112. Memo.Layout.Left := 15;
  113. Memo.Layout.Top := 3;
  114. Memo.Layout.Width := 15;
  115. Memo.Layout.Height := 5;
  116. Memo.Text := 'Emp No.';
  117. Memo.TextAlignment.Vertical := tlCenter;
  118. Memo.Options := [moDisableWordWrap];
  119. Memo.Frame.Shape := fsRectangle;
  120. Memo.Frame.Color := clBlack;
  121. Memo.Frame.BackgroundColor := clLtGray;
  122. Memo := TFPReportMemo.Create(DataHeader);
  123. Memo.Layout.Left := 30;
  124. Memo.Layout.Top := 3;
  125. Memo.Layout.Width := 50;
  126. Memo.Layout.Height := 5;
  127. Memo.Text := 'Employee Name';
  128. Memo.TextAlignment.Vertical := tlCenter;
  129. Memo.TextAlignment.Horizontal := taCentered;
  130. Memo.Frame.Shape := fsRectangle;
  131. Memo.Frame.Color := clBlack;
  132. Memo.Frame.BackgroundColor := clLtGray;
  133. // ======== DetailData band ===========
  134. DetailDataBand := TFPReportDataBand.Create(p);
  135. DetailDataBand.Layout.Height := 5;
  136. DetailDataBand.Data := ReportDetailData;
  137. { associate this band with the MasterData band }
  138. DetailDataBand.MasterBand := MasterDataBand;
  139. { associate DataHeader band }
  140. DetailDataBand.DisplayPosition := 0;
  141. {$ifdef ColorBands}
  142. DetailDataBand.Frame.Shape := fsRectangle;
  143. DetailDataBand.Frame.BackgroundColor := clChildBand;
  144. {$endif}
  145. Memo := TFPReportMemo.Create(DetailDataBand);
  146. Memo.Layout.Left := 15;
  147. Memo.Layout.Top := 0;
  148. Memo.Layout.Width := 15;
  149. Memo.Layout.Height := 5;
  150. Memo.Text := '[reportdetaildata.emp_no]';
  151. Memo.TextAlignment.Vertical := tlCenter;
  152. Memo.Frame.Shape := fsRectangle;
  153. Memo.Frame.Color := clBlack;
  154. Memo := TFPReportMemo.Create(DetailDataBand);
  155. Memo.Layout.Left := 30;
  156. Memo.Layout.Top := 0;
  157. Memo.Layout.Width := 50;
  158. Memo.Layout.Height := 5;
  159. Memo.Text := '[reportdetaildata.first_name] [reportdetaildata.last_name]';
  160. Memo.TextAlignment.Vertical := tlCenter;
  161. Memo.Frame.Shape := fsRectangle;
  162. Memo.Frame.Color := clBlack;
  163. // ======== DataHeader band for DetailData ===========
  164. BudgetDataHeader := TFPReportDataHeaderBand.Create(p);
  165. BudgetDataHeader.Layout.Height := 8;
  166. {$ifdef ColorBands}
  167. BudgetDataHeader.Frame.Shape := fsRectangle;
  168. BudgetDataHeader.Frame.BackgroundColor := clDataHeaderFooter;
  169. {$endif}
  170. BudgetDataHeader.Data:=ReportBudgetData;
  171. Memo := TFPReportMemo.Create(BudgetDataHeader);
  172. Memo.Layout.Left := 15;
  173. Memo.Layout.Top := 3;
  174. Memo.Layout.Width := 15;
  175. Memo.Layout.Height := 5;
  176. Memo.Text := 'Year';
  177. Memo.TextAlignment.Vertical := tlCenter;
  178. Memo.TextAlignment.Horizontal := taCentered;
  179. Memo.Frame.Shape := fsRectangle;
  180. Memo.Frame.Color := clBlack;
  181. Memo.Frame.BackgroundColor := clLtGray;
  182. Memo := TFPReportMemo.Create(BudgetDataHeader);
  183. Memo.Layout.Left := 30;
  184. Memo.Layout.Top := 3;
  185. Memo.Layout.Width := 50;
  186. Memo.Layout.Height := 5;
  187. Memo.Text := 'Budget';
  188. Memo.TextAlignment.Vertical := tlCenter;
  189. Memo.TextAlignment.Horizontal := taCentered;
  190. Memo.Frame.Shape := fsRectangle;
  191. Memo.Frame.Color := clBlack;
  192. Memo.Frame.BackgroundColor := clLtGray;
  193. // ======== Project Budget band ===========
  194. ProjBudgetBand := TFPReportDataBand.Create(p);
  195. ProjBudgetBand.Layout.Height := 5;
  196. ProjBudgetBand.Data := ReportBudgetData;
  197. { associate this band with the MasterData band }
  198. ProjBudgetBand.MasterBand := MasterDataBand;
  199. ProjBudgetBand.DisplayPosition := 1;
  200. {$ifdef ColorBands}
  201. ProjBudgetBand.Frame.Shape := fsRectangle;
  202. ProjBudgetBand.Frame.BackgroundColor := clChildBand;
  203. {$endif}
  204. Memo := TFPReportMemo.Create(ProjBudgetBand);
  205. Memo.Layout.Left := 15;
  206. Memo.Layout.Top := 0;
  207. Memo.Layout.Width := 15;
  208. Memo.Layout.Height := 5;
  209. Memo.Text := '[ReportBudgetData.FISCAL_YEAR]';
  210. Memo.TextAlignment.Vertical := tlCenter;
  211. Memo.TextAlignment.Horizontal := taCentered;
  212. Memo.Frame.Shape := fsRectangle;
  213. Memo.Frame.Color := clBlack;
  214. Memo := TFPReportMemo.Create(ProjBudgetBand);
  215. Memo.Layout.Left := 30;
  216. Memo.Layout.Top := 0;
  217. Memo.Layout.Width := 50;
  218. Memo.Layout.Height := 5;
  219. Memo.Text := '[formatfloat(''#,##0'', ReportBudgetData.PROJECTED_BUDGET)]';
  220. Memo.TextAlignment.Vertical := tlCenter;
  221. Memo.TextAlignment.Horizontal := taRightJustified;
  222. Memo.Frame.Shape := fsRectangle;
  223. Memo.Frame.Color := clBlack;
  224. end;
  225. procedure TMasterDetailDatasetDemo.InitialiseData;
  226. begin
  227. SQLTransaction1 := TSQLTransaction.Create(Self);
  228. IBConnection1 := TIBConnection.Create(Self);
  229. with IBConnection1 do
  230. begin
  231. LoginPrompt := False;
  232. DatabaseName := cDatabase;
  233. KeepConnection := False;
  234. Password := 'masterkey';
  235. Transaction := SQLTransaction1;
  236. UserName := 'sysdba';
  237. Connected := True;
  238. end;
  239. qryProject := TSQLQuery.Create(Self);
  240. with qryProject do
  241. begin
  242. Database := IBConnection1;
  243. Transaction := SQLTransaction1;
  244. ReadOnly := True;
  245. SQL.Text := 'select * from PROJECT order by PROJ_NAME';
  246. Active := True
  247. end;
  248. ProjectDS := TDataSource.Create(Self);
  249. ProjectDS.DataSet := qryProject;
  250. qryEmployee := TSQLQuery.Create(Self);
  251. with qryEmployee do
  252. begin
  253. Database := IBConnection1;
  254. Transaction := SQLTransaction1;
  255. ReadOnly := True;
  256. SQL.Text :=
  257. 'SELECT ' +
  258. ' e.* ' +
  259. 'FROM EMPLOYEE e ' +
  260. ' INNER JOIN EMPLOYEE_PROJECT ep on e.EMP_NO = ep.EMP_NO ' +
  261. 'where ep.proj_id = :proj_id ' +
  262. 'order by e.EMP_NO';
  263. DataSource := ProjectDS;
  264. Active := True
  265. end;
  266. qryProjBudget := TSQLQuery.Create(Self);
  267. with qryProjBudget do
  268. begin
  269. Database := IBConnection1;
  270. Transaction := SQLTransaction1;
  271. ReadOnly := True;
  272. SQL.Text :=
  273. 'SELECT ' +
  274. ' b.FISCAL_YEAR, b.PROJECTED_BUDGET ' +
  275. 'FROM PROJ_DEPT_BUDGET b ' +
  276. 'where b.proj_id = :proj_id ' +
  277. 'order by b.FISCAL_YEAR';
  278. DataSource := ProjectDS;
  279. Active := True
  280. end;
  281. ReportMasterData.DataSet:= qryProject;
  282. ReportDetailData.DataSet:= qryEmployee;
  283. ReportBudgetData.DataSet:= qryProjBudget;
  284. rpt.ReportData.AddReportData(ReportMasterData);
  285. rpt.ReportData.AddReportData(ReportDetailData);
  286. rpt.ReportData.AddReportData(ReportBudgetData);
  287. end;
  288. constructor TMasterDetailDatasetDemo.Create(AOwner: TComponent);
  289. begin
  290. Inherited;
  291. ReportMasterData := TFPReportDatasetData.Create(Self);
  292. ReportMasterData.Name := 'ReportMasterData';
  293. ReportDetailData := TFPReportDatasetData.Create(Self);
  294. ReportDetailData.Name := 'ReportDetailData';
  295. ReportBudgetData := TFPReportDatasetData.Create(Self);
  296. ReportBudgetData.Name := 'ReportBudgetData';
  297. end;
  298. destructor TMasterDetailDatasetDemo.Destroy;
  299. begin
  300. IBConnection1.Close();
  301. FreeAndNil(qryEmployee);
  302. FreeAndNil(qryProject);
  303. FreeAndNil(ReportMasterData);
  304. FreeAndNil(ReportDetailData);
  305. FreeAndNil(ReportBudgetData);
  306. FreeAndNil(ProjectDS);
  307. FreeAndNil(SQLTransaction1);
  308. FreeAndNil(IBConnection1);
  309. inherited Destroy;
  310. end;
  311. class function TMasterDetailDatasetDemo.Description: string;
  312. begin
  313. Result:='Demo for support of master-detail relations with datasets';
  314. end;
  315. end.