Search in sources :

Example 1 with TableExpression

use of com.servoy.j2db.query.TableExpression in project servoy-client by Servoy.

the class QBJoins method add.

/**
 * @clonedesc com.servoy.j2db.querybuilder.IQueryBuilderJoins#add(String, int, String)
 * @sampleas add(String, int)
 *
 * @param dataSource data source
 * @param joinType join type, one of {@link IQueryBuilderJoin#LEFT_OUTER_JOIN}, {@link IQueryBuilderJoin#INNER_JOIN}, {@link IQueryBuilderJoin#RIGHT_OUTER_JOIN}, {@link IQueryBuilderJoin#FULL_JOIN}
 * @param alias the alias for joining table
 */
@JSFunction
public QBJoin add(String dataSource, int joinType, String alias) {
    String name;
    QBJoin join;
    if (alias == null) {
        name = UUID.randomUUID().toString();
        join = null;
    } else {
        name = alias;
        join = getJoin(name);
    }
    if (join == null) {
        Table foreignTable = root.getTable(dataSource);
        join = addJoin(new QueryJoin(name, parent.getQueryTable(), new TableExpression(new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema(), alias)), new AndCondition(), joinType, true), dataSource, name);
    }
    return join;
}
Also used : DerivedTable(com.servoy.j2db.query.DerivedTable) QueryTable(com.servoy.j2db.query.QueryTable) Table(com.servoy.j2db.persistence.Table) QueryJoin(com.servoy.j2db.query.QueryJoin) TableExpression(com.servoy.j2db.query.TableExpression) QueryTable(com.servoy.j2db.query.QueryTable) AndCondition(com.servoy.j2db.query.AndCondition) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 2 with TableExpression

use of com.servoy.j2db.query.TableExpression in project servoy-client by Servoy.

the class QBJoin method createColumn.

@Override
protected QBColumn createColumn(String name) throws RepositoryException {
    ITableReference foreignTableReference = join.getForeignTableReference();
    if (foreignTableReference instanceof TableExpression) {
        Table joinTable = getRoot().getTable(foreignTableReference.getTable().getDataSource());
        if (joinTable == null) {
            throw new RepositoryException("Cannot find column '" + name + "' in data source '" + foreignTableReference.getTable().getDataSource() + "'");
        }
        Column col = joinTable.getColumn(name);
        if (col == null) {
            throw new RepositoryException("Cannot find column '" + name + "' in data source '" + foreignTableReference.getTable().getDataSource() + "'");
        }
        return new QBColumn(getRoot(), this, new QueryColumn(getQueryTable(), col.getID(), col.getSQLName(), col.getType(), col.getLength(), col.getScale(), col.getNativeTypename(), col.getFlags(), false));
    } else if (foreignTableReference instanceof DerivedTable) {
        QuerySelect query = ((DerivedTable) foreignTableReference).getQuery();
        for (IQuerySelectValue qcol : query.getColumns()) {
            if (name.equals(qcol.getAliasOrName()) || (qcol.getColumn() != null && name.equals(generateNormalizedNonReservedOSName(qcol.getColumn().getName())))) {
                return new QBColumn(getRoot(), this, new QueryColumn(foreignTableReference.getTable(), generateNormalizedNonReservedOSName(name)));
            }
        }
    }
    throw new RepositoryException("Cannot find column '" + name + "' in source '" + foreignTableReference + "'");
}
Also used : DerivedTable(com.servoy.j2db.query.DerivedTable) BaseQueryTable(com.servoy.base.query.BaseQueryTable) Table(com.servoy.j2db.persistence.Table) DerivedTable(com.servoy.j2db.query.DerivedTable) QueryColumn(com.servoy.j2db.query.QueryColumn) Column(com.servoy.j2db.persistence.Column) ITableReference(com.servoy.j2db.query.ITableReference) QueryColumn(com.servoy.j2db.query.QueryColumn) RepositoryException(com.servoy.j2db.persistence.RepositoryException) TableExpression(com.servoy.j2db.query.TableExpression) QuerySelect(com.servoy.j2db.query.QuerySelect) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Aggregations

Table (com.servoy.j2db.persistence.Table)2 DerivedTable (com.servoy.j2db.query.DerivedTable)2 TableExpression (com.servoy.j2db.query.TableExpression)2 BaseQueryTable (com.servoy.base.query.BaseQueryTable)1 Column (com.servoy.j2db.persistence.Column)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 AndCondition (com.servoy.j2db.query.AndCondition)1 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)1 ITableReference (com.servoy.j2db.query.ITableReference)1 QueryColumn (com.servoy.j2db.query.QueryColumn)1 QueryJoin (com.servoy.j2db.query.QueryJoin)1 QuerySelect (com.servoy.j2db.query.QuerySelect)1 QueryTable (com.servoy.j2db.query.QueryTable)1 JSFunction (org.mozilla.javascript.annotations.JSFunction)1