1 5 package org.h2.schema; 6 7 import java.sql.SQLException ; 8 import java.util.HashMap ; 9 10 import org.h2.constraint.Constraint; 11 import org.h2.engine.Constants; 12 import org.h2.engine.Database; 13 import org.h2.engine.DbObject; 14 import org.h2.engine.Session; 15 import org.h2.engine.User; 16 import org.h2.index.Index; 17 import org.h2.jdbc.JdbcSQLException; 18 import org.h2.message.Message; 19 import org.h2.message.Trace; 20 import org.h2.table.Table; 21 import org.h2.util.ObjectArray; 22 23 public class Schema extends DbObject { 24 25 private User owner; 26 private boolean system; 27 28 private HashMap tablesAndViews = new HashMap (); 29 private HashMap indexes = new HashMap (); 30 private HashMap sequences = new HashMap (); 31 private HashMap triggers = new HashMap (); 32 private HashMap constraints = new HashMap (); 33 private HashMap constants = new HashMap (); 34 35 public Schema(Database database, int id, String schemaName, User owner, boolean system) { 36 super(database, id, schemaName, Trace.SCHEMA); 37 this.owner = owner; 38 this.system = system; 39 } 40 41 public boolean canDrop() { 42 return !getName().equals(Constants.SCHEMA_INFORMATION) && !getName().equals(Constants.SCHEMA_MAIN); 43 } 44 45 public String getCreateSQLForCopy(Table table, String quotedName) { 46 throw Message.getInternalError(); 47 } 48 49 public String getCreateSQL() { 50 if(system) { 51 return null; 52 } 53 StringBuffer buff = new StringBuffer (); 54 buff.append("CREATE SCHEMA "); 55 buff.append(getSQL()); 56 buff.append(" AUTHORIZATION "); 57 buff.append(owner.getSQL()); 58 return buff.toString(); 59 } 60 61 public int getType() { 62 return DbObject.SCHEMA; 63 } 64 65 public void removeChildrenAndResources(Session session) throws SQLException { 66 while(triggers != null && triggers.size()>0) { 67 TriggerObject obj = (TriggerObject)triggers.values().toArray()[0]; 68 database.removeSchemaObject(session, obj); 69 } 70 while(constraints != null && constraints.size()>0) { 71 Constraint obj = (Constraint)constraints.values().toArray()[0]; 72 database.removeSchemaObject(session, obj); 73 } 74 while(tablesAndViews != null && tablesAndViews.size()>0) { 75 Table obj = (Table)tablesAndViews.values().toArray()[0]; 76 database.removeSchemaObject(session, obj); 77 } 78 while(indexes != null && indexes.size()>0) { 79 Index obj = (Index)indexes.values().toArray()[0]; 80 database.removeSchemaObject(session, obj); 81 } 82 while(sequences != null && sequences.size()>0) { 83 Sequence obj = (Sequence)sequences.values().toArray()[0]; 84 database.removeSchemaObject(session, obj); 85 } 86 while(constants != null && constants.size()>0) { 87 Constant obj = (Constant)constants.values().toArray()[0]; 88 database.removeSchemaObject(session, obj); 89 } 90 owner = null; 91 invalidate(); 92 } 93 94 public void checkRename() throws SQLException { 95 } 96 97 public User getOwner() { 98 return owner; 99 } 100 101 private HashMap getMap(int type) { 102 switch(type) { 103 case DbObject.TABLE_OR_VIEW: 104 return tablesAndViews; 105 case DbObject.SEQUENCE: 106 return sequences; 107 case DbObject.INDEX: 108 return indexes; 109 case DbObject.TRIGGER: 110 return triggers; 111 case DbObject.CONSTRAINT: 112 return constraints; 113 case DbObject.CONSTANT: 114 return constants; 115 default: 116 throw Message.getInternalError("type="+type); 117 } 118 } 119 120 public void add(SchemaObject obj) throws SQLException { 121 if(Constants.CHECK && obj.getSchema() != this) { 122 throw Message.getInternalError("wrong schema"); 123 } 124 String name = obj.getName(); 125 HashMap map = getMap(obj.getType()); 126 if(Constants.CHECK && map.get(name) != null) { 127 throw Message.getInternalError("object already exists"); 128 } 129 map.put(name, obj); 130 } 131 132 public void rename(SchemaObject obj, String newName) throws SQLException { 133 int type = obj.getType(); 134 HashMap map = getMap(type); 135 if(Constants.CHECK) { 136 if(!map.containsKey(obj.getName())) { 137 throw Message.getInternalError("not found: "+obj.getName()); 138 } 139 if(obj.getName().equals(newName) || map.containsKey(newName)) { 140 throw Message.getInternalError("object already exists: "+newName); 141 } 142 } 143 map.remove(obj.getName()); 144 obj.rename(newName); 145 map.put(newName, obj); 146 } 147 148 public Table findTableOrView(Session session, String name) { 149 Table table = (Table) tablesAndViews.get(name); 150 if(table == null && session != null) { 151 table = session.findLocalTempTable(name); 152 } 153 return table; 154 } 155 156 public Index findIndex(String name) { 157 return (Index) indexes.get(name); 158 } 159 160 public TriggerObject findTrigger(String name) { 161 return (TriggerObject) triggers.get(name); 162 } 163 164 public Sequence findSequence(String sequenceName) { 165 return (Sequence) sequences.get(sequenceName); 166 } 167 168 public Constraint findConstraint(String constraintName) { 169 return (Constraint) constraints.get(constraintName); 170 } 171 172 public Constant findConstant(String constantName) { 173 return (Constant) constants.get(constantName); 174 } 175 176 private String getUniqueName(HashMap map, String prefix) { 177 for(int i=0;; i++) { 178 String name = prefix + i; 179 if(map.get(name)==null) { 180 return name; 181 } 182 } 183 } 184 185 public String getUniqueConstraintName() { 186 return getUniqueName(constraints, "CONSTRAINT_"); 187 } 188 189 public String getUniqueIndexName(String prefix) { 190 return getUniqueName(indexes, prefix); 191 } 192 193 public Table getTableOrView(Session session, String name) throws SQLException { 194 Table table = (Table) tablesAndViews.get(name); 195 if(table == null && session != null) { 196 table = session.findLocalTempTable(name); 197 } 198 if (table == null) { 199 throw Message.getSQLException(Message.TABLE_OR_VIEW_NOT_FOUND_1, name); 200 } 201 return table; 202 } 203 204 public Index getIndex(String name) throws JdbcSQLException { 205 Index index = (Index) indexes.get(name); 206 if (index == null) { 207 throw Message.getSQLException(Message.INDEX_NOT_FOUND_1, name); 208 } 209 return index; 210 } 211 212 public Constraint getConstraint(String name) throws SQLException { 213 Constraint constraint = (Constraint) constraints.get(name); 214 if (constraint == null) { 215 throw Message.getSQLException(Message.CONSTRAINT_NOT_FOUND_1, name); 216 } 217 return constraint; 218 } 219 220 public Constant getConstant(Session session, String constantName) throws SQLException { 221 Constant constant = (Constant) constants.get(constantName); 222 if (constant == null) { 223 throw Message.getSQLException(Message.CONSTANT_NOT_FOUND_1, constantName); 224 } 225 return constant; 226 } 227 228 public Sequence getSequence(String sequenceName) throws SQLException { 229 Sequence sequence = (Sequence) sequences.get(sequenceName); 230 if (sequence == null) { 231 throw Message.getSQLException(Message.SEQUENCE_NOT_FOUND_1, sequenceName); 232 } 233 return sequence; 234 } 235 236 public ObjectArray getAll(int type) { 237 HashMap map = getMap(type); 238 return new ObjectArray(map.values()); 239 } 240 241 public void remove(Session session, SchemaObject obj) throws SQLException { 242 String objName = obj.getName(); 243 HashMap map = getMap(obj.getType()); 244 if(Constants.CHECK && !map.containsKey(objName)) { 245 throw Message.getInternalError("not found: "+objName); 246 } 247 map.remove(objName); 248 } 249 250 } 251 | Popular Tags |