1 23 24 30 31 package com.sun.jdo.spi.persistence.support.sqlstore.sql.generator; 32 33 import org.netbeans.modules.dbschema.TableElement; 34 import com.sun.jdo.api.persistence.support.JDOFatalInternalException; 35 import com.sun.jdo.spi.persistence.support.sqlstore.ActionDesc; 36 import com.sun.jdo.spi.persistence.support.sqlstore.SQLStoreManager; 37 import com.sun.jdo.spi.persistence.support.sqlstore.model.ClassDesc; 38 import com.sun.jdo.spi.persistence.support.sqlstore.model.ReferenceKeyDesc; 39 import com.sun.jdo.spi.persistence.support.sqlstore.model.TableDesc; 40 import com.sun.jdo.spi.persistence.utility.I18NHelper; 41 42 import java.util.ArrayList ; 43 import java.util.ResourceBundle ; 44 45 46 49 public abstract class QueryPlan { 50 public static final int ACT_UPDATE = 1; 51 52 public static final int ACT_INSERT = 2; 53 54 public static final int ACT_DELETE = 3; 55 56 public static final int ACT_SELECT = 4; 57 58 public static final int ACT_NOOP = 5; 59 60 protected static final int ST_BUILT = 0x1; 61 62 63 public ArrayList statements; 64 65 protected ClassDesc config; 66 67 72 protected int status; 73 74 protected int action; 75 76 protected SQLStoreManager store; 77 78 79 protected ArrayList tables; 80 81 82 protected final static ResourceBundle messages = I18NHelper.loadBundle( 83 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", QueryPlan.class.getClassLoader()); 85 86 public QueryPlan(ActionDesc desc, SQLStoreManager store) { 87 this.tables = new ArrayList (); 88 this.statements = new ArrayList (); 89 this.store = store; 90 this.config = (ClassDesc) store.getPersistenceConfig(desc.getPersistenceCapableClass()); 91 } 92 93 public QueryTable addQueryTable(TableDesc tableDesc) { 94 QueryTable table = new QueryTable(tableDesc); 95 tables.add(table); 96 table.setTableIndex(new TableIndex(tables.size() - 1)); 97 return table; 98 } 99 100 112 public QueryTable addQueryTable(TableElement tableElement, 113 ClassDesc persistenceConfig) { 114 115 ClassDesc _config = (persistenceConfig == null) ? this.config : persistenceConfig; 116 TableDesc tableDesc = _config.findTableDesc(tableElement); 117 118 if (tableDesc == null) { 119 120 if (tableElement != null) { 121 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 122 "core.configuration.classnotmappedtotable", _config.getPersistenceCapableClass().getName(), 124 tableElement.getName().getName())); 125 } else { 126 throw new JDOFatalInternalException(I18NHelper.getMessage(messages, 127 "core.configuration.classnotmapped", _config.getPersistenceCapableClass().getName())); 129 130 } 131 } 132 133 return addQueryTable(tableDesc); 134 } 135 136 143 public void addQueryTables(ArrayList queryTables) { 144 145 for (int i = 0; i < queryTables.size(); i++) { 146 QueryTable t = (QueryTable) queryTables.get(i); 147 148 if (tables.indexOf(t) == -1) { 149 tables.add(t); 150 t.getTableIndex().setValue(tables.size() - 1); 151 } 152 } 153 } 154 155 159 public QueryTable findQueryTable(TableElement tableElement) { 160 for (int i = 0; i < tables.size(); i++) { 161 QueryTable t = (QueryTable) tables.get(i); 162 163 if (t.getTableDesc().getTableElement() == tableElement) { 164 return t; 166 } 167 } 168 169 return null; 170 } 171 172 176 public QueryTable findQueryTable(TableDesc tableDesc) { 177 for (int i = 0; i < tables.size(); i++) { 178 QueryTable t = (QueryTable) tables.get(i); 179 180 if (t.getTableDesc() == tableDesc) return t; 181 } 182 183 return null; 184 } 185 186 public ArrayList getStatements() { 187 for (int i = 0; i < statements.size(); i++) { 188 Statement s = (Statement) statements.get(i); 189 s.getText(); 191 } 192 193 return statements; 194 } 195 196 protected Statement addStatement(QueryTable t) { 197 Statement s = createStatement(t); 198 statements.add(s); 199 200 return s; 201 } 202 203 protected abstract Statement newStatement(); 204 205 protected Statement createStatement(QueryTable t) { 206 Statement statement = newStatement(); 207 208 statement.action = action; 209 statement.addQueryTable(t); 210 211 return statement; 212 } 213 214 protected Statement getStatement(QueryTable t) { 215 if (t == null) return null; 216 217 for (int i = 0; i < statements.size(); i++) { 218 Statement s = (Statement) statements.get(i); 219 220 if (s.tableList.indexOf(t) != -1) 221 return s; 222 } 223 224 return null; 225 } 226 227 public abstract void build(); 228 229 233 protected void processStatements() { 234 for (int i = 0; i < statements.size(); i++) { 235 Statement s = (Statement) statements.get(i); 236 237 QueryTable qt = (QueryTable) s.getQueryTables().get(0); 238 239 ArrayList secondaryTableKeys = qt.getTableDesc().getSecondaryTableKeys(); 240 241 if (secondaryTableKeys != null) { 242 for (int j = 0; j < secondaryTableKeys.size(); j++) { 243 ReferenceKeyDesc secondaryTableKey = (ReferenceKeyDesc) secondaryTableKeys.get(j); 244 s.addSecondaryTableStatement(getStatement(findQueryTable(secondaryTableKey.getTableDesc()))); 245 } 246 } 247 } 248 } 249 250 public int getAction() { 251 return action; 252 } 253 254 public ClassDesc getConfig() { 255 return config; 256 } 257 258 } 259 260 261 262 263 264 | Popular Tags |