Search in sources :

Example 1 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class QueryBuilderSerializer method marshall.

public Object marshall(SerializerState state, Object p, Object o) throws MarshallException {
    if (!(o instanceof QBSelect)) {
        throw new MarshallException("QueryBuilderSerializer cannot marshall class " + o.getClass());
    }
    QBSelect qbSelect = (QBSelect) o;
    QuerySelect query = qbSelect.getQuery(false);
    // make sure that queries are serialized in full, standard serialization optimizations result in missing data that cannot be resolved
    Writer writer = new StringWriter();
    getXstream().marshal(query, new CompactWriter(writer));
    String xml = writer.toString();
    JSONObject obj = new JSONObject();
    try {
        if (ser.getMarshallClassHints()) {
            obj.put("javaClass", o.getClass().getName());
        }
        // required
        obj.put("query", xml);
        // required
        obj.put("datasource", qbSelect.getDataSource());
        // optional
        obj.put("alias", qbSelect.getTableAlias());
    } catch (JSONException e) {
        throw new MarshallException(e.getMessage(), e);
    }
    return obj;
}
Also used : QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) StringWriter(java.io.StringWriter) JSONObject(org.json.JSONObject) MarshallException(org.jabsorb.serializer.MarshallException) JSONException(org.json.JSONException) QuerySelect(com.servoy.j2db.query.QuerySelect) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter)

Example 2 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class QueryBuilderSerializer method tryUnmarshall.

public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    if (queryBuilderFactoryProvider.getQueryBuilderFactory() == null) {
        throw new UnmarshallException("QueryBuilderSerializer needs query builder factory");
    }
    JSONObject jso = (JSONObject) o;
    String java_class;
    try {
        java_class = jso.getString("javaClass");
    } catch (JSONException e) {
        throw new UnmarshallException("no type hint", e);
    }
    if (java_class == null) {
        throw new UnmarshallException("no type hint");
    }
    if (!(java_class.equals(QBSelect.class.getName()))) {
        throw new UnmarshallException("not a QBSelect");
    }
    state.setSerialized(o, ObjectMatch.OKAY);
    return ObjectMatch.OKAY;
}
Also used : QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 3 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class FoundSetManager method getDataSetByQuery.

public IDataSet getDataSetByQuery(IQueryBuilder query, boolean useTableFilters, int max_returned_rows) throws ServoyException {
    if (!application.haveRepositoryAccess()) {
        // no access to repository yet, have to log in first
        return null;
    }
    QBSelect select = (QBSelect) query;
    String serverName = getDataSourceServerName(select.getDataSource());
    if (serverName == null)
        throw new RuntimeException(new ServoyException(ServoyException.InternalCodes.SERVER_NOT_FOUND, new Object[] { select.getDataSource() }));
    return getDataSetByQuery(serverName, select.build(), useTableFilters, max_returned_rows);
}
Also used : QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) ServoyException(com.servoy.j2db.util.ServoyException)

Example 4 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class JSDatabaseManager method js_getSQLParameters.

/**
 * Returns the internal SQL parameters, as an array, that are used to define the specified (related)foundset.
 * When the founset is in find mode, the arguments for the find conditions are included in the result.
 *
 * @sample var sqlParameterArray = databaseManager.getSQLParameters(foundset,false)
 *
 * @param foundsetOrQBSelect The JSFoundset or QBSelect to get the sql parameters for.
 * @param includeFilters include the parameters for the filters.
 *
 * @return An Array with the sql parameter values.
 */
public Object[] js_getSQLParameters(Object foundsetOrQBSelect, boolean includeFilters) throws ServoyException {
    checkAuthorized();
    if (foundsetOrQBSelect instanceof IFoundSetInternal && ((IFoundSetInternal) foundsetOrQBSelect).getTable() != null) {
        try {
            // TODO parameters from updates and cleanups
            QuerySet querySet = getQuerySet(((IFoundSetInternal) foundsetOrQBSelect).getCurrentStateQuery(true, false), includeFilters);
            Object[][] qsParams = querySet.getSelect().getParameters();
            if (qsParams == null || qsParams.length == 0) {
                return null;
            }
            return qsParams[0];
        } 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);
            Object[][] qsParams = querySet.getSelect().getParameters();
            if (qsParams == null || qsParams.length == 0) {
                return null;
            }
            return qsParams[0];
        } 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) 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)

Example 5 with QBSelect

use of com.servoy.j2db.querybuilder.impl.QBSelect in project servoy-client by Servoy.

the class ViewFoundSet method getQuery.

/**
 * Get the cloned query that created this ViewFoundSset (modifying this QBSelect will not change the foundset).
 * The ViewFoundSets main query can't be altered after creation; you need to make a new ViewFoundSet for that (it can have the same datasource name).
 *
 * @sample
 * var q = foundset.getQuery()
 * q.where.add(q.columns.x.eq(100))
 * var newVF = databaseManager.getViewFoundset("name", q);
 *
 * @return query.
 */
@JSFunction
public QBSelect getQuery() {
    QuerySelect query = AbstractBaseQuery.deepClone(this.select, true);
    IApplication application = manager.getApplication();
    return new QBSelect(manager, manager.getScopesScopeProvider(), application.getFlattenedSolution(), application.getScriptEngine().getSolutionScope(), select.getTable().getDataSource(), null, query);
}
Also used : IApplication(com.servoy.j2db.IApplication) QBSelect(com.servoy.j2db.querybuilder.impl.QBSelect) QuerySelect(com.servoy.j2db.query.QuerySelect) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

QBSelect (com.servoy.j2db.querybuilder.impl.QBSelect)10 QuerySelect (com.servoy.j2db.query.QuerySelect)7 Column (com.servoy.j2db.persistence.Column)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 QueryColumn (com.servoy.j2db.query.QueryColumn)3 ServoyException (com.servoy.j2db.util.ServoyException)3 IBaseColumn (com.servoy.base.persistence.IBaseColumn)2 BaseQueryTable (com.servoy.base.query.BaseQueryTable)2 ApplicationException (com.servoy.j2db.ApplicationException)2 ITable (com.servoy.j2db.persistence.ITable)2 QuerySet (com.servoy.j2db.persistence.QuerySet)2 Table (com.servoy.j2db.persistence.Table)2 QueryTable (com.servoy.j2db.query.QueryTable)2 QBColumn (com.servoy.j2db.querybuilder.impl.QBColumn)2 RemoteException (java.rmi.RemoteException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2