use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.
the class DefaultSelectTranslator method appendGroupByColumn.
/**
* Append single column to GROUP BY clause
* @since 4.0
*/
protected void appendGroupByColumn(StringBuilder buffer, Map.Entry<ColumnDescriptor, List<DbAttributeBinding>> entry) {
String fullName;
if (entry.getKey().isExpression()) {
fullName = entry.getKey().getDataRowKey();
} else {
QuotingStrategy strategy = getAdapter().getQuotingStrategy();
fullName = strategy.quotedIdentifier(queryMetadata.getDataMap(), entry.getKey().getNamePrefix(), entry.getKey().getName());
}
buffer.append(fullName);
if (entry.getKey().getDataRowKey().equals(entry.getKey().getName())) {
for (DbAttributeBinding binding : entry.getValue()) {
addToParamList(binding.getAttribute(), binding.getValue());
}
}
}
use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.
the class FrontBaseAdapter method createTable.
/**
* Customizes table creating procedure for FrontBase.
*/
@Override
public String createTable(DbEntity ent) {
QuotingStrategy context = getQuotingStrategy();
StringBuilder buf = new StringBuilder();
buf.append("CREATE TABLE ");
buf.append(context.quotedFullyQualifiedName(ent));
buf.append(" (");
// columns
Iterator<DbAttribute> it = ent.getAttributes().iterator();
boolean first = true;
while (it.hasNext()) {
if (first) {
first = false;
} else {
buf.append(", ");
}
DbAttribute at = it.next();
// attribute may not be fully valid, do a simple check
if (at.getType() == TypesMapping.NOT_DEFINED) {
throw new CayenneRuntimeException("Undefined type for attribute '%s.%s'.", ent.getFullyQualifiedName(), at.getName());
}
String[] types = externalTypesForJdbcType(at.getType());
if (types == null || types.length == 0) {
throw new CayenneRuntimeException("Undefined type for attribute '%s.%s': %s", ent.getFullyQualifiedName(), at.getName(), at.getType());
}
String type = types[0];
buf.append(context.quotedName(at)).append(' ').append(type);
// to be the limit for FB)
if (at.getType() == Types.LONGVARCHAR) {
int len = at.getMaxLength() > 0 ? at.getMaxLength() : 1073741824;
buf.append("(").append(len).append(")");
} else if (at.getType() == Types.VARBINARY || at.getType() == Types.BINARY) {
// use a BIT column with size * 8
int len = at.getMaxLength() > 0 ? at.getMaxLength() : 1073741824;
len *= 8;
buf.append("(").append(len).append(")");
} else if (typeSupportsLength(at.getType())) {
int len = at.getMaxLength();
int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1;
// sanity check
if (scale > len) {
scale = -1;
}
if (len > 0) {
buf.append('(').append(len);
if (scale >= 0) {
buf.append(", ").append(scale);
}
buf.append(')');
}
}
if (at.isMandatory()) {
buf.append(" NOT NULL");
}
// else: don't appen NULL for FrontBase:
}
// primary key clause
Iterator<DbAttribute> pkit = ent.getPrimaryKeys().iterator();
if (pkit.hasNext()) {
if (first) {
first = false;
} else {
buf.append(", ");
}
buf.append("PRIMARY KEY (");
boolean firstPk = true;
while (pkit.hasNext()) {
if (firstPk) {
firstPk = false;
} else {
buf.append(", ");
}
DbAttribute at = pkit.next();
buf.append(quotingStrategy.quotedName(at));
}
buf.append(')');
}
buf.append(')');
return buf.toString();
}
use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.
the class MySQLAdapter method dropTableStatements.
/**
* @since 3.0
*/
@Override
public Collection<String> dropTableStatements(DbEntity table) {
// note that CASCADE is a noop as of MySQL 5.0, so we have to use FK
// checks
// statement
StringBuilder buf = new StringBuilder();
QuotingStrategy context = getQuotingStrategy();
buf.append(context.quotedFullyQualifiedName(table));
return Arrays.asList("SET FOREIGN_KEY_CHECKS=0", "DROP TABLE IF EXISTS " + buf.toString() + " CASCADE", "SET FOREIGN_KEY_CHECKS=1");
}
use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.
the class OpenBasePkGenerator method createUniquePKIndexString.
/**
* Returns a String to create a unique index on table primary key columns
* per OpenBase recommendations.
*/
protected String createUniquePKIndexString(DbEntity entity) {
Collection<DbAttribute> pk = entity.getPrimaryKeys();
QuotingStrategy context = getAdapter().getQuotingStrategy();
if (pk == null || pk.size() == 0) {
throw new CayenneRuntimeException("Entity '%s' has no PK defined.", entity.getName());
}
StringBuilder buffer = new StringBuilder();
// compound PK doesn't work well with UNIQUE index...
// create a regular one in this case
buffer.append(pk.size() == 1 ? "CREATE UNIQUE INDEX " : "CREATE INDEX ");
buffer.append(context.quotedIdentifier(entity, entity.getName()));
buffer.append(" (");
Iterator<DbAttribute> it = pk.iterator();
// at this point we know that there is at least on PK column
DbAttribute firstColumn = it.next();
buffer.append(context.quotedName(firstColumn));
while (it.hasNext()) {
DbAttribute column = it.next();
buffer.append(", ");
buffer.append(context.quotedName(column));
}
buffer.append(")");
return buffer.toString();
}
use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.
the class OpenBasePkGenerator method createPKString.
/**
* Returns a String to create PK support for an entity.
*/
protected String createPKString(DbEntity entity) {
Collection<DbAttribute> pk = entity.getPrimaryKeys();
if (pk == null || pk.size() == 0) {
throw new CayenneRuntimeException("Entity '%s' has no PK defined.", entity.getName());
}
StringBuilder buffer = new StringBuilder();
buffer.append("CREATE PRIMARY KEY ");
QuotingStrategy context = getAdapter().getQuotingStrategy();
buffer.append(context.quotedIdentifier(entity, entity.getName()));
buffer.append(" (");
Iterator<DbAttribute> it = pk.iterator();
// at this point we know that there is at least on PK column
DbAttribute firstColumn = it.next();
buffer.append(context.quotedName(firstColumn));
while (it.hasNext()) {
DbAttribute column = it.next();
buffer.append(", ");
buffer.append(context.quotedName(column));
}
buffer.append(")");
return buffer.toString();
}
Aggregations