use of com.manydesigns.portofino.persistence.TableCriteria in project Portofino by ManyDesigns.
the class QueryUtilsTest method testMergeQueryWithAlias.
public void testMergeQueryWithAlias() throws NoSuchFieldException {
Model model = new Model();
Database database = new Database();
database.setDatabaseName("db");
model.getDatabases().add(database);
Schema schema = new Schema(database);
schema.setSchemaName("schema");
database.getSchemas().add(schema);
Table table = new Table(schema);
table.setTableName("test_table");
schema.getTables().add(table);
Column column = new Column(table);
column.setColumnName("column1");
column.setColumnType("varchar");
column.setLength(10);
column.setScale(0);
table.getColumns().add(column);
PrimaryKey primaryKey = new PrimaryKey(table);
PrimaryKeyColumn pkColumn = new PrimaryKeyColumn(primaryKey);
primaryKey.getPrimaryKeyColumns().add(pkColumn);
pkColumn.setColumnName("column1");
table.setPrimaryKey(primaryKey);
model.init(new PropertiesConfiguration());
TableAccessor tableAccessor = new TableAccessor(table);
TableCriteria criteria = new TableCriteria(table);
criteria.eq(tableAccessor.getProperty("column1"), "123");
// W/o select
QueryStringWithParameters queryStringWithParameters = QueryUtils.mergeQuery("from test_table t", criteria, null);
assertEquals("FROM test_table t WHERE t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("from test_table t where t.foo = 1", criteria, null);
assertEquals("FROM test_table t WHERE (t.foo = 1) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("from test_table t, other where t.foo = other.bar", criteria, null);
assertEquals("FROM test_table t, other WHERE (t.foo = other.bar) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("from test_table t, other x where t.foo = x.bar", criteria, null);
assertEquals("FROM test_table t, other x WHERE (t.foo = x.bar) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
// W/select
queryStringWithParameters = QueryUtils.mergeQuery("select t from test_table t", criteria, null);
assertEquals("SELECT t FROM test_table t WHERE t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("select t from test_table t where t.foo = 1", criteria, null);
assertEquals("SELECT t FROM test_table t WHERE (t.foo = 1) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("select t from test_table t, other where t.foo = other.bar", criteria, null);
assertEquals("SELECT t FROM test_table t, other WHERE (t.foo = other.bar) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("select t from test_table t, other x where t.foo = x.bar", criteria, null);
assertEquals("SELECT t FROM test_table t, other x WHERE (t.foo = x.bar) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
// W/multiple select
queryStringWithParameters = QueryUtils.mergeQuery("select t, u from test_table t", criteria, null);
assertEquals("SELECT t, u FROM test_table t WHERE t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("select t, u from test_table t where t.foo = 1", criteria, null);
assertEquals("SELECT t, u FROM test_table t WHERE (t.foo = 1) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("select t, u from test_table t, other where t.foo = other.bar", criteria, null);
assertEquals("SELECT t, u FROM test_table t, other WHERE (t.foo = other.bar) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
queryStringWithParameters = QueryUtils.mergeQuery("select t, u from test_table t, other x where t.foo = x.bar", criteria, null);
assertEquals("SELECT t, u FROM test_table t, other x WHERE (t.foo = x.bar) AND t.column1 = :p1", queryStringWithParameters.getQueryString());
}
use of com.manydesigns.portofino.persistence.TableCriteria in project Portofino by ManyDesigns.
the class ManyToManyAction method loadAssociations.
protected void loadAssociations() throws NoSuchFieldException {
Table table = m2mConfiguration.getActualRelationTable();
TableCriteria criteria = new TableCriteria(table);
// TODO chiave multipla
String onePropertyName = m2mConfiguration.getActualOnePropertyName();
PropertyAccessor onePropertyAccessor = relationTableAccessor.getProperty(onePropertyName);
// TODO chiave multipla
SelectionProviderReference manySelectionProvider = m2mConfiguration.getManySelectionProvider();
String manyPropertyName = manySelectionProvider.getActualSelectionProvider().getReferences().get(0).getActualFromColumn().getActualPropertyName();
PropertyAccessor manyPropertyAccessor = relationTableAccessor.getProperty(manyPropertyName);
criteria = criteria.eq(onePropertyAccessor, onePk);
QueryStringWithParameters queryString;
try {
queryString = QueryUtils.mergeQuery(m2mConfiguration.getQuery(), criteria, this);
} catch (RuntimeException e) {
RequestMessages.addErrorMessage("Invalid query");
throw e;
}
existingAssociations = QueryUtils.runHqlQuery(session, queryString.getQueryString(), queryString.getParameters());
availableAssociations = new ArrayList<Object>();
String databaseName = ((DatabaseSelectionProvider) manySelectionProvider.getActualSelectionProvider()).getToDatabase();
String hql = ((DatabaseSelectionProvider) manySelectionProvider.getActualSelectionProvider()).getHql();
if (!StringUtils.isEmpty(hql)) {
Session selectionProviderSession = persistence.getSession(databaseName);
QueryStringWithParameters manyQuery = QueryUtils.mergeQuery(hql, null, this);
potentiallyAvailableAssociations = QueryUtils.runHqlQuery(selectionProviderSession, manyQuery.getQueryString(), manyQuery.getParameters());
} else {
throw new RuntimeException("Couldn't determine many query");
}
PropertyAccessor[] manyKeyProperties = manyTableAccessor.getKeyProperties();
// TODO handle manyKeyProperties.length > 1
PropertyAccessor manyPkAccessor = manyTableAccessor.getProperty(manyKeyProperties[0].getName());
for (Object o : potentiallyAvailableAssociations) {
Object oPk = manyPkAccessor.get(o);
boolean existing = isExistingAssociation(manyPropertyAccessor, oPk);
if (!existing) {
availableAssociations.add(o);
}
}
}
Aggregations