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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
|
------------------------------------------------------------------------------
-- CRM --
-- [Customer Relationship Management] --
-- --
-- Copyright (C) 2009-2018, AdaCore --
------------------------------------------------------------------------------
with GNATCOLL.Traces; use GNATCOLL.Traces;
with GNATCOLL.Utils; use GNATCOLL.Utils;
package body GNATCOLL.SQL.Orm.Impl is
Me : constant Trace_Handle := Create ("ORM");
function Get_Data
(Self : Forward_Cursor'Class) return Shared_List_Data_Access;
pragma Inline (Get_Data);
-- Return access to the data used by a list. The returned access is only
-- valid while the list exists.
-------------
-- Is_Null --
-------------
function Is_Null (Self : Orm_Element) return Boolean is
begin
return Self.Index = -1;
end Is_Null;
--------------
-- Get_Data --
--------------
function Get_Data
(Self : Forward_Cursor'Class) return Shared_List_Data_Access is
begin
-- Not very elegant, but this avoids duplicating this function for
-- both types of lists in all the generated packages.
-- The Unchecked_Access is safe because tagged types are always passed
-- by reference (ARM)
if Self in Forward_List'Class then
return Forward_List (Self).Data'Unchecked_Access;
elsif Self in Direct_List'Class then
return Direct_List (Self).Data'Unchecked_Access;
else
return null;
end if;
end Get_Data;
-------------------
-- Integer_Value --
-------------------
function Integer_Value
(Self : Orm_Element'Class; Field : Field_Index) return Integer is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return Integer'First;
else
return Integer_Value (Self.Current, Self.Column + Field);
end if;
end Integer_Value;
------------------
-- Bigint_Value --
------------------
function Bigint_Value
(Self : Orm_Element'Class; Field : Field_Index)
return Long_Long_Integer is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return Long_Long_Integer'First;
else
return Bigint_Value (Self.Current, Self.Column + Field);
end if;
end Bigint_Value;
-------------------
-- Boolean_Value --
-------------------
function Boolean_Value
(Self : Orm_Element'Class; Field : Field_Index) return Boolean is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return False;
else
return Boolean_Value (Self.Current, Self.Column + Field);
end if;
end Boolean_Value;
-------------------
-- String_Value --
-------------------
function String_Value
(Self : Orm_Element'Class; Field : Field_Index) return String is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return "";
else
return Value (Self.Current, Self.Column + Field);
end if;
end String_Value;
------------------
-- String_Value --
------------------
function String_Value
(Self : Orm_Element'Class; Field : Field_Index) return Unbounded_String is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return Null_Unbounded_String;
else
return Unbounded_Value (Self.Current, Self.Column + Field);
end if;
end String_Value;
----------------
-- Time_Value --
----------------
function Time_Value
(Self : Orm_Element'Class; Field : Field_Index) return Ada.Calendar.Time
is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return No_Time;
else
return Time_Value (Self.Current, Self.Column + Field);
end if;
end Time_Value;
-----------------
-- Float_Value --
------------------
function Float_Value
(Self : Orm_Element'Class; Field : Field_Index) return Float is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return Float'First;
else
return Float_Value (Self.Current, Self.Column + Field);
end if;
end Float_Value;
-----------------
-- Money_Value --
-----------------
function Money_Value
(Self : Orm_Element'Class; Field : Field_Index)
return T_Money is
begin
if Current (Self.Current) /= Self.Index then
raise Cursor_Has_Moved;
end if;
if Is_Null (Self.Current, Self.Column + Field) then
return T_Money'First;
else
return Money_Value (Self.Current, Self.Column + Field);
end if;
end Money_Value;
----------------------
-- Generic_Managers --
----------------------
package body Generic_Managers is
function Internal_Element
(Self : Forward_Cursor'Class;
Column : Field_Index;
Data : Shared_List_Data;
Depth : Natural) return Element_Type;
pragma Inline (Internal_Element);
----------------------
-- Internal_Element --
----------------------
function Internal_Element
(Self : Forward_Cursor'Class;
Column : Field_Index;
Data : Shared_List_Data;
Depth : Natural) return Element_Type
is
begin
return Result : Element_Type do
Result.Index := Current (Self);
Result.Column := Column;
Result.Depth := Depth;
Result.Data := Data;
Result.Current := Forward_Cursor (Self);
end return;
end Internal_Element;
----------------------
-- Internal_Element --
----------------------
function Internal_Element
(Self : Orm_Element'Class;
Field : Field_Index) return Element_Type is
begin
return Internal_Element
(Self.Current, Self.Column + Field, Self.Data, Self.Depth - 1);
end Internal_Element;
-------------
-- Element --
-------------
function Element
(Self : List'Class) return Element_Type
is
Data : constant Shared_List_Data_Access := Get_Data (Self);
begin
Assert (Me, Data /= null, "No data stored in list of elements");
Assert
(Me, Data.Session /= No_Session, "Session from list was released");
return Internal_Element (Self, 0, Get_Data (Self).all, Self.Depth);
end Element;
-------------
-- Element --
-------------
function Element
(Self : Direct_List'Class) return Element_Type
is
begin
return Internal_Element (Self, 0, Get_Data (Self).all, Self.Depth);
end Element;
-----------------
-- Build_Query --
-----------------
function Build_Query
(Self : Manager_Type'Class;
Override_Fields : SQL_Field_List := Empty_Field_List)
return SQL_Query
is
Q : SQL_Query;
F : SQL_Field_List;
T : SQL_Table_List;
C : SQL_Criteria := No_Criteria;
begin
Internal_Query (F, T, C, Self.Select_Related, Self.Follow_LJ);
if Override_Fields /= Empty_Field_List then
F := Override_Fields;
end if;
Query (Self, Q, F, T, C);
return Q;
end Build_Query;
------------------
-- Build_Delete --
------------------
function Build_Delete (Self : Manager_Type'Class) return SQL_Query is
Q : SQL_Query;
F : SQL_Field_List;
T : SQL_Table_List;
C : SQL_Criteria := No_Criteria;
begin
Internal_Query
(Fields => F,
From => T,
Criteria => C,
PK_Only => True,
Depth => 0, -- Never select related
Follow_LJ => False);
Query (Self, Q, F, T, C);
Q := SQL_Delete
(From => Table_Type,
Where => SQL_In (Tuple (F), Q));
Auto_Complete (Q);
return Q;
end Build_Delete;
---------
-- Get --
---------
function Get
(Self : Manager'Class;
Session : Session_Type;
Params : SQL_Parameters := No_Parameters) return List
is
Result : List;
begin
Result.Data := (Session => Session, Follow_LJ => Self.Follow_LJ);
Result.Depth := Select_Related (Self);
if Session.Flush_Before_Query then
Session.Flush;
end if;
Result.Fetch (Session.DB, Build_Query (Self), Params);
return Result;
end Get;
---------
-- Get --
---------
function Get
(Query : String;
Session : Session_Type;
Params : SQL_Parameters := No_Parameters;
Related : Related_Depth := 0;
Follow_LJ : Boolean := False) return List
is
Result : List;
begin
Result.Data := (Session => Session, Follow_LJ => Follow_LJ);
Result.Depth := Integer (Related);
if Session.Flush_Before_Query then
Session.Flush;
end if;
Result.Fetch (Session.DB, Query, Params);
return Result;
end Get;
------------
-- Delete --
------------
procedure Delete (Self : Manager'Class; Session : Session_Type) is
begin
Execute (Session.DB, Build_Delete (Self));
end Delete;
-------------
-- Prepare --
-------------
function Prepare
(Self : Manager'Class;
Use_Cache : Boolean := False;
On_Server : Boolean := False;
Name : String := "") return ORM_Prepared_Statement
is
begin
return ORM_Prepared_Statement'
(Related => Related_Depth (Select_Related (Self)),
Follow_LJ => Self.Follow_LJ,
Stmt => Prepare
(Build_Query (Self),
Name => Name,
Use_Cache => Use_Cache,
On_Server => On_Server));
end Prepare;
----------------
-- Get_Direct --
----------------
function Get_Direct
(Self : Manager'Class;
Session : Session_Type;
Params : SQL_Parameters := No_Parameters) return Direct_List
is
Result : Direct_List;
begin
Result.Data := (Session => Session, Follow_LJ => Self.Follow_LJ);
Result.Depth := Select_Related (Self);
if Session.Flush_Before_Query then
Session.Flush;
end if;
Result.Fetch (Session.DB, Build_Query (Self), Params => Params);
return Result;
end Get_Direct;
---------
-- Get --
---------
function Get
(Query : ORM_Prepared_Statement'Class;
Session : Session_Type;
Params : SQL_Parameters := No_Parameters) return List
is
Result : List;
begin
Result.Data := (Session => Session, Follow_LJ => Query.Follow_LJ);
Result.Depth := Integer (Query.Related);
if Session.Flush_Before_Query then
Session.Flush;
end if;
Result.Fetch (Session.DB, Query.Stmt, Params);
return Result;
end Get;
----------------
-- Get_Direct --
----------------
function Get_Direct
(Query : ORM_Prepared_Statement'Class;
Session : Session_Type;
Params : SQL_Parameters := No_Parameters) return Direct_List
is
Result : Direct_List;
begin
Result.Data := (Session => Session, Follow_LJ => Query.Follow_LJ);
Result.Depth := Integer (Query.Related);
if Session.Flush_Before_Query then
Session.Flush;
end if;
Result.Fetch (Session.DB, Query.Stmt, Params => Params);
return Result;
end Get_Direct;
--------------
-- Distinct --
--------------
function Distinct (Self : Manager) return Manager
is
Result : Manager := Self;
begin
Distinct (Result);
return Result;
end Distinct;
------------
-- Filter --
------------
function Filter
(Self : Manager; Condition : SQL_Criteria) return Manager
is
Result : Manager;
begin
-- Bug in GNAT, maybe: "Result := Self" results in a corruption
-- in the reference counting of the criteria. The workaround is
-- to do an explicit copy.
Copy (Self, Into => Result);
Filter (Result, Condition);
return Result;
end Filter;
-----------
-- Limit --
-----------
function Limit
(Self : Manager;
Count : Natural;
From : Natural := 0) return Manager
is
Result : Manager := Self;
begin
Limit (Result, Count, From);
return Result;
end Limit;
--------------
-- Order_By --
--------------
function Order_By
(Self : Manager;
By : SQL_Field_List) return Manager
is
Result : Manager := Self;
begin
GNATCOLL.SQL.Orm.Order_By (Result, By);
return Result;
end Order_By;
--------------
-- Order_By --
--------------
function Order_By
(Self : Manager; By : SQL_Field'Class) return Manager
is
Result : Manager := Self;
begin
GNATCOLL.SQL.Orm.Order_By (Result, +By);
return Result;
end Order_By;
--------------------
-- Select_Related --
--------------------
function Select_Related
(Self : Manager;
Depth : Related_Depth;
Follow_Left_Join : Boolean := False) return Manager
is
Result : Manager := Self;
begin
Select_Related (Result, Integer (Depth), Follow_Left_Join);
return Result;
end Select_Related;
end Generic_Managers;
end GNATCOLL.SQL.Orm.Impl;
|