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.

DataGenerator.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.vaadin.data.util.sqlcontainer;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import org.junit.Assert;
  7. import com.vaadin.data.util.sqlcontainer.SQLTestsConstants.DB;
  8. import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
  9. public class DataGenerator {
  10. public static void addPeopleToDatabase(JDBCConnectionPool connectionPool)
  11. throws SQLException {
  12. Connection conn = connectionPool.reserveConnection();
  13. Statement statement = conn.createStatement();
  14. try {
  15. statement.execute("drop table PEOPLE");
  16. if (SQLTestsConstants.db == DB.ORACLE) {
  17. statement.execute("drop sequence people_seq");
  18. }
  19. } catch (SQLException e) {
  20. // Will fail if table doesn't exist, which is OK.
  21. conn.rollback();
  22. }
  23. statement.execute(SQLTestsConstants.peopleFirst);
  24. if (SQLTestsConstants.peopleSecond != null) {
  25. statement.execute(SQLTestsConstants.peopleSecond);
  26. }
  27. if (SQLTestsConstants.db == DB.ORACLE) {
  28. statement.execute(SQLTestsConstants.peopleThird);
  29. }
  30. if (SQLTestsConstants.db == DB.MSSQL) {
  31. statement.executeUpdate("insert into people values('Ville', '23')");
  32. statement.executeUpdate("insert into people values('Kalle', '7')");
  33. statement.executeUpdate("insert into people values('Pelle', '18')");
  34. statement.executeUpdate("insert into people values('Börje', '64')");
  35. } else {
  36. statement.executeUpdate(
  37. "insert into people values(default, 'Ville', '23')");
  38. statement.executeUpdate(
  39. "insert into people values(default, 'Kalle', '7')");
  40. statement.executeUpdate(
  41. "insert into people values(default, 'Pelle', '18')");
  42. statement.executeUpdate(
  43. "insert into people values(default, 'Börje', '64')");
  44. }
  45. statement.close();
  46. statement = conn.createStatement();
  47. ResultSet rs = statement.executeQuery("select * from PEOPLE");
  48. Assert.assertTrue(rs.next());
  49. statement.close();
  50. conn.commit();
  51. connectionPool.releaseConnection(conn);
  52. }
  53. public static void addFiveThousandPeople(JDBCConnectionPool connectionPool)
  54. throws SQLException {
  55. Connection conn = connectionPool.reserveConnection();
  56. Statement statement = conn.createStatement();
  57. for (int i = 4; i < 5000; i++) {
  58. if (SQLTestsConstants.db == DB.MSSQL) {
  59. statement.executeUpdate("insert into people values('Person " + i
  60. + "', '" + i % 99 + "')");
  61. } else {
  62. statement.executeUpdate(
  63. "insert into people values(default, 'Person " + i
  64. + "', '" + i % 99 + "')");
  65. }
  66. }
  67. statement.close();
  68. conn.commit();
  69. connectionPool.releaseConnection(conn);
  70. }
  71. public static void addVersionedData(JDBCConnectionPool connectionPool)
  72. throws SQLException {
  73. Connection conn = connectionPool.reserveConnection();
  74. Statement statement = conn.createStatement();
  75. try {
  76. statement.execute("DROP TABLE VERSIONED");
  77. if (SQLTestsConstants.db == DB.ORACLE) {
  78. statement.execute("drop sequence versioned_seq");
  79. statement.execute("drop sequence versioned_version");
  80. }
  81. } catch (SQLException e) {
  82. // Will fail if table doesn't exist, which is OK.
  83. conn.rollback();
  84. }
  85. for (String stmtString : SQLTestsConstants.versionStatements) {
  86. statement.execute(stmtString);
  87. }
  88. if (SQLTestsConstants.db == DB.MSSQL) {
  89. statement.executeUpdate(
  90. "insert into VERSIONED values('Junk', default)");
  91. } else {
  92. statement.executeUpdate(
  93. "insert into VERSIONED values(default, 'Junk', default)");
  94. }
  95. statement.close();
  96. statement = conn.createStatement();
  97. ResultSet rs = statement.executeQuery("select * from VERSIONED");
  98. Assert.assertTrue(rs.next());
  99. statement.close();
  100. conn.commit();
  101. connectionPool.releaseConnection(conn);
  102. }
  103. public static void createGarbage(JDBCConnectionPool connectionPool)
  104. throws SQLException {
  105. Connection conn = connectionPool.reserveConnection();
  106. Statement statement = conn.createStatement();
  107. try {
  108. statement.execute("drop table GARBAGE");
  109. if (SQLTestsConstants.db == DB.ORACLE) {
  110. statement.execute("drop sequence garbage_seq");
  111. }
  112. } catch (SQLException e) {
  113. // Will fail if table doesn't exist, which is OK.
  114. conn.rollback();
  115. }
  116. statement.execute(SQLTestsConstants.createGarbage);
  117. if (SQLTestsConstants.db == DB.ORACLE) {
  118. statement.execute(SQLTestsConstants.createGarbageSecond);
  119. statement.execute(SQLTestsConstants.createGarbageThird);
  120. }
  121. conn.commit();
  122. connectionPool.releaseConnection(conn);
  123. }
  124. }