ado_sqlite_2.3.0_a59fa70f/src/ado-schemas.adb

  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
-----------------------------------------------------------------------
--  ado.schemas -- Database Schemas
--  Copyright (C) 2009, 2010, 2018, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
with Ada.Strings.Equal_Case_Insensitive;
package body ADO.Schemas is

   procedure Free is new
     Ada.Unchecked_Deallocation (Object => Column,
                                 Name   => Column_Definition);
   procedure Free is new
     Ada.Unchecked_Deallocation (Object => Table,
                                 Name   => Table_Definition);
   procedure Free is new
     Ada.Unchecked_Deallocation (Object => Schema,
                                 Name   => Schema_Access);

   --  ------------------------------
   --  Get the hash value associated with the class mapping.
   --  ------------------------------
   function Hash (Mapping : Class_Mapping_Access) return Ada.Containers.Hash_Type is
   begin
      if Mapping = null then
         return 0;
      else
         return Util.Strings.Hash (Mapping.Table);
      end if;
   end Hash;

   --  ------------------------------
   --  Column Representation
   --  ------------------------------

   --  ------------------------------
   --  Get the column name
   --  ------------------------------
   function Get_Name (Column : Column_Definition) return String is
   begin
      return To_String (Column.Name);
   end Get_Name;

   --  ------------------------------
   --  Get the column type
   --  ------------------------------
   function Get_Type (Column : Column_Definition) return Column_Type is
   begin
      return Column.Col_Type;
   end Get_Type;

   --  ------------------------------
   --  Get the default column value
   --  ------------------------------
   function Get_Default (Column : Column_Definition) return String is
   begin
      return To_String (Column.Default);
   end Get_Default;

   --  ------------------------------
   --  Get the column collation (for string based columns)
   --  ------------------------------
   function Get_Collation (Column : Column_Definition) return String is
   begin
      return To_String (Column.Collation);
   end Get_Collation;

   --  ------------------------------
   --  Check whether the column can be null
   --  ------------------------------
   function Is_Null (Column : Column_Definition) return Boolean is
   begin
      return Column.Is_Null;
   end Is_Null;

   --  ------------------------------
   --  Check whether the column is an unsigned number
   --  ------------------------------
   function Is_Unsigned (Column : Column_Definition) return Boolean is
   begin
      return Column.Is_Unsigned;
   end Is_Unsigned;

   --  ------------------------------
   --  Returns true if the column can hold a binary string
   --  ------------------------------
   function Is_Binary (Column : Column_Definition) return Boolean is
   begin
      return Column.Is_Binary;
   end Is_Binary;

   --  ------------------------------
   --  Returns true if the column is a primary key.
   --  ------------------------------
   function Is_Primary (Column : Column_Definition) return Boolean is
   begin
      return Column.Is_Primary;
   end Is_Primary;

   --  ------------------------------
   --  Get the column length
   --  ------------------------------
   function Get_Size (Column : Column_Definition) return Natural is
   begin
      return Column.Size;
   end Get_Size;

   --  ------------------------------
   --  Column iterator
   --  ------------------------------

   --  ------------------------------
   --  Returns true if the iterator contains more column
   --  ------------------------------
   function Has_Element (Cursor : Column_Cursor) return Boolean is
   begin
      return Cursor.Current /= null;
   end Has_Element;

   --  ------------------------------
   --  Move to the next column
   --  ------------------------------
   procedure Next (Cursor : in out Column_Cursor) is
   begin
      if Cursor.Current /= null then
         Cursor.Current := Cursor.Current.Next_Column;
      end if;
   end Next;

   --  ------------------------------
   --  Get the current column definition
   --  ------------------------------
   function Element (Cursor : Column_Cursor) return Column_Definition is
   begin
      return Cursor.Current;
   end Element;

   --  ------------------------------
   --  Table Representation
   --  ------------------------------

   --  ------------------------------
   --  Get the table name
   --  ------------------------------
   function Get_Name (Table : Table_Definition) return String is
   begin
      return To_String (Table.Name);
   end Get_Name;

   --  ------------------------------
   --  Get the column iterator
   --  ------------------------------
   function Get_Columns (Table : Table_Definition) return Column_Cursor is
   begin
      return Column_Cursor '(Current => Table.First_Column);
   end Get_Columns;

   --  ------------------------------
   --  Find the column having the given name
   --  ------------------------------
   function Find_Column (Table : Table_Definition;
                         Name  : String) return Column_Definition is
      Column : Column_Definition := Table.First_Column;
   begin
      while Column /= null loop
         if Ada.Strings.Equal_Case_Insensitive (To_String (Column.Name), Name) then
            return Column;
         end if;
         Column := Column.Next_Column;
      end loop;
      return null;
   end Find_Column;

   --  ------------------------------
   --  Table iterator
   --  ------------------------------

   --  ------------------------------
   --  Returns true if the iterator contains more tables
   --  ------------------------------
   function Has_Element (Cursor : Table_Cursor) return Boolean is
   begin
      return Cursor.Current /= null;
   end Has_Element;

   --  ------------------------------
   --  Move to the next column
   --  ------------------------------
   procedure Next (Cursor : in out Table_Cursor) is
   begin
      if Cursor.Current /= null then
         Cursor.Current := Cursor.Current.Next_Table;
      end if;
   end Next;

   --  ------------------------------
   --  Get the current table definition
   --  ------------------------------
   function Element (Cursor : Table_Cursor) return Table_Definition is
   begin
      return Cursor.Current;
   end Element;

   --  ------------------------------
   --  Database Schema
   --  ------------------------------

   --  ------------------------------
   --  Find a table knowing its name
   --  ------------------------------
   function Find_Table (Schema : Schema_Definition;
                        Name   : String) return Table_Definition is
      Table : Table_Definition;
   begin
      if Schema.Schema /= null then
         Table := Schema.Schema.First_Table;
         while Table /= null loop
            if Ada.Strings.Equal_Case_Insensitive (To_String (Table.Name), Name) then
               return Table;
            end if;
            Table := Table.Next_Table;
         end loop;
      end if;
      return null;
   end Find_Table;

   function Get_Tables (Schema : Schema_Definition) return Table_Cursor is
   begin
      if Schema.Schema = null then
         return Table_Cursor '(Current => null);
      else
         return Table_Cursor '(Current => Schema.Schema.First_Table);
      end if;
   end Get_Tables;

   overriding
   procedure Finalize (Schema : in out Schema_Definition) is
   begin
      if Schema.Schema /= null then
         declare
            Table : Table_Definition;
            Column : Column_Definition;
         begin
            loop
               Table := Schema.Schema.First_Table;
               exit when Table = null;
               loop
                  Column := Table.First_Column;
                  exit when Column = null;
                  Table.First_Column := Column.Next_Column;
                  Free (Column);
               end loop;
               Schema.Schema.First_Table := Table.Next_Table;
               Free (Table);
            end loop;
         end;
         Free (Schema.Schema);
      end if;
   end Finalize;

end ADO.Schemas;