use of org.dbflute.dbmeta.DBMeta in project lastaflute by lastaflute.
the class TransactionSavedRecentResult method prepareEntityUpdateKeyMap.
// ===================================================================================
// Entity Update
// =============
protected Map<String, Object> prepareEntityUpdateKeyMap(BehaviorCommandMeta meta) {
// always can get if entity update
final Entity entity = extractArgumentEntity(meta);
if (entity == null) {
// no way, just in case
return Collections.emptyMap();
}
final DBMeta dbmeta = entity.asDBMeta();
final Map<String, Object> keyMap;
final Set<String> uniqueProps = entity.myuniqueDrivenProperties();
if (!uniqueProps.isEmpty()) {
final Map<String, Object> uniqueMap = uniqueProps.stream().map(prop -> {
return dbmeta.findColumnInfo(prop);
}).collect(Collectors.toMap(col -> col.getColumnDbName(), col -> col.read(entity)));
keyMap = uniqueMap;
} else if (dbmeta.hasPrimaryKey() && entity.hasPrimaryKeyValue()) {
keyMap = dbmeta.extractPrimaryKeyMap(entity);
} else {
// no way if entity update, just in case
keyMap = Collections.emptyMap();
}
return keyMap;
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class AbstractSqlClause method createSelectedSelectColumnInfo.
protected Map<String, SelectedRelationColumn> createSelectedSelectColumnInfo(String foreignTableAliasName, String localTableDbName, String foreignPropertyName, String localRelationPath) {
final DBMeta dbmeta = findDBMeta(localTableDbName);
final ForeignInfo foreignInfo = dbmeta.findForeignInfo(foreignPropertyName);
final int relationNo = foreignInfo.getRelationNo();
String nextRelationPath = RELATION_PATH_DELIMITER + relationNo;
if (localRelationPath != null) {
nextRelationPath = localRelationPath + nextRelationPath;
}
final Map<String, SelectedRelationColumn> resultMap = new LinkedHashMap<String, SelectedRelationColumn>();
final DBMeta foreignDBMeta = foreignInfo.getForeignDBMeta();
final List<ColumnInfo> columnInfoList = foreignDBMeta.getColumnInfoList();
for (ColumnInfo columnInfo : columnInfoList) {
final String columnDbName = columnInfo.getColumnDbName();
final SelectedRelationColumn selectColumnInfo = new SelectedRelationColumn();
selectColumnInfo.setTableAliasName(foreignTableAliasName);
selectColumnInfo.setColumnInfo(columnInfo);
selectColumnInfo.setRelationNoSuffix(nextRelationPath);
resultMap.put(columnDbName, selectColumnInfo);
}
return resultMap;
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class AbstractSqlClause method findDBMeta.
protected DBMeta findDBMeta(String tableDbName) {
DBMeta dbmeta = getCachedDBMetaMap().get(tableDbName);
if (dbmeta != null) {
return dbmeta;
}
if (_dbmetaProvider == null) {
String msg = "The DB meta provider should not be null when using findDBMeta(): " + tableDbName;
throw new IllegalStateException(msg);
}
dbmeta = _dbmetaProvider.provideDBMetaChecked(tableDbName);
getCachedDBMetaMap().put(tableDbName, dbmeta);
return dbmeta;
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class AbstractSqlClause method getClauseQueryDelete.
// -----------------------------------------------------
// Query Delete
// ------------
public String getClauseQueryDelete() {
final DBMeta dbmeta = getDBMeta();
final StringBuilder sb = new StringBuilder();
sb.append("delete");
final boolean useQueryUpdateDirect = isUseQueryUpdateDirect(dbmeta);
String whereClause = null;
if (useQueryUpdateDirect) {
// prepare for direct case
whereClause = processSubQueryIndent(getWhereClause());
if (needsDeleteTableAliasHint(whereClause)) {
sb.append(" ").append(getBasePointAliasName());
}
}
sb.append(" from ").append(dbmeta.getTableSqlName());
if (useQueryUpdateDirect) {
// direct (in-scope unsupported or compound primary keys)
buildQueryUpdateDirectClause(null, whereClause, dbmeta, sb);
} else {
// basically here
buildQueryUpdateInScopeClause(null, dbmeta, sb);
}
return sb.toString();
}
use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.
the class AbstractSqlClause method processSelectClauseLocal.
protected int processSelectClauseLocal(StringBuilder sb) {
final String basePointAliasName = getBasePointAliasName();
final DBMeta dbmeta = getDBMeta();
final Map<String, SpecifiedColumn> localSpecifiedMap;
if (_specifiedSelectColumnMap != null) {
localSpecifiedMap = _specifiedSelectColumnMap.get(basePointAliasName);
} else {
localSpecifiedMap = null;
}
final List<ColumnInfo> columnInfoList;
final boolean validSpecifiedLocal;
final boolean selectClauseTypeUniqueScalar = isSelectClauseTypeUniqueScalar();
// unique scalar, has PK, specified scalar, specified column
ColumnInfo specifiedUniqueScalarColumnInfo = null;
if (selectClauseTypeUniqueScalar) {
// it always has union-query because it's handled before this process
if (dbmeta.hasPrimaryKey()) {
columnInfoList = new ArrayList<ColumnInfo>();
columnInfoList.addAll(dbmeta.getPrimaryInfo().getPrimaryColumnList());
if (isSelectClauseTypeSpecifiedScalar()) {
final ColumnInfo specifiedColumn = getSpecifiedColumnInfoAsOne();
if (specifiedColumn != null && !specifiedColumn.isPrimary()) {
specifiedUniqueScalarColumnInfo = specifiedColumn;
columnInfoList.add(specifiedColumn);
}
// derivingSubQuery is handled after this process
}
} else {
// all columns are target if no-PK and unique-scalar and union-query
columnInfoList = dbmeta.getColumnInfoList();
}
// because specified columns are fixed here
validSpecifiedLocal = false;
} else {
columnInfoList = dbmeta.getColumnInfoList();
validSpecifiedLocal = localSpecifiedMap != null && !localSpecifiedMap.isEmpty();
}
// because 1 origin in JDBC
int selectIndex = 0;
boolean needsDelimiter = false;
for (ColumnInfo columnInfo : columnInfoList) {
final String columnDbName = columnInfo.getColumnDbName();
final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
if (_pkOnlySelectForcedlyEnabled && !columnInfo.isPrimary()) {
continue;
}
if (validSpecifiedLocal && !localSpecifiedMap.containsKey(columnDbName)) {
// a case for scalar-select has been already resolved here
continue;
}
if (canBeNullObjectSpecifiedColumn(columnInfo)) {
registerColumnNullObject(basePointAliasName, columnInfo);
continue;
}
if (needsDelimiter) {
sb.append(", ");
} else {
sb.append("select");
appendSelectHint(sb);
sb.append(" ");
needsDelimiter = true;
}
final String realAliasName;
if (selectClauseTypeUniqueScalar) {
if (specifiedUniqueScalarColumnInfo != null && columnInfo.equals(specifiedUniqueScalarColumnInfo)) {
// has PK, specified column in unique scalar
// might be relation column
realAliasName = getSpecifiedColumnTableAliasNameAsOne();
} else {
// has no PK
realAliasName = basePointAliasName;
}
} else {
realAliasName = basePointAliasName;
}
final String realColumnName = realAliasName + "." + columnSqlName;
final String onQueryName;
++selectIndex;
if (_useSelectIndex) {
final String entityNo = BASE_POINT_HANDLING_ENTITY_NO;
onQueryName = buildSelectIndexAlias(columnSqlName, null, selectIndex, entityNo);
registerSelectIndex(entityNo, columnDbName, onQueryName, selectIndex);
} else {
onQueryName = columnSqlName.toString();
}
sb.append(decryptSelectColumnIfNeeds(columnInfo, realColumnName)).append(" as ").append(onQueryName);
getSelectClauseRealColumnAliasMap().put(realColumnName, onQueryName);
if (validSpecifiedLocal && localSpecifiedMap.containsKey(columnDbName)) {
final SpecifiedColumn specifiedColumn = localSpecifiedMap.get(columnDbName);
// basically for queryInsert()
specifiedColumn.setOnQueryName(onQueryName);
}
}
return selectIndex;
}
Aggregations