Search in sources :

Example 1 with BaseColumnType

use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.

the class FoundSetManager method getViewFoundSet.

@SuppressWarnings("nls")
@Override
public ViewFoundSet getViewFoundSet(String name, QBSelect query) {
    if (query.getQuery().getColumns() == null || query.getQuery().getColumns().size() == 0) {
        throw new RuntimeException("Can't create a ViewFoundset with name: " + name + " and query  " + query + " that has no columns");
    }
    String dataSource = DataSourceUtils.createViewDataSource(name);
    ViewFoundSet vfs = new ViewFoundSet(dataSource, query.build(), application.getFoundSetManager(), pkChunkSize);
    // if this datasource defintion is created already in the developer then we need to check if the query columns are correctly matching it.
    ServoyJSONObject columnsDef = null;
    Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
    while (tblIte.hasNext() && columnsDef == null) {
        TableNode tn = tblIte.next();
        columnsDef = tn.getColumns();
        if (columnsDef != null) {
            TableDef def = DatabaseUtils.deserializeTableInfo(columnsDef);
            for (ColumnInfoDef col : def.columnInfoDefSet) {
                IQuerySelectValue selectValue = getSelectvalue(query, col.name);
                if (selectValue == null) {
                    Debug.error("Column " + col.name + " of type " + col.columnType.toString() + " defined in view datasource '" + dataSource + "' was not found in the provided query.");
                    return null;
                }
                BaseColumnType columnType = selectValue.getColumnType();
                // relax the mapping on default Servoy types
                if (columnType != null && Column.mapToDefaultType(columnType.getSqlType()) != Column.mapToDefaultType(col.columnType.getSqlType())) {
                    Debug.error("Column type for column '" + col.name + " of type " + col.columnType.toString() + "' defined in view datasource '" + dataSource + "' does not match the one " + columnType + " provided in the query.");
                    return null;
                }
            }
        }
    }
    return vfs;
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) TableNode(com.servoy.j2db.persistence.TableNode) TableDef(com.servoy.j2db.util.xmlxport.TableDef) ColumnInfoDef(com.servoy.j2db.util.xmlxport.ColumnInfoDef) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) BaseColumnType(com.servoy.base.query.BaseColumnType)

Example 2 with BaseColumnType

use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.

the class SQLGenerator method getEmptyDataSetForDummyQuery.

/**
 * check if the query is will never return any rows, in that case just return an empty dataset.
 */
public static IDataSet getEmptyDataSetForDummyQuery(ISQLSelect sqlSelect) {
    if (sqlSelect instanceof QuerySelect && ((QuerySelect) sqlSelect).getCondition(CONDITION_SEARCH) != null) {
        // all named conditions in QuerySelecta are AND-ed, if one always results to false, skip the query
        for (IBaseSQLCondition condition : ((QuerySelect) sqlSelect).getCondition(CONDITION_SEARCH).getConditions()) {
            boolean skipQuery = false;
            if (condition instanceof SetCondition && ((SetCondition) condition).isAndCondition()) {
                // check for EQUALS_OPERATOR
                int ncols = ((SetCondition) condition).getKeys().length;
                int[] operators = ((SetCondition) condition).getOperators();
                boolean eqop = true;
                for (int i = 0; i < ncols; i++) {
                    if (operators[i] != IBaseSQLCondition.EQUALS_OPERATOR) {
                        eqop = false;
                    }
                }
                if (eqop) {
                    Object value = ((SetCondition) condition).getValues();
                    if (value instanceof Placeholder) {
                        Object phval = ((Placeholder) value).getValue();
                        // cleared foundset
                        skipQuery = phval instanceof DynamicPkValuesArray && ((DynamicPkValuesArray) phval).getPKs().getRowCount() == 0;
                    } else if (value instanceof Object[][]) {
                        skipQuery = ((Object[][]) value).length == 0 || ((Object[][]) value)[0].length == 0;
                    }
                }
            }
            if (skipQuery) {
                // no need to query, dummy condition (where 1=2) here
                List<IQuerySelectValue> columns = ((QuerySelect) sqlSelect).getColumns();
                String[] columnNames = new String[columns.size()];
                ColumnType[] columnTypes = new ColumnType[columns.size()];
                for (int i = 0; i < columns.size(); i++) {
                    IQuerySelectValue col = columns.get(i);
                    columnNames[i] = col.getAliasOrName();
                    BaseColumnType columnType = col.getColumnType();
                    columnTypes[i] = columnType == null ? ColumnType.getInstance(Types.OTHER, 0, 0) : ColumnType.getInstance(columnType.getSqlType(), columnType.getLength(), columnType.getScale());
                }
                return BufferedDataSetInternal.createBufferedDataSet(columnNames, columnTypes, new SafeArrayList<Object[]>(0), false);
            }
        }
    }
    // query needs to be run
    return null;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) ColumnType(com.servoy.j2db.query.ColumnType) BaseColumnType(com.servoy.base.query.BaseColumnType) IBaseSQLCondition(com.servoy.base.query.IBaseSQLCondition) SetCondition(com.servoy.j2db.query.SetCondition) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseColumnType(com.servoy.base.query.BaseColumnType) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 3 with BaseColumnType

use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.

the class MetaDataUtils method serializeTableMetaDataContents.

/**
 * Serialize contents of buffered dataset to a string, includes column names and type info
 * @param dataSet
 * @return
 * @throws JSONException
 */
public static String serializeTableMetaDataContents(BufferedDataSet dataSet) throws JSONException {
    if (dataSet == null) {
        return null;
    }
    ServoyJSONObject json = new ServoyJSONObject();
    // columns
    JSONArray jsonColumns = new JSONArray();
    String[] columnNames = dataSet.getColumnNames();
    BaseColumnType[] columnTypes = BufferedDataSetInternal.getColumnTypeInfo(dataSet);
    for (int c = 0; c < columnNames.length; c++) {
        JSONObject jsonColumn = new JSONObject();
        jsonColumn.put("name", columnNames[c]);
        jsonColumn.put("type", XMLUtils.serializeColumnType(columnTypes[c]));
        jsonColumns.put(jsonColumn);
    }
    json.put("columns", jsonColumns);
    // rows
    JSONArray jsonRows = new JSONArray();
    for (int r = 0; r < dataSet.getRowCount(); r++) {
        Object[] row = dataSet.getRow(r);
        JSONArray rowobj = new JSONArray();
        for (int i = 0; i < row.length && i < columnNames.length; i++) {
            Object val;
            if (row[i] == null) {
                val = JSONObject.NULL;
            } else if (row[i] instanceof byte[]) {
                val = Utils.encodeBASE64((byte[]) row[i]);
            } else {
                val = row[i];
            }
            rowobj.put(val);
        }
        jsonRows.put(rowobj);
    }
    json.put("rows", jsonRows);
    // toString
    return json.toString(true);
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) BaseColumnType(com.servoy.base.query.BaseColumnType)

Example 4 with BaseColumnType

use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.

the class ViewFoundSet method getTable.

@Override
public ITable getTable() {
    if (table == null) {
        try {
            table = manager.getTable(getDataSource());
            if (table == null) {
                table = new Table(IServer.VIEW_SERVER, DataSourceUtils.getViewDataSourceName(getDataSource()), true, ITable.VIEW, null, null);
                ((Table) table).setDataSource(getDataSource());
                for (IQuerySelectValue col : select.getColumns()) {
                    Column newCol = null;
                    QueryColumn qCol = col.getColumn();
                    if (qCol != null && qCol.getTable() != null) {
                        ITable colTable = manager.getTable(qCol.getTable().getDataSource());
                        if (colTable != null) {
                            Column column = colTable.getColumn(qCol.getName());
                            if (column != null) {
                                String colname = getColunmName(col, qCol);
                                newCol = table.createNewColumn(DummyValidator.INSTANCE, colname, column.getType(), column.getLength(), column.getScale(), column.getAllowNull());
                                if (column.getColumnInfo() != null) {
                                    DatabaseUtils.createNewColumnInfo(manager.getApplication().getFlattenedSolution().getPersistFactory().getNewElementID(null), newCol, false);
                                    newCol.getColumnInfo().copyFrom(column.getColumnInfo());
                                }
                            }
                        }
                    }
                    if (newCol == null) {
                        // existing database column not found, create column on the fly
                        BaseColumnType columnType = col.getColumnType();
                        if (columnType == null) {
                            columnType = ColumnType.getColumnType(IColumnTypes.TEXT);
                        }
                        String colname = getColunmName(col, qCol);
                        table.createNewColumn(DummyValidator.INSTANCE, colname, columnType.getSqlType(), columnType.getLength(), columnType.getScale(), true);
                    }
                }
            }
        } catch (RepositoryException e) {
            Debug.error(e);
        }
    }
    return table;
}
Also used : BaseQueryTable(com.servoy.base.query.BaseQueryTable) ITable(com.servoy.j2db.persistence.ITable) Table(com.servoy.j2db.persistence.Table) DerivedTable(com.servoy.j2db.query.DerivedTable) QueryColumn(com.servoy.j2db.query.QueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) IBaseColumn(com.servoy.base.persistence.IBaseColumn) Column(com.servoy.j2db.persistence.Column) QueryColumn(com.servoy.j2db.query.QueryColumn) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) BaseColumnType(com.servoy.base.query.BaseColumnType)

Example 5 with BaseColumnType

use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.

the class FoundSetManager method getViewFoundSet.

@SuppressWarnings("nls")
@Override
public ViewFoundSet getViewFoundSet(String name, QBSelect query, boolean register) {
    if (query.getQuery().getColumns() == null || query.getQuery().getColumns().size() == 0) {
        throw new RuntimeException("Can't create a ViewFoundset with name: " + name + " and query  " + query + " that has no columns");
    }
    String dataSource = DataSourceUtils.createViewDataSource(name);
    ViewFoundSet vfs = new ViewFoundSet(dataSource, query.build(), application.getFoundSetManager(), config.pkChunkSize());
    // if this datasource defintion is created already in the developer then we need to check if the query columns are correctly matching it.
    ServoyJSONObject columnsDef = null;
    Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
    while (tblIte.hasNext() && columnsDef == null) {
        TableNode tn = tblIte.next();
        columnsDef = tn.getColumns();
        if (columnsDef != null) {
            TableDef def = DatabaseUtils.deserializeTableInfo(columnsDef);
            for (ColumnInfoDef col : def.columnInfoDefSet) {
                IQuerySelectValue selectValue = getSelectvalue(query, col.name);
                if (selectValue == null) {
                    Debug.error("Column " + col.name + " of type " + col.columnType.toString() + " defined in view datasource '" + dataSource + "' was not found in the provided query.");
                    return null;
                }
                BaseColumnType columnType = selectValue.getColumnType();
                // relax the mapping on default Servoy types
                if (columnType != null && Column.mapToDefaultType(columnType.getSqlType()) != Column.mapToDefaultType(col.columnType.getSqlType())) {
                    Debug.error("Column type for column '" + col.name + " of type " + col.columnType.toString() + "' defined in view datasource '" + dataSource + "' does not match the one " + columnType + " provided in the query.");
                    return null;
                }
            }
        }
    }
    registerViewFoundSet(vfs, !register);
    return vfs;
}
Also used : ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) TableNode(com.servoy.j2db.persistence.TableNode) TableDef(com.servoy.j2db.util.xmlxport.TableDef) ColumnInfoDef(com.servoy.j2db.util.xmlxport.ColumnInfoDef) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue) BaseColumnType(com.servoy.base.query.BaseColumnType)

Aggregations

BaseColumnType (com.servoy.base.query.BaseColumnType)5 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)4 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)3 TableNode (com.servoy.j2db.persistence.TableNode)2 ColumnInfoDef (com.servoy.j2db.util.xmlxport.ColumnInfoDef)2 TableDef (com.servoy.j2db.util.xmlxport.TableDef)2 IBaseColumn (com.servoy.base.persistence.IBaseColumn)1 BaseQueryTable (com.servoy.base.query.BaseQueryTable)1 IBaseSQLCondition (com.servoy.base.query.IBaseSQLCondition)1 Column (com.servoy.j2db.persistence.Column)1 IColumn (com.servoy.j2db.persistence.IColumn)1 ITable (com.servoy.j2db.persistence.ITable)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 Table (com.servoy.j2db.persistence.Table)1 ColumnType (com.servoy.j2db.query.ColumnType)1 DerivedTable (com.servoy.j2db.query.DerivedTable)1 Placeholder (com.servoy.j2db.query.Placeholder)1 QueryColumn (com.servoy.j2db.query.QueryColumn)1 QuerySelect (com.servoy.j2db.query.QuerySelect)1 SetCondition (com.servoy.j2db.query.SetCondition)1