KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 abstract class JdbcIndex implements Index
34 {
35     protected JdbcStorage storage;
36     protected String JavaDoc tableName;
37     protected String JavaDoc name;
38     protected String JavaDoc keyColName;
39     protected String JavaDoc 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 JavaDoc tableName,
54         String JavaDoc name,
55         String JavaDoc keyColName,
56         String JavaDoc 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     // implement Index
113
public String JavaDoc getName() throws StorageException
114     {
115         return name;
116     }
117
118     // implement Index
119
public Storage.EntryType getValueType() throws StorageException
120     {
121         return valueType;
122     }
123
124     // implement Index
125
public Storage.EntryType getKeyType() throws StorageException
126     {
127         return keyType;
128     }
129
130     // implement Index
131
public Set keySet() throws StorageException
132     {
133         return new JdbcSet(
134             storage,
135             getKeyType(),
136             sqlKeySetIterator,sqlKeySetSize,sqlKeySetContains);
137     }
138
139     // implement Index
140
public void add(Object JavaDoc key, Object JavaDoc value) throws StorageException
141     {
142         addImpl(key,value);
143     }
144
145     protected void addImpl(Object JavaDoc key, Object JavaDoc value) throws StorageException
146     {
147         Object JavaDoc [] args;
148         if (needSurrogate) {
149             args = new Object JavaDoc[]{key,value,new Long JavaDoc(storage.getSerialNumber())};
150         } else {
151             args = new Object JavaDoc[]{key,value};
152         }
153         storage.executeUpdate(sqlInsert,args);
154     }
155     
156     // implement Index
157
public boolean remove(Object JavaDoc key) throws StorageException
158     {
159         return removeImpl(key);
160     }
161
162     protected boolean removeImpl(Object JavaDoc key) throws StorageException
163     {
164         return storage.executeUpdate(sqlDelete,new Object JavaDoc[]{key}) > 0;
165     }
166 }
167
168 // End JdbcIndex.java
169
Popular Tags