00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.util.Vector;
00042
00043
import java.io.IOException;
00044
00045
import java.sql.ResultSet;
00046
import java.sql.SQLException;
00047
00048
import com.quadcap.util.Debug;
00049
00050
00051
00052
00053
00054
00055
00056 public class StmtUpdate implements Stmt {
00057 String
tableName;
00058 SelectExpression select;
00059 Vector
items;
00060
00061
00062
00063
00064 public StmtUpdate() {}
00065
00066
00067
00068
00069 public StmtUpdate(String tableName) {
00070
this.tableName = tableName;
00071
this.items =
new Vector();
00072 }
00073
00074
00075
00076
00077 public void addItem(
UpdateItem item) {
00078
items.addElement(item);
00079 }
00080
00081
00082
00083
00084 public void addWhere(
Expression where) {
00085
select =
new SelectExpression(
tableName, where);
00086 }
00087
00088
00089
00090
00091 public void execute(
Session session)
throws IOException, SQLException {
00092
Database db = session.getDatabase();
00093
Relation t = db.
getRelation(
tableName);
00094
if (t == null) {
00095
throw new SQLException(
"No such table/view: " +
tableName,
00096
"42000");
00097 }
00098
if (!t.
isUpdatable()) {
00099
throw new SQLException(
"Not updatable: " +
tableName,
00100
"42000");
00101 }
00102 session.getTableWriteLock(
tableName);
00103
Cursor updateCursor =
TableOps.getCursor(session, t,
select);
00104
try {
00105
while (updateCursor.
next()) {
00106
Row row = updateCursor.
getRow();
00107
for (
int i = 0; i <
items.size(); i++) {
00108
UpdateItem item = (
UpdateItem)
items.elementAt(i);
00109 item.
evaluate(session, t, updateCursor);
00110 }
00111
for (
int i = 0; i <
items.size(); i++) {
00112
UpdateItem item = (
UpdateItem)
items.elementAt(i);
00113 item.
update(session, row);
00114 }
00115 session.clearViewCheck();
00116 updateCursor.
updateRow(row);
00117 }
00118 } finally {
00119 updateCursor.
close();
00120 }
00121 }
00122 }