use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class InsertOption method xacceptInsertColumnModifiedPropertiesIfNeeds.
// -----------------------------------------------------
// Modified Properties
// -------------------
public void xacceptInsertColumnModifiedPropertiesIfNeeds(List<? extends Entity> entityList) {
// internal
if (entityList == null) {
throw new IllegalArgumentException("The argument 'entityList' should not be null.");
}
if (_insertColumnSpecification != null) {
// already specified
return;
}
if (entityList.isEmpty()) {
// do nothing
return;
}
if (xisCompatibleBatchInsertDefaultEveryColumn()) {
// every column for compatible
return;
}
final Entity firstEntity = entityList.get(0);
if (firstEntity.createdBySelect()) {
// all columns e.g. copy insert
specify(new SpecifyQuery<CB>() {
public void specify(CB cb) {
final List<ColumnInfo> infoList = firstEntity.asDBMeta().getColumnInfoList();
for (ColumnInfo info : infoList) {
if (!info.isPrimary()) {
// except PK
cb.localSp().xspecifyColumn(info.getColumnDbName());
}
}
}
});
} else {
// least common multiple or same-set columns
final Set<String> targetProps = xgatherInsertColumnModifiedProperties(entityList, firstEntity);
final DBMeta dbmeta = firstEntity.asDBMeta();
specify(new SpecifyQuery<CB>() {
public void specify(CB cb) {
// you don't need to specify primary key because primary key has special handling
for (String prop : targetProps) {
final ColumnInfo info = dbmeta.findColumnInfo(prop);
if (!info.isPrimary()) {
// except PK
cb.localSp().xspecifyColumn(info.getColumnDbName());
}
}
}
});
}
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class InsertOption method xcheckSpecifiedInsertColumnPrimaryKey.
// -----------------------------------------------------
// Insert Process
// --------------
public void xcheckSpecifiedInsertColumnPrimaryKey() {
// checked later by process if it needs
if (_insertColumnSpecification == null) {
return;
}
assertInsertColumnSpecifiedCB();
final CB cb = _insertColumnSpecifiedCB;
final String basePointAliasName = cb.getSqlClause().getBasePointAliasName();
final DBMeta dbmeta = cb.asDBMeta();
if (dbmeta.hasPrimaryKey()) {
final PrimaryInfo pkInfo = dbmeta.getPrimaryInfo();
final List<ColumnInfo> pkList = pkInfo.getPrimaryColumnList();
for (ColumnInfo pk : pkList) {
final String columnDbName = pk.getColumnDbName();
if (cb.getSqlClause().hasSpecifiedSelectColumn(basePointAliasName, columnDbName)) {
String msg = "PK columns should not be allowed to specify as update columns: " + columnDbName;
throw new SpecifyUpdateColumnInvalidException(msg);
}
}
}
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class GreatWallOfOracleType method mappingOracleStructToEntity.
protected Entity mappingOracleStructToEntity(Object oracleStruct, Object entityType) throws SQLException {
if (oracleStruct == null) {
return null;
}
final Entity prototype;
if (entityType instanceof Entity) {
prototype = (Entity) entityType;
} else if (entityType instanceof Class<?>) {
prototype = (Entity) DfReflectionUtil.newInstance((Class<?>) entityType);
} else {
String msg = "The entityType should be entity instance or entity type: " + entityType;
throw new IllegalArgumentException(msg);
}
final DBMeta dbmeta = prototype.asDBMeta();
final Object[] attrs = toStandardStructAttributes(oracleStruct);
return doMappingOracleStructToEntity(dbmeta, attrs);
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class GreatWallOfOracleType method mappingEntityToOracleStruct.
protected Object mappingEntityToOracleStruct(Connection conn, Object paramExp, Entity entity) throws SQLException {
final DBMeta dbmeta = entity.asDBMeta();
final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
final List<Object> attrList = new ArrayList<Object>();
for (ColumnInfo columnInfo : columnInfoList) {
final Object propertyValue = columnInfo.read(entity);
final Object mappedValue;
if (propertyValue instanceof List<?>) {
// array in struct
// it works only when the element type is scalar
// (but property type is Object because Sql2Entity does not support this)
final List<?> nested = ((List<?>) propertyValue);
final String arrayTypeName = columnInfo.getColumnDbType();
final Class<?> nativeType = columnInfo.getObjectNativeType();
Class<?> elementType = nativeType;
if (List.class.isAssignableFrom(nativeType)) {
elementType = columnInfo.getGenericType();
} else if (Object.class.equals(nativeType) && DfCollectionUtil.hasValidElement(nested)) {
final Class<?> firstElementType = DfCollectionUtil.findFirstElementType(nested);
if (firstElementType != null) {
elementType = nested.iterator().next().getClass();
}
}
mappedValue = mappingListToOracleArray(conn, paramExp, nested, arrayTypeName, elementType);
} else if (propertyValue instanceof Entity) {
// struct in struct
// it works only when the entity structure matches with the struct type
// (but property type is Object because Sql2Entity does not support this)
mappedValue = mappingEntityToOracleStruct(conn, paramExp, (Entity) propertyValue);
} else {
mappedValue = mappingScalarToSqlValue(conn, paramExp, propertyValue, columnInfo);
}
attrList.add(mappedValue);
}
final String structTypeName = dbmeta.getTableSqlName().toString();
return toOracleStruct(getOracleConnection(conn), structTypeName, attrList.toArray());
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class HpFixedConditionQueryResolver method resolveFixedInlineView.
// ===================================================================================
// Resolve Fixed InlineView
// ========================
public String resolveFixedInlineView(String foreignTableSqlName, boolean treatedAsInnerJoin) {
// so it can uses bridge variables here
if (_inlineViewResourceMap == null || _inlineViewResourceMap.isEmpty()) {
// not uses InlineView
return foreignTableSqlName;
}
// alias is required because foreignTableSqlName may be (normal) InlineView
final String baseAlias = "dffixedbase";
final String baseIndent;
if (treatedAsInnerJoin) {
// ----------" inner join "
baseIndent = " ";
} else {
// ----------" left outer join "
baseIndent = " ";
}
final StringBuilder joinSb = new StringBuilder();
final Map<ForeignInfo, String> relationMap = new HashMap<ForeignInfo, String>();
final List<String> additionalRealColumnList = new ArrayList<String>();
// basically not null
final String resolvedFixedCondition = _resolvedFixedCondition;
String optimizedCondition = _inlineViewOptimizedCondition;
int groupIndex = 0;
for (InlineViewResource resource : _inlineViewResourceMap.values()) {
final List<ForeignInfo> joinInfoList = resource.getJoinInfoList();
final String aliasBase = "dffixedjoin";
String preForeignAlias = null;
String foreignAlias = null;
int joinIndex = 0;
final Map<ForeignInfo, String> foreignAliasMap = new HashMap<ForeignInfo, String>(joinInfoList.size());
for (ForeignInfo joinInfo : joinInfoList) {
if (relationMap.containsKey(joinInfo)) {
// already joined
// update previous alias
preForeignAlias = relationMap.get(joinInfo);
continue;
}
final TableSqlName foreignTable;
final String localAlias;
{
final DBMeta foreignDBMeta = joinInfo.getForeignDBMeta();
foreignTable = foreignDBMeta.getTableSqlName();
localAlias = (preForeignAlias != null ? preForeignAlias : baseAlias);
foreignAlias = aliasBase + "_" + groupIndex + "_" + joinIndex;
preForeignAlias = foreignAlias;
}
joinSb.append(ln()).append(baseIndent);
joinSb.append(" left outer join ").append(foreignTable).append(" ").append(foreignAlias);
joinSb.append(" on ");
final Map<ColumnInfo, ColumnInfo> columnInfoMap = joinInfo.getLocalForeignColumnInfoMap();
int columnIndex = 0;
for (Entry<ColumnInfo, ColumnInfo> localForeignEntry : columnInfoMap.entrySet()) {
final ColumnInfo localColumnInfo = localForeignEntry.getKey();
final ColumnInfo foreignColumninfo = localForeignEntry.getValue();
if (columnIndex > 0) {
joinSb.append(" and ");
}
joinSb.append(localAlias).append(".").append(localColumnInfo.getColumnSqlName());
joinSb.append(" = ").append(foreignAlias).append(".").append(foreignColumninfo.getColumnSqlName());
++columnIndex;
}
foreignAliasMap.put(joinInfo, foreignAlias);
relationMap.put(joinInfo, foreignAlias);
++joinIndex;
}
if (optimizedCondition != null) {
optimizedCondition = resolvedOptimizedCondition(optimizedCondition, resource, foreignAliasMap);
}
collectAdditionalRealColumnList(additionalRealColumnList, resolvedFixedCondition, resource, foreignAlias);
++groupIndex;
}
if (optimizedCondition != null) {
// foreign alias for in-line view is resolved here
optimizedCondition = replaceString(optimizedCondition, getForeignAliasMark(), baseAlias);
optimizedCondition = filterSubQueryIndentMark(optimizedCondition, false, true);
}
final StringBuilder sqlSb = new StringBuilder();
sqlSb.append("(select ").append(baseAlias).append(".*");
for (String columnName : additionalRealColumnList) {
sqlSb.append(", ").append(columnName);
}
sqlSb.append(ln()).append(baseIndent);
sqlSb.append(" from ").append(foreignTableSqlName).append(" ").append(baseAlias);
sqlSb.append(joinSb);
if (optimizedCondition != null) {
buildOptimizedInlineWhereClause(optimizedCondition, baseIndent, sqlSb);
}
sqlSb.append(ln()).append(baseIndent);
sqlSb.append(")");
return sqlSb.toString();
}
Aggregations