use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.
the class AbstractSqlClause method getClauseQueryInsert.
// ===================================================================================
// Query Update
// ============
// -----------------------------------------------------
// Query Insert
// ------------
public String getClauseQueryInsert(Map<String, String> fixedValueQueryExpMap, SqlClause resourceSqlClause) {
// at first, this should be called (before on-query name handling)
// because an on-query name of mapped info are set in this process
final String resourceViewClause = resourceSqlClause.getClause();
if (_specifiedSelectColumnMap == null) {
String msg = "The specified columns for query-insert are required.";
throw new IllegalConditionBeanOperationException(msg);
}
final Map<String, SpecifiedColumn> elementMap = _specifiedSelectColumnMap.get(getBasePointAliasName());
if (elementMap == null || elementMap.isEmpty()) {
String msg = "The specified columns of inserted table for query-insert are required.";
throw new IllegalConditionBeanOperationException(msg);
}
final DBMeta dbmeta = getDBMeta();
final StringBuilder intoSb = new StringBuilder();
final StringBuilder selectSb = new StringBuilder();
final String resourceAlias = "dfres";
int index = 0;
final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
for (ColumnInfo columnInfo : columnInfoList) {
final String columnDbName = columnInfo.getColumnDbName();
final SpecifiedColumn specifiedColumn = elementMap.get(columnDbName);
final String onQueryName;
if (specifiedColumn != null) {
onQueryName = specifiedColumn.getValidMappedOnQueryName();
} else if (fixedValueQueryExpMap.containsKey(columnDbName)) {
final String fixedValueQueryExp = fixedValueQueryExpMap.get(columnDbName);
if (fixedValueQueryExp != null) {
onQueryName = encryptIfNeeds(columnInfo, fixedValueQueryExp);
} else {
// it uses null literal on query
// because the SQL analyzer blocks null parameters
// (the analyzer should do it for condition-bean)
onQueryName = "null";
}
} else {
continue;
}
if (onQueryName == null || onQueryName.trim().length() == 0) {
// no way
String msg = "The on-query name for query-insert is required: " + specifiedColumn;
throw new IllegalConditionBeanOperationException(msg);
}
final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
if (index > 0) {
intoSb.append(", ");
selectSb.append(", ");
}
intoSb.append(columnSqlName);
if (specifiedColumn != null) {
selectSb.append(resourceAlias).append(".");
}
selectSb.append(onQueryName);
++index;
}
final String subQueryIdentity = "queryInsertResource";
final String subQueryBeginMark = resolveSubQueryBeginMark(subQueryIdentity);
final String subQueryEndMark = resolveSubQueryEndMark(subQueryIdentity);
final StringBuilder mainSb = new StringBuilder();
mainSb.append("insert into ").append(dbmeta.getTableSqlName());
mainSb.append(" (").append(intoSb).append(")").append(ln());
mainSb.append("select ").append(selectSb).append(ln());
mainSb.append(" from (").append(subQueryBeginMark).append(ln());
mainSb.append(resourceViewClause).append(ln());
mainSb.append(" ) ").append(resourceAlias).append(subQueryEndMark);
final String sql = mainSb.toString();
return processSubQueryIndent(sql);
}
use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.
the class DerivedReferrer method buildSubQueryClause.
// ===================================================================================
// SubQuery Clause (Compound PrimaryKey)
// =====================================
/**
* Build the clause of sub-query by compound primary key.
* @param function The expression for deriving function. (NotNull)
* @param correlatedColumnRealNames The real names of correlated column that is main-query table's column. (NotNull)
* @param relatedColumnSqlNames The real names of related column that is sub-query table's column. (NotNull)
* @param correlatedFixedCondition The fixed condition as correlated condition. (NullAllowed)
* @param option The option of DerivedReferrer. (NotNull)
* @return The clause of sub-query. (NotNull)
*/
protected String buildSubQueryClause(String function, ColumnRealName[] correlatedColumnRealNames, ColumnSqlName[] relatedColumnSqlNames, String correlatedFixedCondition, DerivedReferrerOption option) {
final String tableAliasName = getSubQueryLocalAliasName();
final ColumnSqlName derivedColumnSqlName = getDerivedColumnSqlName();
if (derivedColumnSqlName == null) {
throwDerivedReferrerInvalidColumnSpecificationException(function);
}
final ColumnRealName derivedColumnRealName = getDerivedColumnRealName();
final String subQueryClause;
if (_subQuerySqlClause.hasUnionQuery()) {
subQueryClause = buildUnionSubQueryClause(function, correlatedColumnRealNames, relatedColumnSqlNames, option, tableAliasName, derivedColumnRealName, derivedColumnSqlName, correlatedFixedCondition);
} else {
final String selectClause = "select " + buildFunctionPart(function, derivedColumnRealName, option, false);
final String fromWhereClause;
if (option.isSuppressCorrelation()) {
// e.g. myselfDerived
fromWhereClause = buildPlainFromWhereClause(selectClause, tableAliasName, correlatedFixedCondition);
} else {
// basically here
fromWhereClause = buildCorrelationFromWhereClause(selectClause, tableAliasName, correlatedColumnRealNames, relatedColumnSqlNames, correlatedFixedCondition);
}
subQueryClause = selectClause + " " + fromWhereClause;
}
return resolveSubQueryLevelVariable(subQueryClause);
}
use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.
the class DerivedReferrer method buildDerivedReferrer.
// ===================================================================================
// Build Clause
// ============
public String buildDerivedReferrer(String function, String correlatedColumnDbName, String relatedColumnDbName, String correlatedFixedCondition, DerivedReferrerOption option) {
setupOptionAttribute(option);
if (isSinglePrimaryKey(correlatedColumnDbName, relatedColumnDbName)) {
final ColumnRealName correlatedColumnRealName = _localRealNameProvider.provide(correlatedColumnDbName);
final ColumnSqlName relatedColumnSqlName = _subQuerySqlNameProvider.provide(relatedColumnDbName);
final String subQueryClause = buildSubQueryClause(function, correlatedColumnRealName, relatedColumnSqlName, correlatedFixedCondition, option);
final String beginMark = resolveSubQueryBeginMark(_subQueryIdentity) + ln();
final String endMark = resolveSubQueryEndMark(_subQueryIdentity);
final String endIndent = " ";
return doBuildDerivedReferrer(function, correlatedColumnRealName, relatedColumnSqlName, subQueryClause, beginMark, endMark, endIndent);
} else {
final List<String> columnDbNameSplit = Srl.splitListTrimmed(correlatedColumnDbName, ",");
final ColumnRealName[] correlatedColumnRealNames = new ColumnRealName[columnDbNameSplit.size()];
for (int i = 0; i < columnDbNameSplit.size(); i++) {
correlatedColumnRealNames[i] = _localRealNameProvider.provide(columnDbNameSplit.get(i));
}
final List<String> relatedColumnSplit = Srl.splitListTrimmed(relatedColumnDbName, ",");
final ColumnSqlName[] relatedColumnSqlNames = new ColumnSqlName[relatedColumnSplit.size()];
for (int i = 0; i < relatedColumnSplit.size(); i++) {
relatedColumnSqlNames[i] = _subQuerySqlNameProvider.provide(relatedColumnSplit.get(i));
}
final String subQueryClause = buildSubQueryClause(function, correlatedColumnRealNames, relatedColumnSqlNames, correlatedFixedCondition, option);
final String beginMark = resolveSubQueryBeginMark(_subQueryIdentity) + ln();
final String endMark = resolveSubQueryEndMark(_subQueryIdentity);
final String endIndent = " ";
return doBuildDerivedReferrer(function, correlatedColumnRealNames, relatedColumnSqlNames, subQueryClause, beginMark, endMark, endIndent);
}
}
use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.
the class ExistsReferrer method buildExistsReferrer.
// ===================================================================================
// Build Clause
// ============
/**
* Build the clause of sub-query by single primary key.
* @param correlatedColumnDbName The DB name of correlated column that is main-query table's column. (NotNull)
* @param relatedColumnDbName The DB name of related column that is sub-query table's column. (NotNull)
* @param correlatedFixedCondition The fixed condition as correlated condition. (NullAllowed)
* @param existsOption The option of ExistsReferrer. (basically for NotExistsReferrer) (NullAllowed: if null, means ExistsReferrer)
* @return The clause of sub-query. (NotNull)
*/
public String buildExistsReferrer(String correlatedColumnDbName, String relatedColumnDbName, String correlatedFixedCondition, String existsOption) {
existsOption = existsOption != null ? existsOption + " " : "";
final String subQueryClause;
if (isSinglePrimaryKey(correlatedColumnDbName, relatedColumnDbName)) {
final ColumnSqlName relatedColumnSqlName = _subQuerySqlNameProvider.provide(relatedColumnDbName);
final ColumnRealName correlatedColumnRealName = _localRealNameProvider.provide(correlatedColumnDbName);
subQueryClause = buildSubQueryClause(correlatedColumnRealName, relatedColumnSqlName, correlatedFixedCondition);
} else {
// compound primary keys
final List<String> columnDbNameSplit = Srl.splitListTrimmed(correlatedColumnDbName, ",");
final ColumnRealName[] correlatedColumnRealNames = new ColumnRealName[columnDbNameSplit.size()];
for (int i = 0; i < columnDbNameSplit.size(); i++) {
correlatedColumnRealNames[i] = _localRealNameProvider.provide(columnDbNameSplit.get(i));
}
final List<String> relatedColumnSplit = Srl.splitListTrimmed(relatedColumnDbName, ",");
final ColumnSqlName[] relatedColumnSqlNames = new ColumnSqlName[relatedColumnSplit.size()];
for (int i = 0; i < relatedColumnSplit.size(); i++) {
relatedColumnSqlNames[i] = _subQuerySqlNameProvider.provide(relatedColumnSplit.get(i));
}
subQueryClause = buildSubQueryClause(correlatedColumnRealNames, relatedColumnSqlNames, correlatedFixedCondition);
}
final String beginMark = resolveSubQueryBeginMark(_subQueryIdentity) + ln();
final String endMark = resolveSubQueryEndMark(_subQueryIdentity);
final String endIndent = " ";
return existsOption + "exists (" + beginMark + subQueryClause + ln() + endIndent + ")" + endMark;
}
use of org.dbflute.dbmeta.name.ColumnSqlName in project dbflute-core by dbflute.
the class ExistsReferrer method buildSubQueryClause.
/**
* Build the clause of sub-query by compound primary key.
* @param correlatedColumnRealNames The real names of correlated column that is main-query table's column. (NotNull)
* @param relatedColumnSqlNames The real names of related column that is sub-query table's column. (NotNull)
* @param correlatedFixedCondition The fixed condition as correlated condition. (NullAllowed)
* @return The clause of sub-query. (NotNull)
*/
protected String buildSubQueryClause(ColumnRealName[] correlatedColumnRealNames, ColumnSqlName[] relatedColumnSqlNames, String correlatedFixedCondition) {
// only single column allowed (no problem because of select clause for exists)
final ColumnSqlName firstSqlName = relatedColumnSqlNames[0];
final String localAliasName = getSubQueryLocalAliasName();
final String selectClause = "select " + ColumnRealName.create(localAliasName, firstSqlName);
final String fromWhereClause = buildCorrelationFromWhereClause(selectClause, localAliasName, correlatedColumnRealNames, relatedColumnSqlNames, correlatedFixedCondition);
return doBuildSubQueryClause(selectClause, fromWhereClause);
}
Aggregations