KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.interpret.Delete 14 Sep 2001
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 java.util.*;
28 import com.mckoi.database.*;
29
30 /**
31  * Logic for the DELETE FROM SQL statement.
32  *
33  * @author Tobias Downer
34  */

35
36 public class Delete extends Statement {
37
38   /**
39    * The name the table that we are to delete from.
40    */

41   String JavaDoc table_name;
42
43   /**
44    * If the delete statement has a 'where' clause, then this is set here. If
45    * it has no 'where' clause then we apply to the entire table.
46    */

47   SearchExpression where_condition;
48
49   /**
50    * The limit of the number of rows that are updated by this statement. A
51    * limit of < 0 means there is no limit.
52    */

53   int limit = -1;
54
55   // -----
56

57   /**
58    * The DataTable we are deleting from .
59    */

60   private DataTable update_table;
61
62   /**
63    * The TableName object of the table being created.
64    */

65   private TableName tname;
66
67   /**
68    * Tables that are relationally linked to the table being inserted into, set
69    * after 'prepare'. This is used to determine the tables we need to read
70    * lock because we need to validate relational constraints on the tables.
71    */

72   private ArrayList relationally_linked_tables;
73
74   /**
75    * The plan for the set of records we are deleting in this query.
76    */

77   private QueryPlanNode plan;
78
79
80
81
82
83   // ---------- Implemented from Statement ----------
84

85   public void prepare() throws DatabaseException {
86
87     // Get variables from the model.
88
table_name = (String JavaDoc) cmd.getObject("table_name");
89     where_condition = (SearchExpression) cmd.getObject("where_clause");
90     limit = cmd.getInt("limit");
91
92     // ---
93

94     // Resolve the TableName object.
95
tname = resolveTableName(table_name, database);
96     // Does the table exist?
97
if (!database.tableExists(tname)) {
98       throw new DatabaseException("Table '" + tname + "' does not exist.");
99     }
100     // Get the table we are updating
101
update_table = database.getTable(tname);
102
103     // Form a TableSelectExpression that represents the select on the table
104
TableSelectExpression select_expression = new TableSelectExpression();
105     // Create the FROM clause
106
select_expression.from_clause.addTable(table_name);
107     // Set the WHERE clause
108
select_expression.where_clause = where_condition;
109
110     // Generate the TableExpressionFromSet hierarchy for the expression,
111
TableExpressionFromSet from_set =
112                        Planner.generateFromSet(select_expression, database);
113     // Form the plan
114
plan = Planner.formQueryPlan(database, select_expression, from_set, null);
115
116     // Resolve all tables linked to this
117
TableName[] linked_tables =
118                              database.queryTablesRelationallyLinkedTo(tname);
119     relationally_linked_tables = new ArrayList(linked_tables.length);
120     for (int i = 0; i < linked_tables.length; ++i) {
121       relationally_linked_tables.add(database.getTable(linked_tables[i]));
122     }
123
124   }
125
126   public Table evaluate() throws DatabaseException {
127
128     DatabaseQueryContext context = new DatabaseQueryContext(database);
129
130     // Check that this user has privs to delete from the table.
131
if (!database.getDatabase().canUserDeleteFromTableObject(context,
132                                                              user, tname)) {
133       throw new UserAccessException(
134                     "User not permitted to delete from table: " + table_name);
135     }
136
137     // Check the user has select permissions on the tables in the plan.
138
Select.checkUserSelectPermissions(context, user, plan);
139     
140     // Evaluates the delete statement...
141

142     // Evaluate the plan to find the update set.
143
Table delete_set = plan.evaluate(context);
144
145     // Delete from the data table.
146
int delete_count = update_table.delete(delete_set, limit);
147
148     // Notify TriggerManager that we've just done an update.
149
if (delete_count > 0) {
150       database.notifyTriggerEvent(new TriggerEvent(
151                         TriggerEvent.DELETE, tname.toString(), delete_count));
152     }
153
154     // Return the number of columns we deleted.
155
return FunctionTable.resultTable(context, delete_count);
156
157   }
158
159
160 }
161
Popular Tags