Search in sources :

Example 41 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class SQLGenerator method createTableFiltercondition.

public static QueryFilter createTableFiltercondition(BaseQueryTable qTable, Table table, DataproviderTableFilterdefinition filterdefinition) {
    Column c = table.getColumn(filterdefinition.getDataprovider());
    if (c == null) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        Debug.error("Could not apply filter " + filterdefinition + " on table " + table + " : column not found:" + filterdefinition.getDataprovider());
        return null;
    }
    int op = filterdefinition.getOperator();
    int maskedOp = op & IBaseSQLCondition.OPERATOR_MASK;
    Object value = filterdefinition.getValue();
    QueryColumn qColumn = c.queryColumn(qTable);
    ISQLCondition filterWhere;
    if (maskedOp == IBaseSQLCondition.EQUALS_OPERATOR || maskedOp == IBaseSQLCondition.NOT_OPERATOR || maskedOp == IBaseSQLCondition.IN_OPERATOR || maskedOp == IBaseSQLCondition.NOT_IN_OPERATOR || maskedOp == IBaseSQLCondition.GT_OPERATOR || maskedOp == IBaseSQLCondition.LT_OPERATOR || maskedOp == IBaseSQLCondition.GTE_OPERATOR || maskedOp == IBaseSQLCondition.LTE_OPERATOR) {
        Object inValues;
        boolean andCondition = true;
        if (value instanceof List<?>) {
            inValues = new Object[][] { ((List<?>) value).toArray() };
            andCondition = maskedOp != IBaseSQLCondition.NOT_OPERATOR && maskedOp != IBaseSQLCondition.NOT_IN_OPERATOR;
        } else if (value != null && value.getClass().isArray()) {
            inValues = new Object[][] { (Object[]) value };
            andCondition = maskedOp != IBaseSQLCondition.NOT_OPERATOR && maskedOp != IBaseSQLCondition.NOT_IN_OPERATOR;
        } else {
            if (value != null && isSelectQuery(value.toString())) {
                if ((op & IBaseSQLCondition.IS_SQL_MODIFIER) == 0) {
                    Debug.warn("Filter is created using a custom query without using the sql-modifier, this will be removed in a future version of servoy, please use operator '" + RelationItem.getOperatorAsString(op | IBaseSQLCondition.IS_SQL_MODIFIER) + "'");
                }
                // add as subquery
                inValues = new QueryCustomSelect(value.toString(), null);
            } else {
                if (value != null && (op & IBaseSQLCondition.IS_SQL_MODIFIER) != 0) {
                    Debug.warn("Filter has the sql-modifier, but the value is not valid sql for filters: " + SQL_QUERY_VALIDATION_MESSAGE + ":" + value);
                }
                inValues = new Object[][] { new Object[] { value } };
            }
        }
        // replace values with column type value
        if (inValues instanceof Object[][]) {
            Object[][] array = (Object[][]) inValues;
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; array[i] != null && j < array[i].length; j++) {
                    Object v = c.getAsRightType(array[i][j]);
                    if (v == null)
                        v = ValueFactory.createNullValue(c.getType());
                    array[i][j] = v;
                }
            }
        }
        filterWhere = new SetCondition(op, new IQuerySelectValue[] { qColumn }, inValues, andCondition);
    } else if (maskedOp == IBaseSQLCondition.BETWEEN_OPERATOR || maskedOp == IBaseSQLCondition.NOT_BETWEEN_OPERATOR) {
        Object op1 = null;
        Object op2 = null;
        if (value instanceof List<?> && ((List<?>) value).size() > 1) {
            op1 = ((List<?>) value).get(0);
            op2 = ((List<?>) value).get(1);
        } else if (value != null && value.getClass().isArray() && ((Object[]) value).length > 1) {
            op1 = ((Object[]) value)[0];
            op2 = ((Object[]) value)[1];
        }
        op1 = c.getAsRightType(op1);
        if (op1 == null)
            op1 = ValueFactory.createNullValue(c.getType());
        op2 = c.getAsRightType(op2);
        if (op2 == null)
            op2 = ValueFactory.createNullValue(c.getType());
        filterWhere = new CompareCondition(op, qColumn, new Object[] { op1, op2 });
    } else {
        Object operand;
        if (maskedOp == IBaseSQLCondition.LIKE_OPERATOR || maskedOp == IBaseSQLCondition.NOT_LIKE_OPERATOR) {
            operand = value;
        } else {
            operand = c.getAsRightType(value);
            if (operand == null)
                operand = ValueFactory.createNullValue(c.getType());
        }
        filterWhere = new CompareCondition(op, qColumn, operand);
    }
    return new QueryFilter(filterWhere);
}
Also used : QueryCustomSelect(com.servoy.j2db.query.QueryCustomSelect) SetCondition(com.servoy.j2db.query.SetCondition) ISQLCondition(com.servoy.j2db.query.ISQLCondition) QueryFilter(com.servoy.j2db.query.QueryFilter) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) List(java.util.List) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) Collectors.toList(java.util.stream.Collectors.toList) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 42 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class SQLGenerator method createAggregateSelect.

public static QuerySelect createAggregateSelect(QuerySelect sqlSelect, Collection<QuerySelect> aggregates, List<Column> pkColumns) {
    QuerySelect selectClone = deepClone(sqlSelect);
    selectClone.clearSorts();
    selectClone.setDistinct(false);
    selectClone.setColumns(null);
    selectClone.removeUnusedJoins(true);
    QuerySelect aggregateSqlSelect;
    if (selectClone.getJoins() == null) {
        // simple case, no joins
        // Select count(pk) from main where <condition>
        aggregateSqlSelect = selectClone;
    } else {
        // we have joins, change to an exists-query to make the aggregates correct, otherwise duplicate records
        // in the main table cause incorrect aggregate values
        // Select count(pk) from main main1 where exists (select 1 from main main2 join detail on detail.FK = main2.FK where main1.PK = main2.PK and <condition>)
        QuerySelect innerSelect = selectClone;
        ArrayList<IQuerySelectValue> innerColumns = new ArrayList<IQuerySelectValue>();
        innerColumns.add(new QueryColumnValue(Integer.valueOf(1), null, true));
        innerSelect.setColumns(innerColumns);
        BaseQueryTable innerTable = innerSelect.getTable();
        QueryTable outerTable = new QueryTable(innerTable.getName(), innerTable.getDataSource(), innerTable.getCatalogName(), innerTable.getSchemaName());
        aggregateSqlSelect = new QuerySelect(outerTable);
        for (Column column : pkColumns) {
            // $NON-NLS-1$
            innerSelect.addCondition(// $NON-NLS-1$
            "EXISTS", new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, column.queryColumn(innerTable), column.queryColumn(outerTable)));
        }
        // $NON-NLS-1$
        aggregateSqlSelect.addCondition("EXISTS", new ExistsCondition(innerSelect, true));
    }
    ArrayList<IQuerySelectValue> columns = new ArrayList<IQuerySelectValue>();
    for (QuerySelect aggregate : aggregates) {
        columns.addAll(AbstractBaseQuery.relinkTable(aggregate.getTable(), aggregateSqlSelect.getTable(), aggregate.getColumnsClone()));
    }
    aggregateSqlSelect.setColumns(columns);
    return aggregateSqlSelect;
}
Also used : ExistsCondition(com.servoy.j2db.query.ExistsCondition) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) CompareCondition(com.servoy.j2db.query.CompareCondition) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 43 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class SQLSheet method getColumnConverterInfo.

public ConverterInfo getColumnConverterInfo(int columnIndex) {
    if (converterInfos == null) {
        SQLDescription desc = sql.get(SELECT);
        List<String> dataProviderIDsDilivery = desc.getDataProviderIDsDilivery();
        ConverterInfo[] cis = new ConverterInfo[dataProviderIDsDilivery.size()];
        for (int i = 0; i < dataProviderIDsDilivery.size(); i++) {
            String cdp = dataProviderIDsDilivery.get(i);
            Column c = table.getColumn(cdp);
            ColumnInfo ci = c.getColumnInfo();
            if (ci != null && ci.getConverterName() != null && ci.getConverterName().trim().length() != 0) {
                Map<String, String> props = null;
                try {
                    props = ComponentFactory.<String>parseJSonProperties(ci.getConverterProperties());
                } catch (IOException e) {
                    Debug.error("Could not parse column converter properties", e);
                }
                cis[i] = new ConverterInfo(ci.getConverterName(), props);
            }
        }
        converterInfos = cis;
    }
    if (columnIndex < 0 || columnIndex >= converterInfos.length) {
        return null;
    }
    return converterInfos[columnIndex];
}
Also used : IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) IOException(java.io.IOException)

Example 44 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class SQLSheet method getColumnValidatorInfo.

public Pair<String, Map<String, String>> getColumnValidatorInfo(int columnIndex) {
    if (validatorInfos == null) {
        SQLDescription desc = sql.get(SELECT);
        List<?> dataProviderIDsDilivery = desc.getDataProviderIDsDilivery();
        @SuppressWarnings("unchecked") Pair<String, Map<String, String>>[] vis = new Pair[dataProviderIDsDilivery.size()];
        int i = 0;
        Iterator<?> it = dataProviderIDsDilivery.iterator();
        while (it.hasNext()) {
            String cdp = (String) it.next();
            Column c = table.getColumn(cdp);
            ColumnInfo ci = c.getColumnInfo();
            if (ci != null && ci.getValidatorName() != null && ci.getValidatorName().trim().length() != 0) {
                Map<String, String> parsedValidatorProperties = null;
                try {
                    parsedValidatorProperties = ComponentFactory.parseJSonProperties(ci.getValidatorProperties());
                } catch (IOException e) {
                    Debug.error(e);
                }
                vis[i] = new Pair<String, Map<String, String>>(ci.getValidatorName(), parsedValidatorProperties);
            }
            i++;
        }
        validatorInfos = vis;
    }
    return validatorInfos[columnIndex];
}
Also used : ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) IOException(java.io.IOException) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Pair(com.servoy.j2db.util.Pair)

Example 45 with Column

use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.

the class ViewFoundSet method getTable.

@Override
public ITable getTable() {
    if (table == null) {
        try {
            table = manager.getTable(getDataSource());
            if (table == null) {
                table = new Table(IServer.VIEW_SERVER, DataSourceUtils.getViewDataSourceName(getDataSource()), true, ITable.VIEW, null, null);
                ((Table) table).setDataSource(getDataSource());
                for (IQuerySelectValue col : select.getColumns()) {
                    Column newCol = null;
                    QueryColumn qCol = col.getColumn();
                    if (qCol != null && qCol.getTable() != null) {
                        ITable colTable = manager.getTable(qCol.getTable().getDataSource());
                        if (colTable != null) {
                            Column column = colTable.getColumn(qCol.getName());
                            if (column != null) {
                                String colname = getColunmName(col, qCol);
                                newCol = table.createNewColumn(DummyValidator.INSTANCE, colname, column.getType(), column.getLength(), column.getScale(), column.getAllowNull());
                                if (column.getColumnInfo() != null) {
                                    DatabaseUtils.createNewColumnInfo(manager.getApplication().getFlattenedSolution().getPersistFactory().getNewElementID(null), newCol, false);
                                    newCol.getColumnInfo().copyFrom(column.getColumnInfo());
                                }
                            }
                        }
                    }
                    if (newCol == null) {
                        // existing database column not found, create column on the fly
                        BaseColumnType columnType = col.getColumnType();
                        if (columnType == null) {
                            columnType = ColumnType.getColumnType(IColumnTypes.TEXT);
                        }
                        String colname = getColunmName(col, qCol);
                        table.createNewColumn(DummyValidator.INSTANCE, colname, columnType.getSqlType(), columnType.getLength(), columnType.getScale(), true);
                    }
                }
            }
        } catch (RepositoryException e) {
            Debug.error(e);
        }
    }
    return table;
}
Also used : BaseQueryTable(com.servoy.base.query.BaseQueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) DerivedTable(com.servoy.j2db.query.DerivedTable) QueryColumn(com.servoy.j2db.query.QueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QueryColumn(com.servoy.j2db.query.QueryColumn) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) BaseColumnType(com.servoy.base.query.BaseColumnType)

Aggregations

Column (com.servoy.j2db.persistence.Column)76 QueryColumn (com.servoy.j2db.query.QueryColumn)44 IColumn (com.servoy.j2db.persistence.IColumn)37 RepositoryException (com.servoy.j2db.persistence.RepositoryException)32 IBaseColumn (com.servoy.base.persistence.IBaseColumn)31 QuerySelect (com.servoy.j2db.query.QuerySelect)29 ArrayList (java.util.ArrayList)29 QueryTable (com.servoy.j2db.query.QueryTable)27 ITable (com.servoy.j2db.persistence.ITable)23 BaseQueryTable (com.servoy.base.query.BaseQueryTable)22 Table (com.servoy.j2db.persistence.Table)22 ServoyException (com.servoy.j2db.util.ServoyException)21 SafeArrayList (com.servoy.j2db.util.SafeArrayList)19 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)18 RemoteException (java.rmi.RemoteException)17 ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)16 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)14 IDataProvider (com.servoy.j2db.persistence.IDataProvider)12 Relation (com.servoy.j2db.persistence.Relation)12 Placeholder (com.servoy.j2db.query.Placeholder)12