reference.md 17.3 KB
Newer Older
1
# Ada API Reference
Benjamin Blundell's avatar
Benjamin Blundell committed
2 3 4 5 6

The SOCI-Ada library is entirely implemented as a single package named `SOCI`. Additional child packages contain single procedures for static registration of backends - these child packages are not necessary for typical use, but can be useful to force static linking of backend code.

The following describes all publicly visible elements of this package:

7 8 9 10
```ada
--
--  General exception related to database and library usage.
--
Benjamin Blundell's avatar
Benjamin Blundell committed
11

12 13
Database_Error : exception;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
14 15 16

Each problem related to the interaction with the database or to the incorrect usage of the library itself is signalled by raising this exception. Each occurrence of this exception has some human-readable error message that can be obtained by a call to `Ada.Exceptions.Exception_Message`.

17 18 19 20
```ada
--
--  Session.
--
Benjamin Blundell's avatar
Benjamin Blundell committed
21

22
type Session is tagged limited private;
Benjamin Blundell's avatar
Benjamin Blundell committed
23

24 25
not overriding
function Make_Session (Connection_String : in String) return Session;
Benjamin Blundell's avatar
Benjamin Blundell committed
26

27 28
not overriding
procedure Open (This : in out Session; Connection_String : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
29

30 31
not overriding
procedure Close (This : in out Session);
Benjamin Blundell's avatar
Benjamin Blundell committed
32

33 34 35
not overriding
function Is_Open (This : in Session) return Boolean;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
36 37 38 39 40 41 42 43 44 45 46 47 48

The `Session` object can exist in two states: "connected" (or "open") and "disconnected". It can be created as connected at initialization time with a call to the constructor function `Make_Session` or left default-initialized in the disconnected state and later changed to connected with `Open` (the latter option is the only that is available in the Ada 95 version of the library). `Session` objects can be also associated with the connection pool, see below.

The `Connection_String` should have the form `"backendname://parameters"`, where `backendname` is used to construct the name of the dynamically loadable library that will be used to provide specific database services. Backends included in the current distribution of the main SOCI library are:

* `oracle` (implemented as `libsoci_oracle.so` or `libsoci_oracle.dll`)
* `postgresql` (implemented as `libsoci_postgresql.so` or `libsoci_postgresql.dll`)
* `mysql` (implemented as `libsoci_mysql.so` or `libsoci_mysql.dll`)

Other backends can be added to the library in the future or by the user himself, please see the documentation of the main SOCI library for details.

The `parameters` component of the `Connection_String` depends on the given backend, please see the documentation of the main SOCI project for the meaning and recognized options. The web pages related to the backends above are:

49 50 51
* [Oracle](../../backends/oracle.md)
* [PostgreSQL](../../backends/postgresql.md)
* [MySQL](../../backends/mysql.md)
Benjamin Blundell's avatar
Benjamin Blundell committed
52 53 54 55 56

The `Open` operation can be called only in the disconnected state (which changes the state of `Session` object to connected). The `Close` operation can be called in any state (provided that the session is not associated with the connection pool, see below) and after that the `Session` is in the disconnected state.

`Session` objects are closed automatically as part of their finalization. If the `Session` object is associated with the connection pool, the finalizer detaches from the pool without closing the connection.

57 58
```ada
--  Transaction management.
Benjamin Blundell's avatar
Benjamin Blundell committed
59

60 61
not overriding
procedure Start (This : in Session);
Benjamin Blundell's avatar
Benjamin Blundell committed
62

63 64
not overriding
procedure Commit (This : in Session);
Benjamin Blundell's avatar
Benjamin Blundell committed
65

66 67 68
not overriding
procedure Rollback (This : in Session);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
69 70 71

These operations handle transactions. The exact meaning of transactions and whether transactions are automatic for some kinds of statements (and which ones) depend on the target database.

72 73 74 75 76
```ada
--  Immediate query execution.
not overriding
procedure Execute (This : in Session; Query : in String);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
77 78 79

This operation allows to create implicit statement, prepare it for the given `Query` and execute it.

80 81 82 83
```ada
--
--  Connection pool management.
--
Benjamin Blundell's avatar
Benjamin Blundell committed
84

85
type Connection_Pool (Size : Positive) is tagged limited private;
Benjamin Blundell's avatar
Benjamin Blundell committed
86

87 88 89 90 91
not overriding
procedure Open
    (This : in out Connection_Pool;
    Position : in Positive;
    Connection_String : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
92

93 94
not overriding
procedure Close (This : in out Connection_Pool; Position : in Positive);
Benjamin Blundell's avatar
Benjamin Blundell committed
95

96 97 98
not overriding
procedure Lease (This : in out Connection_Pool; S : in out Session'Class);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
99 100 101 102 103 104 105

The `Connection_Pool` encapsulates a fixed-size collection of sessions. Individual sessions are indexed from `1` to `Size` (provided as discriminant) and can be `Open`ed and `Close`d explicitly. Each connection in the pool can be created with different `Connection_String`, if needed.

The `Lease` operation allows to associate a given `Session` object (that has to be in the disconnected state itself) with one connection from the pool. The pool guarantees that at most one task can lease a given connection from the pool. If there are no free connections in the pool, the `Lease` operation will block waiting until some connection is freed.

The `Session` object that is associated with a connection from the pool automatically gives it back to pool as part of the `Session`'s finalizer. There is no other way to "detach" from the pool.

106
Note:
Benjamin Blundell's avatar
Benjamin Blundell committed
107 108 109

It is assumed that the lifetime of `Connection_Pool` encloses the lifetimes of all `Session` objects that are leased from it. There is no particular protection against it and it is possible to construct a code example with allocators that create partially overlapping `Connection_Pool` and `Session`, but this is considered obscure and not representative to the actual use scenarios. To avoid any potential problems, create `Connection_Pool` in the scope that encloses the scopes of leased `Session`s.

110 111 112 113
```ada
--
--  Statement.
--
Benjamin Blundell's avatar
Benjamin Blundell committed
114

115
type Statement (<>) is tagged limited private;
Benjamin Blundell's avatar
Benjamin Blundell committed
116

117
type Data_State is (Data_Null, Data_Not_Null);
Benjamin Blundell's avatar
Benjamin Blundell committed
118

119
type Into_Position is private;
Benjamin Blundell's avatar
Benjamin Blundell committed
120

121 122
type Vector_Index is new Natural;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
123 124 125

The `Statement` type and supporting types. `Data_State` is used to indicate null values in the database sense - each value of into or use elements has a state from this type.

126 127
```ada
--  Statement preparation and execution.
Benjamin Blundell's avatar
Benjamin Blundell committed
128

129 130
not overriding
procedure Prepare (This : in Statement; Query : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
131

132 133 134 135
not overriding
procedure Execute
    (This : in Statement;
    With_Data_Exchange : in Boolean := False);
Benjamin Blundell's avatar
Benjamin Blundell committed
136

137 138 139 140
not overriding
function Execute
    (This : in Statement;
    With_Data_Exchange : in Boolean := False) return Boolean;
Benjamin Blundell's avatar
Benjamin Blundell committed
141

142 143
not overriding
function Fetch (This : in Statement) return Boolean;
Benjamin Blundell's avatar
Benjamin Blundell committed
144

145 146 147
not overriding
function Got_Data (This : in Statement) return Boolean;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
148 149 150 151 152 153 154 155 156

The `Prepare` operation needs to be called before any other operation in the above group and it prepares the execution for the given `Query`. No into and use elements can be created after this operation is called.

The `Execute` operations cause the statement to execute, which might be combined with data exchange if requested. The function version of this operation returns `True` if some data has been returned back from the database server.

The `Fetch` function is used to transfer next portion of data (a single row or a whole bunch) from the database server and returns `True` if some data has been fetched. If this function returns `False` it means that no new data will be ever fetched for this statement and indicates the end-of-row condition.

The `Got_Data` function returns `True` if the last execution or fetch resulted in some data being transmitted from the database server.

157 158 159 160
```ada
--
--  Data items handling.
--
Benjamin Blundell's avatar
Benjamin Blundell committed
161

162 163 164 165
--  Database-specific types.
--  These types are most likely identical to standard Integer,
--  Long_Long_Integer and Long_Float, but are defined distinctly
--  to avoid interfacing problems with other compilers.
Benjamin Blundell's avatar
Benjamin Blundell committed
166

167 168 169 170
type DB_Integer is new Interfaces.C.int;
type DB_Long_Long_Integer is new Interfaces.Integer_64;
type DB_Long_Float is new Interfaces.C.double;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
171 172 173 174 175 176 177 178 179

The data types used for interaction with the database are:

* `String`
* `DB_Integer`, defined above
* `DB_Long_Long_Integer`, defined above
* `DB_Long_Float`, defined above
* `Ada.Calendar.Time`

180 181
```ada
--  Creation of single into elements.
Benjamin Blundell's avatar
Benjamin Blundell committed
182

183 184
not overriding
function Into_String (This : in Statement) return Into_Position;
Benjamin Blundell's avatar
Benjamin Blundell committed
185

186 187
not overriding
function Into_Integer (This : in Statement) return Into_Position;
Benjamin Blundell's avatar
Benjamin Blundell committed
188

189 190
not overriding
function Into_Long_Long_Integer (This : in Statement) return Into_Position;
Benjamin Blundell's avatar
Benjamin Blundell committed
191

192 193
not overriding
function Into_Long_Float (This : in Statement) return Into_Position;
Benjamin Blundell's avatar
Benjamin Blundell committed
194

195 196 197
not overriding
function Into_Time (This : in Statement) return Into_Position;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
198 199 200

These functions instruct the library to create internal simple into elements of the relevant type. They return the position of the into element, which can be later used to identify it.

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
Note: Simple into elements cannot be created together with vector into elements for the same statement.

Note: Simple into elements cannot be created together with vector into elements for the same statement.

```ada
--  Inspection of single into elements.

not overriding
function Get_Into_State
    (This : in Statement;
    Position : in Into_Position)
    return Data_State;

not overriding
function Get_Into_String
    (This : in Statement;
    Position : in Into_Position)
    return String;

not overriding
function Get_Into_Integer
    (This : in Statement;
    Position : in Into_Position)
    return DB_Integer;

not overriding
function Get_Into_Long_Long_Integer
    (This : in Statement;
    Position : in Into_Position)
    return DB_Long_Long_Integer;

not overriding
function Get_Into_Long_Float
    (This : in Statement;
    Position : in Into_Position)
    return DB_Long_Float;

not overriding
function Get_Into_Time
    (This : in Statement;
    Position : in Into_Position)
    return Ada.Calendar.Time;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
244 245 246

These functions allow to inspect the state and value of the simple into element identified by its position. If the state of the given element is `Data_Null`, the data-reading functions raise exceptions for that element.

247 248
```ada
--  Inspection of vector into elements.
Benjamin Blundell's avatar
Benjamin Blundell committed
249

250 251
not overriding
function Get_Into_Vectors_Size (This : in Statement) return Natural;
Benjamin Blundell's avatar
Benjamin Blundell committed
252

253 254
not overriding
function Into_Vectors_First_Index (This : in Statement) return Vector_Index;
Benjamin Blundell's avatar
Benjamin Blundell committed
255

256 257
not overriding
function Into_Vectors_Last_Index (This : in Statement) return Vector_Index;
Benjamin Blundell's avatar
Benjamin Blundell committed
258

259 260 261
not overriding
procedure Into_Vectors_Resize (This : in Statement; New_Size : in Natural);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
262 263 264 265 266 267 268

The `Get_Into_Vectors_Size` returns the number of entries in any of the vector into elements for the given statement.

The `Into_Vectors_First_Index` returns the lowest index value for vector into elements (which is always `0`, even if the vectors are empty). The `Into_Vectors_Last_Index` returns the last index of into vectors, and raises the `CONSTRAINT_ERROR` exception if the vectors are empty.

The `Into_Vectors_Resize` procedure allows to change the size of all use vectors for the given statement.

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
```ada
not overriding
function Get_Into_Vector_State
    (This : in Statement;
    Position : in Into_Position;
    Index : in Vector_Index)
    return Data_State;

not overriding
function Get_Into_Vector_String
    (This : in Statement;
    Position : in Into_Position;
    Index : in Vector_Index)
    return String;

not overriding
function Get_Into_Vector_Integer
    (This : in Statement;
    Position : in Into_Position;
    Index : in Vector_Index)
    return DB_Integer;

not overriding
function Get_Into_Vector_Long_Long_Integer
    (This : in Statement;
    Position : in Into_Position;
    Index : in Vector_Index)
    return DB_Long_Long_Integer;

not overriding
function Get_Into_Vector_Long_Float
    (This : in Statement;
    Position : in Into_Position;
    Index : in Vector_Index)
    return DB_Long_Float;

not overriding
function Get_Into_Vector_Time
    (This : in Statement;
    Position : in Into_Position;
    Index : in Vector_Index)
    return Ada.Calendar.Time;
```
Benjamin Blundell's avatar
Benjamin Blundell committed
312 313 314

These functions allow to inspect the state and value of the vector use element identified by its position and index. If the state of the given element is `Data_Null`, the data-reading functions raise exceptions for that element.

315 316
```ada
--  Creation of single use elements.
Benjamin Blundell's avatar
Benjamin Blundell committed
317

318 319
not overriding
procedure Use_String (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
320

321 322
not overriding
procedure Use_Integer (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
323

324 325
not overriding
procedure Use_Long_Long_Integer (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
326

327 328
not overriding
procedure Use_Long_Float (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
329

330 331 332
not overriding
procedure Use_Time (This : in Statement; Name : in String);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
333 334 335

These functions instruct the library to create internal simple use elements of the relevant type, identified by the given `Name`.

336
Note:
Benjamin Blundell's avatar
Benjamin Blundell committed
337

338 339
* Simple use elements cannot be created together with vector use elements for the same statement.
* Vector use elements cannot be created together with any into elements for the same statement.
Benjamin Blundell's avatar
Benjamin Blundell committed
340

341 342
```ada
--  Creation of vector use elements.
Benjamin Blundell's avatar
Benjamin Blundell committed
343

344 345
not overriding
procedure Use_Vector_String (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
346

347 348
not overriding
procedure Use_Vector_Integer (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
349

350 351
not overriding
procedure Use_Vector_Long_Long_Integer (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
352

353 354
not overriding
procedure Use_Vector_Long_Float (This : in Statement; Name : in String);
Benjamin Blundell's avatar
Benjamin Blundell committed
355

356 357 358
not overriding
procedure Use_Vector_Time (This : in Statement; Name : in String);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
359 360 361

These functions instruct the library to create internal vector use elements of the relevant type, identified by the given `Name`.

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
Note:

* Simple use elements cannot be created together with vector use elements for the same statement.
* Vector use elements cannot be created together with any into elements for the same statement.

```ada
--  Modifiers for single use elements.

not overriding
procedure Set_Use_State
    (This : in Statement;
    Name : in String;
    State : in Data_State);

not overriding
procedure Set_Use_String
    (This : in Statement;
    Name : in String;
    Value : in String);

not overriding
procedure Set_Use_Integer
    (This : in Statement;
    Name : in String;
    Value : in DB_Integer);

not overriding
procedure Set_Use_Long_Long_Integer
    (This : in Statement;
    Name : in String;
    Value : in DB_Long_Long_Integer);

not overriding
procedure Set_Use_Long_Float
    (This : in Statement;
    Name : in String;
    Value : in DB_Long_Float);

not overriding
procedure Set_Use_Time
    (This : in Statement;
    Name : in String;
    Value : in Ada.Calendar.Time);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
406 407 408

These operations allow to modify the state and value of simple use elements. Setting the value of use element automatically sets its state to `Data_Not_Null`.

409 410
```ada
--  Modifiers for vector use elements.
Benjamin Blundell's avatar
Benjamin Blundell committed
411

412 413
not overriding
function Get_Use_Vectors_Size (This : in Statement) return Natural;
Benjamin Blundell's avatar
Benjamin Blundell committed
414

415 416
not overriding
function Use_Vectors_First_Index (This : in Statement) return Vector_Index;
Benjamin Blundell's avatar
Benjamin Blundell committed
417

418 419
not overriding
function Use_Vectors_Last_Index (This : in Statement) return Vector_Index;
Benjamin Blundell's avatar
Benjamin Blundell committed
420

421 422 423
not overriding
procedure Use_Vectors_Resize (This : in Statement; New_Size : in Natural);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
424 425 426 427 428 429 430

The `Get_Use_Vectors_Size` returns the number of entries in any of the vector use elements for the given statement.

The `Use_Vectors_First_Index` returns the lowest index value for vector use elements (which is always `0`, even if the vectors are empty). The `Use_Vectors_Last_Index` returns the last index of use vectors, and raises the `CONSTRAINT_ERROR` exception if the vectors are empty.

The `Use_Vectors_Resize` procedure allows to change the size of all use vectors for the given statement.

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
```ada
not overriding
procedure Set_Use_Vector_State
    (This : in Statement;
    Name : in String;
    Index : in Vector_Index;
    State : in Data_State);

not overriding
procedure Set_Use_Vector_String
    (This : in Statement;
    Name : in String;
    Index : in Vector_Index;
    Value : in String);

not overriding
procedure Set_Use_Vector_Integer
    (This : in Statement;
    Name : in String;
    Index : in Vector_Index;
    Value : in DB_Integer);

not overriding
procedure Set_Use_Vector_Long_Long_Integer
    (This : in Statement;
    Name : in String;
    Index : in Vector_Index;
    Value : in DB_Long_Long_Integer);

not overriding
procedure Set_Use_Vector_Long_Float
    (This : in Statement;
    Name : in String;
    Index : in Vector_Index;
    Value : in DB_Long_Float);

not overriding
procedure Set_Use_Vector_Time
    (This : in Statement;
    Name : in String;
    Index : in Vector_Index;
    Value : in Ada.Calendar.Time);
```
Benjamin Blundell's avatar
Benjamin Blundell committed
474 475 476

These operations allow to modify the state and value of vector use elements. Setting the value of use element automatically sets its state to `Data_Not_Null`.

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
```ada
--  Inspection of single use elements.
--
--  Note: Use elements can be modified by the database if they
--        are bound to out and inout parameters of stored procedures
--        (although this is not supported by all database backends).
--        This feature is available only for single use elements.

not overriding
function Get_Use_State
    (This : in Statement;
    Name : in String)
    return Data_State;

not overriding
function Get_Use_String
    (This : in Statement;
    Name : in String)
    return String;

not overriding
function Get_Use_Integer
    (This : in Statement;
    Name : in String)
    return DB_Integer;

not overriding
function Get_Use_Long_Long_Integer
    (This : in Statement;
    Name : in String)
    return DB_Long_Long_Integer;

not overriding
function Get_Use_Long_Float
    (This : in Statement;
    Name : in String)
    return DB_Long_Float;

not overriding
function Get_Use_Time
    (This : in Statement;
    Name : in String)
    return Ada.Calendar.Time;
```

These functions allow to inspect the state and value of the simple use element identified by its name. If the state of the given element is `Data_Null`, the data-reading functions raise exceptions for that element.