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 27 34 class JdbcSinglevaluedIndex 35 extends JdbcIndex implements SinglevaluedIndex 36 { 37 protected LazyPreparedStatement sqlValuesIterator; 38 protected LazyPreparedStatement sqlValuesSize; 39 protected LazyPreparedStatement sqlValuesContains; 40 protected LazyPreparedStatement sqlUpdate; 41 42 protected void defineSql() 43 { 44 super.defineSql(); 45 46 sqlValuesIterator = new LazyPreparedStatement( 47 "select " + valColName + " from " + tableName); 48 49 sqlValuesSize = new LazyPreparedStatement( 50 "select count(*) from " + tableName); 51 52 sqlValuesContains = new LazyPreparedStatement( 53 "select count(*) from " + tableName + " where " 54 + valColName + " = ?"); 55 56 sqlUpdate = new LazyPreparedStatement( 57 "update " + tableName + " set " + valColName 58 + " = ? where " + keyColName + " = ?"); 59 } 60 61 protected boolean isKeyUnique() 62 { 63 return true; 64 } 65 66 public boolean put(Object key,Object value) throws StorageException 68 { 69 return putImpl(key,value); 70 } 71 72 protected boolean putImpl(Object key,Object value) throws StorageException 73 { 74 int rowCount = storage.executeUpdate( 77 sqlUpdate,new Object []{value,key}); 78 if (rowCount == 0) { 80 addImpl(key,value); 82 return false; 83 } else { 84 return true; 86 } 87 } 88 89 public void replace(Object key, Object value) 91 throws StorageException, StorageBadRequestException 92 { 93 replaceImpl(key,value); 94 } 95 96 protected void replaceImpl(Object key, Object value) 97 throws StorageException, StorageBadRequestException 98 { 99 if (!putImpl(key,value)) { 100 throw new StorageBadRequestException( 101 "Cannot replace item that does not exist in the index."); 102 } 103 } 104 105 public Object get(Object key) 107 throws StorageException, StorageBadRequestException 108 { 109 Object obj = getIfExists(key); 110 if (obj == null) { 111 throw new StorageBadRequestException ("Item not found: " + key); 112 } 113 return obj; 114 } 115 116 public Object getObject(Object key, SinglevaluedIndex repos) 118 throws StorageException 119 { 120 if (keyType == Storage.EntryType.MOFID) { 124 return repos.get(get(key)); 125 } else { 126 return get(key); 127 } 128 } 129 130 public Object getIfExists(Object key) throws StorageException 132 { 133 Iterator iter = storage.getResultSetIterator( 134 sqlFind,new Object []{key},getValueType()); 135 if (!iter.hasNext()) { 136 return null; 137 } 138 return iter.next(); 139 } 140 141 public Object getObjectIfExists(Object key, SinglevaluedIndex repos) 143 throws StorageException 144 { 145 Object val = getIfExists(key); 146 if (val == null) { 147 return null; 148 } else { 149 if (keyType == Storage.EntryType.MOFID) { 150 return repos.get(val); 151 } else { 152 return val; 153 } 154 } 155 } 156 157 public Collection values() 159 throws StorageException 160 { 161 return new JdbcCollection( 162 storage, 163 getValueType(), 164 sqlValuesIterator,sqlValuesSize,sqlValuesContains); 165 } 166 167 public Collection queryByKeyPrefix( 169 Object prefix, SinglevaluedIndex repos) throws StorageException 170 { 171 throw new RuntimeException ("oops, not yet implemented"); 173 } 174 } 175 176 | Popular Tags |