From 3aa85cd588b745fb2537dbe748580d9c3b980133 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 8 Sep 2015 17:38:07 +0200 Subject: Compilable... --- .../org/openslx/imagemaster/db/MysqlStatement.java | 190 +++++++++++++-------- 1 file changed, 116 insertions(+), 74 deletions(-) (limited to 'src/main/java/org/openslx/imagemaster/db/MysqlStatement.java') diff --git a/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java b/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java index 391aed0..3dda36a 100644 --- a/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java +++ b/src/main/java/org/openslx/imagemaster/db/MysqlStatement.java @@ -18,7 +18,8 @@ import java.util.Map; * "http://www.javaworld.com/article/2077706/core-java/named-parameters-for-preparedstatement.html?page=2" * >Named Parameters for PreparedStatement */ -public class MysqlStatement implements Closeable { +public class MysqlStatement implements Closeable +{ private static final QueryCache cache = new QueryCache(); @@ -28,19 +29,24 @@ public class MysqlStatement implements Closeable { private final List openResultSets = new ArrayList<>(); - MysqlStatement(Connection con, String sql) throws SQLException { + MysqlStatement( Connection con, String sql, boolean getKeys ) throws SQLException + { PreparsedQuery query; - synchronized (cache) { - query = cache.get(sql); + synchronized ( cache ) { + query = cache.get( sql ); } - if (query == null) { - query = parse(sql); - synchronized (cache) { - cache.put(sql, query); + if ( query == null ) { + query = parse( sql ); + synchronized ( cache ) { + cache.put( sql, query ); } } this.query = query; - this.statement = con.prepareStatement(query.sql); + if ( getKeys ) { + this.statement = con.prepareStatement( query.sql, Statement.RETURN_GENERATED_KEYS ); + } else { + this.statement = con.prepareStatement( query.sql ); + } } /** @@ -50,10 +56,11 @@ public class MysqlStatement implements Closeable { * @return parameter indexes * @throws IllegalArgumentException if the parameter does not exist */ - private List getIndexes(String name) { - List indexes = query.indexMap.get(name); - if (indexes == null) { - throw new IllegalArgumentException("Parameter not found: " + name); + private List getIndexes( String name ) + { + List indexes = query.indexMap.get( name ); + if ( indexes == null ) { + throw new IllegalArgumentException( "Parameter not found: " + name ); } return indexes; } @@ -67,10 +74,11 @@ public class MysqlStatement implements Closeable { * @throws IllegalArgumentException if the parameter does not exist * @see PreparedStatement#setObject(int, java.lang.Object) */ - public void setObject(String name, Object value) throws SQLException { - List indexes = getIndexes(name); - for (Integer index : indexes) { - statement.setObject(index, value); + public void setObject( String name, Object value ) throws SQLException + { + List indexes = getIndexes( name ); + for ( Integer index : indexes ) { + statement.setObject( index, value ); } } @@ -83,10 +91,11 @@ public class MysqlStatement implements Closeable { * @throws IllegalArgumentException if the parameter does not exist * @see PreparedStatement#setString(int, java.lang.String) */ - public void setString(String name, String value) throws SQLException { - List indexes = getIndexes(name); - for (Integer index : indexes) { - statement.setString(index, value); + public void setString( String name, String value ) throws SQLException + { + List indexes = getIndexes( name ); + for ( Integer index : indexes ) { + statement.setString( index, value ); } } @@ -99,10 +108,11 @@ public class MysqlStatement implements Closeable { * @throws IllegalArgumentException if the parameter does not exist * @see PreparedStatement#setInt(int, int) */ - public void setInt(String name, int value) throws SQLException { - List indexes = getIndexes(name); - for (Integer index : indexes) { - statement.setInt(index, value); + public void setInt( String name, int value ) throws SQLException + { + List indexes = getIndexes( name ); + for ( Integer index : indexes ) { + statement.setInt( index, value ); } } @@ -115,10 +125,11 @@ public class MysqlStatement implements Closeable { * @throws IllegalArgumentException if the parameter does not exist * @see PreparedStatement#setLong(int, long) */ - public void setLong(String name, long value) throws SQLException { - List indexes = getIndexes(name); - for (Integer index : indexes) { - statement.setLong(index, value); + public void setLong( String name, long value ) throws SQLException + { + List indexes = getIndexes( name ); + for ( Integer index : indexes ) { + statement.setLong( index, value ); } } @@ -131,10 +142,11 @@ public class MysqlStatement implements Closeable { * @throws IllegalArgumentException if the parameter does not exist * @see PreparedStatement#setBoolean(int, boolean) */ - public void setBoolean(String name, boolean value) throws SQLException { - List indexes = getIndexes(name); - for (Integer index : indexes) { - statement.setBoolean(index, value); + public void setBoolean( String name, boolean value ) throws SQLException + { + List indexes = getIndexes( name ); + for ( Integer index : indexes ) { + statement.setBoolean( index, value ); } } @@ -147,10 +159,11 @@ public class MysqlStatement implements Closeable { * @throws IllegalArgumentException if the parameter does not exist * @see PreparedStatement#setBoolean(int, boolean) */ - public void setBinary(String name, byte[] value) throws SQLException { - List indexes = getIndexes(name); - for (Integer index : indexes) { - statement.setBytes(index, value); + public void setBinary( String name, byte[] value ) throws SQLException + { + List indexes = getIndexes( name ); + for ( Integer index : indexes ) { + statement.setBytes( index, value ); } } @@ -161,7 +174,8 @@ public class MysqlStatement implements Closeable { * @throws SQLException if an error occurred * @see PreparedStatement#execute() */ - public boolean execute() throws SQLException { + public boolean execute() throws SQLException + { return statement.execute(); } @@ -172,9 +186,10 @@ public class MysqlStatement implements Closeable { * @throws SQLException if an error occurred * @see PreparedStatement#executeQuery() */ - public ResultSet executeQuery() throws SQLException { + public ResultSet executeQuery() throws SQLException + { ResultSet rs = statement.executeQuery(); - openResultSets.add(rs); + openResultSets.add( rs ); return rs; } @@ -187,7 +202,8 @@ public class MysqlStatement implements Closeable { * @throws SQLException if an error occurred * @see PreparedStatement#executeUpdate() */ - public int executeUpdate() throws SQLException { + public int executeUpdate() throws SQLException + { return statement.executeUpdate(); } @@ -197,17 +213,18 @@ public class MysqlStatement implements Closeable { * @see Statement#close() */ @Override - public void close() { - for (ResultSet rs : openResultSets) { + public void close() + { + for ( ResultSet rs : openResultSets ) { try { rs.close(); - } catch (SQLException e) { + } catch ( SQLException e ) { // } } try { statement.close(); - } catch (SQLException e) { + } catch ( SQLException e ) { // Nothing to do } } @@ -217,7 +234,8 @@ public class MysqlStatement implements Closeable { * * @throws SQLException if something went wrong */ - public void addBatch() throws SQLException { + public void addBatch() throws SQLException + { statement.addBatch(); } @@ -229,91 +247,115 @@ public class MysqlStatement implements Closeable { * @return update counts for each statement * @throws SQLException if something went wrong */ - public int[] executeBatch() throws SQLException { + public int[] executeBatch() throws SQLException + { return statement.executeBatch(); } + /** + * Get the generated key from the last insert. Assumes that one row was inserted, and the + * generated key is an int. + * + * @return the generated key + * @throws SQLException if no key was generated by this statement + */ + public int getGeneratedKeys() throws SQLException + { + try ( ResultSet generatedKeys = statement.getGeneratedKeys() ) { + if ( generatedKeys.next() ) { + return generatedKeys.getInt( 1 ); + } + throw new SQLException( "Could not obtain generated key" ); + } + } + // static methods - private static PreparsedQuery parse(String query) { + private static PreparsedQuery parse( String query ) + { int length = query.length(); - StringBuffer parsedQuery = new StringBuffer(length); + StringBuffer parsedQuery = new StringBuffer( length ); Map> paramMap = new HashMap<>(); boolean inSingleQuote = false; boolean inDoubleQuote = false; boolean hasBackslash = false; int index = 1; - for (int i = 0; i < length; i++) { - char c = query.charAt(i); - if (hasBackslash) { + for ( int i = 0; i < length; i++ ) { + char c = query.charAt( i ); + if ( hasBackslash ) { // Last char was a backslash, so we ignore the current char hasBackslash = false; - } else if (c == '\\') { + } else if ( c == '\\' ) { // This is a backslash, next char will be escaped hasBackslash = true; - } else if (inSingleQuote) { + } else if ( inSingleQuote ) { // End of quoted string - if (c == '\'') { + if ( c == '\'' ) { inSingleQuote = false; } - } else if (inDoubleQuote) { + } else if ( inDoubleQuote ) { // End of quoted string - if (c == '"') { + if ( c == '"' ) { inDoubleQuote = false; } } else { // Not in string, look for named params - if (c == '\'') { + if ( c == '\'' ) { inSingleQuote = true; - } else if (c == '"') { + } else if ( c == '"' ) { inDoubleQuote = true; - } else if (c == ':' && i + 1 < length && Character.isJavaIdentifierStart(query.charAt(i + 1))) { + } else if ( c == ':' && i + 1 < length && Character.isJavaIdentifierStart( query.charAt( i + 1 ) ) ) { int j = i + 2; - while (j < length && Character.isJavaIdentifierPart(query.charAt(j))) { + while ( j < length && Character.isJavaIdentifierPart( query.charAt( j ) ) ) { j++; } - String name = query.substring(i + 1, j); + String name = query.substring( i + 1, j ); c = '?'; // replace the parameter with a question mark i += name.length(); // skip past the end of the parameter - List indexList = paramMap.get(name); - if (indexList == null) { + List indexList = paramMap.get( name ); + if ( indexList == null ) { indexList = new ArrayList<>(); - paramMap.put(name, indexList); + paramMap.put( name, indexList ); } - indexList.add(new Integer(index)); + indexList.add( new Integer( index ) ); index++; } } - parsedQuery.append(c); + parsedQuery.append( c ); } - return new PreparsedQuery(parsedQuery.toString(), paramMap); + return new PreparsedQuery( parsedQuery.toString(), paramMap ); } // private helper classes - private static class PreparsedQuery { + private static class PreparsedQuery + { private final Map> indexMap; private final String sql; - public PreparsedQuery(String sql, Map> indexMap) { + public PreparsedQuery( String sql, Map> indexMap ) + { this.sql = sql; this.indexMap = indexMap; } } - private static class QueryCache extends LinkedHashMap { + private static class QueryCache extends LinkedHashMap + { private static final long serialVersionUID = 1L; - public QueryCache() { - super(30, (float) 0.75, true); + public QueryCache() + { + super( 30, (float)0.75, true ); } @Override - protected boolean removeEldestEntry(Map.Entry eldest) { + protected boolean removeEldestEntry( Map.Entry eldest ) + { return size() > 40; } } -- cgit v1.2.3-55-g7522