1 19 package org.netbeans.mdr.persistence.jdbcimpl; 20 21 import org.netbeans.mdr.persistence.*; 22 import org.netbeans.mdr.util.*; 23 24 import java.util.*; 25 import java.io.*; 26 import java.text.*; 27 import java.sql.*; 28 29 36 class JdbcMultivaluedIndex 37 extends JdbcIndex implements MultivaluedIndex 38 { 39 protected boolean queryDuplicates; 40 41 protected LazyPreparedStatement sqlDeleteWithValue; 42 protected LazyPreparedStatement sqlDeleteWithSurrogate; 43 protected LazyPreparedStatement sqlFindCount; 44 protected LazyPreparedStatement sqlFindCountWithValue; 45 protected LazyPreparedStatement sqlFindSurrogate; 46 47 protected void defineSql() 48 { 49 super.defineSql(); 50 51 sqlDeleteWithValue = new LazyPreparedStatement( 52 "delete from " + tableName 53 + " where " + keyColName + " = ?" 54 + " and " + valColName + " = ?"); 55 56 sqlDeleteWithSurrogate = new LazyPreparedStatement( 57 "delete from " + tableName 58 + " where " + keyColName + " = ?" 59 + " and " + JdbcStorage.SURROGATE_COL_NAME + " = ?"); 60 61 sqlFindCount = new LazyPreparedStatement( 62 "select count(*) from " + tableName 63 + " where " + keyColName + " = ?"); 64 65 sqlFindCountWithValue = new LazyPreparedStatement( 66 "select count(*) from " + tableName 67 + " where " + keyColName + " = ?" 68 + " and " + valColName + " = ?"); 69 70 sqlFindSurrogate = new LazyPreparedStatement( 71 "select " + JdbcStorage.SURROGATE_COL_NAME 72 + " from " + tableName 73 + " where " + keyColName + " = ?" 74 + " and " + valColName + " = ?"); 75 } 76 77 public Collection getItems(Object key) throws StorageException 79 { 80 return new ItemCollection(key,null); 81 } 82 83 public Collection getObjects( 85 Object key, SinglevaluedIndex repos) throws StorageException 86 { 87 if (keyType == Storage.EntryType.MOFID) { 88 return new ItemCollection(key,repos); 89 } else { 90 return getItems(key); 91 } 92 } 93 94 public boolean isUnique() throws StorageException 96 { 97 return !needSurrogate; 98 } 99 100 public void add(Object key, Object value) throws StorageException 102 { 103 if (queryDuplicates) { 104 int n = storage.getSingletonInt( 105 sqlFindCountWithValue, 106 new Object [] { key, value } ); 107 if (n > 0) { 108 throw new StorageBadRequestException("duplicate detected"); 109 } 110 } 111 addImpl(key, value); 112 } 113 114 public boolean remove(Object key, Object value) throws StorageException 116 { 117 Object [] pair = new Object []{key,value}; 118 119 int rowCount; 120 if (isUnique()) { 121 rowCount = storage.executeUpdate(sqlDeleteWithValue,pair); 124 } else { 125 int surrogateKey = storage.getSingletonInt(sqlFindSurrogate,pair); 128 if (surrogateKey == -1) { 129 return false; 130 } 131 rowCount = storage.executeUpdate( 132 sqlDeleteWithSurrogate, 133 new Object []{key,new Integer (surrogateKey)}); 134 } 135 return rowCount > 0; 136 } 137 138 public Collection queryByKeyPrefix( 140 Object prefix, SinglevaluedIndex repos) throws StorageException 141 { 142 throw new RuntimeException ("oops, not yet implemented"); 144 } 145 146 private class ItemCollection extends AbstractCollection 147 { 148 private Object key; 149 private SinglevaluedIndex repos; 150 151 ItemCollection(Object key,SinglevaluedIndex repos) 152 { 153 this.key = key; 154 this.repos = repos; 155 } 156 157 public Iterator iterator() 159 { 160 try { 161 return new ItemCollectionIter( 162 storage.getResultSetIterator( 163 sqlFind, 164 new Object []{key}, 165 getValueType()), 166 repos, 167 key); 168 } catch (StorageException ex) { 169 throw new RuntimeStorageException(ex); 170 } 171 } 172 173 public int size() 174 { 175 try { 176 return storage.getSingletonInt(sqlFindCount,new Object []{key}); 177 } catch (StorageException ex) { 178 throw new RuntimeStorageException(ex); 179 } 180 } 181 182 public boolean add(Object value) 184 { 185 try { 186 JdbcMultivaluedIndex.this.add(key,value); 187 return true; 188 } catch (StorageException ex) { 189 throw new RuntimeStorageException(ex); 190 } 191 } 192 } 193 194 private class ItemCollectionIter implements Iterator 195 { 196 private Iterator iter; 197 private SinglevaluedIndex repos; 198 private Object key; 199 private Object value; 200 201 ItemCollectionIter(Iterator iter,SinglevaluedIndex repos,Object key) 202 { 203 this.iter = iter; 204 this.repos = repos; 205 this.key = key; 206 } 207 208 public boolean hasNext() 210 { 211 return iter.hasNext(); 212 } 213 214 public Object next() 216 { 217 value = iter.next(); 218 if (repos != null) { 219 try { 220 return repos.get(value); 221 } catch (StorageException ex) { 222 throw new RuntimeStorageException(ex); 223 } 224 } 225 return value; 226 } 227 228 public void remove() 230 { 231 try { 232 JdbcMultivaluedIndex.this.remove(key,value); 238 } catch (StorageException ex) { 239 throw new RuntimeStorageException(ex); 240 } 241 } 242 } 243 } 244 245 | Popular Tags |