Search in sources :

Example 21 with Column

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

the class SQLGenerator method createDynamicPKSetConditionForFoundset.

static SetCondition createDynamicPKSetConditionForFoundset(FoundSet foundSet, BaseQueryTable queryTable, IDataSet pks) {
    Table table = (Table) foundSet.getTable();
    List<Column> rowIdentColumns = table.getRowIdentColumns();
    QueryColumn[] pkQueryColumns = new QueryColumn[rowIdentColumns.size()];
    // getPrimaryKeys from table
    for (int i = 0; i < rowIdentColumns.size(); i++) {
        Column column = rowIdentColumns.get(i);
        pkQueryColumns[i] = column.queryColumn(queryTable);
    }
    // Dynamic PK condition, the special placeholder will be updated when the foundset pk set changes
    Placeholder placeHolder = new Placeholder(new TablePlaceholderKey(queryTable, SQLGenerator.PLACEHOLDER_FOUNDSET_PKS));
    placeHolder.setValue(new DynamicPkValuesArray(rowIdentColumns, pks.clone()));
    return new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, pkQueryColumns, placeHolder, true);
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) 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) SetCondition(com.servoy.j2db.query.SetCondition)

Example 22 with Column

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

the class SQLSheet method getDuplicateRecordData.

public Object[] getDuplicateRecordData(IServiceProvider app, Row toDuplicateRow) {
    Object[] toDuplicate = toDuplicateRow.getRawColumnData();
    SQLDescription desc = getSQLDescription(SELECT);
    List<?> list = desc.getDataProviderIDsDilivery();
    Object[] array = new Object[toDuplicate.length];
    // $NON-NLS-1$
    if (list.size() != array.length)
        throw new IllegalArgumentException("Data to duplicate MUST be created with (help) of this sheet");
    for (int i = 0; i < toDuplicate.length; i++) {
        Object obj = toDuplicate[i];
        if (obj instanceof ValueFactory.BlobMarkerValue) {
            obj = toDuplicateRow.getValue(i);
        } else if (obj instanceof ValueFactory.DbIdentValue && ((DbIdentValue) obj).getRow() == toDuplicateRow) {
            // only create a new db ident value if that db ident value belongs to the duplicated row (== pk db ident instead a a relation db ident that has to be kept!)
            obj = ValueFactory.createDbIdentValue();
        }
        array[i] = obj;
        try {
            Column c = table.getColumn((String) list.get(i));
            ColumnInfo ci = c.getColumnInfo();
            if (c.isDBIdentity()) {
                array[i] = ValueFactory.createDbIdentValue();
            } else if (ci != null && ci.hasSequence()) {
                array[i] = c.getNewRecordValue(app);
            }
            if (ci != null && ci.hasSystemValue()) {
                array[i] = c.getNewRecordValue(app);
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
    return array;
}
Also used : IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) DbIdentValue(com.servoy.j2db.dataprocessing.ValueFactory.DbIdentValue) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Example 23 with Column

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

the class SQLSheet method getDefaultPKSort.

public List<SortColumn> getDefaultPKSort() {
    if (table == null) {
        return null;
    }
    if (defaultSort == null) {
        List<SortColumn> ds = null;
        if (table.getRowIdentColumns().size() > 1) {
            // find a match with an index
            for (IndexInfo index : Utils.iterate(table.getIndexes())) {
                if (index.isUnique()) {
                    List<SortColumn> indexCols = index.getColumns();
                    List<Column> rowIdentColumnsCopy = new ArrayList<Column>(table.getRowIdentColumns());
                    boolean match = indexCols.size() == rowIdentColumnsCopy.size();
                    for (int c = 0; match && c < indexCols.size(); c++) {
                        match = rowIdentColumnsCopy.remove(indexCols.get(c).getColumn());
                    }
                    if (match) {
                        ds = indexCols;
                        break;
                    }
                }
            }
        }
        if (// no match on index
        ds == null) {
            ds = new ArrayList<SortColumn>(table.getRowIdentColumns().size());
            // get key columns in db defined order
            for (Column column : table.getColumns()) {
                if (table.getRowIdentColumns().contains(column)) {
                    ds.add(new SortColumn(column));
                }
            }
        }
        defaultSort = ds;
    }
    return defaultSort;
}
Also used : IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList)

Example 24 with Column

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

the class SQLSheet method getNewRowData.

/**
 * Returns raw (not using column converters) row data for a new record
 * @param app
 * @param fs
 * @return
 */
Object[] getNewRowData(IServiceProvider app, FoundSet fs) {
    Object[][] creationArgs = null;
    // INSERT
    SQLDescription desc = getSQLDescription(SELECT);
    // RequiredDataProviderIDs();
    List<?> list = desc.getDataProviderIDsDilivery();
    Column[] fcols = null;
    Relation relation = null;
    String relationName = fs.getRelationName();
    if (relationName != null) {
        try {
            relation = app.getFlattenedSolution().getRelation(relationName);
            if (relation != null) {
                fcols = relation.getForeignColumns(app.getFlattenedSolution());
                QuerySelect creationSQLString = fs.getCreationSqlSelect();
                Placeholder ph = creationSQLString.getPlaceholder(SQLGenerator.createRelationKeyPlaceholderKey(creationSQLString.getTable(), relation.getName()));
                if (ph != null && ph.isSet()) {
                    // a matrix as wide as the relation keys and 1 deep
                    creationArgs = (Object[][]) ph.getValue();
                }
            }
        } catch (RepositoryException e) {
            Debug.error(e);
        }
    }
    Object[] array = new Object[list.size()];
    for (int i = 0; i < list.size(); i++) {
        try {
            boolean filled = false;
            Column c = table.getColumn((String) list.get(i));
            if (c.isDBIdentity()) {
                array[i] = ValueFactory.createDbIdentValue();
                filled = true;
            } else {
                ColumnInfo ci = c.getColumnInfo();
                if (c.getRowIdentType() != IBaseColumn.NORMAL_COLUMN && ci != null && ci.hasSequence()) {
                    // this is here for safety, it can happen that a form has (unwanted) still a related foundset which is created by relation based on primary key
                    array[i] = c.getNewRecordValue(app);
                    filled = true;
                } else {
                    if (// created via relation, so fill the foreign key with foreign value
                    creationArgs != null && creationArgs.length != 0 && fcols != null) {
                        for (int j = 0; j < fcols.length; j++) {
                            if (c.equals(fcols[j]) && ((relation.getOperators()[j] & IBaseSQLCondition.OPERATOR_MASK) == IBaseSQLCondition.EQUALS_OPERATOR)) {
                                // creationArgs is a matrix as wide as the relation keys and 1 deep
                                array[i] = creationArgs[j][0];
                                filled = true;
                                break;
                            }
                        }
                    }
                }
            }
            if (!filled) {
                array[i] = c.getNewRecordValue(app);
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
    return array;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Relation(com.servoy.j2db.persistence.Relation) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column)

Example 25 with Column

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

the class SQLSheet method processCopyValues.

void processCopyValues(IRecordInternal s) {
    SQLDescription desc = getSQLDescription(SELECT);
    if (desc == null) {
        return;
    }
    List<?> list = desc.getDataProviderIDsDilivery();
    for (int i = 0; i < list.size(); i++) {
        try {
            String id = (String) list.get(i);
            Column c = table.getColumn(id);
            if (c != null) {
                ColumnInfo ci = c.getColumnInfo();
                if (ci != null && ci.getAutoEnterType() == ColumnInfo.LOOKUP_VALUE_AUTO_ENTER) {
                    String lookupDataProviderID = ci.getLookupValue();
                    Object obj = s.getValue(lookupDataProviderID);
                    if (ScopesUtils.isVariableScope(lookupDataProviderID) && !s.has(lookupDataProviderID)) {
                        ScriptMethod globalScriptMethod = application.getFlattenedSolution().getScriptMethod(null, lookupDataProviderID);
                        if (globalScriptMethod != null) {
                            try {
                                IServer server = application.getSolution().getServer(table.getServerName());
                                obj = application.getScriptEngine().getScopesScope().executeGlobalFunction(globalScriptMethod.getScopeName(), globalScriptMethod.getName(), new Object[] { new JSColumn(c, server, table) }, false, false);
                            } catch (Exception e) {
                                Debug.error(e);
                            }
                        }
                    }
                    // Protect to writing null to null-protected columns. An exception gets written in the log.
                    if (!((obj == null) && !c.getAllowNull()))
                        s.setValue(id, obj, false);
                }
            }
        } catch (Exception ex) {
            Debug.error(ex);
        }
    }
}
Also used : IServer(com.servoy.j2db.persistence.IServer) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

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