use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class ComponentFormat method getComponentFormat.
public static ComponentFormat getComponentFormat(String format, IDataProvider dataProvider, IServiceProvider application, boolean autoFillMaxLength) {
int dpType = IColumnTypes.TEXT;
String formatProperty = format;
if (dataProvider != null) {
dpType = dataProvider.getDataProviderType();
IColumn column = null;
if (dataProvider instanceof ColumnWrapper) {
column = ((ColumnWrapper) dataProvider).getColumn();
} else if (dataProvider instanceof Column) {
column = (Column) dataProvider;
} else if (dataProvider instanceof ScriptCalculation) {
// When it is a stored calculation, the name of the calc is the name of he column
ScriptCalculation calc = (ScriptCalculation) dataProvider;
try {
ITable table = calc.getTable();
if (table != null) {
column = table.getColumn(calc.getName());
}
} catch (RepositoryException e) {
Debug.error(e);
}
}
if (column instanceof AggregateVariable) {
Column columnToAggregate = null;
try {
ITable table = column.getTable();
if (table != null) {
columnToAggregate = table.getColumn(((AggregateVariable) column).getDataProviderIDToAggregate());
}
} catch (RepositoryException e) {
Debug.error(e);
}
// Use aggregated column when they are of the same type (so not count(textcolumn))
if (columnToAggregate != null && column.getDataProviderType() == columnToAggregate.getDataProviderType()) {
column = columnToAggregate;
}
}
if (column instanceof Column) {
ColumnInfo ci = ((Column) column).getColumnInfo();
if (ci != null) {
if (formatProperty == null || formatProperty.length() == 0) {
if (ci.getDefaultFormat() != null && ci.getDefaultFormat().length() > 0) {
formatProperty = ci.getDefaultFormat();
}
}
dpType = application.getFoundSetManager().getConvertedTypeForColumn(column, true);
}
}
}
ComponentFormat componentFormat = ComponentFormat.getComponentFormat(formatProperty, dpType, application);
if (autoFillMaxLength && dataProvider != null && dataProvider.getLength() > 0 && componentFormat.parsedFormat != null && componentFormat.parsedFormat.getMaxLength() == null && (dpType == IColumnTypes.TEXT || dpType == IColumnTypes.MEDIA)) {
componentFormat.parsedFormat.updateMaxLength(Integer.valueOf(dataProvider.getLength()));
}
return componentFormat;
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class JSDatabaseManager method js_getSQL.
/**
* Returns the internal SQL which defines the specified (related)foundset.
* Optionally, the foundset and table filter params can be excluded in the sql (includeFilters=false).
* Make sure to set the applicable filters when the sql is used in a loadRecords() call.
* When the founset is in find mode, the find conditions are included in the resulting query.
*
* @sample var sql = databaseManager.getSQL(foundset)
*
* @param foundsetOrQBSelect The JSFoundset or QBSelect to get the sql for.
* @param includeFilters include the foundset and table filters.
*
* @return String representing the sql of the JSFoundset.
*/
public String js_getSQL(Object foundsetOrQBSelect, boolean includeFilters) throws ServoyException {
checkAuthorized();
if (foundsetOrQBSelect instanceof IFoundSetInternal && ((IFoundSetInternal) foundsetOrQBSelect).getTable() != null) {
try {
QuerySet querySet = getQuerySet(((IFoundSetInternal) foundsetOrQBSelect).getCurrentStateQuery(true, false), includeFilters);
StringBuilder sql = new StringBuilder();
QueryString[] prepares = querySet.getPrepares();
for (int i = 0; prepares != null && i < prepares.length; i++) {
// TODO parameters from updates and cleanups
// sql.append(updates[i].getSql());
// sql.append("\n"); //$NON-NLS-1$
}
sql.append(querySet.getSelect().getSql());
QueryString[] cleanups = querySet.getCleanups();
for (int i = 0; cleanups != null && i < cleanups.length; i++) {
// TODO parameters from updates and cleanups
// sql.append("\n"); //$NON-NLS-1$
// sql.append(cleanups[i].getSql());
}
return sql.toString();
} 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);
return querySet.getSelect().getSql();
} catch (RemoteException e) {
Debug.error(e);
}
}
return null;
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class JSDatabaseManager method js_updateTableFilterParam.
/**
* Updates a filter with a new condition. The server/table name should be unchanged.
*
* @sample
* databaseManager.updateTableFilterParam('database', 'myfilter', 'your_i18n_table', 'message_variant', 'in', [1, 2])
*
* @param serverName The name of the database server connection for the specified table name.
* @param filterName The name of the filter that should be updated.
* @param tableName The name of the specified table.
* @param dataprovider A specified dataprovider column name.
* @param operator One of "=, <, >, >=, <=, !=, LIKE, or IN" optionally augmented with modifiers "#" (ignore case) or "^||" (or-is-null), prefix with "sql:" to allow the value to be interpreted as a custom query.
* @param value The specified filter value.
*
* @return true if the tablefilter could be updated.
*/
public boolean js_updateTableFilterParam(String serverName, String filterName, String tableName, String dataprovider, String operator, Object value) throws ServoyException {
checkAuthorized();
try {
if (value instanceof Wrapper) {
value = ((Wrapper) value).unwrap();
}
IServer server = application.getSolution().getServer(serverName);
if (server == null) {
application.reportJSError("Table filter not applied to unknown server '" + serverName + "', tableName = '" + tableName + "', dataprovider = '" + dataprovider + "', operator = '" + operator + "', value = '" + value + "', filterName = '" + filterName + "'", null);
return false;
}
ITable table = null;
if (tableName != null) {
table = server.getTable(tableName);
if (table == null) {
application.reportJSError("Table filter not applied to unknown table: serverName = '" + serverName + "', tableName = '" + tableName + "', dataprovider = '" + dataprovider + "', operator = '" + operator + "', value = '" + value + "', filterName = '" + filterName + "'", null);
return false;
}
}
// else table remains null: apply to all tables with that column
DataproviderTableFilterdefinition dataproviderTableFilterdefinition = application.getFoundSetManager().createDataproviderTableFilterdefinition(table, dataprovider, operator, value);
if (dataproviderTableFilterdefinition == null) {
application.reportJSError("Table filter not created, column not found in table or operator invalid, filterName = '" + filterName + "', serverName = '" + serverName + "', table = '" + table + "', dataprovider = '" + dataprovider + "', operator = '" + operator + "'", null);
return false;
}
return (((FoundSetManager) application.getFoundSetManager()).updateTableFilterParam(serverName, filterName, table, dataproviderTableFilterdefinition));
} catch (Exception ex) {
Debug.error(ex);
}
return false;
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class JSDatabaseManager method convertFoundSet.
public FoundSet convertFoundSet(Object foundset, Object related) throws ServoyException {
checkAuthorized();
if (foundset instanceof FoundSet && ((FoundSet) foundset).getTable() != null) {
FoundSet fs_old = (FoundSet) foundset;
try {
String relationName;
if (related instanceof RelatedFoundSet) {
relationName = ((RelatedFoundSet) related).getRelationName();
} else if (related instanceof String) {
relationName = (String) related;
} else {
// $NON-NLS-1$
Debug.warn("convertFoundSet: invalid argument " + related);
return null;
}
Relation relation = application.getFlattenedSolution().getRelation(relationName);
if (relation == null || relation.isMultiServer() || fs_old.getTable() == null || !fs_old.getTable().equals(application.getFlattenedSolution().getTable(relation.getPrimaryDataSource()))) {
// $NON-NLS-1$
Debug.warn("convertFoundSet: cannot use relation " + relationName);
return null;
}
ITable ft = application.getFlattenedSolution().getTable(relation.getForeignDataSource());
FoundSet fs_new = (FoundSet) application.getFoundSetManager().getNewFoundSet(ft, null, application.getFoundSetManager().getDefaultPKSortColumns(ft.getDataSource()));
QuerySelect sql = fs_old.getPksAndRecords().getQuerySelectForModification();
SQLSheet sheet_new = fs_old.getSQLSheet().getRelatedSheet(relation, ((FoundSetManager) application.getFoundSetManager()).getSQLGenerator());
if (sheet_new != null) {
BaseQueryTable oldTable = sql.getTable();
ISQLTableJoin join = (ISQLTableJoin) sql.getJoin(oldTable, relation.getName());
if (join == null) {
join = SQLGenerator.createJoin(application.getFlattenedSolution(), relation, oldTable, new QueryTable(ft.getSQLName(), ft.getDataSource(), ft.getCatalog(), ft.getSchema()), true, fs_old);
sql.addJoin(join);
}
BaseQueryTable mainTable = join.getForeignTable();
// invert the join
sql.setTable(mainTable);
// $NON-NLS-1$
join.invert("INVERTED." + join.getName());
// set the columns to be the PKs from the related table
ArrayList<IQuerySelectValue> pkColumns = new ArrayList<IQuerySelectValue>();
Iterator<Column> pks = sheet_new.getTable().getRowIdentColumns().iterator();
while (pks.hasNext()) {
Column column = pks.next();
pkColumns.add(column.queryColumn(mainTable));
}
sql.setColumns(pkColumns);
// sorting will be on the original columns, when distinct is set, this will conflict with the related pk columns
sql.setDistinct(false);
fs_new.setSQLSelect(sql);
return fs_new;
}
} catch (Exception e) {
Debug.error(e);
}
}
return null;
}
use of com.servoy.j2db.persistence.ITable in project servoy-client by Servoy.
the class FoundSetManager method registerViewFoundSet.
@Override
public boolean registerViewFoundSet(ViewFoundSet foundset, boolean onlyWeak) {
if (foundset == null)
return false;
if (onlyWeak) {
noneRegisteredVFS.put(foundset, TRUE);
} else {
ViewFoundSet oldValue = viewFoundSets.put(foundset.getDataSource(), foundset);
ITable table = foundset.getTable();
if (!viewDataSources.containsKey(foundset.getDataSource()))
viewDataSources.put(foundset.getDataSource(), table);
if (oldValue != null) {
for (IFormController controller : application.getFormManager().getCachedFormControllers()) {
if (controller.getFormModel() == oldValue) {
controller.loadAllRecords();
}
}
}
}
return true;
}
Aggregations