Skip to content

Commit e4d1e5f

Browse files
committed
Rethinking QuotingStrategy to remove dependency on the ORM model...
... this allows to provide noop-strategy for the entire operation without if/else checking for every identifier
1 parent c12fd87 commit e4d1e5f

55 files changed

Lines changed: 1125 additions & 738 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/factory/DB2MergerTokenFactory.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public MergerToken createSetColumnTypeToDb(
3737
return new SetColumnTypeToDb(entity, columnOriginal, columnNew) {
3838

3939
@Override
40-
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy context) {
40+
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy quotes) {
4141
sqlBuffer.append("ALTER TABLE ");
42-
sqlBuffer.append(context.quotedFullyQualifiedName(entity));
42+
quotes.appendFQN(sqlBuffer, entity.getCatalog(), entity.getSchema(), entity.getName());
4343
sqlBuffer.append(" ALTER COLUMN ");
44-
sqlBuffer.append(context.quotedName(columnNew));
44+
quotes.appendStart(sqlBuffer);
45+
sqlBuffer.append(columnNew.getName());
46+
quotes.appendEnd(sqlBuffer);
4547
sqlBuffer.append(" SET DATA TYPE ");
4648
}
4749
};

cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/factory/DerbyMergerTokenFactory.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.cayenne.dbsync.merge.token.db.SetColumnTypeToDb;
2626
import org.apache.cayenne.dbsync.merge.token.db.SetGeneratedFlagToDb;
2727
import org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb;
28+
import org.apache.cayenne.map.DataMap;
2829
import org.apache.cayenne.map.DbAttribute;
2930
import org.apache.cayenne.map.DbEntity;
3031

@@ -42,12 +43,14 @@ public MergerToken createSetColumnTypeToDb(
4243
return new SetColumnTypeToDb(entity, columnOriginal, columnNew) {
4344

4445
@Override
45-
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy context) {
46+
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy quotes) {
4647
// http://db.apache.org/derby/manuals/reference/sqlj26.html
4748
sqlBuffer.append("ALTER TABLE ");
48-
sqlBuffer.append(context.quotedFullyQualifiedName(entity));
49+
quotes.appendFQN(sqlBuffer, entity.getCatalog(), entity.getSchema(), entity.getName());
4950
sqlBuffer.append(" ALTER ");
50-
sqlBuffer.append(context.quotedName(columnNew));
51+
quotes.appendStart(sqlBuffer);
52+
sqlBuffer.append(columnNew.getName());
53+
quotes.appendEnd(sqlBuffer);
5154
sqlBuffer.append(" SET DATA TYPE ");
5255
}
5356
};
@@ -59,10 +62,18 @@ public MergerToken createSetNotNullToDb(DbEntity entity, DbAttribute column) {
5962

6063
@Override
6164
public List<String> createSql(DbAdapter adapter) {
62-
QuotingStrategy context = adapter.getQuotingStrategy();
63-
64-
return Collections.singletonList("ALTER TABLE " + context.quotedFullyQualifiedName(getEntity())
65-
+ " ALTER COLUMN " + context.quotedName(getColumn()) + " NOT NULL");
65+
DataMap dataMap = getEntity().getDataMap();
66+
QuotingStrategy quotes = dataMap != null && dataMap.isQuotingSQLIdentifiers()
67+
? adapter.getQuotingStrategy() : QuotingStrategy.NONE;
68+
StringBuilder sql = new StringBuilder("ALTER TABLE ");
69+
quotes.appendFQN(sql, getEntity().getCatalog(), getEntity().getSchema(), getEntity().getName());
70+
sql.append(" ALTER COLUMN ");
71+
quotes.appendStart(sql);
72+
sql.append(getColumn().getName());
73+
quotes.appendEnd(sql);
74+
sql.append(" NOT NULL");
75+
76+
return Collections.singletonList(sql.toString());
6677
}
6778

6879
};
@@ -74,10 +85,18 @@ public MergerToken createSetAllowNullToDb(DbEntity entity, DbAttribute column) {
7485

7586
@Override
7687
public List<String> createSql(DbAdapter adapter) {
77-
QuotingStrategy context = adapter.getQuotingStrategy();
78-
79-
return Collections.singletonList("ALTER TABLE " + context.quotedFullyQualifiedName(getEntity())
80-
+ " ALTER COLUMN " + context.quotedName(getColumn()) + " NULL");
88+
DataMap dataMap = getEntity().getDataMap();
89+
QuotingStrategy quotes = dataMap != null && dataMap.isQuotingSQLIdentifiers()
90+
? adapter.getQuotingStrategy() : QuotingStrategy.NONE;
91+
StringBuilder sql = new StringBuilder("ALTER TABLE ");
92+
quotes.appendFQN(sql, getEntity().getCatalog(), getEntity().getSchema(), getEntity().getName());
93+
sql.append(" ALTER COLUMN ");
94+
quotes.appendStart(sql);
95+
sql.append(getColumn().getName());
96+
quotes.appendEnd(sql);
97+
sql.append(" NULL");
98+
99+
return Collections.singletonList(sql.toString());
81100
}
82101

83102
};

cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/factory/FirebirdMergerTokenFactory.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb;
2727
import org.apache.cayenne.dbsync.merge.token.db.SetAllowNullToDb;
2828
import org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb;
29+
import org.apache.cayenne.map.DataMap;
2930
import org.apache.cayenne.map.DbAttribute;
3031
import org.apache.cayenne.map.DbEntity;
3132

@@ -38,9 +39,16 @@ public class FirebirdMergerTokenFactory extends DefaultMergerTokenFactory {
3839
public MergerToken createDropColumnToDb(DbEntity entity, DbAttribute column) {
3940
return new DropColumnToDb(entity, column) {
4041
public List<String> createSql(DbAdapter adapter) {
41-
QuotingStrategy quoting = adapter.getQuotingStrategy();
42-
return Collections.singletonList("ALTER TABLE " + quoting.quotedFullyQualifiedName(getEntity())
43-
+ " DROP " + quoting.quotedName(getColumn()));
42+
DataMap dataMap = getEntity().getDataMap();
43+
QuotingStrategy quotes = dataMap != null && dataMap.isQuotingSQLIdentifiers()
44+
? adapter.getQuotingStrategy() : QuotingStrategy.NONE;
45+
StringBuilder sql = new StringBuilder("ALTER TABLE ");
46+
quotes.appendFQN(sql, getEntity().getCatalog(), getEntity().getSchema(), getEntity().getName());
47+
sql.append(" DROP ");
48+
quotes.appendStart(sql);
49+
sql.append(getColumn().getName());
50+
quotes.appendEnd(sql);
51+
return Collections.singletonList(sql.toString());
4452
}
4553
};
4654
}
@@ -49,12 +57,15 @@ public List<String> createSql(DbAdapter adapter) {
4957
public MergerToken createSetNotNullToDb(DbEntity entity, DbAttribute column) {
5058
return new SetNotNullToDb(entity, column) {
5159
public List<String> createSql(DbAdapter adapter) {
52-
QuotingStrategy context = adapter.getQuotingStrategy();
53-
String entityName = context.quotedFullyQualifiedName(getEntity()) ;
54-
String columnName = context.quotedName(getColumn());
60+
DataMap dataMap = getEntity().getDataMap();
61+
QuotingStrategy quotes = dataMap != null && dataMap.isQuotingSQLIdentifiers()
62+
? adapter.getQuotingStrategy() : QuotingStrategy.NONE;
63+
String entityName = quotes.quotedFQN(getEntity().getCatalog(), getEntity().getSchema(),
64+
getEntity().getName());
65+
String columnName = quotes.quoted(getColumn().getName());
5566
// Firebird doesn't support ALTER TABLE table_name ALTER column_name SET NOT NULL
56-
// but this might be achived by modyfication of system tables
57-
return Collections.singletonList(String.format("UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = 1 "+
67+
// but this might be achived by modyfication of system tables
68+
return Collections.singletonList(String.format("UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = 1 "+
5869
"WHERE RDB$FIELD_NAME = '%s' AND RDB$RELATION_NAME = '%s'", columnName, entityName));
5970
}
6071
};
@@ -64,11 +75,14 @@ public List<String> createSql(DbAdapter adapter) {
6475
public MergerToken createSetAllowNullToDb(DbEntity entity, DbAttribute column) {
6576
return new SetAllowNullToDb(entity, column) {
6677
public List<String> createSql(DbAdapter adapter) {
67-
QuotingStrategy context = adapter.getQuotingStrategy();
68-
String entityName = context.quotedFullyQualifiedName(getEntity()) ;
69-
String columnName = context.quotedName(getColumn());
78+
DataMap dataMap = getEntity().getDataMap();
79+
QuotingStrategy quotes = dataMap != null && dataMap.isQuotingSQLIdentifiers()
80+
? adapter.getQuotingStrategy() : QuotingStrategy.NONE;
81+
String entityName = quotes.quotedFQN(getEntity().getCatalog(), getEntity().getSchema(),
82+
getEntity().getName());
83+
String columnName = quotes.quoted(getColumn().getName());
7084
// Firebird doesn't support ALTER TABLE table_name ALTER column_name DROP NOT NULL
71-
// but this might be achived by modyfication system tables
85+
// but this might be achived by modyfication system tables
7286
return Collections.singletonList(String.format("UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = NULL "+
7387
" WHERE RDB$FIELD_NAME = '%s' AND RDB$RELATION_NAME = '%s'", columnName, entityName));
7488
}
@@ -78,11 +92,13 @@ public List<String> createSql(DbAdapter adapter) {
7892
@Override
7993
public MergerToken createAddColumnToDb(DbEntity entity, DbAttribute column) {
8094
return new AddColumnToDb(entity, column) {
81-
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy context) {
95+
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy quotes) {
8296
sqlBuffer.append("ALTER TABLE ");
83-
sqlBuffer.append(context.quotedFullyQualifiedName(getEntity()));
97+
quotes.appendFQN(sqlBuffer, getEntity().getCatalog(), getEntity().getSchema(), getEntity().getName());
8498
sqlBuffer.append(" ADD ");
85-
sqlBuffer.append(context.quotedName(getColumn()));
99+
quotes.appendStart(sqlBuffer);
100+
sqlBuffer.append(getColumn().getName());
101+
quotes.appendEnd(sqlBuffer);
86102
sqlBuffer.append(" ");
87103
}
88104
};

cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/factory/H2MergerTokenFactory.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ public MergerToken createSetColumnTypeToDb(final DbEntity entity, DbAttribute co
4444
return new SetColumnTypeToDb(entity, columnOriginal, columnNew) {
4545

4646
@Override
47-
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy context) {
47+
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy quotes) {
4848
sqlBuffer.append("ALTER TABLE ");
49-
sqlBuffer.append(context.quotedFullyQualifiedName(entity));
49+
quotes.appendFQN(sqlBuffer, entity.getCatalog(), entity.getSchema(), entity.getName());
5050
sqlBuffer.append(" ALTER ");
51-
sqlBuffer.append(context.quotedName(columnNew));
51+
quotes.appendStart(sqlBuffer);
52+
sqlBuffer.append(columnNew.getName());
53+
quotes.appendEnd(sqlBuffer);
5254
sqlBuffer.append(" ");
5355
}
5456
};
@@ -74,8 +76,11 @@ public MergerToken createSetPrimaryKeyToDb(DbEntity entity, Collection<DbAttribu
7476

7577
@Override
7678
protected void appendDropOriginalPrimaryKeySQL(DbAdapter adapter, List<String> sqls) {
77-
sqls.add("ALTER TABLE " + adapter.getQuotingStrategy().quotedFullyQualifiedName(getEntity())
78-
+ " DROP PRIMARY KEY");
79+
QuotingStrategy quotes = resolveQuotes(adapter);
80+
StringBuilder sql = new StringBuilder("ALTER TABLE ");
81+
quotes.appendFQN(sql, getEntity().getCatalog(), getEntity().getSchema(), getEntity().getName());
82+
sql.append(" DROP PRIMARY KEY");
83+
sqls.add(sql.toString());
7984
}
8085

8186
};

cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/factory/HSQLMergerTokenFactory.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.cayenne.dbsync.merge.token.db.SetColumnTypeToDb;
2626
import org.apache.cayenne.dbsync.merge.token.db.SetGeneratedFlagToDb;
2727
import org.apache.cayenne.dbsync.merge.token.db.SetPrimaryKeyToDb;
28+
import org.apache.cayenne.map.DataMap;
2829
import org.apache.cayenne.map.DbAttribute;
2930
import org.apache.cayenne.map.DbEntity;
3031

@@ -41,11 +42,13 @@ public MergerToken createSetColumnTypeToDb(final DbEntity entity, DbAttribute co
4142
return new SetColumnTypeToDb(entity, columnOriginal, columnNew) {
4243

4344
@Override
44-
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy context) {
45+
protected void appendPrefix(StringBuffer sqlBuffer, QuotingStrategy quotes) {
4546
sqlBuffer.append("ALTER TABLE ");
46-
sqlBuffer.append(context.quotedFullyQualifiedName(entity));
47+
quotes.appendFQN(sqlBuffer, entity.getCatalog(), entity.getSchema(), entity.getName());
4748
sqlBuffer.append(" ALTER ");
48-
sqlBuffer.append(context.quotedName(columnNew));
49+
quotes.appendStart(sqlBuffer);
50+
sqlBuffer.append(columnNew.getName());
51+
quotes.appendEnd(sqlBuffer);
4952
sqlBuffer.append(" ");
5053
}
5154
};
@@ -57,10 +60,18 @@ public MergerToken createSetAllowNullToDb(DbEntity entity, DbAttribute column) {
5760

5861
@Override
5962
public List<String> createSql(DbAdapter adapter) {
60-
QuotingStrategy context = adapter.getQuotingStrategy();
61-
62-
return Collections.singletonList("ALTER TABLE " + context.quotedFullyQualifiedName(getEntity())
63-
+ " ALTER COLUMN " + context.quotedName(getColumn()) + " SET NULL");
63+
DataMap dataMap = getEntity().getDataMap();
64+
QuotingStrategy quotes = dataMap != null && dataMap.isQuotingSQLIdentifiers()
65+
? adapter.getQuotingStrategy() : QuotingStrategy.NONE;
66+
StringBuilder sql = new StringBuilder("ALTER TABLE ");
67+
quotes.appendFQN(sql, getEntity().getCatalog(), getEntity().getSchema(), getEntity().getName());
68+
sql.append(" ALTER COLUMN ");
69+
quotes.appendStart(sql);
70+
sql.append(getColumn().getName());
71+
quotes.appendEnd(sql);
72+
sql.append(" SET NULL");
73+
74+
return Collections.singletonList(sql.toString());
6475
}
6576

6677
};
@@ -73,8 +84,11 @@ public MergerToken createSetPrimaryKeyToDb(DbEntity entity, Collection<DbAttribu
7384

7485
@Override
7586
protected void appendDropOriginalPrimaryKeySQL(DbAdapter adapter, List<String> sqls) {
76-
sqls.add("ALTER TABLE " + adapter.getQuotingStrategy().quotedFullyQualifiedName(getEntity())
77-
+ " DROP PRIMARY KEY");
87+
QuotingStrategy quotes = resolveQuotes(adapter);
88+
StringBuilder sql = new StringBuilder("ALTER TABLE ");
89+
quotes.appendFQN(sql, getEntity().getCatalog(), getEntity().getSchema(), getEntity().getName());
90+
sql.append(" DROP PRIMARY KEY");
91+
sqls.add(sql.toString());
7892
}
7993

8094
};

0 commit comments

Comments
 (0)