use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.
the class FoundSetManager method getViewFoundSet.
@SuppressWarnings("nls")
@Override
public ViewFoundSet getViewFoundSet(String name, QBSelect query) {
if (query.getQuery().getColumns() == null || query.getQuery().getColumns().size() == 0) {
throw new RuntimeException("Can't create a ViewFoundset with name: " + name + " and query " + query + " that has no columns");
}
String dataSource = DataSourceUtils.createViewDataSource(name);
ViewFoundSet vfs = new ViewFoundSet(dataSource, query.build(), application.getFoundSetManager(), pkChunkSize);
// if this datasource defintion is created already in the developer then we need to check if the query columns are correctly matching it.
ServoyJSONObject columnsDef = null;
Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
while (tblIte.hasNext() && columnsDef == null) {
TableNode tn = tblIte.next();
columnsDef = tn.getColumns();
if (columnsDef != null) {
TableDef def = DatabaseUtils.deserializeTableInfo(columnsDef);
for (ColumnInfoDef col : def.columnInfoDefSet) {
IQuerySelectValue selectValue = getSelectvalue(query, col.name);
if (selectValue == null) {
Debug.error("Column " + col.name + " of type " + col.columnType.toString() + " defined in view datasource '" + dataSource + "' was not found in the provided query.");
return null;
}
BaseColumnType columnType = selectValue.getColumnType();
// relax the mapping on default Servoy types
if (columnType != null && Column.mapToDefaultType(columnType.getSqlType()) != Column.mapToDefaultType(col.columnType.getSqlType())) {
Debug.error("Column type for column '" + col.name + " of type " + col.columnType.toString() + "' defined in view datasource '" + dataSource + "' does not match the one " + columnType + " provided in the query.");
return null;
}
}
}
}
return vfs;
}
use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.
the class SQLGenerator method getEmptyDataSetForDummyQuery.
/**
* check if the query is will never return any rows, in that case just return an empty dataset.
*/
public static IDataSet getEmptyDataSetForDummyQuery(ISQLSelect sqlSelect) {
if (sqlSelect instanceof QuerySelect && ((QuerySelect) sqlSelect).getCondition(CONDITION_SEARCH) != null) {
// all named conditions in QuerySelecta are AND-ed, if one always results to false, skip the query
for (IBaseSQLCondition condition : ((QuerySelect) sqlSelect).getCondition(CONDITION_SEARCH).getConditions()) {
boolean skipQuery = false;
if (condition instanceof SetCondition && ((SetCondition) condition).isAndCondition()) {
// check for EQUALS_OPERATOR
int ncols = ((SetCondition) condition).getKeys().length;
int[] operators = ((SetCondition) condition).getOperators();
boolean eqop = true;
for (int i = 0; i < ncols; i++) {
if (operators[i] != IBaseSQLCondition.EQUALS_OPERATOR) {
eqop = false;
}
}
if (eqop) {
Object value = ((SetCondition) condition).getValues();
if (value instanceof Placeholder) {
Object phval = ((Placeholder) value).getValue();
// cleared foundset
skipQuery = phval instanceof DynamicPkValuesArray && ((DynamicPkValuesArray) phval).getPKs().getRowCount() == 0;
} else if (value instanceof Object[][]) {
skipQuery = ((Object[][]) value).length == 0 || ((Object[][]) value)[0].length == 0;
}
}
}
if (skipQuery) {
// no need to query, dummy condition (where 1=2) here
List<IQuerySelectValue> columns = ((QuerySelect) sqlSelect).getColumns();
String[] columnNames = new String[columns.size()];
ColumnType[] columnTypes = new ColumnType[columns.size()];
for (int i = 0; i < columns.size(); i++) {
IQuerySelectValue col = columns.get(i);
columnNames[i] = col.getAliasOrName();
BaseColumnType columnType = col.getColumnType();
columnTypes[i] = columnType == null ? ColumnType.getInstance(Types.OTHER, 0, 0) : ColumnType.getInstance(columnType.getSqlType(), columnType.getLength(), columnType.getScale());
}
return BufferedDataSetInternal.createBufferedDataSet(columnNames, columnTypes, new SafeArrayList<Object[]>(0), false);
}
}
}
// query needs to be run
return null;
}
use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.
the class MetaDataUtils method serializeTableMetaDataContents.
/**
* Serialize contents of buffered dataset to a string, includes column names and type info
* @param dataSet
* @return
* @throws JSONException
*/
public static String serializeTableMetaDataContents(BufferedDataSet dataSet) throws JSONException {
if (dataSet == null) {
return null;
}
ServoyJSONObject json = new ServoyJSONObject();
// columns
JSONArray jsonColumns = new JSONArray();
String[] columnNames = dataSet.getColumnNames();
BaseColumnType[] columnTypes = BufferedDataSetInternal.getColumnTypeInfo(dataSet);
for (int c = 0; c < columnNames.length; c++) {
JSONObject jsonColumn = new JSONObject();
jsonColumn.put("name", columnNames[c]);
jsonColumn.put("type", XMLUtils.serializeColumnType(columnTypes[c]));
jsonColumns.put(jsonColumn);
}
json.put("columns", jsonColumns);
// rows
JSONArray jsonRows = new JSONArray();
for (int r = 0; r < dataSet.getRowCount(); r++) {
Object[] row = dataSet.getRow(r);
JSONArray rowobj = new JSONArray();
for (int i = 0; i < row.length && i < columnNames.length; i++) {
Object val;
if (row[i] == null) {
val = JSONObject.NULL;
} else if (row[i] instanceof byte[]) {
val = Utils.encodeBASE64((byte[]) row[i]);
} else {
val = row[i];
}
rowobj.put(val);
}
jsonRows.put(rowobj);
}
json.put("rows", jsonRows);
// toString
return json.toString(true);
}
use of com.servoy.base.query.BaseColumnType 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;
}
use of com.servoy.base.query.BaseColumnType in project servoy-client by Servoy.
the class FoundSetManager method getViewFoundSet.
@SuppressWarnings("nls")
@Override
public ViewFoundSet getViewFoundSet(String name, QBSelect query, boolean register) {
if (query.getQuery().getColumns() == null || query.getQuery().getColumns().size() == 0) {
throw new RuntimeException("Can't create a ViewFoundset with name: " + name + " and query " + query + " that has no columns");
}
String dataSource = DataSourceUtils.createViewDataSource(name);
ViewFoundSet vfs = new ViewFoundSet(dataSource, query.build(), application.getFoundSetManager(), config.pkChunkSize());
// if this datasource defintion is created already in the developer then we need to check if the query columns are correctly matching it.
ServoyJSONObject columnsDef = null;
Iterator<TableNode> tblIte = application.getFlattenedSolution().getTableNodes(dataSource);
while (tblIte.hasNext() && columnsDef == null) {
TableNode tn = tblIte.next();
columnsDef = tn.getColumns();
if (columnsDef != null) {
TableDef def = DatabaseUtils.deserializeTableInfo(columnsDef);
for (ColumnInfoDef col : def.columnInfoDefSet) {
IQuerySelectValue selectValue = getSelectvalue(query, col.name);
if (selectValue == null) {
Debug.error("Column " + col.name + " of type " + col.columnType.toString() + " defined in view datasource '" + dataSource + "' was not found in the provided query.");
return null;
}
BaseColumnType columnType = selectValue.getColumnType();
// relax the mapping on default Servoy types
if (columnType != null && Column.mapToDefaultType(columnType.getSqlType()) != Column.mapToDefaultType(col.columnType.getSqlType())) {
Debug.error("Column type for column '" + col.name + " of type " + col.columnType.toString() + "' defined in view datasource '" + dataSource + "' does not match the one " + columnType + " provided in the query.");
return null;
}
}
}
}
registerViewFoundSet(vfs, !register);
return vfs;
}
Aggregations