File: simple_pkg.gpq

package info (click to toggle)
gnade 1.6.2-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,220 kB
  • ctags: 847
  • sloc: ada: 40,154; sh: 4,124; sql: 3,590; makefile: 1,372; xml: 120; awk: 29
file content (228 lines) | stat: -rw-r--r-- 6,860 bytes parent folder | download | duplicates (4)
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
--|
--| Filename        : $Source: /cvsroot/gnade/gnade/samples/esql/simple_pkg.gpq,v $
--| Description     : This is the main driver for the embedded SQL translator
--| Author          : Michael Erdmann
--| Created On      : 8.12.2000
--| Last Modified By: $Author: merdmann $
--| Last Modified On: $Date: 2004/03/20 08:30:20 $
--| Status          : $State: Exp $
--|
--| Copyright (C) 2000 Michael Erdmann
--|
--| This program is free software; you can redistribute it and/or
--| modify it under the terms of the GNU General Public License
--| as published by the Free Software Foundation; either version 2
--| of the License, or (at your option) any later version.
--|
--| This program/code 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.  See the
--| GNU General Public License for more details.
--|
--| You should have received a copy of the GNU General Public License along
--| with this program; if not, write to the Free Software Foundation, Inc.,
--| 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
--|
--| 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.
--|
--| Functional Description
--| ======================
--|
--| This is an example for embedded SQL. It demonstrates the ISO/92
--| features and addtional esql extensions.
--|
--|
--| Restrictions
--| ============
--|
--| References
--| ==========
--|
--|
with Ada.Text_IO;               use Ada.Text_IO;
with SQL_STANDARD;              use SQL_STANDARD;
with Ada.Strings.Fixed;         use Ada.Strings.Fixed;
with Ada.Strings;               use Ada.Strings;

with GNU.DB.SQLCLI;             use GNU.DB.SQLCLI;

package body Simple_Pkg is

   Nothing_Found : exception;
   Some_Error    : exception;

   package IO is new Ada.Text_IO.Integer_IO (Integer);
   use  IO;

   EXEC SQL DECLARE DB01 DATABASE ;

   -- **************************************************************** --

   EXEC SQL DECLARE DEPARTMENTS TABLE (
        DEPTNO   INT,
        NAME     CHAR ( 1..40 ),
        LOCATION CHAR ( 1..40 )
   );

   EXEC SQL DECLARE EMPLOYEES TABLE (
        EMPNO     INT,
        NAME      CHAR( 1..40 ),
        FIRSTNAME CHAR( 1..40 ),
        DEPTNO    INT,
        SINCE     CHAR( 1..20 ),
        MANAGER   INT,
        JOB       CHAR( 1..20 ),
        PROMOTION CHAR( 1..50 ),
        SALARY    REAL
   ) ;

   -- ***************************************************************** --

   EXEC SQL DECLARE test_sql  STATEMENT ;

   -- ***************************************************************** --

   procedure Get_Manager(
      manager : in Integer ) is
      ---
      EXEC SQL BEGIN DECLARE SECTION END-EXEC

      name                    : CHAR(1..40);
      depno_ind               : INDICATOR_TYPE := 0;
      empno, depno            : INT;
      Manager_To_Find         : INT := INT(manager) ;
      dep_location            : CHAR (1..40);

      EXEC SQL END DECLARE SECTION END-EXEC

      H  : SQLHSTMT;
      C  : SQLHDBC ;

      procedure Not_Found is
      begin
         Put_Line("** procedure Nothing_Found called" );
         Put_Line("** Error message: " & Trim( SQLCA.Message, Right ) );
      end Not_Found;

      --
      -- test a single query without cursor
      --
      procedure Print_Employee(
         His_Number : Integer;
         EXEC SQL DECLARE DB01x DATABASE   ) is
         ---
         EXEC SQL INCLUDE SQLCA NAMED BY MY_SQLCA;
         ---
      begin
         empno := INT(His_Number);

         EXEC SQL WHENEVER NOT FOUND DO Not_Found;

         EXEC SQL AT DB01x
            SELECT NAME, DEPTNO INTO :name, :depno
               FROM EMPLOYEES
               WHERE EMPNO = :empno ;

         EXEC SQL AT DB01x
            SELECT LOCATION INTO :dep_location
               FROM DEPARTMENTS
               WHERE DEPTNO = :depno ;

         if SQLCODE not in SQL_STANDARD.NOT_FOUND then
            Put_Line(
               "Employee : " & Trim(To_String(Name),Right)  &
               " working in dep. " & INT'Image(depno)       &
               " located at " & Trim(To_String(dep_location),Right) );
         end if;

         -- get the ODBC handles
         H := EXEC SQL INCLUDE STATEMENT HANDLE OF DB01x ;
         C := EXEC SQL INCLUDE CONNECTION HANDLE OF DB01x ;

         EXEC SQL WHENEVER NOT FOUND CONTINUE;

         EXEC SQL RESET SQLCA ;
      end Print_Employee;
      ---
   begin

      Put_Line("Printing all employees working for Manager " &
                Integer'Image(Manager) );
      Put_Line("");

      EXEC SQL AT DB01
         DECLARE emp_cursor CURSOR FOR
            SELECT EMPNO, NAME, DEPTNO
            FROM EMPLOYEES
            WHERE MANAGER = :Manager_To_Find  ;

      EXEC SQL WHENEVER NOT FOUND DO Not_Found;
      EXEC SQL WHENEVER SQLERROR RAISE Some_Error;

      Put_Line("Emp. Number  Employee");
      Put_Line("=========== ======================");

      EXEC SQL AT DB01
         OPEN emp_cursor ;

      -- read out all data seleted by the query
      loop
         EXEC SQL AT DB01
            FETCH FROM emp_cursor
            INTO :empno, :name, :depno INDICATOR :depno_ind  ;

         exit when SQLCODE in SQL_STANDARD.NOT_FOUND;

         Put( Integer(empno), width => 10  );         -- employee number
         Put( "   " & To_String(Name) & " " );         -- name of the employee

         if Is_Null( depno_ind ) then
            Put( "department number not defined"  );
         else
            Put (Integer(depno), Width => 10);
         end if;
         New_Line;
      end loop;

      Put_Line("");
      EXEC SQL AT DB01 CLOSE emp_cursor;

      -- This will perform a single query
      Put_Line( "Printing Employee Data" );

      Print_Employee(1, EXEC SQL INCLUDE DATABASE NAMED DB01 );
      Print_Employee(2, EXEC SQL INCLUDE DATABASE NAMED DB01 );

   exception
      when Nothing_Found =>
         Put_Line("Nobody found");

         EXEC SQL WHENEVER NOT FOUND CONTINUE;
         EXEC SQL WHENEVER SQLERROR CONTINUE;

         EXEC SQL AT DB01 END ;

      when Others =>
         Put_Line( "Exception " );
         EXEC SQL WHENEVER NOT FOUND CONTINUE;
         EXEC SQL WHENEVER SQLERROR CONTINUE;

         EXEC SQL AT DB01 END ;
         raise;
   end Get_Manager;

begin

   EXEC SQL CONNECT "$DBUSER"
            IDENTIFIED BY "$DBPASSWD"
            BY DB01
            TO "$DBSOURCE" ;         -- Hallo Test

end Simple_Pkg;

--- end of file