KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > SQLServerRDBMSAdapter


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/SQLServerRDBMSAdapter.java,v 1.6 2004/07/28 09:34:16 ib Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/07/28 09:34:16 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2003 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.impl.rdbms;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 import org.apache.slide.common.Service;
32 import org.apache.slide.common.ServiceAccessException;
33 import org.apache.slide.macro.ConflictException;
34 import org.apache.slide.util.logger.Logger;
35
36 /**
37  * Adapter for MS SQLServer. Actually does not change a thing from StandardRDBMSAdapter.
38  *
39  * @version $Revision: 1.6 $
40  */

41
42 public class SQLServerRDBMSAdapter extends StandardRDBMSAdapter implements SequenceAdapter {
43
44     protected static final String JavaDoc LOG_CHANNEL = SQLServerRDBMSAdapter.class.getName();
45
46     protected static String JavaDoc normalizeSequenceName(String JavaDoc sequenceName) {
47         return sequenceName.replace('-', '_').toUpperCase() + "_SEQ";
48     }
49
50     public SQLServerRDBMSAdapter(Service service, Logger logger) {
51         super(service, logger);
52     }
53
54     protected ServiceAccessException createException(SQLException JavaDoc e, String JavaDoc uri) {
55
56         switch (e.getErrorCode()) {
57             case 1205 : // thread was deadlock victim
58
getLogger().log(e.getErrorCode() + ": Deadlock resolved on " + uri, LOG_CHANNEL, Logger.WARNING);
59                 return new ServiceAccessException(service, new ConflictException(uri));
60
61             case 547 : // referential integraty constaint was violated (like in storeObject on table URI )
62
case 2627 : // primary key constraint violation (like in storeContent on table VERSION_CONTENT)
63
getLogger().log(e.getErrorCode() + ": Low isolation conflict for " + uri, LOG_CHANNEL, Logger.WARNING);
64                 return new ServiceAccessException(service, new ConflictException(uri));
65
66             default :
67                 getLogger().log(
68                     "SQL error " + e.getErrorCode() + " on " + uri + ": " + e.getMessage(),
69                     LOG_CHANNEL,
70                     Logger.ERROR);
71
72                 return new ServiceAccessException(service, e);
73         }
74
75     }
76
77     public boolean isSequenceSupported(Connection JavaDoc conn) {
78         return true;
79     }
80
81     public boolean createSequence(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
82
83         // XXX DUMMY is a keyword in Sybase, so spell it wrong
84
String JavaDoc query =
85             "CREATE TABLE dbo."
86                 + normalizeSequenceName(sequenceName)
87                 + " (ID id_type IDENTITY UNIQUE NOT NULL, DUMY bit NOT NULL)";
88
89         PreparedStatement JavaDoc statement = null;
90
91         try {
92             statement = conn.prepareStatement(query);
93             statement.executeUpdate();
94             return true;
95         } catch (SQLException JavaDoc e) {
96             throw new ServiceAccessException(service, e);
97         } finally {
98             close(statement);
99         }
100
101     }
102
103     public long nextSequenceValue(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
104         String JavaDoc query = "INSERT INTO dbo." + normalizeSequenceName(sequenceName) + " (DUMY) VALUES(1)";
105
106         String JavaDoc selectQuery = "SELECT @@identity";
107
108         PreparedStatement JavaDoc statement = null;
109         PreparedStatement JavaDoc selectStatement = null;
110         ResultSet JavaDoc res = null;
111
112         try {
113             statement = conn.prepareStatement(query);
114             statement.executeUpdate();
115
116             selectStatement = conn.prepareStatement(selectQuery);
117             res = selectStatement.executeQuery();
118             if (!res.next()) {
119                 throw new ServiceAccessException(service, "Could not increment sequence " + sequenceName);
120             }
121             long value = res.getLong(1);
122             return value;
123         } catch (SQLException JavaDoc e) {
124             throw new ServiceAccessException(service, e);
125         } finally {
126             close(statement);
127             close(selectStatement, res);
128         }
129     }
130
131     public boolean sequenceExists(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
132         String JavaDoc selectQuery = getExistsQuery(sequenceName);
133
134         PreparedStatement JavaDoc selectStatement = null;
135         ResultSet JavaDoc res = null;
136
137         try {
138             selectStatement = conn.prepareStatement(selectQuery);
139             res = selectStatement.executeQuery();
140             return res.next();
141         } catch (SQLException JavaDoc e) {
142             throw new ServiceAccessException(service, e);
143         } finally {
144             close(selectStatement, res);
145         }
146     }
147
148     protected String JavaDoc getExistsQuery(String JavaDoc sequenceName) {
149         String JavaDoc selectQuery =
150             "SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].["
151                 + normalizeSequenceName(sequenceName)
152                 + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
153         return selectQuery;
154     }
155
156 }
157
Popular Tags