Search in sources :

Example 11 with QuerySet

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

the class FoundSet method getSerializedQuery.

String getSerializedQuery() {
    QuerySelect currentQuery = pksAndRecords.getQuerySelectForReading();
    String serverName = DataSourceUtils.getDataSourceServerName(getDataSource());
    ArrayList<TableFilter> tableFilterParams = fsm.getTableFilterParams(serverName, currentQuery);
    try {
        QuerySet qs = fsm.getDataServer().getSQLQuerySet(serverName, currentQuery, tableFilterParams, 0, -1, true, true);
        return qs.getSelect().getSql();
    } catch (RepositoryException | RemoteException e) {
        // $NON-NLS-1$
        Debug.error("Can't get a serialized state from " + currentQuery, e);
    }
    // $NON-NLS-1$
    return "";
}
Also used : QuerySet(com.servoy.j2db.persistence.QuerySet) RepositoryException(com.servoy.j2db.persistence.RepositoryException) RemoteException(java.rmi.RemoteException) QuerySelect(com.servoy.j2db.query.QuerySelect)

Example 12 with QuerySet

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

the class JSDatabaseManager method js_getSQL.

/**
 * Returns the internal SQL which defines the specified (related)foundset.
 * Optionally, the foundset and table filter params can be excluded in the sql (includeFilters=false).
 * Make sure to set the applicable filters when the sql is used in a loadRecords() call.
 * When the founset is in find mode, the find conditions are included in the resulting query.
 *
 * @sample var sql = databaseManager.getSQL(foundset)
 *
 * @param foundsetOrQBSelect The JSFoundset or QBSelect to get the sql for.
 * @param includeFilters include the foundset and table filters.
 *
 * @return String representing the sql of the JSFoundset.
 */
public String js_getSQL(Object foundsetOrQBSelect, boolean includeFilters) throws ServoyException {
    checkAuthorized();
    if (foundsetOrQBSelect instanceof IFoundSetInternal && ((IFoundSetInternal) foundsetOrQBSelect).getTable() != null) {
        try {
            QuerySet querySet = getQuerySet(((IFoundSetInternal) foundsetOrQBSelect).getCurrentStateQuery(true, false), includeFilters);
            StringBuilder sql = new StringBuilder();
            QueryString[] prepares = querySet.getPrepares();
            for (int i = 0; prepares != null && i < prepares.length; i++) {
            // TODO parameters from updates and cleanups
            // sql.append(updates[i].getSql());
            // sql.append("\n"); //$NON-NLS-1$
            }
            sql.append(querySet.getSelect().getSql());
            QueryString[] cleanups = querySet.getCleanups();
            for (int i = 0; cleanups != null && i < cleanups.length; i++) {
            // TODO parameters from updates and cleanups
            // sql.append("\n"); //$NON-NLS-1$
            // sql.append(cleanups[i].getSql());
            }
            return sql.toString();
        } catch (Exception e) {
            Debug.error(e);
        }
    } else if (foundsetOrQBSelect instanceof QBSelect) {
        try {
            QuerySelect select = ((QBSelect) foundsetOrQBSelect).build();
            if (select.getColumns() == null) {
                // no columns, add pk
                // note that QBSelect.build() already returns a clone
                ITable table = application.getFoundSetManager().getTable(select.getTable().getDataSource());
                Iterator<Column> pkIt = ((Table) table).getRowIdentColumns().iterator();
                if (!pkIt.hasNext()) {
                    throw new RepositoryException(ServoyException.InternalCodes.PRIMARY_KEY_NOT_FOUND, new Object[] { table.getName() });
                }
                while (pkIt.hasNext()) {
                    Column c = pkIt.next();
                    select.addColumn(c.queryColumn(select.getTable()));
                }
            }
            QuerySet querySet = getQuerySet(select, includeFilters);
            return querySet.getSelect().getSql();
        } catch (RemoteException e) {
            Debug.error(e);
        }
    }
    return null;
}
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) QueryString(com.servoy.j2db.persistence.QueryString) QuerySet(com.servoy.j2db.persistence.QuerySet) 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) QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) 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) Iterator(java.util.Iterator) ITable(com.servoy.j2db.persistence.ITable) NativeObject(org.mozilla.javascript.NativeObject) RemoteException(java.rmi.RemoteException)

Aggregations

QuerySet (com.servoy.j2db.persistence.QuerySet)12 RepositoryException (com.servoy.j2db.persistence.RepositoryException)4 QuerySelect (com.servoy.j2db.query.QuerySelect)4 Column (com.servoy.j2db.persistence.Column)3 ITable (com.servoy.j2db.persistence.ITable)3 Table (com.servoy.j2db.persistence.Table)3 RemoteException (java.rmi.RemoteException)3 IBaseColumn (com.servoy.base.persistence.IBaseColumn)2 BaseQueryTable (com.servoy.base.query.BaseQueryTable)2 ApplicationException (com.servoy.j2db.ApplicationException)2 QueryString (com.servoy.j2db.persistence.QueryString)2 QueryColumn (com.servoy.j2db.query.QueryColumn)2 QueryTable (com.servoy.j2db.query.QueryTable)2 QBColumn (com.servoy.j2db.querybuilder.impl.QBColumn)2 QBSelect (com.servoy.j2db.querybuilder.impl.QBSelect)2 ServoyException (com.servoy.j2db.util.ServoyException)2 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 NativeObject (org.mozilla.javascript.NativeObject)2 BufferedDataSet (com.servoy.j2db.dataprocessing.BufferedDataSet)1