1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--                           SQL Database Access                            --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2011-2012, 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: 3614 $ $Date: 2012-12-25 15:19:53 +0400 (Tue, 25 Dec 2012) $
43------------------------------------------------------------------------------
44--  This package provides low level binding to PostgreSQL database client
45--  library.
46------------------------------------------------------------------------------
47with Interfaces.C.Strings;
48
49package Matreshka.Internals.SQL_Drivers.PostgreSQL is
50
51   pragma Preelaborate;
52
53   -----------
54   -- Types --
55   -----------
56
57   type PGconn is limited private;
58
59   type PGconn_Access is access all PGconn;
60   pragma Convention (C, PGconn_Access);
61
62   type PGresult is limited private;
63
64   type PGresult_Access is access all PGresult;
65   pragma Convention (C, PGresult_Access);
66
67   type Oid is new Interfaces.C.int;
68
69   type Oid_Access is access all oid;
70   pragma Convention (C, Oid_Access);
71
72   type ConnStatusType is
73    (CONNECTION_OK,
74     CONNECTION_BAD,
75     CONNECTION_STARTED,
76     CONNECTION_MADE,
77     CONNECTION_AWAITING_RESPONSE,
78     CONNECTION_AUTH_OK,
79     CONNECTION_SETENV,
80     CONNECTION_SSL_STARTUP,
81     CONNECTION_NEEDED);
82   pragma Convention (C, ConnStatusType);
83
84   type ExecStatusType is
85    (PGRES_EMPTY_QUERY,
86     PGRES_COMMAND_OK,
87     PGRES_TUPLES_OK,
88     PGRES_COPY_OUT,
89     PGRES_COPY_IN,
90     PGRES_BAD_RESPONSE,
91     PGRES_NONFATAL_ERROR,
92     PGRES_FATAL_ERROR);
93   pragma Convention (C, ExecStatusType);
94
95   type chars_ptr_Access is access all Interfaces.C.Strings.chars_ptr;
96   pragma Convention (C, chars_ptr_Access);
97
98   type int_Access is access all Interfaces.C.int;
99   pragma Convention (C, int_Access);
100
101   -----------------
102   -- Subprograms --
103   -----------------
104
105   function pg_encoding_to_char
106    (encoding_id : Interfaces.C.int) return Interfaces.C.Strings.chars_ptr;
107   pragma Import (C, pg_encoding_to_char);
108
109   procedure PQclear (res : PGresult_Access);
110   pragma Import (C, PQclear, "PQclear");
111
112   function PQclientEncoding (conn : PGconn_Access) return Interfaces.C.int;
113   pragma Import (C, PQclientEncoding, "PQclientEncoding");
114
115   function PQconnectdbParams
116    (keywords     : Interfaces.C.Strings.chars_ptr_array;
117     values       : Interfaces.C.Strings.chars_ptr_array;
118     expand_dbnam : Interfaces.C.int) return PGconn_Access;
119   pragma Import (C, PQconnectdbParams, "PQconnectdbParams");
120
121   function PQerrorMessage
122    (conn : PGconn_Access) return Interfaces.C.Strings.chars_ptr;
123   pragma Import (C, PQerrorMessage, "PQerrorMessage");
124
125   function PQexec
126    (conn    : PGconn_Access;
127     command : Interfaces.C.Strings.chars_ptr) return PGresult_Access;
128   pragma Import (C, PQexec, "PQexec");
129
130   function PQexecPrepared
131    (conn         : PGconn_Access;
132     stmtName     : Interfaces.C.Strings.chars_ptr;
133     nParams      : Interfaces.C.int;
134--     paramValues  : chars_ptr_Access;
135     paramValues  : Interfaces.C.Strings.chars_ptr_array;
136     paramLengths : int_Access;
137     paramFormats : int_Access;
138     resultFormat : Interfaces.C.int) return PGresult_Access;
139   pragma Import (C, PQexecPrepared, "PQexecPrepared");
140
141   procedure PQfinish (conn : PGconn_Access);
142   pragma Import (C, PQfinish, "PQfinish");
143
144   function PQftype
145    (res : PGresult_Access; column_number : Interfaces.C.int) return Oid;
146   pragma Import (C, PQftype, "PQftype");
147
148   function PQgetisnull
149    (res           : PGresult_Access;
150     row_number    : Interfaces.C.int;
151     column_number : Interfaces.C.int) return Interfaces.C.int;
152   pragma Import (C, PQgetisnull, "PQgetisnull");
153
154   function PQgetlength
155    (res           : PGresult_Access;
156     row_number    : Interfaces.C.int;
157     column_number : Interfaces.C.int) return Interfaces.C.int;
158   pragma Import (C, PQgetlength, "PQgetlength");
159
160   function PQgetvalue
161    (res           : PGresult_Access;
162     row_number    : Interfaces.C.int;
163     column_number : Interfaces.C.int) return Interfaces.C.Strings.chars_ptr;
164   pragma Import (C, PQgetvalue, "PQgetvalue");
165
166   function PQisthreadsafe return Interfaces.C.int;
167   pragma Import (C, PQisthreadsafe, "PQisthreadsafe");
168
169   function PQntuples (res : PGresult_Access) return Interfaces.C.int;
170   pragma Import (C, PQntuples, "PQntuples");
171
172   function PQprepare
173    (conn       : PGconn_Access;
174     stmtName   : Interfaces.C.Strings.chars_ptr;
175     query      : Interfaces.C.Strings.chars_ptr;
176     nParams    : Interfaces.C.int;
177     paramTypes : Oid_Access) return PGresult_Access;
178   pragma Import (C, PQprepare, "PQprepare");
179
180   function PQresultStatus (res : PGresult_Access) return ExecStatusType;
181   pragma Import (C, PQresultStatus, "PQresultStatus");
182
183   function PQsetClientEncoding
184    (conn     : PGconn_Access;
185     encoding : Interfaces.C.Strings.chars_ptr) return Interfaces.C.int;
186   pragma Import (C, PQsetClientEncoding, "PQsetClientEncoding");
187
188   function PQstatus (conn : PGconn_Access) return ConnStatusType;
189   pragma Import (C, PQstatus, "PQstatus");
190
191private
192
193   type PGconn is limited null record;
194
195   type PGresult is limited null record;
196
197end Matreshka.Internals.SQL_Drivers.PostgreSQL;
198