Search in sources :

Example 11 with QuotingStrategy

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());
        }
    }
}
Also used : DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 12 with QuotingStrategy

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();
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 13 with QuotingStrategy

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");
}
Also used : QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy) DefaultQuotingStrategy(org.apache.cayenne.dba.DefaultQuotingStrategy)

Example 14 with QuotingStrategy

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();
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 15 with QuotingStrategy

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();
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Aggregations

QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)42 DbAdapter (org.apache.cayenne.dba.DbAdapter)11 DbAttribute (org.apache.cayenne.map.DbAttribute)11 DbEntity (org.apache.cayenne.map.DbEntity)5 DbJoin (org.apache.cayenne.map.DbJoin)5 DbRelationship (org.apache.cayenne.map.DbRelationship)5 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 SetAllowNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetAllowNullToDb)3 SetNotNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb)3 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DropColumnToDb (org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb)2 EJBQLPath (org.apache.cayenne.ejbql.parser.EJBQLPath)2 DataMap (org.apache.cayenne.map.DataMap)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2