DMPersistence.pas 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 DMPersistence;
  26. {$MODE DELPHI}
  27. interface
  28. uses
  29. SysUtils,
  30. Classes,
  31. SQLDB,
  32. BufDataset,
  33. SQLite3Conn;
  34. type
  35. TPersistence = class(TDataModule)
  36. SQLConnector: TSQLConnector;
  37. SQLQuery: TSQLQuery;
  38. SQLTransaction: TSQLTransaction;
  39. public
  40. function ListPersons: TStream;
  41. procedure SavePersons(const ABytes: TBytes);
  42. end;
  43. var
  44. Persistence: TPersistence;
  45. implementation
  46. {$R *.lfm}
  47. function TPersistence.ListPersons: TStream;
  48. begin
  49. Result := TBytesStream.Create;
  50. SQLQuery.Close;
  51. SQLQuery.Open;
  52. SQLQuery.SaveToStream(Result, dfBinary);
  53. Result.Seek(0, TSeekOrigin.soBeginning);
  54. end;
  55. procedure TPersistence.SavePersons(const ABytes: TBytes);
  56. var
  57. VData: TBytesStream;
  58. begin
  59. VData := TBytesStream.Create(ABytes);
  60. try
  61. SQLQuery.Close;
  62. SQLQuery.Prepare;
  63. SQLQuery.LoadFromStream(VData, dfBinary);
  64. SQLQuery.ApplyUpdates;
  65. SQLTransaction.Commit;
  66. finally
  67. VData.Free;
  68. end;
  69. end;
  70. end.