Search in sources :

Example 1 with ITable

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

the class FoundSetManager method insertToDataSource.

public Object[] insertToDataSource(String name, IDataSet dataSet, ColumnType[] columnTypes, String[] pkNames, boolean create, boolean skipOnLoad, String server) throws ServoyException {
    if (name == null) {
        return null;
    }
    String dataSource = IServer.VIEW_SERVER.equals(server) ? DataSourceUtils.createViewDataSource(name) : DataSourceUtils.createInmemDataSource(name);
    IDataSet fixedDataSet = dataSet;
    List<ColumnType> fixedColumnTypes;
    if (columnTypes == null) {
        ColumnType[] dataSetTypes = BufferedDataSetInternal.getColumnTypeInfo(dataSet);
        fixedColumnTypes = dataSetTypes == null ? null : asList(dataSetTypes);
    } else {
        fixedColumnTypes = asList(columnTypes);
    }
    // get column def from the first in-mem datasource found
    ServoyJSONObject columnsDef = null;
    Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
    int onLoadMethodId = -1;
    while (tblIte.hasNext()) {
        TableNode tn = tblIte.next();
        if (columnsDef == null)
            columnsDef = tn.getColumns();
        if (onLoadMethodId == -1)
            onLoadMethodId = tn.getOnFoundSetLoadMethodID();
    }
    HashMap<String, ColumnInfoDef> columnInfoDefinitions = null;
    boolean[] rowsToStringserialize = null;
    if (columnsDef == null) {
        // if we have array columns, convert values using StringSerializer
        if (containsArrayType(fixedColumnTypes)) {
            rowsToStringserialize = new boolean[fixedColumnTypes.size()];
            for (int i = 0; i < fixedColumnTypes.size(); i++) {
                ColumnType columnType = fixedColumnTypes.get(i);
                if (columnType.getSqlType() == Types.ARRAY) {
                    fixedColumnTypes.set(i, ColumnType.getColumnType(IColumnTypes.TEXT));
                    rowsToStringserialize[i] = true;
                }
            }
        }
    } else {
        TableDef tableInfo = DatabaseUtils.deserializeTableInfo(columnsDef);
        columnInfoDefinitions = new HashMap<String, ColumnInfoDef>();
        List<String> inmemColumnNames = new ArrayList<>();
        List<String> inmemPKs = new ArrayList<>();
        List<ColumnType> inmemColumnTypes = new ArrayList<>();
        for (int j = 0; j < tableInfo.columnInfoDefSet.size(); j++) {
            ColumnInfoDef cid = tableInfo.columnInfoDefSet.get(j);
            if (cid.autoEnterType != ColumnInfo.SEQUENCE_AUTO_ENTER || cid.autoEnterSubType != ColumnInfo.DATABASE_IDENTITY) {
                inmemColumnNames.add(cid.name);
                inmemColumnTypes.add(cid.columnType);
            }
            if ((cid.flags & IBaseColumn.IDENT_COLUMNS) != 0) {
                inmemPKs.add(cid.name);
            }
            columnInfoDefinitions.put(cid.name, cid);
            // apply stringserializer on designed datasources
            if (JSONSerializerWrapper.STRING_SERIALIZER_NAME.equals(cid.converterName)) {
                if (rowsToStringserialize == null) {
                    rowsToStringserialize = new boolean[fixedColumnTypes.size()];
                }
                rowsToStringserialize[j] = true;
            }
        }
        if (pkNames == null && inmemPKs.size() > 0) {
            pkNames = inmemPKs.toArray(new String[inmemPKs.size()]);
        }
        if (!asList(dataSet.getColumnNames()).equals(inmemColumnNames) || !compareColumnTypes(fixedColumnTypes, inmemColumnTypes)) {
            if (dataSet.getColumnCount() > 0 && !asList(dataSet.getColumnNames()).equals(inmemColumnNames)) {
                Debug.warn("Dataset column names definition does not match inmem table definition for datasource : " + dataSource + " columns of dataset: " + Arrays.toString(dataSet.getColumnNames()) + ", columns of in mem definition: " + inmemColumnNames);
            }
            if (fixedColumnTypes != null && !compareColumnTypes(fixedColumnTypes, inmemColumnTypes)) {
                Debug.warn("Dataset column types definition does not match inmem table definition for datasource : " + dataSource + " types of dataset: " + fixedColumnTypes + ", types of in mem definition: " + inmemColumnTypes);
            }
            fixedColumnTypes = inmemColumnTypes;
            fixedDataSet = BufferedDataSetInternal.createBufferedDataSet(inmemColumnNames.toArray(new String[inmemColumnNames.size()]), fixedColumnTypes.toArray(new ColumnType[fixedColumnTypes.size()]), new ArrayList<Object[]>(), false);
        }
    }
    // - in the else branch above (already defined in the table node), columns that are not auto sequences or dbidents; see inmemColumnNames/inmemColumnTypes(which is at this point the same as fixedColumnTypes)
    if (dataSet.getRowCount() > 0 && dataSet.getRow(0).length != fixedColumnTypes.size()) {
        // $NON-NLS-1$
        throw new RepositoryException("Data set rows do not match column count");
    }
    if (rowsToStringserialize != null) {
        replaceValuesWithSerializedString(dataSet, rowsToStringserialize);
    }
    try {
        ITable table = IServer.VIEW_SERVER.equals(server) ? viewDataSources.get(dataSource) : inMemDataSources.get(dataSource);
        if (table == null && !create) {
            throw new RepositoryException("Appending to non-existing datasource: " + dataSource);
        }
        GlobalTransaction gt = getGlobalTransaction();
        String tid = null;
        String serverName = server == null ? (table == null ? IServer.INMEM_SERVER : table.getServerName()) : server;
        if (gt != null) {
            tid = gt.getTransactionID(serverName);
        }
        if (create && table != null) {
            // temp table was used before, delete all data in it
            FoundSet foundSet = (FoundSet) getSharedFoundSet(dataSource);
            foundSet.removeLastFound();
            try {
                QueryDelete delete = new QueryDelete(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema(), true));
                SQLStatement deleteStatement = new SQLStatement(ISQLActionTypes.DELETE_ACTION, table.getServerName(), table.getName(), null, tid, delete, null);
                application.getDataServer().performUpdates(application.getClientID(), new ISQLStatement[] { deleteStatement });
            } catch (Exception e) {
                Debug.log(e);
                table = null;
            }
            RowManager element = rowManagers.get(dataSource);
            if (element != null) {
                element.flushAllCachedRows();
            }
        }
        InsertResult insertResult = application.getDataServer().insertDataSet(application.getClientID(), fixedDataSet, dataSource, table == null ? IServer.INMEM_SERVER : table.getServerName(), table == null ? null : table.getName(), /* create temp table when null */
        tid, fixedColumnTypes == null ? null : fixedColumnTypes.toArray(new ColumnType[fixedColumnTypes.size()]), /* inferred from dataset when null */
        pkNames, columnInfoDefinitions);
        if (insertResult != null) {
            table = insertResult.getTable();
            // then call insertDataSet again so the data is inserted with the columns defined in the the dataset.
            if (dataSet != fixedDataSet && dataSet.getRowCount() > 0) {
                insertResult = application.getDataServer().insertDataSet(application.getClientID(), dataSet, dataSource, table.getServerName(), table.getName(), tid, fixedColumnTypes == null ? null : fixedColumnTypes.toArray(new ColumnType[fixedColumnTypes.size()]), /* inferred from dataset when null */
                pkNames, columnInfoDefinitions);
            }
            if (IServer.INMEM_SERVER.equals(serverName)) {
                inMemDataSources.put(dataSource, table);
            } else {
                viewDataSources.put(dataSource, table);
            }
            fireTableEvent(table);
            if (!skipOnLoad && fixedDataSet.getRowCount() == 0 && onLoadMethodId > 0) {
                IFoundSetInternal sharedFoundSet = getSharedFoundSet(dataSource);
                executeFoundsetTriggerReturnFirst(sharedFoundSet.getTable(), new Object[] { DataSourceUtils.getInmemDataSourceName(dataSource) }, StaticContentSpecLoader.PROPERTY_ONFOUNDSETLOADMETHODID, false, (Scriptable) sharedFoundSet);
            }
            if (create) {
                // only refresh when it is a new full load, when adding data to an existing table, it is only applicable to the (shared) foundset
                refreshFoundSetsFromDB(dataSource, null, false);
            }
            return insertResult.getGeneratedPks();
        }
    } catch (RemoteException e) {
        throw new RepositoryException(e);
    }
    return null;
}
Also used : ColumnType(com.servoy.j2db.query.ColumnType) BaseColumnType(com.servoy.base.query.BaseColumnType) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TableDef(com.servoy.j2db.util.xmlxport.TableDef) ColumnInfoDef(com.servoy.j2db.util.xmlxport.ColumnInfoDef) ITable(com.servoy.j2db.persistence.ITable) InsertResult(com.servoy.j2db.dataprocessing.IDataServer.InsertResult) QueryDelete(com.servoy.j2db.query.QueryDelete) RepositoryException(com.servoy.j2db.persistence.RepositoryException) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) MarshallException(org.jabsorb.serializer.MarshallException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) TableNode(com.servoy.j2db.persistence.TableNode) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) RemoteException(java.rmi.RemoteException)

Example 2 with ITable

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

the class FoundSetManager method registerViewFoundSet.

@Override
public boolean registerViewFoundSet(ViewFoundSet foundset) {
    if (foundset == null)
        return false;
    ViewFoundSet oldValue = viewFoundSets.put(foundset.getDataSource(), foundset);
    ITable table = foundset.getTable();
    if (!viewDataSources.containsKey(foundset.getDataSource()))
        viewDataSources.put(foundset.getDataSource(), table);
    if (oldValue != null) {
        for (IFormController controller : application.getFormManager().getCachedFormControllers()) {
            if (controller.getFormModel() == oldValue) {
                controller.loadAllRecords();
            }
        }
    }
    return true;
}
Also used : ITable(com.servoy.j2db.persistence.ITable) IFormController(com.servoy.j2db.IFormController)

Example 3 with ITable

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

the class RemoteDebugScriptEngine method recompileScriptCalculation.

public boolean recompileScriptCalculation(ScriptCalculation sc) {
    try {
        ITable table = sc.getTable();
        String dataSource = table.getDataSource();
        if (DataSourceUtils.getInmemDataSourceName(dataSource) != null) {
            table = application.getFoundSetManager().getTable(dataSource);
        }
        Scriptable tableScope = getExistingTableScrope(table);
        if (tableScope instanceof LazyCompilationScope) {
            try {
                ((LazyCompilationScope) tableScope).put(sc, sc);
                return true;
            } catch (Exception ex) {
                application.reportJSError("compile failed: " + sc.getDataProviderID(), ex);
            }
        }
    } catch (Exception e) {
        Debug.error(e);
    }
    return false;
}
Also used : ITable(com.servoy.j2db.persistence.ITable) LazyCompilationScope(com.servoy.j2db.scripting.LazyCompilationScope) Scriptable(org.mozilla.javascript.Scriptable) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException)

Example 4 with ITable

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

the class WebCellAdapter method isDBDataprovider.

private boolean isDBDataprovider() {
    if (isDBDataproviderObj == null) {
        isDBDataproviderObj = Boolean.FALSE;
        ITable table = view.getDataAdapterList().getFormController().getTable();
        if (table instanceof Table) {
            Table tableObj = (Table) table;
            Iterator<Column> columns = tableObj.getColumns().iterator();
            while (columns.hasNext()) {
                Column col = columns.next();
                ColumnInfo ci = col.getColumnInfo();
                if (ci != null && ci.isExcluded()) {
                    continue;
                }
                if (col.getDataProviderID() == dataprovider) {
                    isDBDataproviderObj = Boolean.TRUE;
                    break;
                }
            }
            Iterator<TableNode> tableNodes = view.getDataAdapterList().getApplication().getFlattenedSolution().getTableNodes(table);
            while (tableNodes.hasNext()) {
                TableNode tableNode = tableNodes.next();
                if (tableNode != null) {
                    Iterator<IPersist> it2 = tableNode.getAllObjects();
                    while (it2.hasNext()) {
                        IPersist persist = it2.next();
                        if (persist instanceof IDataProvider && (((IDataProvider) persist).getDataProviderID() == dataprovider)) {
                            isDBDataproviderObj = Boolean.FALSE;
                        }
                    }
                }
            }
        }
    }
    return isDBDataproviderObj.booleanValue();
}
Also used : ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) Column(com.servoy.j2db.persistence.Column) IPersist(com.servoy.j2db.persistence.IPersist) TableNode(com.servoy.j2db.persistence.TableNode) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) ITable(com.servoy.j2db.persistence.ITable) IDataProvider(com.servoy.j2db.persistence.IDataProvider)

Example 5 with ITable

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

the class TitleStringPropertyType method toSabloComponentDefaultValue.

/*
	 * (non-Javadoc)
	 *
	 * @see com.servoy.j2db.server.ngclient.property.types.NGConversions.IFormElementDefaultValueToSabloComponent#toSabloComponentDefaultValue(org.sablo.
	 * specification.PropertyDescription, com.servoy.j2db.server.ngclient.INGFormElement, com.servoy.j2db.server.ngclient.WebFormComponent,
	 * com.servoy.j2db.server.ngclient.DataAdapterList)
	 */
@Override
public BasicTagStringTypeSabloValue toSabloComponentDefaultValue(PropertyDescription pd, INGFormElement formElement, WebFormComponent component, DataAdapterList dataAdapterList) {
    TitleStringConfig titleStringConfig = (TitleStringConfig) pd.getConfig();
    String forDataprovider = titleStringConfig.getForDataprovider();
    if (forDataprovider != null) {
        PropertyDescription forProperty = formElement.getPropertyDescription(forDataprovider);
        if (forProperty != null) {
            IPropertyType<?> type = forProperty.getType();
            if (type instanceof FoundsetLinkedPropertyType) {
                Object config = forProperty.getConfig();
                if (config instanceof FoundsetLinkedConfig && ((FoundsetLinkedConfig) config).getForFoundsetName() != null) {
                    String forFoundset = ((FoundsetLinkedConfig) config).getForFoundsetName();
                    String dataproviderID = (String) formElement.getPropertyValue(forDataprovider);
                    JSONObject foundsetValue = (JSONObject) formElement.getPropertyValue(forFoundset);
                    if (foundsetValue != null) {
                        String foundsetID = foundsetValue.optString(FoundsetPropertyType.FOUNDSET_SELECTOR);
                        INGApplication application = ((WebFormComponent) component.getUnderlyingWebObject()).getDataConverterContext().getApplication();
                        Form form = ((IContextProvider) component.getUnderlyingWebObject()).getDataConverterContext().getForm().getForm();
                        ITable table = FoundsetTypeSabloValue.getTableBasedOfFoundsetPropertyFromFoundsetIdentifier(foundsetID, application, form);
                        if (table != null) {
                            Column dataproviderColumn = table.getColumn(dataproviderID);
                            if (dataproviderColumn != null) {
                                return toSabloComponentValue(dataproviderColumn.getTitle(), pd, formElement, component, dataAdapterList);
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : Form(com.servoy.j2db.persistence.Form) FoundsetLinkedConfig(com.servoy.j2db.server.ngclient.property.FoundsetLinkedConfig) PropertyDescription(org.sablo.specification.PropertyDescription) INGApplication(com.servoy.j2db.server.ngclient.INGApplication) JSONObject(org.json.JSONObject) IContextProvider(com.servoy.j2db.server.ngclient.IContextProvider) Column(com.servoy.j2db.persistence.Column) JSONObject(org.json.JSONObject) ITable(com.servoy.j2db.persistence.ITable) FoundsetLinkedPropertyType(com.servoy.j2db.server.ngclient.property.FoundsetLinkedPropertyType)

Aggregations

ITable (com.servoy.j2db.persistence.ITable)51 RepositoryException (com.servoy.j2db.persistence.RepositoryException)23 BaseQueryTable (com.servoy.base.query.BaseQueryTable)15 QueryTable (com.servoy.j2db.query.QueryTable)15 Column (com.servoy.j2db.persistence.Column)14 ServoyException (com.servoy.j2db.util.ServoyException)14 ArrayList (java.util.ArrayList)14 RemoteException (java.rmi.RemoteException)13 ApplicationException (com.servoy.j2db.ApplicationException)11 IColumn (com.servoy.j2db.persistence.IColumn)9 Relation (com.servoy.j2db.persistence.Relation)9 Table (com.servoy.j2db.persistence.Table)9 QueryColumn (com.servoy.j2db.query.QueryColumn)8 Form (com.servoy.j2db.persistence.Form)7 QuerySelect (com.servoy.j2db.query.QuerySelect)7 IBaseColumn (com.servoy.base.persistence.IBaseColumn)6 IServer (com.servoy.j2db.persistence.IServer)6 SQLException (java.sql.SQLException)6 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)5 IOException (java.io.IOException)5