use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class SQLGenerator method createTableFiltercondition.
public static QueryFilter createTableFiltercondition(BaseQueryTable qTable, Table table, DataproviderTableFilterdefinition filterdefinition) {
Column c = table.getColumn(filterdefinition.getDataprovider());
if (c == null) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Debug.error("Could not apply filter " + filterdefinition + " on table " + table + " : column not found:" + filterdefinition.getDataprovider());
return null;
}
int op = filterdefinition.getOperator();
int maskedOp = op & IBaseSQLCondition.OPERATOR_MASK;
Object value = filterdefinition.getValue();
QueryColumn qColumn = c.queryColumn(qTable);
ISQLCondition filterWhere;
if (maskedOp == IBaseSQLCondition.EQUALS_OPERATOR || maskedOp == IBaseSQLCondition.NOT_OPERATOR || maskedOp == IBaseSQLCondition.IN_OPERATOR || maskedOp == IBaseSQLCondition.NOT_IN_OPERATOR || maskedOp == IBaseSQLCondition.GT_OPERATOR || maskedOp == IBaseSQLCondition.LT_OPERATOR || maskedOp == IBaseSQLCondition.GTE_OPERATOR || maskedOp == IBaseSQLCondition.LTE_OPERATOR) {
Object inValues;
boolean andCondition = true;
if (value instanceof List<?>) {
inValues = new Object[][] { ((List<?>) value).toArray() };
andCondition = maskedOp != IBaseSQLCondition.NOT_OPERATOR && maskedOp != IBaseSQLCondition.NOT_IN_OPERATOR;
} else if (value != null && value.getClass().isArray()) {
inValues = new Object[][] { (Object[]) value };
andCondition = maskedOp != IBaseSQLCondition.NOT_OPERATOR && maskedOp != IBaseSQLCondition.NOT_IN_OPERATOR;
} else {
if (value != null && isSelectQuery(value.toString())) {
if ((op & IBaseSQLCondition.IS_SQL_MODIFIER) == 0) {
Debug.warn("Filter is created using a custom query without using the sql-modifier, this will be removed in a future version of servoy, please use operator '" + RelationItem.getOperatorAsString(op | IBaseSQLCondition.IS_SQL_MODIFIER) + "'");
}
// add as subquery
inValues = new QueryCustomSelect(value.toString(), null);
} else {
if (value != null && (op & IBaseSQLCondition.IS_SQL_MODIFIER) != 0) {
Debug.warn("Filter has the sql-modifier, but the value is not valid sql for filters: " + SQL_QUERY_VALIDATION_MESSAGE + ":" + value);
}
inValues = new Object[][] { new Object[] { value } };
}
}
// replace values with column type value
if (inValues instanceof Object[][]) {
Object[][] array = (Object[][]) inValues;
for (int i = 0; i < array.length; i++) {
for (int j = 0; array[i] != null && j < array[i].length; j++) {
Object v = c.getAsRightType(array[i][j]);
if (v == null)
v = ValueFactory.createNullValue(c.getType());
array[i][j] = v;
}
}
}
filterWhere = new SetCondition(op, new IQuerySelectValue[] { qColumn }, inValues, andCondition);
} else if (maskedOp == IBaseSQLCondition.BETWEEN_OPERATOR || maskedOp == IBaseSQLCondition.NOT_BETWEEN_OPERATOR) {
Object op1 = null;
Object op2 = null;
if (value instanceof List<?> && ((List<?>) value).size() > 1) {
op1 = ((List<?>) value).get(0);
op2 = ((List<?>) value).get(1);
} else if (value != null && value.getClass().isArray() && ((Object[]) value).length > 1) {
op1 = ((Object[]) value)[0];
op2 = ((Object[]) value)[1];
}
op1 = c.getAsRightType(op1);
if (op1 == null)
op1 = ValueFactory.createNullValue(c.getType());
op2 = c.getAsRightType(op2);
if (op2 == null)
op2 = ValueFactory.createNullValue(c.getType());
filterWhere = new CompareCondition(op, qColumn, new Object[] { op1, op2 });
} else {
Object operand;
if (maskedOp == IBaseSQLCondition.LIKE_OPERATOR || maskedOp == IBaseSQLCondition.NOT_LIKE_OPERATOR) {
operand = value;
} else {
operand = c.getAsRightType(value);
if (operand == null)
operand = ValueFactory.createNullValue(c.getType());
}
filterWhere = new CompareCondition(op, qColumn, operand);
}
return new QueryFilter(filterWhere);
}
use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class SQLGenerator method createAggregateSelect.
public static QuerySelect createAggregateSelect(QuerySelect sqlSelect, Collection<QuerySelect> aggregates, List<Column> pkColumns) {
QuerySelect selectClone = deepClone(sqlSelect);
selectClone.clearSorts();
selectClone.setDistinct(false);
selectClone.setColumns(null);
selectClone.removeUnusedJoins(true);
QuerySelect aggregateSqlSelect;
if (selectClone.getJoins() == null) {
// simple case, no joins
// Select count(pk) from main where <condition>
aggregateSqlSelect = selectClone;
} else {
// we have joins, change to an exists-query to make the aggregates correct, otherwise duplicate records
// in the main table cause incorrect aggregate values
// Select count(pk) from main main1 where exists (select 1 from main main2 join detail on detail.FK = main2.FK where main1.PK = main2.PK and <condition>)
QuerySelect innerSelect = selectClone;
ArrayList<IQuerySelectValue> innerColumns = new ArrayList<IQuerySelectValue>();
innerColumns.add(new QueryColumnValue(Integer.valueOf(1), null, true));
innerSelect.setColumns(innerColumns);
BaseQueryTable innerTable = innerSelect.getTable();
QueryTable outerTable = new QueryTable(innerTable.getName(), innerTable.getDataSource(), innerTable.getCatalogName(), innerTable.getSchemaName());
aggregateSqlSelect = new QuerySelect(outerTable);
for (Column column : pkColumns) {
// $NON-NLS-1$
innerSelect.addCondition(// $NON-NLS-1$
"EXISTS", new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, column.queryColumn(innerTable), column.queryColumn(outerTable)));
}
// $NON-NLS-1$
aggregateSqlSelect.addCondition("EXISTS", new ExistsCondition(innerSelect, true));
}
ArrayList<IQuerySelectValue> columns = new ArrayList<IQuerySelectValue>();
for (QuerySelect aggregate : aggregates) {
columns.addAll(AbstractBaseQuery.relinkTable(aggregate.getTable(), aggregateSqlSelect.getTable(), aggregate.getColumnsClone()));
}
aggregateSqlSelect.setColumns(columns);
return aggregateSqlSelect;
}
use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class SQLSheet method getColumnConverterInfo.
public ConverterInfo getColumnConverterInfo(int columnIndex) {
if (converterInfos == null) {
SQLDescription desc = sql.get(SELECT);
List<String> dataProviderIDsDilivery = desc.getDataProviderIDsDilivery();
ConverterInfo[] cis = new ConverterInfo[dataProviderIDsDilivery.size()];
for (int i = 0; i < dataProviderIDsDilivery.size(); i++) {
String cdp = dataProviderIDsDilivery.get(i);
Column c = table.getColumn(cdp);
ColumnInfo ci = c.getColumnInfo();
if (ci != null && ci.getConverterName() != null && ci.getConverterName().trim().length() != 0) {
Map<String, String> props = null;
try {
props = ComponentFactory.<String>parseJSonProperties(ci.getConverterProperties());
} catch (IOException e) {
Debug.error("Could not parse column converter properties", e);
}
cis[i] = new ConverterInfo(ci.getConverterName(), props);
}
}
converterInfos = cis;
}
if (columnIndex < 0 || columnIndex >= converterInfos.length) {
return null;
}
return converterInfos[columnIndex];
}
use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class SQLSheet method getColumnValidatorInfo.
public Pair<String, Map<String, String>> getColumnValidatorInfo(int columnIndex) {
if (validatorInfos == null) {
SQLDescription desc = sql.get(SELECT);
List<?> dataProviderIDsDilivery = desc.getDataProviderIDsDilivery();
@SuppressWarnings("unchecked") Pair<String, Map<String, String>>[] vis = new Pair[dataProviderIDsDilivery.size()];
int i = 0;
Iterator<?> it = dataProviderIDsDilivery.iterator();
while (it.hasNext()) {
String cdp = (String) it.next();
Column c = table.getColumn(cdp);
ColumnInfo ci = c.getColumnInfo();
if (ci != null && ci.getValidatorName() != null && ci.getValidatorName().trim().length() != 0) {
Map<String, String> parsedValidatorProperties = null;
try {
parsedValidatorProperties = ComponentFactory.parseJSonProperties(ci.getValidatorProperties());
} catch (IOException e) {
Debug.error(e);
}
vis[i] = new Pair<String, Map<String, String>>(ci.getValidatorName(), parsedValidatorProperties);
}
i++;
}
validatorInfos = vis;
}
return validatorInfos[columnIndex];
}
use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class ViewFoundSet method getTable.
@Override
public ITable getTable() {
if (table == null) {
try {
table = manager.getTable(getDataSource());
if (table == null) {
table = new Table(IServer.VIEW_SERVER, DataSourceUtils.getViewDataSourceName(getDataSource()), true, ITable.VIEW, null, null);
((Table) table).setDataSource(getDataSource());
for (IQuerySelectValue col : select.getColumns()) {
Column newCol = null;
QueryColumn qCol = col.getColumn();
if (qCol != null && qCol.getTable() != null) {
ITable colTable = manager.getTable(qCol.getTable().getDataSource());
if (colTable != null) {
Column column = colTable.getColumn(qCol.getName());
if (column != null) {
String colname = getColunmName(col, qCol);
newCol = table.createNewColumn(DummyValidator.INSTANCE, colname, column.getType(), column.getLength(), column.getScale(), column.getAllowNull());
if (column.getColumnInfo() != null) {
DatabaseUtils.createNewColumnInfo(manager.getApplication().getFlattenedSolution().getPersistFactory().getNewElementID(null), newCol, false);
newCol.getColumnInfo().copyFrom(column.getColumnInfo());
}
}
}
}
if (newCol == null) {
// existing database column not found, create column on the fly
BaseColumnType columnType = col.getColumnType();
if (columnType == null) {
columnType = ColumnType.getColumnType(IColumnTypes.TEXT);
}
String colname = getColunmName(col, qCol);
table.createNewColumn(DummyValidator.INSTANCE, colname, columnType.getSqlType(), columnType.getLength(), columnType.getScale(), true);
}
}
}
} catch (RepositoryException e) {
Debug.error(e);
}
}
return table;
}
Aggregations