use of com.servoy.j2db.query.QueryTable in project servoy-client by Servoy.
the class SQLGenerator method makeRelatedSQL.
synchronized void makeRelatedSQL(SQLSheet relatedSheet, Relation r) {
if (relatedSheet.getRelatedSQLDescription(r.getName()) != null)
return;
try {
FlattenedSolution fs = application.getFlattenedSolution();
if (!Relation.isValid(r, fs) || r.isParentRef()) {
return;
}
ITable ft = fs.getTable(r.getForeignDataSource());
if (ft == null) {
return;
}
// add primary keys if missing
QueryTable foreignQTable = new QueryTable(ft.getSQLName(), ft.getDataSource(), ft.getCatalog(), ft.getSchema());
QuerySelect relatedSelect = new QuerySelect(foreignQTable);
List<String> parentRequiredDataProviderIDs = new ArrayList<String>();
Column[] relcols = r.getForeignColumns(fs);
for (Column column : relcols) {
parentRequiredDataProviderIDs.add(column.getDataProviderID());
}
relatedSelect.setCondition(CONDITION_RELATION, createRelatedCondition(application, r, foreignQTable));
Collection<Column> rcolumns = ft.getColumns();
relatedSelect.setColumns(makeQueryColumns(rcolumns.iterator(), foreignQTable, null));
// fill dataprovider map
List<String> dataProviderIDsDilivery = new ArrayList<String>();
Iterator<Column> it = rcolumns.iterator();
while (it.hasNext()) {
Column col = it.next();
dataProviderIDsDilivery.add(col.getDataProviderID());
}
relatedSheet.addRelatedSelect(r.getName(), relatedSelect, dataProviderIDsDilivery, parentRequiredDataProviderIDs, null);
createAggregates(relatedSheet, foreignQTable);
} catch (RepositoryException e) {
Debug.error(e);
}
}
use of com.servoy.j2db.query.QueryTable in project servoy-client by Servoy.
the class SQLGenerator method createUpdateLockSelect.
public static QuerySelect createUpdateLockSelect(Table table, Object[][] pkValues, boolean lockInDb) {
QuerySelect lockSelect = new QuerySelect(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()));
if (lockInDb)
lockSelect.setLockMode(ISQLSelect.LOCK_MODE_LOCK_NOWAIT);
LinkedHashMap<Column, QueryColumn> allQueryColumns = new LinkedHashMap<>();
for (Column column : table.getColumns()) {
allQueryColumns.put(column, column.queryColumn(lockSelect.getTable()));
}
lockSelect.setColumns(new ArrayList<IQuerySelectValue>(allQueryColumns.values()));
// get the pk columns, make sure the order is in pk-order (alphabetical)
ArrayList<QueryColumn> pkQueryColumns = new ArrayList<>();
for (Column pkColumn : table.getRowIdentColumns()) {
pkQueryColumns.add(allQueryColumns.get(pkColumn));
}
// values is an array as wide as the columns, each element consists of the values for that column
Object[][] values = new Object[pkQueryColumns.size()][];
for (int k = 0; k < pkQueryColumns.size(); k++) {
values[k] = new Object[pkValues.length];
for (int r = 0; r < pkValues.length; r++) {
values[k][r] = pkValues[r][k];
}
}
lockSelect.setCondition(CONDITION_LOCK, new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, pkQueryColumns.toArray(new QueryColumn[pkQueryColumns.size()]), values, true));
return lockSelect;
}
use of com.servoy.j2db.query.QueryTable in project servoy-client by Servoy.
the class SQLGenerator method createAggregates.
private void createAggregates(SQLSheet sheet, QueryTable queryTable) throws RepositoryException {
Table table = sheet.getTable();
Iterator<AggregateVariable> it = application.getFlattenedSolution().getAggregateVariables(table, false);
while (it.hasNext()) {
AggregateVariable aggregate = it.next();
QuerySelect sql = new QuerySelect(queryTable);
sql.addColumn(new QueryAggregate(aggregate.getType(), new QueryColumn(queryTable, -1, aggregate.getColumnNameToAggregate(), aggregate.getDataProviderType(), aggregate.getLength(), 0, null, aggregate.getFlags()), aggregate.getName()));
sheet.addAggregate(aggregate.getDataProviderID(), aggregate.getDataProviderIDToAggregate(), sql);
}
}
use of com.servoy.j2db.query.QueryTable in project servoy-client by Servoy.
the class SQLGenerator method createDynamicPKSetConditionForFoundset.
static SetCondition createDynamicPKSetConditionForFoundset(FoundSet foundSet, BaseQueryTable queryTable, IDataSet pks) {
Table table = (Table) foundSet.getTable();
List<Column> rowIdentColumns = table.getRowIdentColumns();
QueryColumn[] pkQueryColumns = new QueryColumn[rowIdentColumns.size()];
// getPrimaryKeys from table
for (int i = 0; i < rowIdentColumns.size(); i++) {
Column column = rowIdentColumns.get(i);
pkQueryColumns[i] = column.queryColumn(queryTable);
}
// Dynamic PK condition, the special placeholder will be updated when the foundset pk set changes
Placeholder placeHolder = new Placeholder(new TablePlaceholderKey(queryTable, SQLGenerator.PLACEHOLDER_FOUNDSET_PKS));
placeHolder.setValue(new DynamicPkValuesArray(rowIdentColumns, pks.clone()));
return new SetCondition(IBaseSQLCondition.EQUALS_OPERATOR, pkQueryColumns, placeHolder, true);
}
use of com.servoy.j2db.query.QueryTable in project servoy-client by Servoy.
the class MetaDataUtils method createTableMetadataQuery.
public static QuerySelect createTableMetadataQuery(ITable table, LinkedHashMap<Column, QueryColumn> queryColumns) {
QuerySelect query = new QuerySelect(new QueryTable(table.getSQLName(), table.getDataSource(), table.getCatalog(), table.getSchema()));
// LinkedHashMap to keep order for column names
LinkedHashMap<Column, QueryColumn> qColumns = queryColumns == null ? new LinkedHashMap<Column, QueryColumn>() : queryColumns;
Iterator<Column> columns = table.getColumnsSortedByName();
while (columns.hasNext()) {
Column column = columns.next();
if (!column.hasFlag(IBaseColumn.EXCLUDED_COLUMN)) {
QueryColumn qColumn = column.queryColumn(query.getTable());
query.addColumn(qColumn);
qColumns.put(column, qColumn);
}
}
for (Column column : table.getRowIdentColumns()) {
if (qColumns.containsKey(column)) {
query.addSort(new QuerySort(qColumns.get(column), true, SortOptions.NONE));
}
}
return query;
}
Aggregations