Persistence.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. {$MODE DELPHI}
  27. interface
  28. uses
  29. SysUtils,
  30. Classes,
  31. BufDataset,
  32. SQLdb,
  33. SQLite3Conn;
  34. function ListPersons: TStream;
  35. procedure SavePersons(const ABytes: TBytes);
  36. implementation
  37. const
  38. SQL_SELECT_PERSONS = 'SELECT * FROM persons';
  39. SQL_UDPATE_PERSONS = 'UPDATE persons SET name = :name WHERE id = :id';
  40. var
  41. DBConnection: TSQLConnector;
  42. procedure CreateAndConfigureDBConnection;
  43. begin
  44. DBConnection := TSQLConnector.Create(nil);
  45. DBConnection.Transaction := TSQLTransaction.Create(DBConnection);
  46. DBConnection.ConnectorType := 'SQLite3';
  47. DBConnection.DatabaseName := '../../../DB/DataBase.sqlite3';
  48. end;
  49. procedure DestroyDBConnection;
  50. begin
  51. FreeAndNil(DBConnection);
  52. end;
  53. function CreateQuery(const ASQL: string): TSQLQuery;
  54. begin
  55. Result := TSQLQuery.Create(nil);
  56. Result.SQLConnection := DBConnection;
  57. Result.SQLTransaction := DBConnection.Transaction;
  58. Result.SQL.Text := ASQL;
  59. end;
  60. function ListPersons: TStream;
  61. var
  62. VQuery: TSQLQuery;
  63. begin
  64. Result := TBytesStream.Create;
  65. VQuery := CreateQuery(SQL_SELECT_PERSONS);
  66. try
  67. VQuery.Open;
  68. VQuery.SaveToStream(Result, dfBinary);
  69. Result.Seek(0, TSeekOrigin.soBeginning);
  70. finally
  71. VQuery.Destroy;
  72. end;
  73. end;
  74. procedure SavePersons(const ABytes: TBytes);
  75. var
  76. VQuery: TSQLQuery;
  77. VData: TBytesStream;
  78. begin
  79. VQuery := CreateQuery(SQL_SELECT_PERSONS);
  80. VData := TBytesStream.Create(ABytes);
  81. try
  82. VQuery.UpdateSQL.Text := SQL_UDPATE_PERSONS;
  83. VQuery.Prepare;
  84. VQuery.LoadFromStream(VData, dfBinary);
  85. VQuery.ApplyUpdates;
  86. DBConnection.Transaction.Commit;
  87. finally
  88. VQuery.Destroy;
  89. VData.Free;
  90. end;
  91. end;
  92. initialization
  93. CreateAndConfigureDBConnection;
  94. finalization
  95. DestroyDBConnection;
  96. end.