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: 4982 $ $Date: 2014-11-04 13:25:16 +0300 (Tue, 04 Nov 2014) $
43------------------------------------------------------------------------------
44private with Ada.Containers.Hashed_Maps;
45private with System.Storage_Elements;
46
47private with League.Strings.Hash;
48with Matreshka.Internals.SQL_Drivers.Oracle.Databases;
49private with Matreshka.Internals.Strings;
50limited with Matreshka.Internals.SQL_Drivers.Oracle.Plug_In;
51with Matreshka.Internals.SQL_Drivers.Oracle.Utils;
52
53package Matreshka.Internals.SQL_Drivers.Oracle.Queries is
54
55   type OCI_Database_Access is access all Databases.OCI_Database;
56
57   type OCI_Query (DB : not null access Databases.OCI_Database) is
58     new Abstract_Query with private;
59
60private
61
62   type Plug_In_Access is access all
63     Matreshka.Internals.SQL_Drivers.Oracle.Plug_In.Abstract_Plug_In'Class;
64
65   type Bound_Value_Node (Length : System.Storage_Elements.Storage_Count)
66   is limited record
67      Value       : League.Holders.Holder;
68      Bind        : aliased Oracle.Bind;
69      Is_Null     : aliased Sb2;
70      String_Size : aliased Ub4;
71      String      : Matreshka.Internals.Strings.Shared_String_Access;
72      Int         : aliased League.Holders.Universal_Integer;
73      Float       : aliased League.Holders.Universal_Float;
74      Date        : aliased Oracle.Utils.OCIDate;
75      Timestamp   : aliased Date_Time;
76      Direction   : SQL.Parameter_Directions;
77      Plugin      : Plug_In_Access;
78      Extra_Type  : Data_Type;
79      Extra_Size  : System.Storage_Elements.Storage_Count;
80      Extra       : System.Storage_Elements.Storage_Array (1 .. Length);
81   end record;
82
83   type Bound_Value_Access is access Bound_Value_Node;
84
85   package Parameter_Maps is
86     new Ada.Containers.Hashed_Maps
87          (League.Strings.Universal_String,
88           Bound_Value_Access,
89           League.Strings.Hash,
90           League.Strings."=");
91   --  XXX Database independent parameter mapping must be extended and reused
92   --  here.
93
94   type Column_Types is
95     (String_Column, Integer_Column, Float_Column, Date_Column, Time_Column);
96
97   type Storage_Array_Access is
98     access all System.Storage_Elements.Storage_Array;
99
100   type Defined_Value is limited record
101      Column_Type : Column_Types;
102      Define      : aliased Oracle.Define;
103      Is_Null     : aliased Sb2;
104      Size        : Utf16.Utf16_String_Index;
105      String      : Matreshka.Internals.Strings.Shared_String_Access;
106      Int         : aliased League.Holders.Universal_Integer;
107      Float       : aliased League.Holders.Universal_Float;
108      Date        : aliased Oracle.Utils.OCIDate;
109      Timestamp   : aliased Date_Time;
110      Plugin      : Plug_In_Access;
111      Extra_Type  : Data_Type;
112      Extra_Size  : System.Storage_Elements.Storage_Count;
113      Extra       : Storage_Array_Access;
114   end record;
115
116   type Defined_Value_Array is array (Positive range <>) of Defined_Value;
117
118   type Defined_Value_Array_Access is access Defined_Value_Array;
119
120   type Query_States is (Created, Prepared, Executed, Has_Row, No_More_Rows);
121
122   subtype Active   is Query_States range Executed .. No_More_Rows;
123   subtype Fetching is Query_States range Executed .. Has_Row;
124   subtype Ready    is Query_States range Prepared .. No_More_Rows;
125
126   type OCI_Query  (DB : not null access Databases.OCI_Database) is
127     new Abstract_Query with
128   record
129      Handle       : aliased Statement_Handle;
130      Column_Count : Natural      := 0;
131      State        : Query_States := Created;
132      Is_Described : Boolean      := False;
133      Is_Select    : Boolean      := False;
134      Parameters   : Parameter_Maps.Map;
135      Columns      : Defined_Value_Array_Access;
136   end record;
137
138   overriding procedure Bind_Value
139    (Self      : not null access OCI_Query;
140     Name      : League.Strings.Universal_String;
141     Value     : League.Holders.Holder;
142     Direction : SQL.Parameter_Directions);
143
144   overriding function Bound_Value
145    (Self : not null access OCI_Query;
146     Name : League.Strings.Universal_String)
147       return League.Holders.Holder;
148
149   overriding function Error_Message
150    (Self : not null access OCI_Query) return League.Strings.Universal_String;
151
152   overriding function Execute
153    (Self : not null access OCI_Query) return Boolean;
154
155   overriding procedure Finish (Self : not null access OCI_Query);
156
157   overriding procedure Invalidate (Self : not null access OCI_Query);
158
159   overriding function Is_Active
160    (Self : not null access OCI_Query) return Boolean;
161
162   overriding function Is_Valid
163    (Self : not null access OCI_Query) return Boolean;
164
165   overriding function Next (Self : not null access OCI_Query) return Boolean;
166
167   overriding function Prepare
168    (Self  : not null access OCI_Query;
169     Query : League.Strings.Universal_String) return Boolean;
170
171   overriding function Value
172    (Self  : not null access OCI_Query;
173     Index : Positive) return League.Holders.Holder;
174
175end Matreshka.Internals.SQL_Drivers.Oracle.Queries;
176