gnoga_2.1.2_5f127c56/src/gnoga-server-database.ads

  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
------------------------------------------------------------------------------
--                                                                          --
--                   GNOGA - The GNU Omnificent GUI for Ada                 --
--                                                                          --
--                  G N O G A . S E R V E R . D A T A B A S E               --
--                                                                          --
--                                 S p e c                                  --
--                                                                          --
--                                                                          --
--                     Copyright (C) 2014 David Botton                      --
--                                                                          --
--  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 --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    --
--                                                                          --
--  As a special exception under Section 7 of GPL version 3, you are        --
--  granted additional permissions described in the GCC Runtime Library     --
--  Exception, version 3.1, as published by the Free Software Foundation.   --
--                                                                          --
--  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/>.                                         --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file might be   --
--  covered by the  GNU Public License.                                     --
--                                                                          --
--  For more information please go to http://www.gnoga.com                  --
------------------------------------------------------------------------------

--  Abstract class for database access. Use one of the specific implementations
--  for MySQL, SQLLite, etc.

with Ada.Containers.Indefinite_Vectors;

with Gnoga.Types;

package Gnoga.Server.Database is

   type Connection is limited interface;
   type Connection_Access is access all Connection'Class;

   procedure Disconnect (C : in out Connection) is abstract;
   --  Disconnect from server

   procedure Execute_Query
     (C   : in out Connection;
      SQL :        String) is abstract;
   --  Execute an SQL Query with no result set

   function Execute_Update
     (C   : in out Connection;
      SQL :        String)
      return Natural is abstract;
   --  Executes and SQL Query and returns the number of affected rows

   function Affected_Rows
     (C : Connection)
      return Natural is abstract;
   --  Returns the number of rows affected by an Execute_Query

   function Insert_ID
     (C : Connection)
      return Natural is abstract;
   --  Returns the last value assigned to an auto increment field upon insert

   function Error_Message
     (C : Connection)
      return String is abstract;
   --  Returns the last error message that has occurred on this connection

   function List_Of_Tables
     (C : Connection)
      return Gnoga.Types.Data_Array_Type is abstract;
   --  Return an array of table names

   function List_Fields_Of_Table
     (C          : Connection;
      Table_Name : String)
      return Gnoga.Types.Data_Array_Type is abstract;
   --  Return an array of field names for table

   type Field_Description is record
      Column_Name   : String;
      Data_Type     : String;
      Can_Be_Null   : Boolean;
      Default_Value : String;
   end record;

   package Field_Description_Arrays is new Ada.Containers.Indefinite_Vectors (Natural, Field_Description);
   subtype Field_Description_Array_Type is Field_Description_Arrays.Vector;

   function Field_Descriptions
     (C          : Connection;
      Table_Name : String)
      return Field_Description_Array_Type is abstract;
   --  Return an array of Field_Description records describe the fields of
   --  a table

   function Field_Type
     (Field : Field_Description)
      return String;
   --  Returns the field type portion of a data type, for example:
   --  If the Field.Data_Type = Varchar(80) then will return Varchar

   function Field_Size
     (Field : Field_Description)
      return Natural;
   --  Returns the field size portion of a data type, for example:
   --  If the Field.Data_Type = varchar(80) then will return 80
   --  If the Data_Type does not have a size portion will return 0
   --  If the Data_Type is a numeric with decimals, e.g. decimal(10,2)
   --  then it will return the non-decimal portion.

   function Field_Decimals
     (Field : Field_Description)
      return Natural;
   --  Returns the decimal portion of a field size if it exists or 0
   --  for example: if the Data_Type = float(10,2) it will return 2

   function Field_Options
     (Field : Field_Description)
      return String;
   --  Returns the field options portion of a data type, for example:
   --  If the Field.Data_Type = enum('N','Y') then will return 'N','Y'
   --  as this is described in the database in the same way as field
   --  size, this may be used for a string representation of the size
   --  as well. For example varchar(80) will return the string 80
   --  This is also used for descriptions like decimal(10,2), etc.

   function ID_Field_String
     (C : Connection)
      return String is abstract;
   --  Returns the propper type format for the ID field that should be part
   --  of every table used by GRAW.
   --  e.g. for SQLlite = "id INTEGER PRIMARY KEY AUTOINCREMENT"
   --       for MySQL   = "id INTEGER PRIMARY KEY AUTO_INCREMENT"

   type Recordset is interface;
   type Recordset_Access is access all Recordset'Class;

   function Query
     (C   : Connection;
      SQL : String)
      return Recordset'Class is abstract;
   --  Execute query that returns Recordset

   procedure Close (RS : in out Recordset) is abstract;
   --  Close current recordset and free resources

   procedure Next (RS : in out Recordset) is abstract;
   --  Go to next row

   function Next
     (RS : in out Recordset)
      return Boolean is abstract;
   --  Go to next row and return true if not End of Recordset

   procedure Iterate
     (C       : in out Connection;
      SQL     : in     String;
      Process :        not null access procedure (RS : Recordset'Class)) is abstract;
   --  Iterate through all rows in the result set of the query

   procedure Iterate
     (RS      : in out Recordset;
      Process :        not null access procedure (RS : Recordset'Class)) is abstract;
   --  Iterate through all rows in the recordset

   procedure Iterate
     (C       : in out Connection;
      SQL     :        String;
      Process :        not null access procedure (Row : Gnoga.Types.Data_Map_Type)) is abstract;
   --  Iterate through all rows in the result set of the query

   procedure Iterate
     (RS      : in out Recordset;
      Process :        not null access procedure (Row : Gnoga.Types.Data_Map_Type)) is abstract;
   --  Iterate through all rows in the recordset

   function Number_Of_Rows
     (RS : Recordset)
      return Natural is abstract;
   --  Return number of rows in recordset
   --  This function is not available in many implementations, check the
   --  database specific package before considering use.

   function Number_Of_Fields
     (RS : Recordset)
      return Natural is abstract;
   --  Return number of fields in recordset

   function Field_Name
     (RS           : Recordset;
      Field_Number : Natural)
      return String is abstract;
   --  Return name of field

   function Is_Null
     (RS           : Recordset;
      Field_Number : Natural)
      return Boolean is abstract;
   function Is_Null
     (RS         : Recordset;
      Field_Name : String)
      return Boolean is abstract;
   --  return True if value of field is null

   function Field_Value
     (RS           : Recordset;
      Field_Number : Natural;
      Handle_Nulls : Boolean := True)
      return String is abstract;
   function Field_Value
     (RS           : Recordset;
      Field_Name   : String;
      Handle_Nulls : Boolean := True)
      return String is abstract;
   --  return value of field, if Handle_Nulls is true, Null values will
   --  return as empty Strings

   function Field_Values
     (RS : Recordset)
      return Gnoga.Types.Data_Map_Type is abstract;
   --  return map of all values for current row, NULL values are set to
   --  an empty String

   function Escape_String
     (C : Connection;
      S : String)
      return String is abstract;
   --  prepares a string for safe storage in a query

   Connection_Error : exception;
   --  Unable to connect to MYSQL Server or Not connected to Server

   Database_Error : exception;
   --  Unable to switch to specified Database

   Table_Error : exception;
   --  Unable to locate table or table has no fields

   Query_Error : exception;
   --  Unable to execute query

   Empty_Recordset_Error : exception;
   --  The recordset is currently empty

   Empty_Row_Error : exception;
   --  Attempt to read value from Row before calling Next

   End_Of_Recordset : exception;
   --  Attempt to go pass the last row in recordset

   No_Such_Field : exception;
   --  The value for a field name was requested that does not exits

   Null_Field : exception;
   --  The value for a Null field was requested

   Not_Implemented : exception;
   --  If a database method is called that is not implemented by the specific
   --  database engined used this exception will be raised.
end Gnoga.Server.Database;