KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.interpret.Function 30 Mar 2003
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.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * A handler for defining and dropping functions.
33  *
34  * @author Tobias Downer
35  */

36
37 public class Function extends Statement {
38
39   /**
40    * The type of command we are running through this Function object.
41    */

42   private String JavaDoc type;
43
44   /**
45    * The name of the function.
46    */

47   private TableName fun_name;
48   
49   // ----------- Implemented from Statement ----------
50

51   public void prepare() throws DatabaseException {
52     type = (String JavaDoc) cmd.getObject("type");
53     String JavaDoc function_name = (String JavaDoc) cmd.getObject("function_name");
54
55     // Resolve the function name into a TableName object.
56
String JavaDoc schema_name = database.getCurrentSchema();
57     fun_name = TableName.resolve(schema_name, function_name);
58     fun_name = database.tryResolveCase(fun_name);
59
60   }
61
62   public Table evaluate() throws DatabaseException {
63     
64     DatabaseQueryContext context = new DatabaseQueryContext(database);
65
66     // Does the schema exist?
67
boolean ignore_case = database.isInCaseInsensitiveMode();
68     SchemaDef schema =
69             database.resolveSchemaCase(fun_name.getSchema(), ignore_case);
70     if (schema == null) {
71       throw new DatabaseException("Schema '" + fun_name.getSchema() +
72                                   "' doesn't exist.");
73     }
74     else {
75       fun_name = new TableName(schema.getName(), fun_name.getName());
76     }
77
78     if (type.equals("create")) {
79
80       // Does the user have privs to create this function?
81
if (!database.getDatabase().canUserCreateProcedureObject(context,
82                                                              user, fun_name)) {
83         throw new UserAccessException(
84                         "User not permitted to create function: " + fun_name);
85       }
86
87       // Does a table already exist with this name?
88
if (database.tableExists(fun_name)) {
89         throw new DatabaseException("Database object with name '" + fun_name +
90                                     "' already exists.");
91       }
92
93       // Get the information about the function we are creating
94
List JavaDoc arg_names = (List JavaDoc) cmd.getObject("arg_names");
95       List JavaDoc arg_types = (List JavaDoc) cmd.getObject("arg_types");
96       TObject loc_name = (TObject) cmd.getObject("location_name");
97       TType return_type = (TType) cmd.getObject("return_type");
98
99       // Note that we currently ignore the arg_names list.
100

101       
102       // Convert arg types to an array
103
TType[] arg_type_array =
104                      (TType[]) arg_types.toArray(new TType[arg_types.size()]);
105
106       // We must parse the location name into a class name, and method name
107
String JavaDoc java_specification = loc_name.getObject().toString();
108       // Resolve the java_specification to an invokation method.
109
java.lang.reflect.Method JavaDoc proc_method =
110             ProcedureManager.javaProcedureMethod(java_specification,
111                                                  arg_type_array);
112       if (proc_method == null) {
113         throw new DatabaseException("Unable to find invokation method for " +
114                           "Java stored procedure name: " + java_specification);
115       }
116
117       // Convert the information into an easily digestible form.
118
ProcedureName proc_name = new ProcedureName(fun_name);
119       int sz = arg_types.size();
120       TType[] arg_list = new TType[sz];
121       for (int i = 0; i < sz; ++i) {
122         arg_list[i] = (TType) arg_types.get(i);
123       }
124
125       // Create the (Java) function,
126
ProcedureManager manager = database.getProcedureManager();
127       manager.defineJavaProcedure(proc_name, java_specification,
128                                   return_type, arg_list, user.getUserName());
129
130       // The initial grants for a procedure is to give the user who created it
131
// full access.
132
database.getGrantManager().addGrant(
133            Privileges.PROCEDURE_ALL_PRIVS, GrantManager.TABLE,
134            proc_name.toString(), user.getUserName(), true,
135            Database.INTERNAL_SECURE_USERNAME);
136
137     }
138     else if (type.equals("drop")) {
139
140       // Does the user have privs to create this function?
141
if (!database.getDatabase().canUserDropProcedureObject(context,
142                                                              user, fun_name)) {
143         throw new UserAccessException(
144                         "User not permitted to drop function: " + fun_name);
145       }
146
147       // Drop the function
148
ProcedureName proc_name = new ProcedureName(fun_name);
149       ProcedureManager manager = database.getProcedureManager();
150       manager.deleteProcedure(proc_name);
151
152       // Drop the grants for this object
153
database.getGrantManager().revokeAllGrantsOnObject(
154                                     GrantManager.TABLE, proc_name.toString());
155       
156     }
157     else {
158       throw new RuntimeException JavaDoc("Unknown type: " + type);
159     }
160
161     // Return an update result table.
162
return FunctionTable.resultTable(context, 0);
163
164   }
165
166 }
167
168
Popular Tags