KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > jdbcimpl > JdbcSinglevaluedIndex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
28  * JdbcSinglevaluedIndex implements the MDR SinglevaluedIndex interface
29  * using JDBC.
30  *
31  * @author John V. Sichi
32  * @version $Id: JdbcSinglevaluedIndex.java,v 1.2 2006/06/30 20:56:10 jtulach Exp $
33  */

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     // implement SinglevaluedIndex
67
public boolean put(Object JavaDoc key,Object JavaDoc value) throws StorageException
68     {
69         return putImpl(key,value);
70     }
71     
72     protected boolean putImpl(Object JavaDoc key,Object JavaDoc value) throws StorageException
73     {
74         // REVIEW: this optimizes for update rather than insert; could
75
// use UPSERT on databases that support it
76
int rowCount = storage.executeUpdate(
77             sqlUpdate,new Object JavaDoc[]{value,key});
78         // TODO: assert rowCount == 0 or 1
79
if (rowCount == 0) {
80             // key did not exist; insert instead
81
addImpl(key,value);
82             return false;
83         } else {
84             // key existed and has already been updated
85
return true;
86         }
87     }
88     
89     // implement SinglevaluedIndex
90
public void replace(Object JavaDoc key, Object JavaDoc value)
91         throws StorageException, StorageBadRequestException
92     {
93         replaceImpl(key,value);
94     }
95     
96     protected void replaceImpl(Object JavaDoc key, Object JavaDoc 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     // implement SinglevaluedIndex
106
public Object JavaDoc get(Object JavaDoc key)
107         throws StorageException, StorageBadRequestException
108     {
109         Object JavaDoc obj = getIfExists(key);
110         if (obj == null) {
111             throw new StorageBadRequestException ("Item not found: " + key);
112         }
113         return obj;
114     }
115
116     // implement SinglevaluedIndex
117
public Object JavaDoc getObject(Object JavaDoc key, SinglevaluedIndex repos)
118         throws StorageException
119     {
120         // NOTE: If JdbcPrimaryIndex did no caching, we could optimize this
121
// with an SQL join. But we can't join against the cache. Same goes
122
// for all of the other calls that take a repos argument.
123
if (keyType == Storage.EntryType.MOFID) {
124             return repos.get(get(key));
125         } else {
126             return get(key);
127         }
128     }
129
130     // implement SinglevaluedIndex
131
public Object JavaDoc getIfExists(Object JavaDoc key) throws StorageException
132     {
133         Iterator iter = storage.getResultSetIterator(
134             sqlFind,new Object JavaDoc[]{key},getValueType());
135         if (!iter.hasNext()) {
136             return null;
137         }
138         return iter.next();
139     }
140     
141     // implement SinglevaluedIndex
142
public Object JavaDoc getObjectIfExists(Object JavaDoc key, SinglevaluedIndex repos)
143         throws StorageException
144     {
145         Object JavaDoc 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     // implement SinglevaluedIndex
158
public Collection values()
159         throws StorageException
160     {
161         return new JdbcCollection(
162             storage,
163             getValueType(),
164             sqlValuesIterator,sqlValuesSize,sqlValuesContains);
165     }
166     
167     // implement SinglevaluedIndex
168
public Collection queryByKeyPrefix(
169         Object JavaDoc prefix, SinglevaluedIndex repos) throws StorageException
170     {
171         // TODO: optimize with LIKE
172
throw new RuntimeException JavaDoc("oops, not yet implemented");
173     }
174 }
175
176 // End JdbcSinglevaluedIndex.java
177
Popular Tags