Search in sources :

Example 6 with TableCriteria

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());
}
Also used : TableAccessor(com.manydesigns.portofino.reflection.TableAccessor) QueryStringWithParameters(com.manydesigns.elements.text.QueryStringWithParameters) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) Model(com.manydesigns.portofino.model.Model) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration)

Example 7 with TableCriteria

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);
        }
    }
}
Also used : PropertyAccessor(com.manydesigns.elements.reflection.PropertyAccessor) TableCriteria(com.manydesigns.portofino.persistence.TableCriteria) SelectionProviderReference(com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference) QueryStringWithParameters(com.manydesigns.elements.text.QueryStringWithParameters) JSONObject(org.json.JSONObject) Session(org.hibernate.Session)

Aggregations

TableCriteria (com.manydesigns.portofino.persistence.TableCriteria)7 TableAccessor (com.manydesigns.portofino.reflection.TableAccessor)4 Session (org.hibernate.Session)4 QueryStringWithParameters (com.manydesigns.elements.text.QueryStringWithParameters)3 FileObject (org.apache.commons.vfs2.FileObject)3 PropertyAccessor (com.manydesigns.elements.reflection.PropertyAccessor)2 Model (com.manydesigns.portofino.model.Model)1 SelectionProviderReference (com.manydesigns.portofino.resourceactions.m2m.configuration.SelectionProviderReference)1 BigInteger (java.math.BigInteger)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)1 JSONObject (org.json.JSONObject)1