File: sql.adb

package info (click to toggle)
gnade 1.5.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,084 kB
  • ctags: 308
  • sloc: ada: 21,567; sh: 3,648; makefile: 843; sql: 378; awk: 29; xml: 9
file content (190 lines) | stat: -rw-r--r-- 7,062 bytes parent folder | download
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
-------------------------------------------------------------------------------
--
--  Filename        : $Source: /cvsroot/gnade/gnade/tools/sql.adb,v $
--  Description     : Execute an sql command                                 --
--  Author          : Michael Erdmann                                        --
--  Created         : 5.4.2003                                               --
--  Last Modified By: $Author: merdmann $
--  Last Modified On: $Date: 2003/05/10 16:24:03 $
--  Status          : $State: Exp $
--
--  Copyright (C) 2000 - 2003 Michael Erdmann                                --
--                                                                           --
--  GNADE is free software;  you can redistribute it  and/or modify it under --
--  terms of the  GNU General Public License as published  by the Free Soft- --
--  ware  Foundation;  either version 2,  or (at your option) any later ver- --
--  sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
--  OUT 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  distributed with GNAT;  see file COPYING.  If not, write --
--  to  the Free Software Foundation,  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.                                      --
--                                                                           --
--  Author: Michael Erdmann <michael.erdmann@snafu.de>                       --
--                                                                           --
--  GNADE is implemented to work with GNAT, the GNU Ada compiler.            --
--                                                                           --
--  Functional Description                                                   --
--  ======================                                                   --
--                                                                           --
--  This programm allows to explore an odbc interface                        --
--       								     --
--                                                                           --
--  Restrictions                                                             --
--  ============                                                             --
--  None                                                                     --
--                                                                           --
--  References                                                               --
--  ==========                                                               --
--  None                                                                     --
--                                                                           --
-------------------------------------------------------------------------------
with Ada.Text_IO;		use Ada.Text_IO;
with Ada.Command_Line;          use Ada.Command_Line;
with Ada.Strings.Unbounded;     use Ada.Strings.Unbounded;
with ODBC;      		use ODBC;
with Tools;                     use Tools;

procedure SQL is
   Version : constant String := "$Id: sql.adb,v 1.6 2003/05/10 16:24:03 merdmann Exp $";

   Q       : Unbounded_String := Null_Unbounded_String;
   DB      : ODBC.Object;
   Login   : Unbounded_String;
   Pwd     : Unbounded_String;
   Source  : Unbounded_String;
   Argc    : Natural := 1;
   Quiet   : Boolean := False;

   Error, Message : Unbounded_String;

   ----------
   -- Help --
   ----------
   procedure Help is
   begin
      Put_Line("usage : " );
      Put_Line("    sql -u <login> [ -l <passwd> ]  <source> <query> " );
      Put_Line("");
   end Help;

   ------------
   -- Header --
   ------------
   procedure Print_Header( 
      R : in Result_Record ) is 
      -- Display the column name of a given result set
   begin
      if Quiet then
         return;
      end if;

      for I in R'Range loop
         Put( Field_Name(DB, I) & " " ); 
      end loop;
      Put_Line("");
   end Print_Header;

   ---------
   -- Row --
   ---------
   procedure Print_Row( 
      R : in Result_Record )  is 
   begin
      for I in R'Range loop
         Put( To_String( R(I) ) & ";" );
      end loop;
      Put_Line("");
   end Print_Row;

begin 
   Initialize(DB);

   -- Handle the command line arguments
   while Argc in 1..Argument_Count loop
      declare
         Arg : constant String := Argument( Argc );
      begin
         if Arg = "-u" then
            Argc := Argc + 1;
            Login := To_Unbounded_String( Argument( Argc ) );
         elsif Arg = "-p" then
            Argc := Argc + 1;
            Pwd := To_Unbounded_String( Argument( Argc ) );
         elsif Arg = "-h" then
	    Help;
	    return;
	 elsif Arg = "-q" then
	    Quiet := True;
	 else
	    if Source = Null_Unbounded_String then
	    	Source := To_Unbounded_String( Arg );
	    else 
	        Q:= Q & To_Unbounded_String( Arg & " " );
	    end if;
	 end if;
         Argc := Argc + 1;
      end;
   end loop;

   if not Quiet then
      Put_Line("GNADE SQL Executor, Version " & Tools.Version);
      Put_Line("Copyright (C) 2003, Michael Erdmann (michael.erdmann@snafu.de)");
      Put_Line("");
   end if;     

   if Source = Null_Unbounded_String then
      Put_Line("source not specified");
      Put_Line("");
      Help;
      Set_Exit_Status(Failure);
      return;
   end if;

   -- connect to the data base
   Authorization( DB, To_String(Login), To_String(Pwd) );
   Data_Source( DB, To_String(Source) );
   Connect( DB );

   -- if a query is available execute it make the result visible
   if Q /= Null_Unbounded_String then
      declare
         R : Result_Record := null;
      begin
         Query( DB, To_String(Q) );
	 R := Fetch( DB );

         Print_Header(R);

	 while R /= null loop
 
	    Print_Row( R );
	    Free( R );
	    R := Fetch( DB );	
	 end loop;

      exception
         when others =>
            Get_Error( DB, Error, Message );
	    Put_Line( To_String( Error ) & ":" & To_String( Message ) );	   
	    Set_Exit_Status( Failure ); 
      end;
   end if; 

   --Finalize( DB );
   Set_Exit_Status( Success );

exception
   when Others =>
      Get_Error( DB, Error, Message );
      Put_Line( To_String( Error ) & ":" & To_String( Message ) );	   
      Set_Exit_Status( Failure ); 
end SQL;