KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/MySqlRDBMSAdapter.java,v 1.7 2004/07/28 09:34:17 ib Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/07/28 09:34:17 $
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.*;
32 import org.apache.slide.macro.ConflictException;
33 import org.apache.slide.util.logger.Logger;
34
35 /**
36  * Adapter for MySQL 4.
37  *
38  * @version $Revision: 1.7 $
39  */

40
41 public class MySqlRDBMSAdapter extends StandardRDBMSAdapter implements SequenceAdapter {
42
43     protected static final String JavaDoc LOG_CHANNEL = MySqlRDBMSAdapter.class.getName();
44
45     protected static String JavaDoc normalizeSequenceName(String JavaDoc sequenceName) {
46         return sequenceName.replace('-', '_').toUpperCase() + "_SEQ";
47     }
48
49     public MySqlRDBMSAdapter(Service service, Logger logger) {
50         super(service, logger);
51     }
52
53     protected ServiceAccessException createException(SQLException JavaDoc e, String JavaDoc uri) {
54
55         switch (e.getErrorCode()) {
56             case 1213 : // thread was deadlock victim
57
getLogger().log(e.getErrorCode() + ": Deadlock resolved on " + uri, LOG_CHANNEL, Logger.WARNING);
58                 return new ServiceAccessException(service, new ConflictException(uri));
59
60             default :
61                 getLogger().log(
62                     "SQL error " + e.getErrorCode() + " on " + uri + ": " + e.getMessage(),
63                     LOG_CHANNEL,
64                     Logger.ERROR);
65
66                 return new ServiceAccessException(service, e);
67         }
68     }
69
70     protected String JavaDoc convertRevisionNumberToComparable(String JavaDoc revisioNumber) {
71
72         return "convert(SUBSTRING_INDEX("
73             + revisioNumber
74             + ", '.', 1), unsigned), convert(SUBSTRING_INDEX("
75             + revisioNumber
76             + ", '.', -1), unsigned)";
77     }
78
79     public boolean isSequenceSupported(Connection JavaDoc conn) {
80         return true;
81     }
82
83     public boolean createSequence(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
84
85         String JavaDoc query = "CREATE TABLE " + normalizeSequenceName(sequenceName) + "(id INT auto_increment NOT NULL,PRIMARY KEY (id))";
86
87         PreparedStatement JavaDoc statement = null;
88
89         try {
90             statement = conn.prepareStatement(query);
91             statement.executeUpdate();
92             return true;
93         } catch (SQLException JavaDoc e) {
94             throw new ServiceAccessException(service, e);
95         } finally {
96             close(statement);
97         }
98
99     }
100
101     public long nextSequenceValue(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
102         String JavaDoc query = "INSERT INTO " + normalizeSequenceName(sequenceName) + " VALUES(0)";
103
104         String JavaDoc selectQuery = "SELECT LAST_INSERT_ID()";
105
106         PreparedStatement JavaDoc statement = null;
107         PreparedStatement JavaDoc selectStatement = null;
108         ResultSet JavaDoc res = null;
109
110         try {
111             statement = conn.prepareStatement(query);
112             statement.executeUpdate();
113
114             selectStatement = conn.prepareStatement(selectQuery);
115             res = selectStatement.executeQuery();
116             if (!res.next()) {
117                 throw new ServiceAccessException(service, "Could not increment sequence " + sequenceName);
118             }
119             long value = res.getLong(1);
120             return value;
121         } catch (SQLException JavaDoc e) {
122             throw new ServiceAccessException(service, e);
123         } finally {
124             close(statement);
125             close(selectStatement, res);
126         }
127     }
128
129     public boolean sequenceExists(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
130
131         PreparedStatement JavaDoc selectStatement = null;
132         ResultSet JavaDoc res = null;
133
134         try {
135             selectStatement = conn.prepareStatement("SELECT * FROM " + normalizeSequenceName(sequenceName));
136             res = selectStatement.executeQuery();
137             return true;
138         } catch (SQLException JavaDoc e) {
139             return false;
140         } finally {
141             close(selectStatement, res);
142         }
143     }
144
145 }
Popular Tags