use of org.apache.cayenne.CayenneRuntimeException 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.CayenneRuntimeException in project cayenne by apache.
the class OpenBaseAdapter method createTable.
/**
* Returns a SQL string that can be used to create database table
* corresponding to <code>ent</code> parameter.
*/
@Override
public String createTable(DbEntity ent) {
StringBuilder buf = new StringBuilder();
buf.append("CREATE TABLE ");
buf.append(quotingStrategy.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(quotingStrategy.quotedName(at)).append(' ').append(type);
// append size and precision (if applicable)
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 {
buf.append(" NULL");
}
}
buf.append(')');
return buf.toString();
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class OpenBaseAdapter method createFkConstraint.
/**
* Returns a SQL string that can be used to create a foreign key constraint
* for the relationship.
*/
@Override
public String createFkConstraint(DbRelationship rel) {
StringBuilder buf = new StringBuilder();
// OpendBase Specifics is that we need to create a constraint going
// from destination to source for this to work...
DbEntity sourceEntity = (DbEntity) rel.getSourceEntity();
DbEntity targetEntity = (DbEntity) rel.getTargetEntity();
String toMany = (!rel.isToMany()) ? "'1'" : "'0'";
// TODO: doesn't seem like OpenBase supports compound joins...
// need to doublecheck that
int joinsLen = rel.getJoins().size();
if (joinsLen == 0) {
throw new CayenneRuntimeException("Relationship has no joins: %s", rel.getName());
} else if (joinsLen > 1) {
// ignore extra joins
}
DbJoin join = rel.getJoins().get(0);
buf.append("INSERT INTO _SYS_RELATIONSHIP (").append("dest_table, dest_column, source_table, source_column, ").append("block_delete, cascade_delete, one_to_many, operator, relationshipName").append(") VALUES ('").append(sourceEntity.getFullyQualifiedName()).append("', '").append(join.getSourceName()).append("', '").append(targetEntity.getFullyQualifiedName()).append("', '").append(join.getTargetName()).append("', 0, 0, ").append(toMany).append(", '=', '").append(rel.getName()).append("')");
return buf.toString();
}
use of org.apache.cayenne.CayenneRuntimeException 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.CayenneRuntimeException 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