@@ -24,8 +24,12 @@ public class DB {
2424 // ids are defined identically in each table
2525 static public String getCreateTableBegin (String schema , String tableName ) {
2626 StringBuilder sb = new StringBuilder ();
27-
28- sb .append ("CREATE TABLE " );
27+
28+ sb .append ("DROP TABLE IF EXISTS " );
29+ sb .append (DB .getFullTableName (schema , tableName ));
30+ sb .append ("; " );
31+
32+ sb .append ("CREATE TABLE IF NOT EXISTS " );
2933 sb .append (DB .getFullTableName (schema , tableName ));
3034 sb .append (" (" );
3135 sb .append (DB .getCreatePrimaryKeyDescription (schema , tableName ));
@@ -35,9 +39,10 @@ static public String getCreateTableBegin(String schema, String tableName) {
3539
3640 // primary key are created identically
3741 static public String getCreatePrimaryKeyDescription (String schema , String tableName ) {
38- return "id bigint PRIMARY KEY DEFAULT nextval('"
42+ return "id bigint PRIMARY KEY DEFAULT nextval('"
3943 + DB .getSequenceName (DB .getFullTableName (schema , tableName ))
4044 + "'::regclass)" ;
45+
4146 }
4247
4348 // primary key are created identically
@@ -56,7 +61,7 @@ static public void createSequence(Connection targetConnection, String schema, St
5661
5762 static public void createSequence (SQLStatementQueue targetQueue , String schema , String tableName ) throws SQLException {
5863
59- targetQueue .append ("CREATE SEQUENCE " );
64+ targetQueue .append ("CREATE SEQUENCE IF NOT EXISTS " );
6065 targetQueue .append (DB .getSequenceName (DB .getFullTableName (schema , tableName )));
6166 targetQueue .append (" INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1;" );
6267 targetQueue .forceExecute ();
@@ -78,7 +83,7 @@ static public void drop(SQLStatementQueue sq, String schema, String tableName) {
7883
7984 static public void drop (SQLStatementQueue sq , String fullTableName ) {
8085 try {
81- sq .append ("DROP SEQUENCE " );
86+ sq .append ("DROP SEQUENCE IF EXISTS " );
8287 sq .append (DB .getSequenceName (fullTableName ));
8388 sq .append (" CASCADE;" );
8489 sq .forceExecute ();
@@ -100,15 +105,20 @@ static public void drop(SQLStatementQueue sq, String fullTableName) {
100105 ////////////////////////////////////////////////////////////////////////
101106 // names //
102107 ////////////////////////////////////////////////////////////////////////
103-
104- static public String getSequenceName (String tableName ) {
105- return tableName + "_id " ;
108+
109+
110+ static public String getFullTableName_ (String schema , String tableName ) {
111+ return schema + ".\" " + tableName ;
112+ }static public String getSequenceName (String tableName ) {
113+ return tableName .substring (0 , tableName .length () - 1 ) + "_id\" " ;
106114 }
107115
108116 static public String getFullTableName (String schema , String tableName ) {
109- return schema + "." + tableName ;
117+ return schema + ".\" " + tableName + " \" " ;
110118 }
111119
120+
121+
112122 ////////////////////////////////////////////////////////////////////////
113123 // helper //
114124 ////////////////////////////////////////////////////////////////////////
@@ -187,7 +197,7 @@ public static SQLStatementQueue createSQLStatementQueue(Connection target, Param
187197 public static void writeTimeStamp (SQLStatementQueue sq , String targetSchema ) throws SQLException {
188198 DB .drop (sq , targetSchema , DB .TIME_STAMP_TABLE );
189199
190- sq .append ("CREATE TABLE " );
200+ sq .append ("CREATE TABLE IF NOT EXISTS " );
191201 sq .append (DB .getFullTableName (targetSchema , DB .TIME_STAMP_TABLE ));
192202 sq .append (" (" );
193203 sq .append ("timestampstring character varying" );
0 commit comments