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;
}
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;
}
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);
}
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;
}
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);
}
Aggregations