Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.JDBC.Statement;
  2.  
  3. import eg.edu.alexu.csd.oop.DBMSAdaptar;
  4. import eg.edu.alexu.csd.oop.DBMSAdapterPool;
  5. import eg.edu.alexu.csd.oop.JDBC.Connection.DBConnectionPool;
  6.  
  7. import java.sql.BatchUpdateException;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.LinkedList;
  12. import java.util.Queue;
  13. import java.util.concurrent.*;
  14.  
  15. public class DBStatementAdapter extends DBStatement{
  16. private Queue<String> SQLQueries = new LinkedList<>();
  17. private int timeOut = 0 ;
  18. private DBMSAdaptar adapter;
  19. private ResultSet resultSet;
  20. private String path;
  21. private int updateCount;
  22. private ExecutorService excutor ;
  23. public DBStatementAdapter(String path) {
  24. this.adapter= DBMSAdapterPool.getinstance();
  25. this.path = path;
  26. excutor = Executors.newSingleThreadExecutor();
  27. }
  28.  
  29.  
  30. public void addBatch(String sql) throws SQLException {
  31. this.SQLQueries.add(sql);
  32. }
  33.  
  34. public void clearBatch() throws SQLException {
  35. this.SQLQueries.clear();
  36. }
  37.  
  38. public void close() throws SQLException {
  39. clearBatch();
  40. adapter = null ;
  41. DBStatementPool.removeInstance();
  42. }
  43.  
  44. public int[] executeBatch() throws BatchUpdateException {
  45.  
  46. int[] arr = new int[SQLQueries.size()];
  47.  
  48. for(int i = 0 ; i < arr.length ; i++) {
  49.  
  50. String sql = SQLQueries.poll();
  51. Check check = new Check(sql);
  52. String checker = check.Checker();
  53. if (checker.equalsIgnoreCase("C")) {
  54.  
  55. try {
  56. execute(sql);
  57. arr[i] = 0 ;
  58. } catch (SQLException e) {
  59. throw new BatchUpdateException();
  60. }
  61. }
  62. else if (checker.equalsIgnoreCase("U")){
  63.  
  64. try {
  65. int z = executeUpdate(sql) ;
  66. arr[i] =z ;
  67. } catch (SQLException e) {
  68. throw new BatchUpdateException() ;
  69. }
  70. }
  71. else{
  72. throw new BatchUpdateException() ;
  73. }
  74. }
  75. return arr ;
  76. }
  77.  
  78.  
  79. public Connection getConnection() throws SQLException {
  80. return DBConnectionPool.getConnection(path);
  81. }
  82.  
  83. public int getQueryTimeout() throws SQLException {
  84. return this.timeOut ;
  85. }
  86.  
  87. public void setQueryTimeout(int seconds) throws SQLException {
  88. this.timeOut = seconds ;
  89. }
  90. public boolean execute(String sql) throws SQLException {// excute and if done return true
  91. boolean returned;
  92. Future<Boolean> future = excutor.submit(new Task(sql,adapter));
  93. if (getQueryTimeout() != 0) {
  94. try {
  95. returned = future.get(getQueryTimeout(), TimeUnit.SECONDS);
  96. return returned;
  97.  
  98. } catch (InterruptedException e) {
  99. e.printStackTrace();
  100. throw new SQLException();
  101.  
  102. } catch (ExecutionException e) {
  103. e.printStackTrace();
  104. throw new SQLException();
  105.  
  106. } catch (TimeoutException e) {
  107. e.printStackTrace();
  108. throw new SQLException();
  109.  
  110. }
  111. } else {
  112. try {
  113. return future.get() ;
  114.  
  115.  
  116. } catch (InterruptedException e) {
  117. e.printStackTrace();
  118. throw new SQLException();
  119. } catch (ExecutionException e) {
  120. e.printStackTrace();
  121. throw new SQLException();
  122. }
  123. }
  124. }
  125. public ResultSet executeQuery(String sql) throws SQLException {
  126. Future <Boolean> future = excutor.submit(new Task(sql,adapter));
  127. if(getQueryTimeout() != 0 ) {
  128. try {
  129. future.get(getQueryTimeout(), TimeUnit.SECONDS);
  130. return resultSet ;
  131.  
  132. } catch (InterruptedException e) {
  133. e.printStackTrace();
  134. throw new SQLException();
  135.  
  136. } catch (ExecutionException e) {
  137. e.printStackTrace();
  138. throw new SQLException();
  139.  
  140. } catch (TimeoutException e) {
  141. e.printStackTrace();
  142. throw new SQLException();
  143.  
  144. }
  145. }else {
  146. try {
  147. future.get();
  148. return resultSet ;
  149.  
  150. } catch (InterruptedException e) {
  151. e.printStackTrace();
  152. throw new SQLException();
  153.  
  154. } catch (ExecutionException e) {
  155. e.printStackTrace();
  156. throw new SQLException();
  157.  
  158. }
  159. }
  160. }
  161.  
  162.  
  163. public int executeUpdate(String sql) throws SQLException {
  164. Future <Boolean> future = excutor.submit(new Task(sql,adapter));
  165. if(getQueryTimeout() != 0 ) {
  166. try {
  167. future.get(getQueryTimeout(), TimeUnit.SECONDS);
  168. return updateCount ;
  169.  
  170. } catch (InterruptedException e) {
  171. e.printStackTrace();
  172. throw new SQLException();
  173.  
  174. } catch (ExecutionException e) {
  175. e.printStackTrace();
  176. throw new SQLException();
  177.  
  178. } catch (TimeoutException e) {
  179. e.printStackTrace();
  180. throw new SQLException();
  181.  
  182. }
  183. }else {
  184. try {
  185. future.get();
  186. return updateCount ;
  187.  
  188. } catch (InterruptedException e) {
  189. e.printStackTrace();
  190. throw new SQLException();
  191.  
  192. } catch (ExecutionException e) {
  193. e.printStackTrace();
  194. throw new SQLException();
  195.  
  196. }
  197. }
  198. }
  199.  
  200. class Task implements Callable<Boolean>{
  201. String query;
  202. DBMSAdaptar adapter ;
  203. public Task (String query , DBMSAdaptar adapter){
  204. this.query=query;
  205. this.adapter = adapter ;
  206. }
  207. @Override
  208. public Boolean call() throws Exception {
  209. Check check = new Check(query);
  210. String checker = check.Checker();
  211. if(checker.equals("C")){
  212. return adapter.createDatabase(query);
  213. }
  214. else if (checker.equalsIgnoreCase("U")) {
  215. updateCount=adapter.update(query) ;
  216. return true ;
  217. }
  218. else if (checker.equalsIgnoreCase("S")){
  219. resultSet =adapter.select(query) ;
  220. if(resultSet.first()) {
  221. resultSet.beforeFirst();
  222. return true;
  223. }else {
  224. return false;
  225. }
  226. }
  227. else{
  228. throw new SQLException() ;
  229. }
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement