Persistence.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit Persistence;
  26. interface
  27. uses
  28. System.SysUtils,
  29. System.Classes,
  30. FireDAC.DApt,
  31. FireDAC.Stan.Def,
  32. FireDAC.Stan.Intf,
  33. FireDAC.Stan.Async,
  34. FireDAC.Phys.SQLite,
  35. FireDAC.Comp.Client,
  36. FireDAC.Stan.StorageBin;
  37. function ListPersons: TStream;
  38. procedure SavePersons(const ABytes: TBytes);
  39. implementation
  40. const
  41. SQL_SELECT_PERSONS = 'SELECT * FROM persons';
  42. SQL_INSERT_PERSONS = 'INSERT INTO persons (name) VALUES (:name)';
  43. var
  44. DBConnection: TFDConnection;
  45. procedure CreateAndConfigureDBConnection;
  46. begin
  47. DBConnection := TFDConnection.Create(nil);
  48. DBConnection.DriverName := 'SQLite';
  49. DBConnection.Params.Database := '../../../DB/DataBase.sqlite3';
  50. end;
  51. procedure DestroyDBConnection;
  52. begin
  53. FreeAndNil(DBConnection);
  54. end;
  55. function CreateQuery(const ASelectSQL: string;
  56. const AInsertSQL: string = ''): TFDQuery;
  57. var
  58. VUpdate: TFDUpdateSQL;
  59. begin
  60. Result := TFDQuery.Create(nil);
  61. if not AInsertSQL.IsEmpty then
  62. begin
  63. VUpdate := TFDUpdateSQL.Create(Result);
  64. VUpdate.Connection := DBConnection;
  65. VUpdate.InsertSQL.Text := AInsertSQL;
  66. Result.UpdateObject := VUpdate;
  67. end;
  68. Result.Connection := DBConnection;
  69. Result.CachedUpdates := True;
  70. Result.SQL.Text := ASelectSQL;
  71. end;
  72. function ListPersons: TStream;
  73. var
  74. VQuery: TFDQuery;
  75. begin
  76. Result := TBytesStream.Create;
  77. VQuery := CreateQuery(SQL_SELECT_PERSONS);
  78. try
  79. VQuery.Open;
  80. VQuery.SaveToStream(Result, sfBinary);
  81. Result.Seek(0, TSeekOrigin.soBeginning);
  82. finally
  83. VQuery.Destroy;
  84. end;
  85. end;
  86. procedure SavePersons(const ABytes: TBytes);
  87. var
  88. VQuery: TFDQuery;
  89. VData: TBytesStream;
  90. begin
  91. VQuery := CreateQuery(SQL_SELECT_PERSONS, SQL_INSERT_PERSONS);
  92. VData := TBytesStream.Create(ABytes);
  93. try
  94. VQuery.LoadFromStream(VData, sfBinary);
  95. VQuery.ApplyUpdates;
  96. DBConnection.Commit;
  97. finally
  98. VQuery.Destroy;
  99. VData.Free;
  100. end;
  101. end;
  102. initialization
  103. CreateAndConfigureDBConnection;
  104. finalization
  105. DestroyDBConnection;
  106. end.