Search in sources :

Example 11 with ColumnInfo

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

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

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

Example 14 with ColumnInfo

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

the class JSDatabaseManager method copyMatchingFields.

public boolean copyMatchingFields(Object src, IRecordInternal dest, boolean overwrite, Object[] names) throws ServoyException {
    checkAuthorized();
    if (dest.getParentFoundSet().getSQLSheet() == null) {
        return false;
    }
    List<Object> al = new ArrayList<Object>();
    if (names != null) {
        al = Arrays.asList(names);
    }
    try {
        SQLSheet destSheet = dest.getParentFoundSet().getSQLSheet();
        Table dest_table = destSheet.getTable();
        boolean wasEditing = dest.isEditing();
        Map<String, Method> getters = new HashMap<String, Method>();
        if (dest.startEditing()) {
            Iterator<Column> it = dest_table.getColumns().iterator();
            while (it.hasNext()) {
                Column c = it.next();
                ColumnInfo ci = c.getColumnInfo();
                if (ci != null && ci.isExcluded()) {
                    continue;
                }
                if (al.contains(c.getDataProviderID())) {
                    // skip, also if value in dest_rec is null
                    continue;
                }
                Object dval = dest.getValue(c.getDataProviderID());
                if (dval == null || (!dest_table.getRowIdentColumns().contains(c) && (overwrite || (al.size() > 0 && !al.contains(c.getDataProviderID()))))) {
                    if (src instanceof IRecordInternal) {
                        IRecordInternal src_rec = (IRecordInternal) src;
                        int index = src_rec.getParentFoundSet().getColumnIndex(c.getDataProviderID());
                        if (index != -1) {
                            Object sval = src_rec.getValue(c.getDataProviderID());
                            try {
                                int type = ((FoundSetManager) application.getFoundSetManager()).getConvertedTypeForColumn(c, false);
                                dest.setValue(c.getDataProviderID(), Column.getAsRightType(type, c.getFlags(), sval, c.getLength(), true, true));
                            } catch (Exception e) {
                                application.reportJSError("Could not copy matching field to " + dest_table.getName() + "." + c.getDataProviderID() + ". The value: '" + sval + "' does not match the type of the destination.", e);
                            }
                        }
                    } else if (src instanceof NativeObject) {
                        NativeObject no = ((NativeObject) src);
                        if (no.has(c.getDataProviderID(), no)) {
                            Object raw_val = no.get(c.getDataProviderID(), no);
                            Object val = c.getAsRightType(raw_val);
                            dest.setValue(c.getDataProviderID(), val);
                        }
                    } else if (src != null) {
                        Method m = getMethod(src, c.getDataProviderID(), getters);
                        if (m != null) {
                            Object raw_val = m.invoke(src, (Object[]) null);
                            Object val = c.getAsRightType(raw_val);
                            dest.setValue(c.getDataProviderID(), val);
                        }
                    }
                }
            }
            if (!wasEditing) {
                dest.stopEditing();
            }
            return true;
        }
    } catch (Exception e) {
        Debug.error(e);
    }
    return false;
}
Also used : BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) QueryString(com.servoy.j2db.persistence.QueryString) Method(java.lang.reflect.Method) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) SQLException(java.sql.SQLException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) NativeObject(org.mozilla.javascript.NativeObject) QueryColumn(com.servoy.j2db.query.QueryColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QBColumn(com.servoy.j2db.querybuilder.impl.QBColumn) NativeObject(org.mozilla.javascript.NativeObject)

Example 15 with ColumnInfo

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

the class SQLGenerator method createTableSQL.

private SQLSheet createTableSQL(String dataSource, boolean cache) throws ServoyException {
    if (dataSource == null) {
        return createNoTableSQL(cache);
    }
    Table table = (Table) application.getFoundSetManager().getTable(dataSource);
    if (table == null) {
        // $NON-NLS-1$
        throw new RepositoryException("Cannot create sql: table not found for data source '" + dataSource + '\'');
    }
    SQLSheet retval = new SQLSheet(application, table.getServerName(), table);
    // never remove this line, due to recursive behaviour, register a state when immediately!
    if (cache)
        cachedDataSourceSQLSheets.put(dataSource, retval);
    QueryTable queryTable = new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema());
    QuerySelect select = new QuerySelect(queryTable);
    QueryDelete delete = new QueryDelete(queryTable);
    QueryInsert insert = new QueryInsert(queryTable);
    QueryUpdate update = new QueryUpdate(queryTable);
    List<Column> columns = new ArrayList<Column>();
    Iterator<Column> it1 = table.getColumns().iterator();
    while (it1.hasNext()) {
        Column c = it1.next();
        ColumnInfo ci = c.getColumnInfo();
        if (ci != null && ci.isExcluded()) {
            continue;
        }
        columns.add(c);
    }
    List<String> requiredDataProviderIDs = new ArrayList<String>();
    Iterator<Column> pks = table.getRowIdentColumns().iterator();
    if (!pks.hasNext()) {
        throw new RepositoryException(ServoyException.InternalCodes.PRIMARY_KEY_NOT_FOUND, new Object[] { table.getName() });
    }
    List<QueryColumn> pkQueryColumns = new ArrayList<QueryColumn>();
    while (pks.hasNext()) {
        Column column = pks.next();
        if (!columns.contains(column))
            columns.add(column);
        requiredDataProviderIDs.add(column.getDataProviderID());
        pkQueryColumns.add(column.queryColumn(queryTable));
    }
    Iterator<Column> it2 = columns.iterator();
    select.setColumns(makeQueryColumns(it2, queryTable, insert));
    SetCondition pkSelect = new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]), new Placeholder(new TablePlaceholderKey(queryTable, PLACEHOLDER_PRIMARY_KEY)), true);
    select.setCondition(CONDITION_SEARCH, pkSelect);
    delete.setCondition(deepClone(pkSelect));
    update.setCondition(deepClone(pkSelect));
    // fill dataprovider map
    List<String> dataProviderIDsDilivery = new ArrayList<String>();
    for (Column col : columns) {
        dataProviderIDsDilivery.add(col.getDataProviderID());
    }
    retval.addSelect(select, dataProviderIDsDilivery, requiredDataProviderIDs, null);
    retval.addDelete(delete, requiredDataProviderIDs);
    retval.addInsert(insert, dataProviderIDsDilivery);
    retval.addUpdate(update, dataProviderIDsDilivery, requiredDataProviderIDs);
    // related stuff
    createAggregates(retval, queryTable);
    return retval;
}
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) QueryDelete(com.servoy.j2db.query.QueryDelete) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SetCondition(com.servoy.j2db.query.SetCondition) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) 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) QueryInsert(com.servoy.j2db.query.QueryInsert) QueryUpdate(com.servoy.j2db.query.QueryUpdate)

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