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 33 abstract class JdbcIndex implements Index 34 { 35 protected JdbcStorage storage; 36 protected String tableName; 37 protected String name; 38 protected String keyColName; 39 protected String valColName; 40 protected Storage.EntryType keyType; 41 protected Storage.EntryType valueType; 42 protected boolean needSurrogate; 43 44 protected LazyPreparedStatement sqlKeySetIterator; 45 protected LazyPreparedStatement sqlKeySetSize; 46 protected LazyPreparedStatement sqlKeySetContains; 47 protected LazyPreparedStatement sqlInsert; 48 protected LazyPreparedStatement sqlDelete; 49 protected LazyPreparedStatement sqlFind; 50 51 void init( 52 JdbcStorage storage, 53 String tableName, 54 String name, 55 String keyColName, 56 String valColName, 57 Storage.EntryType keyType, 58 Storage.EntryType valueType, 59 boolean needSurrogate) 60 { 61 this.storage = storage; 62 this.tableName = tableName; 63 this.name = name; 64 this.keyColName = keyColName; 65 this.valColName = valColName; 66 this.keyType = keyType; 67 this.valueType = valueType; 68 this.needSurrogate = needSurrogate; 69 defineSql(); 70 } 71 72 protected void defineSql() 73 { 74 if (isKeyUnique()) { 75 sqlKeySetIterator = new LazyPreparedStatement( 76 "select " + keyColName + " from " + tableName); 77 sqlKeySetSize = new LazyPreparedStatement( 78 "select count(*) from " + tableName); 79 } else { 80 sqlKeySetIterator = new LazyPreparedStatement( 81 "select distinct " + keyColName 82 + " from " + tableName); 83 sqlKeySetSize = new LazyPreparedStatement( 84 "select count(distinct " + keyColName 85 + ") from " + tableName); 86 } 87 88 sqlKeySetContains = new LazyPreparedStatement( 89 "select count(*) from " + tableName + " where " 90 + keyColName + " = ?"); 91 92 sqlInsert = new LazyPreparedStatement( 93 "insert into " + tableName 94 + " values(?,?" 95 + (needSurrogate ? ",?" : "") 96 + ")"); 97 98 sqlDelete = new LazyPreparedStatement( 99 "delete from " + tableName 100 + " where " + keyColName + " = ?"); 101 102 sqlFind = new LazyPreparedStatement( 103 "select " + valColName + " from " + tableName 104 + " where " + keyColName + " = ?"); 105 } 106 107 protected boolean isKeyUnique() 108 { 109 return false; 110 } 111 112 public String getName() throws StorageException 114 { 115 return name; 116 } 117 118 public Storage.EntryType getValueType() throws StorageException 120 { 121 return valueType; 122 } 123 124 public Storage.EntryType getKeyType() throws StorageException 126 { 127 return keyType; 128 } 129 130 public Set keySet() throws StorageException 132 { 133 return new JdbcSet( 134 storage, 135 getKeyType(), 136 sqlKeySetIterator,sqlKeySetSize,sqlKeySetContains); 137 } 138 139 public void add(Object key, Object value) throws StorageException 141 { 142 addImpl(key,value); 143 } 144 145 protected void addImpl(Object key, Object value) throws StorageException 146 { 147 Object [] args; 148 if (needSurrogate) { 149 args = new Object []{key,value,new Long (storage.getSerialNumber())}; 150 } else { 151 args = new Object []{key,value}; 152 } 153 storage.executeUpdate(sqlInsert,args); 154 } 155 156 public boolean remove(Object key) throws StorageException 158 { 159 return removeImpl(key); 160 } 161 162 protected boolean removeImpl(Object key) throws StorageException 163 { 164 return storage.executeUpdate(sqlDelete,new Object []{key}) > 0; 165 } 166 } 167 168 | Popular Tags |