Search in sources :

Example 21 with QueryTable

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

the class MetaDataUtils method loadMetadataInTable.

public static int loadMetadataInTable(ITable table, String json) throws IOException, ServoyException, JSONException {
    // parse dataset
    BufferedDataSet dataSet = MetaDataUtils.deserializeTableMetaDataContents(json);
    // check if all columns exist
    List<String> missingColumns = null;
    for (String colname : dataSet.getColumnNames()) {
        if (table.getColumn(colname) == null) {
            if (missingColumns == null) {
                missingColumns = new ArrayList<String>();
            }
            missingColumns.add(colname);
        }
    }
    if (missingColumns != null) {
        StringBuilder message = new StringBuilder("Missing columns from meta data for table '").append(table.getName()).append("'").append(" in server '").append(table.getServerName()).append("' : ");
        for (String name : missingColumns) {
            message.append('\'').append(name).append("' ");
        }
        throw new RepositoryException(message.toString());
    }
    // delete existing data
    ApplicationServerRegistry.get().getDataServer().performUpdates(ApplicationServerRegistry.get().getClientId(), new ISQLStatement[] { new // 
    SQLStatement(// 
    IDataServer.META_DATA_QUERY, // 
    table.getServerName(), // 
    table.getName(), // 
    null, // delete entire table
    new QueryDelete(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()))) });
    // insert the data
    ApplicationServerRegistry.get().getDataServer().insertDataSet(ApplicationServerRegistry.get().getClientId(), dataSet, table.getDataSource(), table.getServerName(), table.getName(), null, null, null, null);
    return dataSet.getRowCount();
}
Also used : QueryDelete(com.servoy.j2db.query.QueryDelete) RepositoryException(com.servoy.j2db.persistence.RepositoryException) QueryTable(com.servoy.j2db.query.QueryTable)

Example 22 with QueryTable

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

the class FoundSetManager method getTableCount.

public int getTableCount(ITable table) {
    if (table != null) {
        try {
            long time = System.currentTimeMillis();
            IDataServer ds = application.getDataServer();
            String transaction_id = getTransactionID(table.getServerName());
            QuerySelect countSelect = new QuerySelect(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()));
            // $NON-NLS-1$
            countSelect.addColumn(new QueryAggregate(QueryAggregate.COUNT, new QueryColumnValue(Integer.valueOf(1), "n", true), null));
            IDataSet set = ds.performQuery(application.getClientID(), table.getServerName(), transaction_id, countSelect, null, getTableFilterParams(table.getServerName(), countSelect), false, 0, 10, IDataServer.FOUNDSET_LOAD_QUERY);
            if (Debug.tracing()) {
                Debug.trace(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                "Table count time: " + (System.currentTimeMillis() - time) + " thread: " + Thread.currentThread().getName() + ", SQL: " + countSelect.toString());
            }
            if (set.getRowCount() > 0) {
                Object[] row = set.getRow(0);
                if (row.length > 0) {
                    return Utils.getAsInteger(row[0]);
                }
            }
        } catch (Exception e) {
            Debug.error(e);
        }
    }
    return -1;
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) QuerySelect(com.servoy.j2db.query.QuerySelect) 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)

Example 23 with QueryTable

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

the class SQLGenerator method createExistsCondition.

public static ISQLCondition createExistsCondition(IDataProviderHandler flattenedSolution, QuerySelect sqlSelect, ISQLCondition condition, List<IRelation> relations, BaseQueryTable columnTable, IGlobalValueEntry provider, BaseQueryColumn[] pkQueryColumns) throws RepositoryException {
    // exists (select 1 from innermain join related1 ... join relatedn where innermain.pk = main.pk having aggregate(relatedn))
    if (relations.size() == 0) {
        // searching for aggregate in main table, does no make sense.. ignore in search
        return null;
    }
    QuerySelect existsSelect = new QuerySelect(new QueryTable(sqlSelect.getTable().getName(), sqlSelect.getTable().getDataSource(), sqlSelect.getTable().getCatalogName(), sqlSelect.getTable().getSchemaName()));
    existsSelect.addColumn(new QueryColumnValue(Integer.valueOf(1), null, true));
    // innermain.pk = main.pk
    QueryColumn[] innerPkColumns = new QueryColumn[pkQueryColumns.length];
    for (int p = 0; p < pkQueryColumns.length; p++) {
        BaseQueryColumn pk = pkQueryColumns[p];
        innerPkColumns[p] = new QueryColumn(existsSelect.getTable(), pk.getId(), pk.getName(), pk.getColumnType().getSqlType(), pk.getColumnType().getLength(), pk.getColumnType().getScale(), pk.getNativeTypename(), pk.getFlags(), pk.isIdentity());
        // group by on the inner pk, some dbs (hxtt dbf) require that
        existsSelect.addGroupBy(innerPkColumns[p]);
    }
    existsSelect.addCondition("AGGREGATE-SEARCH", new // $NON-NLS-1$
    SetCondition(// $NON-NLS-1$
    new int[] { IBaseSQLCondition.EQUALS_OPERATOR }, // $NON-NLS-1$
    innerPkColumns, pkQueryColumns, true));
    // add the joins
    BaseQueryTable prevTable = existsSelect.getTable();
    for (IRelation relation : relations) {
        ITable foreignTable = flattenedSolution.getTable(relation.getForeignDataSource());
        QueryTable foreignQtable = new QueryTable(foreignTable.getSQLName(), foreignTable.getDataSource(), foreignTable.getCatalog(), foreignTable.getSchema());
        existsSelect.addJoin(createJoin(flattenedSolution, relation, prevTable, foreignQtable, true, provider));
        prevTable = foreignQtable;
    }
    existsSelect.addHaving(AbstractBaseQuery.relinkTable(columnTable, prevTable, condition));
    return new ExistsCondition(existsSelect, true);
}
Also used : ExistsCondition(com.servoy.j2db.query.ExistsCondition) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IRelation(com.servoy.j2db.persistence.IRelation) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) ITable(com.servoy.j2db.persistence.ITable) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable)

Example 24 with QueryTable

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

the class SQLGenerator method createAggregateSelect.

/**
 * Create the sql for a single aggregate on a column.
 *
 * @param aggregee
 * @return
 */
public static QuerySelect createAggregateSelect(int aggregateType, ITable table, Object aggregee) {
    Column column = null;
    if (aggregee instanceof Column) {
        column = (Column) aggregee;
    }
    QuerySelect select = new QuerySelect(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()));
    select.addColumn(new QueryAggregate(aggregateType, // $NON-NLS-1$
    (column == null) ? // $NON-NLS-1$
    (IQuerySelectValue) new QueryColumnValue(aggregee, "n", aggregee instanceof Integer || QueryAggregate.ASTERIX.equals(aggregee)) : column.queryColumn(select.getTable()), // $NON-NLS-1$
    "maxval"));
    return select;
}
Also used : QueryAggregate(com.servoy.j2db.query.QueryAggregate) QueryColumnValue(com.servoy.j2db.query.QueryColumnValue) QueryColumn(com.servoy.j2db.query.QueryColumn) BaseQueryColumn(com.servoy.base.query.BaseQueryColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) QuerySelect(com.servoy.j2db.query.QuerySelect) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryTable(com.servoy.j2db.query.QueryTable)

Example 25 with QueryTable

use of com.servoy.j2db.query.QueryTable 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

QueryTable (com.servoy.j2db.query.QueryTable)35 BaseQueryTable (com.servoy.base.query.BaseQueryTable)22 QueryColumn (com.servoy.j2db.query.QueryColumn)22 QuerySelect (com.servoy.j2db.query.QuerySelect)19 Column (com.servoy.j2db.persistence.Column)18 ArrayList (java.util.ArrayList)18 ITable (com.servoy.j2db.persistence.ITable)15 RepositoryException (com.servoy.j2db.persistence.RepositoryException)13 IColumn (com.servoy.j2db.persistence.IColumn)12 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)10 Table (com.servoy.j2db.persistence.Table)10 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)9 SafeArrayList (com.servoy.j2db.util.SafeArrayList)9 ServoyException (com.servoy.j2db.util.ServoyException)9 RemoteException (java.rmi.RemoteException)9 CompareCondition (com.servoy.j2db.query.CompareCondition)8 QueryDelete (com.servoy.j2db.query.QueryDelete)8 ApplicationException (com.servoy.j2db.ApplicationException)7 SetCondition (com.servoy.j2db.query.SetCondition)7 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)6