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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
------------------------------------------------------------------------------
-- G N A T C O L L --
-- --
-- Copyright (C) 2005-2018, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- --
-- --
-- --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;
with Interfaces.C;
with GNAT.Sockets;
with GNATCOLL.SQL.Postgres.Builder;
with GNATCOLL.SQL.Postgres.Gnade;
with GNATCOLL.Utils; use GNATCOLL.Utils;
package body GNATCOLL.SQL.Postgres is
N_OID : aliased constant String := "OID";
Comparison_Regexp : aliased constant String := " ~* ";
type Query_Postgres_Contents is new Query_Contents with record
Base : SQL_Query;
Extra : SQL_PG_Extension_Access;
end record;
overriding procedure Free (Self : in out Query_Postgres_Contents);
overriding function To_String
(Self : Query_Postgres_Contents;
Format : Formatter'Class) return Unbounded_String;
overriding procedure Auto_Complete
(Self : in out Query_Postgres_Contents;
Auto_Complete_From : Boolean := True;
Auto_Complete_Group_By : Boolean := True);
-- Supports adding a suffix string to the base_query
type SQL_PG_For_Update is new SQL_PG_Extension with record
Tables : SQL_Table_List := Empty_Table_List;
-- List of updated tables (empty means ALL tables in query)
No_Wait : Boolean := False;
-- Set True if NO WAIT
end record;
overriding function To_String
(Self : SQL_PG_For_Update;
Format : Formatter'Class) return Unbounded_String;
-- Extensions for UPDATE
type SQL_PG_Returning is new SQL_PG_Extension with record
Returning : SQL_Field_List;
end record;
overriding function To_String
(Self : SQL_PG_Returning;
Format : Formatter'Class) return Unbounded_String;
-- Extensions for SELECT
type Postgres_Engine is new Database_Engine with null record;
overriding function Setup
(Engine : Postgres_Engine;
Options : Name_Values.Map;
Errors : access Error_Reporter'Class) return Database_Description;
----------
-- Free --
----------
overriding procedure Free (Self : in out Query_Postgres_Contents) is
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
(SQL_PG_Extension'Class, SQL_PG_Extension_Access);
begin
Unchecked_Free (Self.Extra);
Free (Query_Contents (Self));
end Free;
---------------
-- To_String --
---------------
overriding function To_String
(Self : Query_Postgres_Contents;
Format : Formatter'Class) return Unbounded_String is
begin
return To_String (Self.Base, Format)
& To_String (Self.Extra.all, Format);
end To_String;
-------------------
-- Auto_Complete --
-------------------
overriding procedure Auto_Complete
(Self : in out Query_Postgres_Contents;
Auto_Complete_From : Boolean := True;
Auto_Complete_Group_By : Boolean := True) is
begin
Auto_Complete (Self.Base, Auto_Complete_From, Auto_Complete_Group_By);
end Auto_Complete;
-----------
-- Setup --
-----------
function Setup
(Database : String;
User : String := "";
Host : String := "";
Password : String := "";
Port : Integer := -1;
SSL : SSL_Mode := Allow;
Cache_Support : Boolean := True;
Errors : access Error_Reporter'Class := null)
return Database_Description
is
Result : Postgres_Description_Access;
begin
Result := new Postgres_Description
(Caching => Cache_Support, Errors => Errors);
Result.SSL := SSL;
Result.Dbname := To_XString (Database);
Result.User := To_XString (User);
Result.Password := To_XString (Password);
Result.Port := Port;
Result.Host := To_XString (Host);
return Database_Description (Result);
end Setup;
-----------
-- Setup --
-----------
overriding function Setup
(Engine : Postgres_Engine;
Options : Name_Values.Map;
Errors : access Error_Reporter'Class) return Database_Description
is
pragma Unreferenced (Engine);
type Setup_Parameters is
(Database, User, Host, Password, Port, SSL, Caching);
Params : array (Setup_Parameters) of Name_Values.Cursor;
function Value (P : Setup_Parameters; Default : String) return String is
(if Name_Values.Has_Element (Params (P))
then Name_Values.Element (Params (P)) else Default);
begin
for C in Options.Iterate loop
Params (Setup_Parameters'Value (Name_Values.Key (C))) := C;
end loop;
return Setup
(Database => Value (Database, ""),
User => Value (User, ""),
Host => Value (Host, ""),
Password => Value (Password, ""),
Port => Integer'Value (Value (Port, "-1")),
SSL => SSL_Mode'Value (Value (SSL, "Allow")),
Cache_Support => Boolean'Value (Value (Caching, "True")),
Errors => Errors);
end Setup;
----------------------
-- Build_Connection --
----------------------
overriding function Build_Connection
(Self : access Postgres_Description) return Database_Connection
is
DB : Database_Connection;
begin
DB := GNATCOLL.SQL.Postgres.Builder.Build_Connection (Self);
Reset_Connection (DB);
return DB;
end Build_Connection;
--------------
-- Notifies --
--------------
procedure Notifies
(DB : Database_Connection;
Message : out Notification;
Done : out Boolean) is
begin
Builder.To_Native (DB).Notifies (Message, Done);
end Notifies;
-------------------
-- Consume_Input --
-------------------
procedure Consume_Input (DB : Database_Connection) is
DBG : constant access Gnade.Database'Class := Builder.To_Native (DB);
begin
if not DBG.Consume_Input then
DB.Set_Failure (DBG.Error);
end if;
end Consume_Input;
--------------------
-- Wait_For_Input --
--------------------
function Wait_For_Input
(DB : Database_Connection;
Timeout : Duration := Duration'Last) return Boolean
is
use GNAT.Sockets;
function To_Ada is new Ada.Unchecked_Conversion
(Interfaces.C.int, Socket_Type);
DBG : constant access Gnade.Database'Class := Builder.To_Native (DB);
Sel : Selector_Type;
Soc : constant Socket_Type := To_Ada (DBG.Socket);
St : Selector_Status;
SS : Socket_Set_Type;
SE : Socket_Set_Type;
Rq : Request_Type (N_Bytes_To_Read);
begin
if DBG.Is_Non_Blocking then
raise Program_Error with "Non blocking connection is not supported";
end if;
Set (SS, Soc);
Create_Selector (Sel);
Check_Selector
(Sel, R_Socket_Set => SS, W_Socket_Set => SE, Status => St,
Timeout => Duration'Min (Forever, Timeout));
Close_Selector (Sel);
if St = Completed then
Control_Socket (Soc, Rq);
if Rq.Size = 0 then
-- Socket ready to read but without data available mean socket
-- closed by peer.
DB.Set_Failure ("Connection closed on PostgreSQL server side");
return False;
end if;
if not DBG.Consume_Input then
DB.Set_Failure (DBG.Error);
return False;
end if;
return True;
end if;
return False;
end Wait_For_Input;
---------------
-- OID_Field --
---------------
function OID_Field (Table : SQL_Table'Class) return SQL_Field_Integer is
begin
return SQL_Field_Integer'
(Table => Table.Table_Name,
Instance => Table.Instance,
Instance_Index => Table.Instance_Index,
Name => N_OID'Access);
end OID_Field;
------------
-- Regexp --
------------
function Regexp
(Self : Text_Fields.Field'Class;
Str : String) return SQL_Criteria is
begin
return Compare (Self, Expression (Str), Comparison_Regexp'Access);
end Regexp;
----------------
-- For_Update --
----------------
function For_Update
(Tables : SQL_Table_List := Empty_Table_List;
No_Wait : Boolean := False) return SQL_PG_Extension'Class
is
begin
return SQL_PG_For_Update'(Tables => Tables, No_Wait => No_Wait);
end For_Update;
---------------
-- Returning --
---------------
function Returning
(Fields : SQL_Field_List) return SQL_PG_Extension'Class
is
begin
return SQL_PG_Returning'(Returning => Fields);
end Returning;
---------
-- "&" --
---------
function "&"
(Query : SQL_Query;
Extension : SQL_PG_Extension'Class) return SQL_Query
is
Data : Query_Postgres_Contents;
Q : SQL_Query;
begin
if Query.Get.all in Query_Postgres_Contents'Class then
-- Merge the information with what has already been set.
-- For now, assume that Extension is the same type as was
-- already set, since we have a single extension for Update
-- and a single extension for Select. Any other combination
-- is invalid.
if Extension in SQL_PG_For_Update'Class then
declare
Orig : SQL_PG_For_Update'Class renames
SQL_PG_For_Update'Class
(Query_Postgres_Contents'Class (Query.Get.all).Extra.all);
begin
Orig.Tables := Orig.Tables &
SQL_PG_For_Update'Class (Extension).Tables;
Orig.No_Wait := Orig.No_Wait or else
SQL_PG_For_Update'Class (Extension).No_Wait;
end;
else
declare
Orig : SQL_PG_Returning'Class renames
SQL_PG_Returning'Class
(Query_Postgres_Contents'Class (Query.Get.all).Extra.all);
begin
Orig.Returning := Orig.Returning &
SQL_PG_Returning'Class (Extension).Returning;
end;
end if;
return Query;
else
Data.Base := Query;
Data.Extra := new SQL_PG_Extension'Class'(Extension);
Q.Set (Data);
return Q;
end if;
end "&";
---------------
-- To_String --
---------------
overriding function To_String
(Self : SQL_PG_For_Update;
Format : Formatter'Class) return Unbounded_String
is
Result : Unbounded_String;
begin
Append (Result, " FOR UPDATE");
if Self.Tables /= Empty_Table_List then
Append (Result, " OF ");
Append (Result, To_String (Self.Tables, Format));
end if;
if Self.No_Wait then
Append (Result, " NO WAIT");
end if;
return Result;
end To_String;
---------------
-- To_String --
---------------
overriding function To_String
(Self : SQL_PG_Returning;
Format : Formatter'Class) return Unbounded_String
is
Result : Unbounded_String;
begin
Append (Result, " RETURNING ");
Append (Result, To_String (Self.Returning, Format, Long => True));
return Result;
end To_String;
---------------------------
-- Get_Connection_String --
---------------------------
function Get_Connection_String
(Description : Database_Description;
With_Password : Boolean) return String
is
Descr : constant Postgres_Description_Access :=
Postgres_Description_Access (Description);
Str : XString;
procedure Escape (Value : XString);
procedure Escape (Value : XString) is
begin
for C of Value loop
if C = ''' or else C = '\' then
Str.Append ('\');
end if;
Str.Append (C);
end loop;
end Escape;
begin
Str.Append ("dbname='");
Escape (Descr.Dbname);
Str.Append (''');
if Descr.User /= Null_XString then
Str.Append (" user='");
Escape (Descr.User);
Str.Append (''');
end if;
if Descr.Host /= Null_XString then
Str.Append (" host='");
Escape (Descr.Host);
Str.Append (''');
end if;
if Descr.Port /= -1 then
Str.Append (" port=" & Image (Descr.Port, Min_Width => 1));
end if;
if With_Password and then Descr.Password /= Null_XString then
Str.Append (" password='");
Escape (Descr.Password);
Str.Append (''');
end if;
case Descr.SSL is
when Disable => Str.Append (" sslmode=disable");
when Allow => Str.Append (" sslmode=allow");
when Prefer => Str.Append (" sslmode=prefer");
when Require => Str.Append (" sslmode=require");
end case;
return Str.To_String;
end Get_Connection_String;
end GNATCOLL.SQL.Postgres;
|