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.

SQLTestsConstants.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.vaadin.v7.data.util.sqlcontainer;
  2. import com.vaadin.v7.data.util.sqlcontainer.query.generator.DefaultSQLGenerator;
  3. import com.vaadin.v7.data.util.sqlcontainer.query.generator.MSSQLGenerator;
  4. import com.vaadin.v7.data.util.sqlcontainer.query.generator.OracleGenerator;
  5. import com.vaadin.v7.data.util.sqlcontainer.query.generator.SQLGenerator;
  6. public class SQLTestsConstants {
  7. /* Set the DB used for testing here! */
  8. public enum DB {
  9. HSQLDB, MYSQL, POSTGRESQL, MSSQL, ORACLE;
  10. }
  11. /* 0 = HSQLDB, 1 = MYSQL, 2 = POSTGRESQL, 3 = MSSQL, 4 = ORACLE */
  12. public static final DB db = DB.HSQLDB;
  13. /* Auto-increment column offset (HSQLDB = 0, MYSQL = 1, POSTGRES = 1) */
  14. public static int offset;
  15. /* Garbage table creation query (=three queries for oracle) */
  16. public static String createGarbage;
  17. public static String createGarbageSecond;
  18. public static String createGarbageThird;
  19. /* DB Drivers, urls, usernames and passwords */
  20. public static String dbDriver;
  21. public static String dbURL;
  22. public static String dbUser;
  23. public static String dbPwd;
  24. /* People -test table creation statement(s) */
  25. public static String peopleFirst;
  26. public static String peopleSecond;
  27. public static String peopleThird;
  28. /* Schema test creation statement(s) */
  29. public static String createSchema;
  30. public static String createProductTable;
  31. public static String dropSchema;
  32. /* Versioned -test table createion statement(s) */
  33. public static String[] versionStatements;
  34. /* SQL Generator used during the testing */
  35. public static SQLGenerator sqlGen;
  36. /* Set DB-specific settings based on selected DB */
  37. static {
  38. sqlGen = new DefaultSQLGenerator();
  39. switch (db) {
  40. case HSQLDB:
  41. offset = 0;
  42. createGarbage = "create table garbage (id integer generated always as identity, type varchar(32), PRIMARY KEY(id))";
  43. dbDriver = "org.hsqldb.jdbc.JDBCDriver";
  44. dbURL = "jdbc:hsqldb:mem:sqlcontainer";
  45. dbUser = "SA";
  46. dbPwd = "";
  47. peopleFirst = "create table people (id integer generated always as identity, name varchar(32), AGE INTEGER)";
  48. peopleSecond = "alter table people add primary key (id)";
  49. versionStatements = new String[] {
  50. "create table versioned (id integer generated always as identity, text varchar(255), version tinyint default 0)",
  51. "alter table versioned add primary key (id)" };
  52. // TODO these should ideally exist for all databases
  53. createSchema = "create schema oaas authorization DBA";
  54. createProductTable = "create table oaas.product (\"ID\" integer generated always as identity primary key, \"NAME\" VARCHAR(32))";
  55. dropSchema = "drop schema if exists oaas cascade";
  56. break;
  57. case MYSQL:
  58. offset = 1;
  59. createGarbage = "create table GARBAGE (ID integer auto_increment, type varchar(32), PRIMARY KEY(ID))";
  60. dbDriver = "com.mysql.jdbc.Driver";
  61. dbURL = "jdbc:mysql:///sqlcontainer";
  62. dbUser = "sqlcontainer";
  63. dbPwd = "sqlcontainer";
  64. peopleFirst = "create table PEOPLE (ID integer auto_increment not null, NAME varchar(32), AGE INTEGER, primary key(ID))";
  65. peopleSecond = null;
  66. versionStatements = new String[] {
  67. "create table VERSIONED (ID integer auto_increment not null, TEXT varchar(255), VERSION tinyint default 0, primary key(ID))",
  68. "CREATE TRIGGER upd_version BEFORE UPDATE ON VERSIONED"
  69. + " FOR EACH ROW SET NEW.VERSION = OLD.VERSION+1" };
  70. break;
  71. case POSTGRESQL:
  72. offset = 1;
  73. createGarbage = "create table GARBAGE (\"ID\" serial PRIMARY KEY, \"TYPE\" varchar(32))";
  74. dbDriver = "org.postgresql.Driver";
  75. dbURL = "jdbc:postgresql://localhost:5432/test";
  76. dbUser = "postgres";
  77. dbPwd = "postgres";
  78. peopleFirst = "create table PEOPLE (\"ID\" serial primary key, \"NAME\" VARCHAR(32), \"AGE\" INTEGER)";
  79. peopleSecond = null;
  80. versionStatements = new String[] {
  81. "create table VERSIONED (\"ID\" serial primary key, \"TEXT\" VARCHAR(255), \"VERSION\" INTEGER DEFAULT 0)",
  82. "CREATE OR REPLACE FUNCTION zz_row_version() RETURNS TRIGGER AS $$"
  83. + "BEGIN" + " IF TG_OP = 'UPDATE'"
  84. + " AND NEW.\"VERSION\" = old.\"VERSION\""
  85. + " AND ROW(NEW.*) IS DISTINCT FROM ROW (old.*)"
  86. + " THEN"
  87. + " NEW.\"VERSION\" := NEW.\"VERSION\" + 1;"
  88. + " END IF;" + " RETURN NEW;" + "END;"
  89. + "$$ LANGUAGE plpgsql;",
  90. "CREATE TRIGGER \"mytable_modify_dt_tr\" BEFORE UPDATE"
  91. + " ON VERSIONED FOR EACH ROW"
  92. + " EXECUTE PROCEDURE \"public\".\"zz_row_version\"();" };
  93. createSchema = "create schema oaas";
  94. createProductTable = "create table oaas.product (\"ID\" serial primary key, \"NAME\" VARCHAR(32))";
  95. dropSchema = "drop schema oaas cascade";
  96. break;
  97. case MSSQL:
  98. offset = 1;
  99. createGarbage = "create table GARBAGE (\"ID\" int identity(1,1) primary key, \"TYPE\" varchar(32))";
  100. dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  101. dbURL = "jdbc:sqlserver://localhost:1433;databaseName=tempdb;";
  102. dbUser = "sa";
  103. dbPwd = "sa";
  104. peopleFirst = "create table PEOPLE (\"ID\" int identity(1,1) primary key, \"NAME\" VARCHAR(32), \"AGE\" INTEGER)";
  105. peopleSecond = null;
  106. versionStatements = new String[] {
  107. "create table VERSIONED (\"ID\" int identity(1,1) primary key, \"TEXT\" VARCHAR(255), \"VERSION\" rowversion not null)" };
  108. sqlGen = new MSSQLGenerator();
  109. break;
  110. case ORACLE:
  111. offset = 1;
  112. createGarbage = "create table GARBAGE (\"ID\" integer primary key, \"TYPE\" varchar2(32))";
  113. createGarbageSecond = "create sequence garbage_seq start with 1 increment by 1 nomaxvalue";
  114. createGarbageThird = "create trigger garbage_trigger before insert on GARBAGE for each row begin select garbage_seq.nextval into :new.ID from dual; end;";
  115. dbDriver = "oracle.jdbc.OracleDriver";
  116. dbURL = "jdbc:oracle:thin:test/test@localhost:1521:XE";
  117. dbUser = "test";
  118. dbPwd = "test";
  119. peopleFirst = "create table PEOPLE (\"ID\" integer primary key, \"NAME\" VARCHAR2(32), \"AGE\" INTEGER)";
  120. peopleSecond = "create sequence people_seq start with 1 increment by 1 nomaxvalue";
  121. peopleThird = "create trigger people_trigger before insert on PEOPLE for each row begin select people_seq.nextval into :new.ID from dual; end;";
  122. versionStatements = new String[] {
  123. "create table VERSIONED (\"ID\" integer primary key, \"TEXT\" VARCHAR(255), \"VERSION\" INTEGER DEFAULT 0)",
  124. "create sequence versioned_seq start with 1 increment by 1 nomaxvalue",
  125. "create trigger versioned_trigger before insert on VERSIONED for each row begin select versioned_seq.nextval into :new.ID from dual; end;",
  126. "create sequence versioned_version start with 1 increment by 1 nomaxvalue",
  127. "create trigger versioned_version_trigger before insert or update on VERSIONED for each row begin select versioned_version.nextval into :new.VERSION from dual; end;" };
  128. sqlGen = new OracleGenerator();
  129. break;
  130. }
  131. }
  132. }