Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2019 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.engine.persistence;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
/**
* @author Baptiste Mesta
*/
public class SQLQueryBuilder extends QueryBuilder {
public static final String TRUE_VALUE_PARAMETER = "trueValue";
private static Map hqlToSqlAlias = new HashMap<>();
static {
hqlToSqlAlias.put("user", "user_");
}
SQLQueryBuilder(Session session, Query baseQuery,
OrderByBuilder orderByBuilder,
Map classAliasMappings, char likeEscapeCharacter,
boolean wordSearchEnabled,
OrderByCheckingMode orderByCheckingMode,
SelectListDescriptor selectDescriptor) {
super(session, baseQuery, orderByBuilder, classAliasMappings, likeEscapeCharacter, wordSearchEnabled,
orderByCheckingMode,
selectDescriptor);
}
public void addConstantsAsParameters(Query sqlQuery) {
if (sqlQuery.getQueryString().contains(":" + TRUE_VALUE_PARAMETER)) {
// there is no need to convert the true value to a integer for Oracle and Sqlserver, hibernate does that already.
sqlQuery.setParameter(TRUE_VALUE_PARAMETER, true);
}
}
private String replaceHQLAliasesBySQLAliases(String builtQuery) {
for (Map.Entry aliasToReplace : hqlToSqlAlias.entrySet()) {
if (builtQuery.contains(aliasToReplace.getKey() + ".")) {
builtQuery = builtQuery.replace(aliasToReplace.getKey() + ".", aliasToReplace.getValue() + ".");
}
}
return builtQuery;
}
@Override
public void setTenantId(Query query, long tenantId) {
if (query.getQueryString().contains(":tenantId")) {
query.setParameter("tenantId", tenantId);
}
}
@Override
Query rebuildQuery(AbstractSelectDescriptor selectDescriptor, Session session, Query query) {
String builtQuery = stringQueryBuilder.toString();
builtQuery = replaceHQLAliasesBySQLAliases(builtQuery);
NativeQuery generatedSqlQuery = session.createSQLQuery(builtQuery);
for (Map.Entry parameter : getQueryParameters().entrySet()) {
generatedSqlQuery.setParameter(parameter.getKey(), parameter.getValue());
}
for (NativeSQLQueryReturn queryReturn : (List) ((NativeQuery) query)
.getQueryReturns()) {
if (queryReturn instanceof NativeSQLQueryScalarReturn) {
generatedSqlQuery.addScalar(((NativeSQLQueryScalarReturn) queryReturn).getColumnAlias(),
((NativeSQLQueryScalarReturn) queryReturn).getType());
} else if (queryReturn instanceof NativeSQLQueryRootReturn) {
generatedSqlQuery.addEntity(((NativeSQLQueryRootReturn) queryReturn).getAlias(),
((NativeSQLQueryRootReturn) queryReturn).getReturnEntityName());
} else {
throw new IllegalStateException(
"Not yet implemented. Query return type " + queryReturn.getClass().getName());
}
}
return generatedSqlQuery;
}
}