Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. import com.google.common.base.Function;
  2. import com.google.common.util.concurrent.*;
  3.  
  4. import javax.annotation.Nullable;
  5. import java.sql.*;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.atomic.AtomicReference;
  8. import java.util.function.Consumer;
  9.  
  10. /**
  11. * @author vRchr
  12. */
  13. public class SQLClass {
  14. private final String HOST;
  15. private final String NAME;
  16. private final String PASSWORD;
  17. private final String DATABASE;
  18. private final ListeningExecutorService THREAD_POOL = MoreExecutors
  19. .listeningDecorator( Executors.newCachedThreadPool() );
  20. private Connection connection;
  21.  
  22. public SQLClass( String host, String name, String password, String database ) {
  23. this.HOST = host;
  24. this.NAME = name;
  25. this.PASSWORD = password;
  26. this.DATABASE = database;
  27. }
  28.  
  29. public void connect( ) {
  30. try {
  31. //connect to database
  32. this.connection = DriverManager
  33. .getConnection( "jdbc:mysql://" + this.getHOST() + "/" + this.getDATABASE() +
  34. "?autoReconnect=true", this.getNAME(), this.getPASSWORD() );
  35. } catch ( SQLException e ) {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. public void disconnect( ) {
  41. try {
  42. this.getConnection().close();
  43. } catch ( SQLException e ) {
  44. e.printStackTrace();
  45. }
  46. }
  47.  
  48. /**
  49. * --CONSUMER--
  50. **/
  51.  
  52. public <T> void queryAsync( final String sql, Function<ResultSet, T> function, Consumer<T> consumer,
  53. final Object... parameters ) {
  54. Futures.addCallback( Futures.transform( this.queryAsync( sql, parameters ), new Function<Tuple<ResultSet,
  55. Runnable>, Tuple<T, Runnable>>() {
  56. @Nullable
  57. @Override
  58. public Tuple<T, Runnable> apply( @Nullable Tuple<ResultSet, Runnable> resultSetRunnableTuple ) {
  59. return Tuple.of(function.apply( resultSetRunnableTuple.getOne() ), resultSetRunnableTuple.getTwo());
  60. }
  61. } ),
  62. new FutureCallback<Tuple<T, Runnable>>() {
  63. @Override
  64. public void onSuccess( Tuple<T, Runnable> result ) {
  65. consumer.accept( result.getOne() );
  66. result.getTwo().run();
  67. }
  68.  
  69. @Override
  70. public void onFailure( Throwable t ) {
  71. consumer.accept( null );
  72. }
  73. } );
  74. }
  75.  
  76. public void updateAsync( final String sql, Consumer<Boolean> consumer, final Object... parameters ) {
  77. Futures.addCallback( this.updateAsync( sql, parameters ), new FutureCallback<Boolean>() {
  78. @Override
  79. public void onSuccess( Boolean result ) {
  80. consumer.accept( result );
  81. }
  82.  
  83. @Override
  84. public void onFailure( Throwable t ) {
  85. System.out.println( t.getMessage() );
  86. consumer.accept( null );
  87. }
  88. } );
  89. }
  90.  
  91.  
  92. /**
  93. * --LISTENING FUTURE--
  94. **/
  95.  
  96. private ListenableFuture<Tuple<ResultSet, Runnable>> queryAsync( final String sql, final Object... parameters ) {
  97. return this.getTHREAD_POOL().submit( ( ) -> query( sql, parameters ) );
  98. }
  99.  
  100. private ListenableFuture<Boolean> updateAsync( final String sql, final Object... parameters ) {
  101. return this.getTHREAD_POOL().submit( ( ) -> update( sql, parameters ) );
  102. }
  103.  
  104.  
  105. /**
  106. * --SYNC--
  107. **/
  108.  
  109. private Tuple<ResultSet, Runnable> query( String sql, Object... parameters ) {
  110. AtomicReference<PreparedStatement> preparedStatement = new AtomicReference<>( null );
  111. AtomicReference<ResultSet> resultSet = new AtomicReference<>( null );
  112. try {
  113. preparedStatement.set( this.getConnection().prepareStatement( sql ) );
  114.  
  115. int count = 0;
  116. for ( Object parameter : parameters ) {
  117. preparedStatement.get().setObject( count, parameter );
  118. count++;
  119. }
  120.  
  121. resultSet.set( preparedStatement.get().executeQuery() );
  122.  
  123. return Tuple.of( resultSet.get(), ( ) -> {
  124. try {
  125. resultSet.get().close();
  126. preparedStatement.get().close();
  127. } catch ( SQLException e ) {
  128. e.printStackTrace();
  129. }
  130. } );
  131. } catch ( SQLException e ) {
  132. e.printStackTrace();
  133. }
  134. return Tuple.of( null, () -> {} );
  135. }
  136.  
  137. private boolean update( String sql, Object... parameters ) {
  138. PreparedStatement preparedStatement = null;
  139. try {
  140. preparedStatement = this.getConnection().prepareStatement( sql );
  141.  
  142. int count = 0;
  143. for ( Object parameter : parameters ) {
  144. preparedStatement.setObject( count, parameter );
  145. count++;
  146. }
  147.  
  148. preparedStatement.executeUpdate();
  149.  
  150. return true;
  151. } catch ( SQLException e ) {
  152. e.printStackTrace();
  153. } finally {
  154. try {
  155. preparedStatement.close();
  156. } catch ( SQLException e ) {
  157. e.printStackTrace();
  158. }
  159. }
  160. return false;
  161. }
  162.  
  163. public String getHOST( ) {
  164. return HOST;
  165. }
  166.  
  167. public String getNAME( ) {
  168. return NAME;
  169. }
  170.  
  171. public String getPASSWORD( ) {
  172. return PASSWORD;
  173. }
  174.  
  175. public String getDATABASE( ) {
  176. return DATABASE;
  177. }
  178.  
  179. public ListeningExecutorService getTHREAD_POOL( ) {
  180. return THREAD_POOL;
  181. }
  182.  
  183. public Connection getConnection( ) {
  184. return connection;
  185. }
  186.  
  187. private static final class Tuple<O, T> {
  188. private O one;
  189. private T two;
  190.  
  191. public Tuple( O one, T two ) {
  192. this.one = one;
  193. this.two = two;
  194. }
  195.  
  196. public static <O, T> Tuple<O, T> of( O one, T two ) {
  197. return new Tuple<>( one, two );
  198. }
  199.  
  200. public O getOne( ) {
  201. return one;
  202. }
  203.  
  204. public void setOne( O one ) {
  205. this.one = one;
  206. }
  207.  
  208. public T getTwo( ) {
  209. return two;
  210. }
  211.  
  212. public void setTwo( T two ) {
  213. this.two = two;
  214. }
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement