KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.interpret.Sequence 07 Apr 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 com.mckoi.debug.*;
29
30 /**
31  * A statement tree for creating and dropping sequence generators.
32  *
33  * @author Tobias Downer
34  */

35
36 public class Sequence extends Statement {
37
38   String JavaDoc type;
39   
40   TableName seq_name;
41   
42   Expression increment;
43   Expression min_value;
44   Expression max_value;
45   Expression start_value;
46   Expression cache_value;
47   boolean cycle;
48   
49   // ----------- Implemented from Statement ----------
50

51   public void prepare() throws DatabaseException {
52     type = (String JavaDoc) cmd.getObject("type");
53     String JavaDoc sname = (String JavaDoc) cmd.getObject("seq_name");
54     String JavaDoc schema_name = database.getCurrentSchema();
55     seq_name = TableName.resolve(schema_name, sname);
56     seq_name = database.tryResolveCase(seq_name);
57
58     if (type.equals("create")) {
59       // Resolve the function name into a TableName object.
60
increment = (Expression) cmd.getObject("increment");
61       min_value = (Expression) cmd.getObject("min_value");
62       max_value = (Expression) cmd.getObject("max_value");
63       start_value = (Expression) cmd.getObject("start");
64       cache_value = (Expression) cmd.getObject("cache");
65       cycle = cmd.getObject("cycle") != null;
66     }
67
68   }
69
70   public Table evaluate() throws DatabaseException {
71     
72     DatabaseQueryContext context = new DatabaseQueryContext(database);
73
74     // Does the schema exist?
75
boolean ignore_case = database.isInCaseInsensitiveMode();
76     SchemaDef schema =
77               database.resolveSchemaCase(seq_name.getSchema(), ignore_case);
78     if (schema == null) {
79       throw new DatabaseException("Schema '" + seq_name.getSchema() +
80                                   "' doesn't exist.");
81     }
82     else {
83       seq_name = new TableName(schema.getName(), seq_name.getName());
84     }
85
86     if (type.equals("create")) {
87
88       // Does the user have privs to create this sequence generator?
89
if (!database.getDatabase().canUserCreateSequenceObject(context,
90                                                               user, seq_name)) {
91         throw new UserAccessException(
92                         "User not permitted to create sequence: " + seq_name);
93       }
94
95       // Does a table already exist with this name?
96
if (database.tableExists(seq_name)) {
97         throw new DatabaseException("Database object with name '" + seq_name +
98                                     "' already exists.");
99       }
100
101       // Resolve the expressions,
102
long v_start_value = 0;
103       if (start_value != null) {
104         v_start_value =
105            start_value.evaluate(null, null, context).toBigNumber().longValue();
106       }
107       long v_increment_by = 1;
108       if (increment != null) {
109         v_increment_by =
110              increment.evaluate(null, null, context).toBigNumber().longValue();
111       }
112       long v_min_value = 0;
113       if (min_value != null) {
114         v_min_value =
115              min_value.evaluate(null, null, context).toBigNumber().longValue();
116       }
117       long v_max_value = Long.MAX_VALUE;
118       if (max_value != null) {
119         v_max_value =
120              max_value.evaluate(null, null, context).toBigNumber().longValue();
121       }
122       long v_cache = 16;
123       if (cache_value != null) {
124         v_cache =
125            cache_value.evaluate(null, null, context).toBigNumber().longValue();
126         if (v_cache <= 0) {
127           throw new DatabaseException("Cache size can not be <= 0");
128         }
129       }
130
131       if (v_min_value >= v_max_value) {
132         throw new DatabaseException("Min value can not be >= the max value.");
133       }
134       if (v_start_value < v_min_value ||
135           v_start_value >= v_max_value) {
136         throw new DatabaseException(
137                        "Start value is outside the min/max sequence bounds.");
138       }
139
140       database.createSequenceGenerator(seq_name,
141              v_start_value, v_increment_by, v_min_value, v_max_value,
142              v_cache, cycle);
143
144       // The initial grants for a sequence is to give the user who created it
145
// full access.
146
database.getGrantManager().addGrant(
147            Privileges.PROCEDURE_ALL_PRIVS, GrantManager.TABLE,
148            seq_name.toString(), user.getUserName(), true,
149            Database.INTERNAL_SECURE_USERNAME);
150
151     }
152     else if (type.equals("drop")) {
153
154       // Does the user have privs to create this sequence generator?
155
if (!database.getDatabase().canUserDropSequenceObject(context,
156                                                             user, seq_name)) {
157         throw new UserAccessException(
158                         "User not permitted to drop sequence: " + seq_name);
159       }
160
161       database.dropSequenceGenerator(seq_name);
162
163       // Drop the grants for this object
164
database.getGrantManager().revokeAllGrantsOnObject(
165                                     GrantManager.TABLE, seq_name.toString());
166
167     }
168     else {
169       throw new RuntimeException JavaDoc("Unknown type: " + type);
170     }
171
172     // Return an update result table.
173
return FunctionTable.resultTable(context, 0);
174
175   }
176
177 }
178
179
Popular Tags