1 package com.quadcap.sql; 2 3 40 41 import java.util.Vector ; 42 43 import java.io.IOException ; 44 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 48 import com.quadcap.util.Debug; 49 50 55 56 public class StmtUpdate implements Stmt { 57 String tableName; 58 SelectExpression select; 59 Vector items; 60 61 64 public StmtUpdate() {} 65 66 69 public StmtUpdate(String tableName) { 70 this.tableName = tableName; 71 this.items = new Vector (); 72 } 73 74 77 public void addItem(UpdateItem item) { 78 items.addElement(item); 79 } 80 81 84 public void addWhere(Expression where) { 85 select = new SelectExpression(tableName, where); 86 } 87 88 91 public void execute(Session session) throws IOException , SQLException { 92 Database db = session.getDatabase(); 93 Relation t = db.getRelation(tableName); 94 if (t == null) { 95 throw new SQLException ("No such table/view: " + tableName, 96 "42000"); 97 } 98 if (!t.isUpdatable()) { 99 throw new SQLException ("Not updatable: " + tableName, 100 "42000"); 101 } 102 session.getTableWriteLock(tableName); 103 Cursor updateCursor = TableOps.getCursor(session, t, select); 104 try { 105 while (updateCursor.next()) { 106 Row row = updateCursor.getRow(); 107 for (int i = 0; i < items.size(); i++) { 108 UpdateItem item = (UpdateItem)items.elementAt(i); 109 item.evaluate(session, t, updateCursor); 110 } 111 for (int i = 0; i < items.size(); i++) { 112 UpdateItem item = (UpdateItem)items.elementAt(i); 113 item.update(session, row); 114 } 115 session.clearViewCheck(); 116 updateCursor.updateRow(row); } 118 } finally { 119 updateCursor.close(); 120 } 121 } 122 } 123 | Popular Tags |