Search in sources :

Example 6 with ColumnType

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

the class JSDatabaseManager method js_convertToDataSet.

// why did we add this method ?
// @JSFunction
// public Collection<Procedure> js_getProcedures(String serverName) throws Exception
// {
// Collection<Procedure> procedures = application.getSolution().getServer(serverName).getProcedures();
// return procedures;
// }
/**
 * @clonedesc js_convertToDataSet(IFoundSetInternal)
 *
 * @sampleas js_convertToDataSet(IFoundSetInternal)
 *
 * @param ids Concatenated values to be put into dataset.
 *
 * @return JSDataSet with the data.
 */
public JSDataSet js_convertToDataSet(String ids) {
    if (ids == null) {
        return null;
    }
    // $NON-NLS-1$
    String[] dpnames = { "id" };
    ColumnType[] dptypes = { ColumnType.getInstance(IColumnTypes.INTEGER, Integer.MAX_VALUE, 0) };
    List<Object[]> lst = new ArrayList<Object[]>();
    // $NON-NLS-1$
    StringTokenizer st = new StringTokenizer(ids, ",;\n\r\t ");
    while (st.hasMoreElements()) {
        Object o = st.nextElement();
        if (o instanceof Double && ((Double) o).doubleValue() == ((Double) o).intValue()) {
            o = new Integer(((Double) o).intValue());
        }
        lst.add(new Object[] { o });
    }
    return new JSDataSet(application, BufferedDataSetInternal.createBufferedDataSet(dpnames, dptypes, lst, false));
}
Also used : StringTokenizer(java.util.StringTokenizer) ColumnType(com.servoy.j2db.query.ColumnType) ArrayList(java.util.ArrayList) NativeObject(org.mozilla.javascript.NativeObject) QueryString(com.servoy.j2db.persistence.QueryString)

Example 7 with ColumnType

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

the class XMLUtils method parseColumnTypeArray.

/**
 * Parse an array string '[[tp,len,scale], [tp,len,scale], ...]' as ColumnType list
 */
public static List<ColumnType> parseColumnTypeArray(String s) {
    if (s == null)
        return null;
    List<ColumnType> list = null;
    JSONSerializer serializer = new JSONSerializer();
    try {
        serializer.registerDefaultSerializers();
        Integer[][] array = (Integer[][]) serializer.fromJSON(s);
        if (array != null && array.length > 0) {
            list = new ArrayList<ColumnType>(array.length);
            for (Integer[] elem : array) {
                list.add(ColumnType.getInstance(elem[0].intValue(), elem[1].intValue(), elem[2].intValue()));
            }
        }
    } catch (Exception e) {
        Debug.error(e);
    }
    return list;
}
Also used : ColumnType(com.servoy.j2db.query.ColumnType) BaseColumnType(com.servoy.base.query.BaseColumnType) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSONSerializer(org.jabsorb.JSONSerializer)

Example 8 with ColumnType

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

the class JSDatabaseManager method js_convertToDataSet.

/**
 * @clonedesc js_convertToDataSet(IFoundSetInternal)
 *
 * @sampleas js_convertToDataSet(IFoundSetInternal)
 *
 * @param values The values array.
 * @param dataproviderNames The property names array.
 *
 * @return JSDataSet with the data.
 */
public JSDataSet js_convertToDataSet(Object[] values, String[] dataproviderNames) {
    if (values == null) {
        return null;
    }
    // $NON-NLS-1$
    String[] dpnames = { "id" };
    ColumnType[] dptypes = { ColumnType.getInstance(IColumnTypes.INTEGER, Integer.MAX_VALUE, 0) };
    List<Object[]> lst = new ArrayList<Object[]>();
    Object[] array = values;
    if (dataproviderNames != null) {
        dpnames = dataproviderNames;
    }
    Map<String, Method> getters = new HashMap<String, Method>();
    for (Object o : array) {
        if (o instanceof Number || o instanceof String || o instanceof UUID || o instanceof Date) {
            if (o instanceof Double && ((Double) o).doubleValue() == ((Double) o).intValue()) {
                o = new Integer(((Double) o).intValue());
            }
            lst.add(new Object[] { o });
        } else if (o instanceof Scriptable) {
            List<Object> row = new ArrayList<Object>();
            for (String dpname : dpnames) {
                if (((Scriptable) o).has(dpname, (Scriptable) o))
                    row.add(ScriptVariableScope.unwrap(((Scriptable) o).get(dpname, (Scriptable) o)));
            }
            if (dpnames.length != row.size() || dpnames.length == 0) {
                // for backward compatibility
                lst.add(new Object[] { o });
            } else {
                lst.add(row.toArray());
            }
        } else if (o != null) {
            // try reflection
            List<Object> row = new ArrayList<Object>();
            for (String dpname : dpnames) {
                Method m = getMethod(o, dpname, getters);
                if (m != null) {
                    try {
                        row.add(m.invoke(o, (Object[]) null));
                    } catch (Exception e) {
                        Debug.error(e);
                    }
                }
            }
            if (dpnames.length != row.size() || dpnames.length == 0) {
                // for backward compatibility
                lst.add(new Object[] { o });
            } else {
                lst.add(row.toArray());
            }
        }
    }
    return new JSDataSet(application, BufferedDataSetInternal.createBufferedDataSet(dpnames, dptypes, lst, false));
}
Also used : ColumnType(com.servoy.j2db.query.ColumnType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryString(com.servoy.j2db.persistence.QueryString) Method(java.lang.reflect.Method) Scriptable(org.mozilla.javascript.Scriptable) IScriptable(com.servoy.j2db.scripting.IScriptable) Date(java.util.Date) 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) List(java.util.List) ArrayList(java.util.ArrayList) UUID(com.servoy.j2db.util.UUID)

Example 9 with ColumnType

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

the class JSDatabaseManager method js_convertToDataSet.

/**
 * @clonedesc js_convertToDataSet(IFoundSetInternal)
 *
 * @sampleas js_convertToDataSet(IFoundSetInternal)
 *
 * @param foundset The foundset to be converted.
 * @param dataproviderNames Array with column names.
 *
 * @return JSDataSet with the data.
 */
public JSDataSet js_convertToDataSet(IFoundSetInternal foundset, String[] dataproviderNames) throws RepositoryException {
    if (foundset == null) {
        return null;
    }
    // $NON-NLS-1$
    String[] dpnames = { "id" };
    ColumnType[] dptypes = { ColumnType.getInstance(IColumnTypes.INTEGER, Integer.MAX_VALUE, 0) };
    List<Object[]> lst = new ArrayList<Object[]>();
    FoundSet fs = (FoundSet) foundset;
    if (fs.getTable() != null) {
        if (dataproviderNames != null) {
            dpnames = dataproviderNames;
        } else {
            dpnames = fs.getSQLSheet().getPKColumnDataProvidersAsArray();
        }
        FoundSetManager fsm = (FoundSetManager) application.getFoundSetManager();
        boolean getInOneQuery = !fs.isInFindMode() && (fs.hadMoreRows() || fs.getSize() > fsm.config.pkChunkSize()) && !fsm.getEditRecordList().hasEditedRecords(fs);
        dptypes = new ColumnType[dpnames.length];
        Table table = fs.getSQLSheet().getTable();
        Map<String, Column> columnMap = new HashMap<String, Column>();
        for (int i = 0; i < dpnames.length; i++) {
            IDataProvider dp = application.getFlattenedSolution().getDataProviderForTable(table, dpnames[i]);
            dptypes[i] = dp == null ? ColumnType.getInstance(0, 0, 0) : ColumnType.getInstance(dp instanceof Column ? ((Column) dp).getType() : dp.getDataProviderType(), dp.getLength(), dp instanceof Column ? ((Column) dp).getScale() : 0);
            if (getInOneQuery) {
                // only columns and data we can get from the foundset (calculations only when stored)
                if (dp instanceof Column) {
                    columnMap.put(dpnames[i], (Column) dp);
                    // Blobs require special resultset handling
                    getInOneQuery = !SQLGenerator.isBlobColumn((Column) dp);
                } else {
                    // aggregates, globals
                    getInOneQuery = fs.containsDataProvider(dpnames[i]);
                }
            }
        }
        if (getInOneQuery && columnMap.size() > 0) {
            // large foundset, query the columns in 1 go
            QuerySelect sqlSelect = AbstractBaseQuery.deepClone(fs.getQuerySelectForReading());
            ArrayList<IQuerySelectValue> cols = new ArrayList<IQuerySelectValue>(columnMap.size());
            ArrayList<String> distinctColumns = new ArrayList<String>(columnMap.size());
            for (String dpname : dpnames) {
                Column column = columnMap.get(dpname);
                if (column != null && !distinctColumns.contains(dpname)) {
                    distinctColumns.add(dpname);
                    cols.add(column.queryColumn(sqlSelect.getTable()));
                }
            }
            boolean hasJoins = sqlSelect.getJoins() != null;
            if (hasJoins) {
                // add pk columns so distinct-in-memory can be used
                List<Column> rowIdentColumns = ((Table) fs.getTable()).getRowIdentColumns();
                for (Column column : rowIdentColumns) {
                    if (!columnMap.containsKey(column.getDataProviderID())) {
                        cols.add(column.queryColumn(sqlSelect.getTable()));
                    }
                }
            }
            sqlSelect.setColumns(cols);
            try {
                SQLSheet sheet = fs.getSQLSheet();
                IConverterManager<IColumnConverter> columnConverterManager = ((FoundSetManager) fs.getFoundSetManager()).getColumnConverterManager();
                SQLStatement trackingInfo = null;
                if (fsm.getEditRecordList().hasAccess(sheet.getTable(), IRepository.TRACKING_VIEWS)) {
                    trackingInfo = new SQLStatement(ISQLActionTypes.SELECT_ACTION, sheet.getServerName(), sheet.getTable().getName(), null, null);
                    trackingInfo.setTrackingData(sqlSelect.getColumnNames(), new Object[][] {}, new Object[][] {}, fsm.getApplication().getUserUID(), fsm.getTrackingInfo(), fsm.getApplication().getClientID());
                }
                IDataSet dataSet = fsm.getDataServer().performQuery(fsm.getApplication().getClientID(), sheet.getServerName(), fsm.getTransactionID(sheet), sqlSelect, null, fsm.getTableFilterParams(sheet.getServerName(), sqlSelect), hasJoins, 0, -1, IDataServer.FOUNDSET_LOAD_QUERY, trackingInfo);
                lst = new ArrayList<Object[]>(dataSet.getRowCount());
                for (int i = 0; i < dataSet.getRowCount(); i++) {
                    Object[] row = new Object[dpnames.length];
                    // may contain more data: pk columns for distinct-in-memory
                    Object[] dataseRow = dataSet.getRow(i);
                    for (int j = 0; j < dpnames.length; j++) {
                        Column column = columnMap.get(dpnames[j]);
                        if (column == null) {
                            // fs.containsDataProvider returned true for this dpname
                            row[j] = fs.getDataProviderValue(dpnames[j]);
                        } else {
                            row[j] = sheet.convertValueToObject(dataseRow[distinctColumns.indexOf(dpnames[j])], sheet.getColumnIndex(dpnames[j]), columnConverterManager);
                        }
                    }
                    lst.add(row);
                }
            } catch (RepositoryException e) {
                throw e;
            } catch (Exception e) {
                Debug.error(e);
                throw new RepositoryException(e.getMessage());
            }
        } else {
            // loop over the records
            for (int i = 0; i < fs.getSize(); i++) {
                IRecordInternal record = fs.getRecord(i);
                Object[] pk = new Object[dpnames.length];
                for (int j = 0; j < dpnames.length; j++) {
                    pk[j] = record.getValue(dpnames[j]);
                }
                lst.add(pk);
            }
        }
    }
    return new JSDataSet(application, BufferedDataSetInternal.createBufferedDataSet(dpnames, dptypes, lst, false));
}
Also used : ColumnType(com.servoy.j2db.query.ColumnType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryString(com.servoy.j2db.persistence.QueryString) IDataProvider(com.servoy.j2db.persistence.IDataProvider) 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) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QuerySelect(com.servoy.j2db.query.QuerySelect) 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) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Aggregations

ColumnType (com.servoy.j2db.query.ColumnType)9 BaseColumnType (com.servoy.base.query.BaseColumnType)6 ArrayList (java.util.ArrayList)6 RepositoryException (com.servoy.j2db.persistence.RepositoryException)5 ApplicationException (com.servoy.j2db.ApplicationException)4 ServoyException (com.servoy.j2db.util.ServoyException)4 RemoteException (java.rmi.RemoteException)4 BaseQueryTable (com.servoy.base.query.BaseQueryTable)3 ITable (com.servoy.j2db.persistence.ITable)3 QueryString (com.servoy.j2db.persistence.QueryString)3 QueryTable (com.servoy.j2db.query.QueryTable)3 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)3 NativeObject (org.mozilla.javascript.NativeObject)3 InsertResult (com.servoy.j2db.dataprocessing.IDataServer.InsertResult)2 TableNode (com.servoy.j2db.persistence.TableNode)2 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)2 QueryDelete (com.servoy.j2db.query.QueryDelete)2 QuerySelect (com.servoy.j2db.query.QuerySelect)2 ColumnInfoDef (com.servoy.j2db.util.xmlxport.ColumnInfoDef)2 TableDef (com.servoy.j2db.util.xmlxport.TableDef)2