You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.9 KiB

7 years ago
  1. package hsfulda.de;
  2. import java.util.Scanner;
  3. public class FedTestEnvironment {
  4. private FedConnection fedConnection;
  5. public FedTestEnvironment(FedConnection fedConnection) {
  6. this.fedConnection = fedConnection;
  7. try {
  8. this.fedConnection.setAutoCommit(false);
  9. } catch (FedException fedException) {
  10. System.err.println(fedException.getMessage());
  11. System.out.flush();
  12. System.err.flush();
  13. }
  14. }
  15. public void run(String filename) {
  16. run(filename, false);
  17. }
  18. public void run(String filename, boolean debug) {
  19. System.out.println("**************************************************************************");
  20. System.out.println("Executing script file '" + filename + "'...");
  21. long start, end, delta, op = 0;
  22. start = System.currentTimeMillis();
  23. Scanner scanner = new Scanner(getClass().getClassLoader().getResourceAsStream(filename)).useDelimiter(";");
  24. while (scanner.hasNext()) {
  25. String statement = scanner.next().trim();
  26. while (statement.startsWith("/*") || statement.startsWith("--")) {
  27. if (statement.startsWith("/*")) {
  28. String comment = statement.substring(0, statement.indexOf("*/") + 2);
  29. if (debug) {
  30. System.out.println("--> " + comment + " <--");
  31. }
  32. statement = statement.substring(statement.indexOf("*/") + 2).trim();
  33. } else {
  34. String comment = statement.substring(0, statement.indexOf("\n") - 1);
  35. if (debug) {
  36. System.out.println("--> " + comment + " <--");
  37. }
  38. statement = statement.substring(statement.indexOf("\n") + 1).trim();
  39. }
  40. }
  41. if (!"".equals(statement)) {
  42. if (!"SET ECHO ON".equals(statement.toUpperCase()) && !statement.toUpperCase().startsWith("ALTER SESSION")) {
  43. if (debug) {
  44. System.out.println("Executing \"" + statement + "\"...\n");
  45. System.out.flush();
  46. }
  47. if (statement.toUpperCase().equals("COMMIT")) {
  48. try {
  49. fedConnection.commit();
  50. if (debug) {
  51. System.out.println("Transaction commit");
  52. }
  53. } catch (FedException fedException) {
  54. System.out.println(fedException.getMessage());
  55. System.out.flush();
  56. }
  57. } else {
  58. if (statement.toUpperCase().equals("ROLLBACK")) {
  59. try {
  60. fedConnection.rollback();
  61. if (debug) {
  62. System.out.println("Transaction rollback");
  63. }
  64. } catch (FedException fedException) {
  65. System.out.println(fedException.getMessage());
  66. System.out.flush();
  67. }
  68. } else {
  69. if (statement.toUpperCase().startsWith("SELECT")) {
  70. // SELECT
  71. try {
  72. FedStatement fedStatement = fedConnection.getStatement();
  73. FedResultSet fedResultSet = fedStatement.executeQuery(statement);
  74. op++;
  75. if (debug) {
  76. for (int i = 1; i <= fedResultSet.getColumnCount(); i++) {
  77. System.out.printf("%-15s", fedResultSet.getColumnName(i));
  78. }
  79. System.out.println();
  80. for (int i = 1; i <= fedResultSet.getColumnCount(); i++) {
  81. System.out.print("-------------- ");
  82. }
  83. System.out.println();
  84. while (fedResultSet.next()) {
  85. for (int i = 1; i <= fedResultSet.getColumnCount(); i++) {
  86. System.out.printf("%-15s", fedResultSet.getString(i));
  87. }
  88. System.out.println();
  89. }
  90. System.out.println();
  91. }
  92. fedStatement.close();
  93. } catch (FedException fedException) {
  94. System.out.println(fedException.getMessage());
  95. System.out.flush();
  96. }
  97. } else {
  98. // UPDATE, INSERT, DELETE
  99. try {
  100. FedStatement fedStatement = fedConnection.getStatement();
  101. int count = fedStatement.executeUpdate(statement);
  102. op++;
  103. if (statement.toUpperCase().startsWith("UPDATE")) {
  104. if (debug) {
  105. System.out.println(count + " rows updated\n");
  106. }
  107. } else {
  108. if (statement.toUpperCase().startsWith("INSERT")) {
  109. if (debug) {
  110. System.out.println(count + " rows inserted\n");
  111. }
  112. } else {
  113. if (statement.toUpperCase().startsWith("DELETE")) {
  114. if (debug) {
  115. System.out.println(count + " rows deleted\n");
  116. }
  117. } else {
  118. if (debug) {
  119. System.out.println(count + " OK");
  120. }
  121. }
  122. }
  123. }
  124. fedStatement.close();
  125. } catch (FedException fedException) {
  126. System.out.println(fedException.getMessage());
  127. System.out.flush();
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. end = System.currentTimeMillis();
  136. delta = end - start;
  137. System.out.println("File '" + filename + "', " + op + " operations, " + delta + " milliseconds");
  138. System.out.println("**************************************************************************\n");
  139. }
  140. }