1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--                           SQL Database Access                            --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2011-2014, Vadim Godunko <vgodunko@gmail.com>                --
12-- All rights reserved.                                                     --
13--                                                                          --
14-- Redistribution and use in source and binary forms, with or without       --
15-- modification, are permitted provided that the following conditions       --
16-- are met:                                                                 --
17--                                                                          --
18--  * Redistributions of source code must retain the above copyright        --
19--    notice, this list of conditions and the following disclaimer.         --
20--                                                                          --
21--  * Redistributions in binary form must reproduce the above copyright     --
22--    notice, this list of conditions and the following disclaimer in the   --
23--    documentation and/or other materials provided with the distribution.  --
24--                                                                          --
25--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
26--    contributors may be used to endorse or promote products derived from  --
27--    this software without specific prior written permission.              --
28--                                                                          --
29-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
30-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
31-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
32-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
33-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
34-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
35-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
36-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
37-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
38-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
39-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
40--                                                                          --
41------------------------------------------------------------------------------
42--  $Revision: 4981 $ $Date: 2014-11-03 23:06:46 +0300 (Mon, 03 Nov 2014) $
43------------------------------------------------------------------------------
44--  Implementation of database abstraction for PostgreSQL.
45------------------------------------------------------------------------------
46private with Ada.Containers.Ordered_Maps;
47
48package Matreshka.Internals.SQL_Drivers.PostgreSQL.Databases is
49
50   type PostgreSQL_Database is new Abstract_Database with private;
51
52   type Data_Types is (Text_Data, Integer_Data, Float_Data, Timestamp_Data);
53
54   function Allocate_Statement_Name
55    (Self : not null access PostgreSQL_Database'Class)
56       return Interfaces.C.Strings.chars_ptr;
57   --  Allocates statement identifier. Returns name as C pointer to be used
58   --  in low level API. Returned name must be deallocated by caller.
59
60   function Handle
61    (Self : not null access PostgreSQL_Database'Class) return PGconn_Access;
62   --  Returns database connection handle.
63
64   function Get_Error_Message
65    (Self : not null access PostgreSQL_Database'Class)
66       return League.Strings.Universal_String;
67   --  Returns current error message.
68
69   function Get_Type
70    (Self     : not null access PostgreSQL_Database'Class;
71     Type_Oid : Oid) return Data_Types;
72   --  Returns base type of the type with specified Oid.
73
74   function New_String
75    (Item : League.Strings.Universal_String)
76       return Interfaces.C.Strings.chars_ptr;
77   --  Converts Universal_String into client encoding, allocates and returns C
78   --  style string. Returned object must be dellocated by caller.
79
80private
81
82   package Maps is new Ada.Containers.Ordered_Maps (Oid, Data_Types);
83
84   type PostgreSQL_Database is new Abstract_Database with record
85      Handle    : PGconn_Access;
86      Error     : League.Strings.Universal_String;
87      Statement : Natural;
88      Type_Map  : Maps.Map;
89   end record;
90
91   overriding procedure Close (Self : not null access PostgreSQL_Database);
92
93   overriding procedure Commit (Self : not null access PostgreSQL_Database);
94
95   overriding function Error_Message
96    (Self : not null access PostgreSQL_Database)
97       return League.Strings.Universal_String;
98
99   overriding procedure Finalize (Self : not null access PostgreSQL_Database);
100
101   overriding function Open
102    (Self    : not null access PostgreSQL_Database;
103     Options : SQL.Options.SQL_Options) return Boolean;
104
105   overriding function Query
106    (Self : not null access PostgreSQL_Database) return not null Query_Access;
107
108end Matreshka.Internals.SQL_Drivers.PostgreSQL.Databases;
109