1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 31 import org.apache.slide.common.*; 32 import org.apache.slide.macro.ConflictException; 33 import org.apache.slide.util.logger.Logger; 34 35 40 41 public class MySqlRDBMSAdapter extends StandardRDBMSAdapter implements SequenceAdapter { 42 43 protected static final String LOG_CHANNEL = MySqlRDBMSAdapter.class.getName(); 44 45 protected static String normalizeSequenceName(String 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 e, String uri) { 54 55 switch (e.getErrorCode()) { 56 case 1213 : 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 convertRevisionNumberToComparable(String 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 conn) { 80 return true; 81 } 82 83 public boolean createSequence(Connection conn, String sequenceName) throws ServiceAccessException { 84 85 String query = "CREATE TABLE " + normalizeSequenceName(sequenceName) + "(id INT auto_increment NOT NULL,PRIMARY KEY (id))"; 86 87 PreparedStatement statement = null; 88 89 try { 90 statement = conn.prepareStatement(query); 91 statement.executeUpdate(); 92 return true; 93 } catch (SQLException e) { 94 throw new ServiceAccessException(service, e); 95 } finally { 96 close(statement); 97 } 98 99 } 100 101 public long nextSequenceValue(Connection conn, String sequenceName) throws ServiceAccessException { 102 String query = "INSERT INTO " + normalizeSequenceName(sequenceName) + " VALUES(0)"; 103 104 String selectQuery = "SELECT LAST_INSERT_ID()"; 105 106 PreparedStatement statement = null; 107 PreparedStatement selectStatement = null; 108 ResultSet 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 e) { 122 throw new ServiceAccessException(service, e); 123 } finally { 124 close(statement); 125 close(selectStatement, res); 126 } 127 } 128 129 public boolean sequenceExists(Connection conn, String sequenceName) throws ServiceAccessException { 130 131 PreparedStatement selectStatement = null; 132 ResultSet res = null; 133 134 try { 135 selectStatement = conn.prepareStatement("SELECT * FROM " + normalizeSequenceName(sequenceName)); 136 res = selectStatement.executeQuery(); 137 return true; 138 } catch (SQLException e) { 139 return false; 140 } finally { 141 close(selectStatement, res); 142 } 143 } 144 145 } | Popular Tags |