Search in sources :

Example 16 with ColumnInfo

use of com.servoy.j2db.persistence.ColumnInfo 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 17 with ColumnInfo

use of com.servoy.j2db.persistence.ColumnInfo 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 18 with ColumnInfo

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

the class FoundSetManager method getConvertedTypeForColumn.

@Override
public int getConvertedTypeForColumn(IColumn column, boolean mapToDefaultType) {
    int type = mapToDefaultType ? column.getDataProviderType() : (column instanceof Column ? ((Column) column).getType() : column.getDataProviderType());
    ColumnInfo ci = column.getColumnInfo();
    if (ci != null && ci.getConverterName() != null && ci.getConverterName().trim().length() != 0) {
        IColumnConverter columnConverter = ((FoundSetManager) application.getFoundSetManager()).getColumnConverterManager().getConverter(ci.getConverterName());
        if (columnConverter instanceof ITypedColumnConverter) {
            try {
                int convType = ((ITypedColumnConverter) columnConverter).getToObjectType(ComponentFactory.<String>parseJSonProperties(ci.getConverterProperties()));
                if (convType != Integer.MAX_VALUE) {
                    type = Column.mapToDefaultType(convType);
                }
            } catch (IOException e) {
                Debug.error("Exception loading properties for converter " + columnConverter.getName() + ", properties: " + ci.getConverterProperties(), e);
            }
        }
    }
    return type;
}
Also used : Column(com.servoy.j2db.persistence.Column) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) IOException(java.io.IOException)

Example 19 with ColumnInfo

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

the class RowManager method getRowUpdateInfo.

RowUpdateInfo getRowUpdateInfo(Row row, boolean tracking) throws ServoyException {
    try {
        if (row.getRowManager() != this) {
            // $NON-NLS-1$
            throw new IllegalArgumentException("I'm not the row manager from row");
        }
        if (adjustingForChangeByOtherPKHashKey.get() != null && adjustingForChangeByOtherPKHashKey.get().equals(row.getPKHashKey())) {
            row.flagExistInDB();
            // we ignore changes here because stored calc with time element are always changed,resulting in endlessloop between clients
            return null;
        }
        if (row.getLastException() instanceof DataException) {
            // cannot update an row which is not changed (which clears the dataexception)
            return null;
        }
        if (!row.isChanged())
            return null;
        boolean mustRequeryRow = false;
        List<Column> dbPKReturnValues = new ArrayList<Column>();
        SQLSheet.SQLDescription sqlDesc = null;
        int statement_action;
        ISQLUpdate sqlUpdate = null;
        IServer server = fsm.getApplication().getSolution().getServer(sheet.getServerName());
        boolean oracleServer = SQLSheet.isOracleServer(server);
        boolean usesLobs = false;
        Table table = sheet.getTable();
        boolean doesExistInDB = row.existInDB();
        List<String> aggregatesToRemove = new ArrayList<String>(8);
        List<String> changedColumns = null;
        if (doesExistInDB) {
            statement_action = ISQLActionTypes.UPDATE_ACTION;
            sqlDesc = sheet.getSQLDescription(SQLSheet.UPDATE);
            sqlUpdate = (QueryUpdate) AbstractBaseQuery.deepClone(sqlDesc.getSQLQuery());
            List<String> req = sqlDesc.getRequiredDataProviderIDs();
            List<String> old = sqlDesc.getOldRequiredDataProviderIDs();
            Object[] olddata = row.getRawOldColumnData();
            if (// for safety only, nothing changed
            olddata == null) {
                return null;
            }
            Object[] newdata = row.getRawColumnData();
            for (int i = 0; i < olddata.length; i++) {
                String dataProviderID = req.get(i);
                Column c = table.getColumn(dataProviderID);
                ColumnInfo ci = c.getColumnInfo();
                if (ci != null && ci.isDBManaged()) {
                    mustRequeryRow = true;
                } else {
                    Object modificationValue = c.getModificationValue(fsm.getApplication());
                    if (modificationValue != null) {
                        row.setRawValue(dataProviderID, modificationValue);
                    }
                    if (newdata[i] instanceof BlobMarkerValue) {
                        // because that would be a byte[]
                        continue;
                    }
                    if (!Utils.equalObjects(olddata[i], newdata[i])) {
                        if (sheet.isUsedByAggregate(dataProviderID)) {
                            aggregatesToRemove.addAll(sheet.getAggregateName(dataProviderID));
                        }
                        Object robj = c.getAsRightType(newdata[i]);
                        if (robj == null)
                            robj = ValueFactory.createNullValue(c.getType());
                        ((QueryUpdate) sqlUpdate).addValue(c.queryColumn(((QueryUpdate) sqlUpdate).getTable()), robj);
                        if (changedColumns == null) {
                            changedColumns = new ArrayList<String>(olddata.length - i);
                        }
                        changedColumns.add(c.getName());
                        if (oracleServer && !usesLobs) {
                            int type = c.getType();
                            if (type == Types.BLOB && robj instanceof byte[] && ((byte[]) robj).length > 4000) {
                                usesLobs = true;
                            } else if (type == Types.CLOB && robj instanceof String && ((String) robj).length() > 4000) {
                                usesLobs = true;
                            }
                        }
                    }
                }
            }
            if (// nothing changed after all
            changedColumns == null) {
                // clear the old data now else it will be kept and in a changed state.
                row.flagExistInDB();
                return null;
            }
            // add PK
            Object[] pkValues = new Object[old.size()];
            for (int j = 0; j < old.size(); j++) {
                String dataProviderID = old.get(j);
                pkValues[j] = row.getOldRequiredValue(dataProviderID);
            }
            // TODO: check for success
            AbstractBaseQuery.setPlaceholderValue(sqlUpdate, new TablePlaceholderKey(((QueryUpdate) sqlUpdate).getTable(), SQLGenerator.PLACEHOLDER_PRIMARY_KEY), pkValues);
        } else {
            List<Object> argsArray = new ArrayList<Object>();
            statement_action = ISQLActionTypes.INSERT_ACTION;
            sqlDesc = sheet.getSQLDescription(SQLSheet.INSERT);
            sqlUpdate = (ISQLUpdate) AbstractBaseQuery.deepClone(sqlDesc.getSQLQuery());
            List<String> req = sqlDesc.getRequiredDataProviderIDs();
            if (Debug.tracing())
                Debug.trace(sqlUpdate.toString());
            for (int i = 0; i < req.size(); i++) {
                String dataProviderID = req.get(i);
                if (sheet.isUsedByAggregate(dataProviderID)) {
                    aggregatesToRemove.addAll(sheet.getAggregateName(dataProviderID));
                }
                Column c = table.getColumn(dataProviderID);
                QueryColumn queryColumn = c.queryColumn(((QueryInsert) sqlUpdate).getTable());
                ColumnInfo ci = c.getColumnInfo();
                if (c.isDBIdentity()) {
                    dbPKReturnValues.add(c);
                    argsArray.add(row.getDbIdentValue());
                } else if (ci != null && ci.isDBManaged()) {
                    mustRequeryRow = true;
                } else {
                    int columnIndex = getSQLSheet().getColumnIndex(dataProviderID);
                    // HACK: DIRTY way, should use some kind of identifier preferably
                    if (c.getDatabaseDefaultValue() != null && row.getRawValue(columnIndex, false) == null && c.getRowIdentType() == IBaseColumn.NORMAL_COLUMN) {
                        // The database has a default value, and the value is null, and this is an insert...
                        // Remove the column from the query entirely and make sure the default value is requeried from the db.
                        mustRequeryRow = true;
                        ((QueryInsert) sqlUpdate).removeColumn(queryColumn);
                    } else {
                        Object robj = c.getAsRightType(row.getRawValue(columnIndex, false));
                        if (robj == null)
                            robj = ValueFactory.createNullValue(c.getType());
                        argsArray.add(robj);
                        if (oracleServer && !usesLobs) {
                            int type = c.getType();
                            if (type == Types.BLOB && robj instanceof byte[] && ((byte[]) robj).length > 4000) {
                                usesLobs = true;
                            } else if (type == Types.CLOB && robj instanceof String && ((String) robj).length() > 4000) {
                                usesLobs = true;
                            }
                        }
                    }
                }
            }
            AbstractBaseQuery.setPlaceholderValue(sqlUpdate, new TablePlaceholderKey(((QueryInsert) sqlUpdate).getTable(), SQLGenerator.PLACEHOLDER_INSERT_KEY), argsArray.toArray());
        }
        Object[] pk = row.getPK();
        IDataSet pks = new BufferedDataSet();
        pks.addRow(pk);
        String tid = null;
        GlobalTransaction gt = fsm.getGlobalTransaction();
        if (gt != null) {
            tid = gt.getTransactionID(sheet.getServerName());
        }
        QuerySelect requerySelect = null;
        if (mustRequeryRow) {
            requerySelect = (QuerySelect) AbstractBaseQuery.deepClone(sheet.getSQL(SQLSheet.SELECT));
            if (!requerySelect.setPlaceholderValue(new TablePlaceholderKey(requerySelect.getTable(), SQLGenerator.PLACEHOLDER_PRIMARY_KEY), pk)) {
                Debug.error(new RuntimeException(// $NON-NLS-1$
                "Could not set placeholder " + new TablePlaceholderKey(requerySelect.getTable(), SQLGenerator.PLACEHOLDER_PRIMARY_KEY) + " in query " + requerySelect + // $NON-NLS-1$//$NON-NLS-2$
                "-- continuing"));
            }
        }
        SQLStatement statement = new SQLStatement(statement_action, sheet.getServerName(), table.getName(), pks, tid, sqlUpdate, fsm.getTableFilterParams(sheet.getServerName(), sqlUpdate), requerySelect);
        // check that the row is updated (skip check for insert)
        if (doesExistInDB)
            statement.setExpectedUpdateCount(1);
        if (changedColumns != null) {
            statement.setChangedColumns(changedColumns.toArray(new String[changedColumns.size()]));
        }
        statement.setOracleFixTrackingData(usesLobs && !tracking);
        statement.setIdentityColumn(dbPKReturnValues.size() == 0 ? null : dbPKReturnValues.get(0));
        if (tracking || usesLobs) {
            statement.setTrackingData(sheet.getColumnNames(), row.getRawOldColumnData() != null ? new Object[][] { row.getRawOldColumnData() } : null, row.getRawColumnData() != null ? new Object[][] { row.getRawColumnData() } : null, fsm.getApplication().getUserUID(), fsm.getTrackingInfo(), fsm.getApplication().getClientID());
        }
        return new RowUpdateInfo(row, statement, dbPKReturnValues, aggregatesToRemove);
    } catch (RemoteException e) {
        throw new RepositoryException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) QueryColumn(com.servoy.j2db.query.QueryColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QueryInsert(com.servoy.j2db.query.QueryInsert) IServer(com.servoy.j2db.persistence.IServer) QueryTable(com.servoy.j2db.query.QueryTable) Table(com.servoy.j2db.persistence.Table) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect) ISQLUpdate(com.servoy.j2db.query.ISQLUpdate) BlobMarkerValue(com.servoy.j2db.dataprocessing.ValueFactory.BlobMarkerValue) QueryColumn(com.servoy.j2db.query.QueryColumn) QueryUpdate(com.servoy.j2db.query.QueryUpdate) RemoteException(java.rmi.RemoteException)

Example 20 with ColumnInfo

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

the class ComponentFormat method getComponentFormat.

public static ComponentFormat getComponentFormat(String format, IDataProvider dataProvider, IServiceProvider application, boolean autoFillMaxLength) {
    int dpType = IColumnTypes.TEXT;
    String formatProperty = format;
    if (dataProvider != null) {
        dpType = dataProvider.getDataProviderType();
        IColumn column = null;
        if (dataProvider instanceof ColumnWrapper) {
            column = ((ColumnWrapper) dataProvider).getColumn();
        } else if (dataProvider instanceof Column) {
            column = (Column) dataProvider;
        } else if (dataProvider instanceof ScriptCalculation) {
            // When it is a stored calculation, the name of the calc is the name of he column
            ScriptCalculation calc = (ScriptCalculation) dataProvider;
            try {
                ITable table = calc.getTable();
                if (table != null) {
                    column = table.getColumn(calc.getName());
                }
            } catch (RepositoryException e) {
                Debug.error(e);
            }
        }
        if (column instanceof AggregateVariable) {
            Column columnToAggregate = null;
            try {
                ITable table = column.getTable();
                if (table != null) {
                    columnToAggregate = table.getColumn(((AggregateVariable) column).getDataProviderIDToAggregate());
                }
            } catch (RepositoryException e) {
                Debug.error(e);
            }
            // Use aggregated column when they are of the same type (so not count(textcolumn))
            if (columnToAggregate != null && column.getDataProviderType() == columnToAggregate.getDataProviderType()) {
                column = columnToAggregate;
            }
        }
        if (column instanceof Column) {
            ColumnInfo ci = ((Column) column).getColumnInfo();
            if (ci != null) {
                if (formatProperty == null || formatProperty.length() == 0) {
                    if (ci.getDefaultFormat() != null && ci.getDefaultFormat().length() > 0) {
                        formatProperty = ci.getDefaultFormat();
                    }
                }
                dpType = application.getFoundSetManager().getConvertedTypeForColumn(column, true);
            }
        }
    }
    ComponentFormat componentFormat = ComponentFormat.getComponentFormat(formatProperty, dpType, application);
    if (autoFillMaxLength && dataProvider != null && dataProvider.getLength() > 0 && componentFormat.parsedFormat != null && componentFormat.parsedFormat.getMaxLength() == null && (dpType == IColumnTypes.TEXT || dpType == IColumnTypes.MEDIA)) {
        componentFormat.parsedFormat.updateMaxLength(Integer.valueOf(dataProvider.getLength()));
    }
    return componentFormat;
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) IColumn(com.servoy.j2db.persistence.IColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) ColumnWrapper(com.servoy.j2db.persistence.ColumnWrapper) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable)

Aggregations

ColumnInfo (com.servoy.j2db.persistence.ColumnInfo)21 Column (com.servoy.j2db.persistence.Column)14 RepositoryException (com.servoy.j2db.persistence.RepositoryException)9 IBaseColumn (com.servoy.base.persistence.IBaseColumn)8 IColumn (com.servoy.j2db.persistence.IColumn)8 IOException (java.io.IOException)6 ITable (com.servoy.j2db.persistence.ITable)5 QueryColumn (com.servoy.j2db.query.QueryColumn)5 IDataProvider (com.servoy.j2db.persistence.IDataProvider)4 Table (com.servoy.j2db.persistence.Table)4 ServoyException (com.servoy.j2db.util.ServoyException)4 ArrayList (java.util.ArrayList)4 IServer (com.servoy.j2db.persistence.IServer)3 Placeholder (com.servoy.j2db.query.Placeholder)3 QuerySelect (com.servoy.j2db.query.QuerySelect)3 QueryTable (com.servoy.j2db.query.QueryTable)3 TablePlaceholderKey (com.servoy.j2db.query.TablePlaceholderKey)3 SafeArrayList (com.servoy.j2db.util.SafeArrayList)3 RemoteException (java.rmi.RemoteException)3 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)2