awa_unit_2.4.0_59135a52/awa/plugins/awa-storages/src/awa-storages-stores-databases.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-----------------------------------------------------------------------
--  awa-storages-stores-databases -- Database store
--  Copyright (C) 2012, 2015, 2016, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Streams.Stream_IO;
with Util.Log.Loggers;
package body AWA.Storages.Stores.Databases is

   Log : constant Util.Log.Loggers.Logger := Util.Log.Loggers.Create ("AWA.Storages.Stores.Files");

   --  Create a storage
   overriding
   procedure Create (Storage : in Database_Store;
                     Session : in out ADO.Sessions.Master_Session;
                     From    : in AWA.Storages.Models.Storage_Ref'Class;
                     Into    : in out AWA.Storages.Storage_File) is
   begin
      Storage.Tmp.Create (Session, From, Into);
   end Create;

   --  ------------------------------
   --  Save the file represented by the `Path` variable into a store and associate that
   --  content with the storage reference represented by `Into`.
   --  ------------------------------
   overriding
   procedure Save (Storage : in Database_Store;
                   Session : in out ADO.Sessions.Master_Session;
                   Into    : in out AWA.Storages.Models.Storage_Ref'Class;
                   Path    : in String) is
      pragma Unreferenced (Storage);

      Store : AWA.Storages.Models.Storage_Data_Ref;
      Blob  : constant ADO.Blob_Ref := ADO.Create_Blob (Path);
   begin
      Log.Info ("Save database file {0}", Path);
      Store.Set_Data (Blob);
      Store.Save (Session);
      Into.Set_File_Size (Natural (Blob.Value.Len));
      Into.Set_Store_Data (Store);
   end Save;

   overriding
   procedure Load (Storage : in Database_Store;
                   Session : in out ADO.Sessions.Session'Class;
                   From    : in AWA.Storages.Models.Storage_Ref'Class;
                   Into    : in out AWA.Storages.Storage_File) is
      Store : AWA.Storages.Models.Storage_Data_Ref'Class := From.Get_Store_Data;
      File  : Ada.Streams.Stream_IO.File_Type;
      DB    : ADO.Sessions.Master_Session := ADO.Sessions.Master_Session (Session);
   begin
      Storage.Tmp.Create (DB, From, Into);
      Log.Info ("Load database file {0} to {1}",
                ADO.Identifier'Image (Store.Get_Id), Get_Path (Into));
      Store.Load (Session, Store.Get_Id);
      Ada.Streams.Stream_IO.Create (File => File,
                                    Mode => Ada.Streams.Stream_IO.Out_File,
                                    Name => Get_Path (Into));
      Ada.Streams.Stream_IO.Write (File, Store.Get_Data.Value.Data);
      Ada.Streams.Stream_IO.Close (File);
   end Load;

   --  ------------------------------
   --  Delete the content associate with the storage represented by `From`.
   --  ------------------------------
   overriding
   procedure Delete (Storage : in Database_Store;
                     Session : in out ADO.Sessions.Master_Session;
                     From    : in out AWA.Storages.Models.Storage_Ref'Class) is
      pragma Unreferenced (Storage);

      Store : AWA.Storages.Models.Storage_Data_Ref'Class := From.Get_Store_Data;
   begin
      if not Store.Is_Null then
         Log.Info ("Delete file {0}", ADO.Identifier'Image (From.Get_Id));
         Store.Delete (Session);
      end if;
   end Delete;

end AWA.Storages.Stores.Databases;