1 package org.openslx.imagemaster.db;
3 import java.io.Closeable;
4 import java.sql.Connection;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.SQLFeatureNotSupportedException;
9 import java.sql.Statement;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.LinkedHashMap;
13 import java.util.List;
36 synchronized (
cache ) {
37 query = cache.get( sql );
39 if ( query == null ) {
41 synchronized (
cache ) {
42 cache.put( sql, query );
46 this.statement = con.prepareStatement( query.
sql, Statement.RETURN_GENERATED_KEYS );
63 List<Integer> indexes = query.
indexMap.get( name );
64 if ( indexes == null ) {
65 throw new IllegalArgumentException(
"Parameter not found: " + name );
79 public void setObject( String name, Object value )
throws SQLException
82 for ( Integer index : indexes ) {
83 statement.setObject( index, value );
96 public void setString( String name, String value )
throws SQLException
99 for ( Integer index : indexes ) {
100 statement.setString( index, value );
113 public void setInt( String name,
int value )
throws SQLException
116 for ( Integer index : indexes ) {
117 statement.setInt( index, value );
130 public void setLong( String name,
long value )
throws SQLException
133 for ( Integer index : indexes ) {
134 statement.setLong( index, value );
147 public void setBoolean( String name,
boolean value )
throws SQLException
150 for ( Integer index : indexes ) {
151 statement.setBoolean( index, value );
164 public void setBinary( String name, byte[] value )
throws SQLException
167 for ( Integer index : indexes ) {
168 statement.setBytes( index, value );
181 return statement.execute();
193 ResultSet rs = statement.executeQuery();
194 openResultSets.add( rs );
209 return statement.executeUpdate();
231 ResultSet rs = statement.getGeneratedKeys();
232 openResultSets.add( rs );
239 try ( ResultSet rs = statement.getGeneratedKeys() ) {
241 result = rs.getInt( 1 );
255 for ( ResultSet rs : openResultSets ) {
258 }
catch ( SQLException e ) {
264 }
catch ( SQLException e ) {
269 }
catch ( SQLException e ) {
281 statement.addBatch();
294 return statement.executeBatch();
301 int length = query.length();
302 StringBuffer parsedQuery =
new StringBuffer( length );
303 Map<String, List<Integer>> paramMap =
new HashMap<>();
304 boolean inSingleQuote =
false;
305 boolean inDoubleQuote =
false;
306 boolean hasBackslash =
false;
309 for (
int i = 0; i < length; i++ ) {
310 char c = query.charAt( i );
311 if ( hasBackslash ) {
313 hasBackslash =
false;
314 }
else if ( c ==
'\\' ) {
317 }
else if ( inSingleQuote ) {
320 inSingleQuote =
false;
322 }
else if ( inDoubleQuote ) {
325 inDoubleQuote =
false;
330 inSingleQuote =
true;
331 }
else if ( c ==
'"' ) {
332 inDoubleQuote =
true;
333 }
else if ( c ==
':' && i + 1 < length && Character.isJavaIdentifierStart( query.charAt( i + 1 ) ) ) {
335 while ( j < length && Character.isJavaIdentifierPart( query.charAt( j ) ) ) {
338 String name = query.substring( i + 1, j );
342 List<Integer> indexList = paramMap.get( name );
343 if ( indexList == null ) {
344 indexList =
new ArrayList<>();
345 paramMap.put( name, indexList );
347 indexList.add( Integer.valueOf( index ) );
352 parsedQuery.append( c );
372 private static class QueryCache extends LinkedHashMap<String, PreparsedQuery>
378 super( 30, (
float)0.75,
true );
void close()
Closes the statement.
static final QueryCache cache
final List< ResultSet > openResultSets
final Map< String, List< Integer > > indexMap
boolean execute()
Executes the statement.
ResultSet getGeneratedKeys()
Retrieves any auto-generated keys created as a result of executing this Statement object...
ResultSet executeQuery()
Executes the statement, which must be a query.
boolean removeEldestEntry(Map.Entry< String, PreparsedQuery > eldest)
static final long serialVersionUID
int executeUpdate()
Executes the statement, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement ...
void addBatch()
Adds the current set of parameters as a batch entry.
void setInt(String name, int value)
Sets a parameter.
PreparsedQuery(String sql, Map< String, List< Integer >> indexMap)
void setBoolean(String name, boolean value)
Sets a parameter.
int[] executeBatch()
Executes all of the batched statements.
void setString(String name, String value)
Sets a parameter.
static PreparsedQuery parse(String query)
Class for creating PreparedStatements with named parameters.
void setBinary(String name, byte[] value)
Sets a parameter.
final PreparsedQuery query
void setObject(String name, Object value)
Sets a parameter.
List< Integer > getIndexes(String name)
Returns the indexes for a parameter.
void setLong(String name, long value)
Sets a parameter.
MysqlStatement(Connection con, String sql)
final PreparedStatement statement