KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > Call


1 /**
2  * com.mckoi.database.interpret.Call 15 Sep 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import com.mckoi.database.*;
28 import java.io.*;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * A statement that calls a procedure, and returns a resultant table. This
34  * is used to perform some sort of function over the database. For example,
35  * "CALL SYSTEM_MAKE_BACKUP('/my_backups/1')" makes a copy of the database in
36  * the given directory on the disk.
37  *
38  * @author Tobias Downer
39  */

40
41 public class Call extends Statement {
42
43   // ---------- Implemented from Statement ----------
44

45   public void prepare() throws DatabaseException {
46   }
47
48   public Table evaluate() throws DatabaseException {
49
50     DatabaseQueryContext context = new DatabaseQueryContext(database);
51
52     String JavaDoc proc_name = (String JavaDoc) cmd.getObject("proc_name");
53     Expression[] args = (Expression[]) cmd.getObject("args");
54
55     // Get the procedure manager
56
ProcedureManager manager = database.getProcedureManager();
57     ProcedureName name;
58
59     TableName p_name = null;
60
61     // If no schema def given in the procedure name, first check for the
62
// function in the SYS_INFO schema.
63
if (proc_name.indexOf(".") == -1) {
64       // Resolve the procedure name into a TableName object.
65
String JavaDoc schema_name = database.getCurrentSchema();
66       TableName tp_name = TableName.resolve(Database.SYSTEM_SCHEMA, proc_name);
67       tp_name = database.tryResolveCase(tp_name);
68       
69       // If exists then use this
70
if (manager.procedureExists(tp_name)) {
71         p_name = tp_name;
72       }
73     }
74
75     if (p_name == null) {
76       // Resolve the procedure name into a TableName object.
77
String JavaDoc schema_name = database.getCurrentSchema();
78       TableName tp_name = TableName.resolve(schema_name, proc_name);
79       tp_name = database.tryResolveCase(tp_name);
80
81       // Does the schema exist?
82
boolean ignore_case = database.isInCaseInsensitiveMode();
83       SchemaDef schema =
84                   database.resolveSchemaCase(tp_name.getSchema(), ignore_case);
85       if (schema == null) {
86         throw new DatabaseException("Schema '" + tp_name.getSchema() +
87                                     "' doesn't exist.");
88       }
89       else {
90         tp_name = new TableName(schema.getName(), tp_name.getName());
91       }
92
93       // If this doesn't exist then generate the error
94
if (!manager.procedureExists(tp_name)) {
95         throw new DatabaseException("Stored procedure '" + proc_name +
96                                     "' was not found.");
97       }
98
99       p_name = tp_name;
100     }
101
102     // Does the procedure exist in the system schema?
103
name = new ProcedureName(p_name);
104
105     // Check the user has privs to use this stored procedure
106
if (!database.getDatabase().canUserExecuteStoredProcedure(context,
107                                                      user, name.toString())) {
108       throw new UserAccessException("User not permitted to call: " + proc_name);
109     }
110
111     // Evaluate the arguments
112
TObject[] vals = new TObject[args.length];
113     for (int i = 0; i < args.length; ++i) {
114       if (args[i].isConstant()) {
115         vals[i] = args[i].evaluate(null, null, context);
116       }
117       else {
118         throw new StatementException(
119                          "CALL argument is not a constant: " + args[i].text());
120       }
121     }
122
123     // Invoke the procedure
124
TObject result = manager.invokeProcedure(name, vals);
125
126     // Return the result of the procedure,
127
return FunctionTable.resultTable(context, result);
128
129   }
130
131 }
132
133
Popular Tags