Procedure
AdaBase.Statement.Base.[STMT].discard_rest
This procedure frees up the resources used by the statement during the iteration of the current result set. The resources are normally freed after the last row is fetched from the result set, but the discard_rest procedure discards the remaining rows directly and without having to complete the iteration. It is analogous to "close cursor" routines seen on other drivers.
Flushing unretrieved rows is required before the statement can perform another query. If the driver cannot natively discard the data, it will internally iterate though the result set to simulate closing the cursor.
with AdaBase;
with Connect;
with CommonText;
with Ada.Text_IO;
with AdaBase.Results.Sets;
procedure Bad_Select is
package CON renames Connect;
package TIO renames Ada.Text_IO;
package ARS renames AdaBase.Results.Sets;
package CT renames CommonText;
row : ARS.Datarow;
sql : String := "SELECT fruit, calories FROM froits " &
"WHERE color = 'red'";
success : Boolean := True;
begin
-- PostgreSQL will abort a transaction even for a bad select
-- so in this case, let's not start any transactions
CON.DR.set_trait_autocommit (True);
CON.connect_database;
declare
stmt : CON.Stmt_Type := CON.DR.query (sql);
begin
TIO.Put_Line ("Query successful: " & stmt.successful'Img);
if not stmt.successful then
TIO.Put_Line (" Driver message: " & stmt.last_driver_message);
TIO.Put_Line (" Driver code: " & stmt.last_driver_code'Img);
TIO.Put_Line (" SQL State: " & stmt.last_sql_state);
success := False;
end if;
end;
if not success then
-- Fix SQL typo
sql (31) := 'u';
TIO.Put_Line ("");
TIO.Put_Line ("SQL now: " & sql);
declare
stmt : CON.Stmt_Type := CON.DR.query (sql);
begin
TIO.Put_Line ("Query successful: " & stmt.successful'Img);
row := stmt.fetch_next;
if not row.data_exhausted then
TIO.Put_Line (" Number fields:" & row.count'Img);
stmt.discard_rest;
TIO.Put_Line (" Data discarded: " & stmt.data_discarded'Img);
end if;
end;
end if;
CON.DR.disconnect;
end Bad_Select;
Example code: testcases/bad_select/bad_select.adb
Query successful: FALSE
Driver message: Table 'adabase_examples.froits' doesn't exist
Driver code: 1146
SQL State: 42S02
SQL now: SELECT fruit, calories FROM fruits WHERE color = 'red'
Query successful: TRUE
Number fields: 2
Data discarded: TRUE
Output using the MySQL driver
Query successful: FALSE
Driver message: no such table: froits
Driver code: 1
SQL State: HY000
SQL now: SELECT fruit, calories FROM fruits WHERE color = 'red'
Query successful: TRUE
Number fields: 2
Data discarded: TRUE
Output using the SQLite driver
Query successful: FALSE
Driver message: ERROR: relation "froits" does not exist
LINE 1: SELECT fruit, calories FROM froits WHERE color = 'red'
^
Driver code: 2
SQL State: 42P01
SQL now: SELECT fruit, calories FROM fruits WHERE color = 'red'
Query successful: TRUE
Number fields: 2
Data discarded: TRUE
Output using the PostgreSQL driver