Search in sources :

Example 6 with QueryColumnValue

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

the class SQLGenerator method createRelatedCondition.

public static ISQLCondition createRelatedCondition(IServiceProvider app, Relation relation, QueryTable foreignTable) throws RepositoryException {
    IDataProvider[] primary = relation.getPrimaryDataProviders(app.getFlattenedSolution());
    Column[] foreign = relation.getForeignColumns(app.getFlattenedSolution());
    int[] operators = relation.getOperators();
    IQuerySelectValue[] keys = new IQuerySelectValue[primary.length];
    int[] swapped = new int[primary.length];
    for (int x = 0; x < primary.length; x++) {
        // need all keys as columns on the left side......
        int operator = RelationItem.swapOperator(operators[x]);
        if (operator == -1) {
            throw new RepositoryException("Cannot swap relation operator for relation " + relation.getName());
        }
        // column = ? construct
        IQuerySelectValue key = foreign[x].queryColumn(foreignTable);
        // When we have a text and non-text column we can cast the non-text column to string
        int primaryType = primary[x].getDataProviderType();
        int foreignType = mapToDefaultType(key.getColumn().getColumnType());
        if (!"uuid".equalsIgnoreCase(key.getColumn().getNativeTypename()) && foreignType == IColumnTypes.TEXT && primaryType != IColumnTypes.TEXT && primaryType != 0) {
            // key is text, value is non-text, cast the value to text when we supply it
            operator |= IBaseSQLCondition.CAST_TO_MODIFIER;
        } else if (primaryType == IColumnTypes.TEXT && foreignType != IColumnTypes.TEXT) {
            // value is text, key is non-text, cast the key to text
            key = new QueryFunction(cast, new IQuerySelectValue[] { key, new QueryColumnValue(IQueryConstants.TYPE_STRING, null, true) }, null);
        }
        keys[x] = key;
        swapped[x] = operator;
    }
    return new SetCondition(swapped, keys, new Placeholder(createRelationKeyPlaceholderKey(foreignTable, relation.getName())), true);
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IDataProvider(com.servoy.j2db.persistence.IDataProvider) SetCondition(com.servoy.j2db.query.SetCondition) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QueryFunction(com.servoy.j2db.query.QueryFunction) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 7 with QueryColumnValue

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

the class SQLGenerator method createAggregateSelect.

/**
 * Create the sql for a single aggregate on a column.
 *
 * @param aggregee
 * @return
 */
public static QuerySelect createAggregateSelect(int aggregateType, ITable table, Object aggregee) {
    Column column = null;
    if (aggregee instanceof Column) {
        column = (Column) aggregee;
    }
    QuerySelect select = new QuerySelect(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()));
    select.addColumn(new QueryAggregate(aggregateType, // $NON-NLS-1$
    (column == null) ? // $NON-NLS-1$
    (IQuerySelectValue) new QueryColumnValue(aggregee, "n", aggregee instanceof Integer || QueryAggregate.ASTERIX.equals(aggregee)) : column.queryColumn(select.getTable()), // $NON-NLS-1$
    "maxval"));
    return select;
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable)

Example 8 with QueryColumnValue

use of com.servoy.j2db.query.QueryColumnValue 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 9 with QueryColumnValue

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

the class I18NUtil method loadSortedMessagesFromRepository.

public static TreeMap<String, MessageEntry> loadSortedMessagesFromRepository(IRepository repository, IDataServer dataServer, String clientID, String i18NServerName, String i18NTableName, String filterName, String[] filterValue, IFoundSetManagerInternal fm) throws Exception {
    TreeMap<String, MessageEntry> sortedMessages = new TreeMap<String, MessageEntry>();
    IServer i18NServer = repository.getServer(i18NServerName);
    if (i18NServer != null) {
        Table i18NTable = (Table) i18NServer.getTable(i18NTableName);
        if (i18NTable != null) {
            QueryTable messagesTable = new QueryTable(i18NTable.getSQLName(), i18NTable.getDataSource(), i18NTable.getCatalog(), i18NTable.getSchema());
            QuerySelect sql = new QuerySelect(messagesTable);
            QueryColumn msgLang = new QueryColumn(messagesTable, -1, "message_language", Types.VARCHAR, 150, 0, null, 0);
            QueryColumn msgKey = new QueryColumn(messagesTable, -1, "message_key", Types.VARCHAR, 150, 0, null, 0);
            QueryColumn msgVal = new QueryColumn(messagesTable, -1, "message_value", Types.VARCHAR, 2000, 0, null, 0);
            sql.addColumn(msgLang);
            sql.addColumn(msgKey);
            sql.addColumn(msgVal);
            // Filter to only include records with the default (null) value for columns flagged as Tenant column
            for (Column column : i18NTable.getTenantColumns()) {
                QueryColumn tenantColumn = new QueryColumn(messagesTable, column.getID(), column.getSQLName(), column.getType(), column.getLength(), column.getScale(), null, column.getFlags());
                CompareCondition cc = new CompareCondition(IBaseSQLCondition.ISNULL_OPERATOR, tenantColumn, null);
                sql.addCondition("_svy_tenant_id_filter_" + column.getName(), cc);
            }
            if (filterName != null) {
                Column filterColumn = i18NTable.getColumn(filterName);
                if (filterColumn != null && filterValue != null && filterValue.length > 0) {
                    QueryColumn columnFilter = new QueryColumn(messagesTable, filterColumn.getID(), filterColumn.getSQLName(), filterColumn.getType(), filterColumn.getLength(), filterColumn.getScale(), null, filterColumn.getFlags());
                    CompareCondition cc = new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, columnFilter, new QueryColumnValue(filterValue[0], null));
                    // $NON-NLS-1$
                    sql.addCondition("FILTER", cc);
                }
            }
            sql.addSort(new QuerySort(msgLang, true, SortOptions.NONE));
            sql.addSort(new QuerySort(msgKey, true, SortOptions.NONE));
            IDataSet set = dataServer.performQuery(clientID, i18NServerName, null, sql, null, fm != null ? fm.getTableFilterParams(i18NServerName, sql) : null, false, 0, Integer.MAX_VALUE, IDataServer.MESSAGES_QUERY);
            int rowCount = set.getRowCount();
            if (rowCount > 0) {
                for (int i = 0; i < rowCount; i++) {
                    Object[] row = set.getRow(i);
                    MessageEntry messageEntry = new MessageEntry((String) row[0], (String) row[1], (String) row[2]);
                    sortedMessages.put(messageEntry.getLanguageKey(), messageEntry);
                }
            }
        }
    }
    return sortedMessages;
}
Also used : QueryTable(com.servoy.j2db.query.QueryTable) TreeMap(java.util.TreeMap) QuerySelect(com.servoy.j2db.query.QuerySelect) QueryTable(com.servoy.j2db.query.QueryTable) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) QueryColumn(com.servoy.j2db.query.QueryColumn) QueryColumn(com.servoy.j2db.query.QueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) QuerySort(com.servoy.j2db.query.QuerySort) IDataSet(com.servoy.j2db.dataprocessing.IDataSet)

Example 10 with QueryColumnValue

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

the class I18NUtil method writeMessagesToRepository.

public static void writeMessagesToRepository(String i18NServerName, String i18NTableName, IRepository repository, IDataServer dataServer, String clientID, TreeMap<String, MessageEntry> messages, boolean noUpdates, boolean noRemoves, TreeMap<String, MessageEntry> remoteMessages, String filterName, String[] filterValue, IFoundSetManagerInternal fm) throws Exception {
    // get remote messages snapshot
    if (remoteMessages == null)
        remoteMessages = loadSortedMessagesFromRepository(repository, dataServer, clientID, i18NServerName, i18NTableName, filterName, filterValue, fm);
    if (remoteMessages != null) {
        IServer i18NServer = repository.getServer(i18NServerName);
        Table i18NTable = null;
        if (i18NServer != null) {
            i18NTable = (Table) i18NServer.getTable(i18NTableName);
        }
        if (i18NTable != null) {
            // runtime exception when no ident columns
            Column pkColumn = i18NTable.getRowIdentColumns().get(0);
            QueryTable messagesTable = new QueryTable(i18NTable.getSQLName(), i18NTable.getDataSource(), i18NTable.getCatalog(), i18NTable.getSchema());
            QueryColumn pkCol = pkColumn.queryColumn(messagesTable);
            QueryColumn msgLang = new QueryColumn(messagesTable, -1, "message_language", Types.VARCHAR, 150, 0, null, 0);
            QueryColumn msgKey = new QueryColumn(messagesTable, -1, "message_key", Types.VARCHAR, 150, 0, null, 0);
            QueryColumn msgVal = new QueryColumn(messagesTable, -1, "message_value", Types.VARCHAR, 2000, 0, null, 0);
            ArrayList<SQLStatement> updateStatements = new ArrayList<SQLStatement>();
            // go thorough messages, update exiting, add news to remote
            // in case we need to insert a record, we must know if it is database managed or servoy managed
            boolean logIdIsServoyManaged = false;
            ColumnInfo ci = pkColumn.getColumnInfo();
            if (ci != null) {
                int autoEnterType = ci.getAutoEnterType();
                int autoEnterSubType = ci.getAutoEnterSubType();
                logIdIsServoyManaged = (autoEnterType == ColumnInfo.SEQUENCE_AUTO_ENTER) && (autoEnterSubType != ColumnInfo.NO_SEQUENCE_SELECTED) && (autoEnterSubType != ColumnInfo.DATABASE_IDENTITY);
            }
            List<Column> tenantColumns = i18NTable.getTenantColumns();
            Iterator<Map.Entry<String, MessageEntry>> messagesIte = messages.entrySet().iterator();
            Map.Entry<String, MessageEntry> messageEntry;
            while (messagesIte.hasNext()) {
                messageEntry = messagesIte.next();
                String key = messageEntry.getKey();
                String value = messageEntry.getValue().getValue();
                String lang = messageEntry.getValue().getLanguage();
                if (lang.equals(""))
                    lang = null;
                String messageKey = messageEntry.getValue().getKey();
                if (// insert
                !remoteMessages.containsKey(key)) {
                    QueryInsert insert = new QueryInsert(messagesTable);
                    QueryColumn[] insertColumns = null;
                    Object[] insertColumnValues = null;
                    if (logIdIsServoyManaged) {
                        Object messageId = dataServer.getNextSequence(i18NServerName, i18NTableName, pkColumn.getName(), -1, i18NServerName);
                        if (lang == null) {
                            insertColumns = new QueryColumn[] { pkCol, msgKey, msgVal };
                            insertColumnValues = new Object[] { messageId, messageKey, value };
                        } else {
                            insertColumns = new QueryColumn[] { pkCol, msgKey, msgLang, msgVal };
                            insertColumnValues = new Object[] { messageId, messageKey, lang, value };
                        }
                    } else {
                        if (lang == null) {
                            insertColumns = new QueryColumn[] { msgKey, msgVal };
                            insertColumnValues = new Object[] { messageKey, value };
                        } else {
                            insertColumns = new QueryColumn[] { msgKey, msgLang, msgVal };
                            insertColumnValues = new Object[] { messageKey, lang, value };
                        }
                    }
                    Column filterColumn = i18NTable.getColumn(filterName);
                    if (filterColumn != null && filterValue != null && filterValue.length > 0) {
                        insertColumns = Utils.arrayAdd(insertColumns, filterColumn.queryColumn(messagesTable), true);
                        insertColumnValues = Utils.arrayAdd(insertColumnValues, filterValue[0], true);
                    }
                    insert.setColumnValues(insertColumns, insertColumnValues);
                    updateStatements.add(new SQLStatement(ISQLActionTypes.INSERT_ACTION, i18NServerName, i18NTableName, null, null, insert, null));
                } else if (// update
                !remoteMessages.get(key).getValue().equals(value) && !noUpdates) {
                    QueryUpdate update = new QueryUpdate(messagesTable);
                    update.addValue(msgVal, value);
                    update.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, msgKey, messageKey));
                    update.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, msgLang, lang));
                    if (filterName != null) {
                        Column filterColumn = i18NTable.getColumn(filterName);
                        if (filterColumn != null && filterValue != null && filterValue.length > 0) {
                            QueryColumn columnFilter = filterColumn.queryColumn(messagesTable);
                            CompareCondition cc = new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, columnFilter, new QueryColumnValue(filterValue[0], null));
                            // $NON-NLS-1$
                            update.addCondition("FILTER", cc);
                        }
                    }
                    // Add condition to update only records having the default tenant value (null)
                    for (Column column : tenantColumns) {
                        QueryColumn tenantColumn = column.queryColumn(messagesTable);
                        CompareCondition cc = new CompareCondition(IBaseSQLCondition.ISNULL_OPERATOR, tenantColumn, null);
                        update.addCondition(cc);
                    }
                    updateStatements.add(new SQLStatement(ISQLActionTypes.UPDATE_ACTION, i18NServerName, i18NTableName, null, null, update, fm != null ? fm.getTableFilterParams(i18NServerName, update) : null));
                }
            }
            if (!noRemoves) {
                // go thorough remote, remove if not existing locally
                Iterator<Map.Entry<String, MessageEntry>> remoteMessagesIte = remoteMessages.entrySet().iterator();
                Map.Entry<String, MessageEntry> remoteMessageEntry;
                while (remoteMessagesIte.hasNext()) {
                    remoteMessageEntry = remoteMessagesIte.next();
                    String key = remoteMessageEntry.getKey();
                    if (// delete
                    !messages.containsKey(key)) {
                        String lang = remoteMessageEntry.getValue().getLanguage();
                        if (lang.equals(""))
                            lang = null;
                        String messageKey = remoteMessageEntry.getValue().getKey();
                        QueryDelete delete = new QueryDelete(messagesTable);
                        delete.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, msgKey, messageKey));
                        delete.addCondition(new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, msgLang, lang));
                        if (filterName != null) {
                            Column filterColumn = i18NTable.getColumn(filterName);
                            if (filterColumn != null && filterValue != null && filterValue.length > 0) {
                                QueryColumn columnFilter = filterColumn.queryColumn(messagesTable);
                                CompareCondition cc = new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, columnFilter, new QueryColumnValue(filterValue[0], null));
                                delete.addCondition(cc);
                            }
                        }
                        // Add condition to remove only records having the default tenant value (null)
                        for (Column column : tenantColumns) {
                            QueryColumn tenantColumn = column.queryColumn(messagesTable);
                            CompareCondition cc = new CompareCondition(IBaseSQLCondition.ISNULL_OPERATOR, tenantColumn, null);
                            delete.addCondition(cc);
                        }
                        updateStatements.add(new SQLStatement(ISQLActionTypes.DELETE_ACTION, i18NServerName, i18NTableName, null, null, delete, fm != null ? fm.getTableFilterParams(i18NServerName, delete) : null));
                    }
                }
            }
            for (SQLStatement st : updateStatements) {
                st.setDataType(ISQLStatement.I18N_DATA_TYPE);
            }
            dataServer.performUpdates(clientID, updateStatements.toArray(new ISQLStatement[updateStatements.size()]));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ISQLStatement(com.servoy.j2db.dataprocessing.ISQLStatement) SQLStatement(com.servoy.j2db.dataprocessing.SQLStatement) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) QueryColumn(com.servoy.j2db.query.QueryColumn) CompareCondition(com.servoy.j2db.query.CompareCondition) QueryInsert(com.servoy.j2db.query.QueryInsert) ISQLStatement(com.servoy.j2db.dataprocessing.ISQLStatement) QueryTable(com.servoy.j2db.query.QueryTable) QueryDelete(com.servoy.j2db.query.QueryDelete) QueryTable(com.servoy.j2db.query.QueryTable) QueryColumn(com.servoy.j2db.query.QueryColumn) QueryUpdate(com.servoy.j2db.query.QueryUpdate) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

QueryColumnValue (com.servoy.j2db.query.QueryColumnValue)11 QueryColumn (com.servoy.j2db.query.QueryColumn)9 QueryTable (com.servoy.j2db.query.QueryTable)8 QuerySelect (com.servoy.j2db.query.QuerySelect)7 Column (com.servoy.j2db.persistence.Column)6 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)5 CompareCondition (com.servoy.j2db.query.CompareCondition)5 BaseQueryTable (com.servoy.base.query.BaseQueryTable)4 IColumn (com.servoy.j2db.persistence.IColumn)4 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)4 IDataSet (com.servoy.j2db.dataprocessing.IDataSet)3 Placeholder (com.servoy.j2db.query.Placeholder)3 SetCondition (com.servoy.j2db.query.SetCondition)3 ArrayList (java.util.ArrayList)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ExistsCondition (com.servoy.j2db.query.ExistsCondition)2 QueryAggregate (com.servoy.j2db.query.QueryAggregate)2 TablePlaceholderKey (com.servoy.j2db.query.TablePlaceholderKey)2 SafeArrayList (com.servoy.j2db.util.SafeArrayList)2 TreeMap (java.util.TreeMap)2