use of com.servoy.j2db.dataprocessing.SQLSheet.VariableInfo in project servoy-client by Servoy.
the class JSFoundSetUpdater method js_performUpdate.
/**
* Do the actual update in the database, returns true if successful. It will first try to save all editing records (from all foundsets), if cannot save will return false before doing the update.
* There are 3 types of possible use with the foundset updater
* 1) update entire foundset by a single sql statement; that is not possible when the table of the foundset has tracking enabled then it will loop over the whole foundset.
* When a single sql statement is done, modification columns will not be updated and associated Table Events won't be triggered, because it does the update directly in the database, without getting the records.
* NOTE: this mode will refresh all foundsets based on same datasource
* 2) update part of foundset, for example the first 4 row (starts with selected row)
* 3) safely loop through foundset (starts with selected row)
*
* after the perform update call there are no records in edit mode, that where not already in edit mode, because all of them are saved directly to the database,
* or in mode 1 the records are not touched at all and the database is updated directly.
*
* @sample
* //1) update entire foundset
* var fsUpdater = databaseManager.getFoundSetUpdater(foundset)
* fsUpdater.setColumn('customer_type',1)
* fsUpdater.setColumn('my_flag',0)
* fsUpdater.performUpdate()
*
* //2) update part of foundset, for example the first 4 row (starts with selected row)
* var fsUpdater = databaseManager.getFoundSetUpdater(foundset)
* fsUpdater.setColumn('customer_type',new Array(1,2,3,4))
* fsUpdater.setColumn('my_flag',new Array(1,0,1,0))
* fsUpdater.performUpdate()
*
* //3) safely loop through foundset (starts with selected row)
* controller.setSelectedIndex(1)
* var count = 0
* var fsUpdater = databaseManager.getFoundSetUpdater(foundset)
* while(fsUpdater.next())
* {
* fsUpdater.setColumn('my_flag',count++)
* }
*
* @return true if succeeded, false if failed.
*/
public boolean js_performUpdate() throws ServoyException {
if (list.size() == 0 || foundset.getTable() == null) {
return false;
}
if (!foundset.hasAccess(IRepository.UPDATE)) {
throw new ApplicationException(ServoyException.NO_MODIFY_ACCESS, new Object[] { foundset.getTable().getName() });
}
// first stop all edits, 'force' stop the edit by saying that it is a javascript stop
if (application.getFoundSetManager().getEditRecordList().stopEditing(true) != ISaveConstants.STOPPED)
return false;
try {
QuerySelect sqlParts;
IDataSet currentPKs;
synchronized (foundset.getPksAndRecords()) {
sqlParts = foundset.getPksAndRecords().getQuerySelectForReading();
currentPKs = foundset.getPksAndRecords().getPks();
}
FoundSetManager fsm = (FoundSetManager) application.getFoundSetManager();
if (rowsToUpdate == -1 && !foundset.hasAccess(IRepository.TRACKING) && sqlParts.getJoins() == null && // does not have join to other table
!fsm.hasTableFiltersWithJoins(foundset.getTable().getServerName(), sqlParts)) {
// all rows at once, via sql
Table table = (Table) foundset.getTable();
SQLSheet sheet = foundset.getSQLSheet();
QueryUpdate sqlUpdate = new QueryUpdate(sqlParts.getTable());
for (Pair<String, Object> p : list) {
String name = p.getLeft();
Object val = p.getRight();
int columnIndex = sheet.getColumnIndex(name);
VariableInfo variableInfo = sheet.getCalculationOrColumnVariableInfo(name, columnIndex);
if (// do not convert null to 0 incase of numbers, this means the calcs the value whould change each time //$NON-NLS-1$
val != null && !("".equals(val) && Column.mapToDefaultType(variableInfo.type) == IColumnTypes.TEXT)) {
val = sheet.convertObjectToValue(name, val, foundset.getFoundSetManager().getColumnConverterManager(), foundset.getFoundSetManager().getColumnValidatorManager(), null);
}
Column c = table.getColumn(name);
if (val == null) {
val = ValueFactory.createNullValue(c.getType());
}
sqlUpdate.addValue(c.queryColumn(sqlParts.getTable()), val);
}
sqlUpdate.setCondition(sqlParts.getWhereClone());
IDataSet pks;
boolean allFoundsetRecordsLoaded = currentPKs != null && currentPKs.getRowCount() <= fsm.config.pkChunkSize() && !currentPKs.hadMoreRows();
if (allFoundsetRecordsLoaded) {
pks = currentPKs;
} else {
pks = new BufferedDataSet();
pks.addRow(new Object[] { ValueFactory.createTableFlushValue() });
}
String transaction_id = fsm.getTransactionID(foundset.getSQLSheet());
try {
SQLStatement statement = new SQLStatement(ISQLActionTypes.UPDATE_ACTION, table.getServerName(), table.getName(), pks, transaction_id, sqlUpdate, fsm.getTableFilterParams(table.getServerName(), sqlUpdate));
if (allFoundsetRecordsLoaded) {
statement.setExpectedUpdateCount(pks.getRowCount());
}
Object[] results = fsm.getDataServer().performUpdates(fsm.getApplication().getClientID(), new ISQLStatement[] { statement });
for (int i = 0; results != null && i < results.length; i++) {
if (results[i] instanceof ServoyException) {
if (((ServoyException) results[i]).getErrorCode() == ServoyException.UNEXPECTED_UPDATE_COUNT) {
performLoopUpdate();
clear();
return true;
}
fsm.flushCachedDatabaseData(fsm.getDataSource(table));
throw (ServoyException) results[i];
}
}
fsm.flushCachedDatabaseData(fsm.getDataSource(table));
clear();
return true;
} catch (ApplicationException aex) {
if (allFoundsetRecordsLoaded || aex.getErrorCode() != ServoyException.RECORD_LOCKED) {
throw aex;
}
// a record was locked by another client, try per-record
Debug.log("foundsetUpdater could not update all records in 1 statement (a record may be locked), trying per-record");
}
}
performLoopUpdate();
} catch (Exception ex) {
// $NON-NLS-1$
application.handleException(// $NON-NLS-1$
application.getI18NMessage("servoy.foundsetupdater.updateFailed"), new ApplicationException(ServoyException.SAVE_FAILED, ex));
return false;
}
clear();
return true;
}
use of com.servoy.j2db.dataprocessing.SQLSheet.VariableInfo in project servoy-client by Servoy.
the class Row method setValue.
// returns the oldvalue, or value if no change
public Object setValue(IRowChangeListener src, String dataProviderID, Object value) {
Object o = getRawValue(dataProviderID);
// this column is controlled by the database - so do not allow sets until the database chose a value
if (o instanceof DbIdentValue)
return o;
Object convertedValue = value;
SQLSheet sheet = parent.getSQLSheet();
int columnIndex = sheet.getColumnIndex(dataProviderID);
VariableInfo variableInfo = sheet.getCalculationOrColumnVariableInfo(dataProviderID, columnIndex);
if (// do not convert null to 0 incase of numbers, this means the calcs the value whould change each time //$NON-NLS-1$
convertedValue != null && !("".equals(convertedValue) && Column.mapToDefaultType(variableInfo.type) == IColumnTypes.TEXT)) {
convertedValue = sheet.convertObjectToValue(dataProviderID, convertedValue, parent.getFoundsetManager().getColumnConverterManager(), parent.getFoundsetManager().getColumnValidatorManager(), src);
} else if (parent.getFoundsetManager().isNullColumnValidatorEnabled() && // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Settings.getInstance().getProperty("servoy.execute.column.validators.only.on.validate_and_save", "true").equals("false")) {
// check for not null constraint
Column c = null;
try {
c = sheet.getTable().getColumn(dataProviderID);
} catch (Exception e) {
Debug.error(e);
}
if (c != null && !c.getAllowNull()) {
// $NON-NLS-1$
throw new IllegalArgumentException(Messages.getString("servoy.record.error.validation", new Object[] { dataProviderID, convertedValue }));
}
}
boolean wasUNINITIALIZED = false;
if (o == UNINITIALIZED) {
o = null;
wasUNINITIALIZED = true;
}
boolean isCalculation = containsCalculation(dataProviderID);
// if we receive NULL from the db for Empty strings in Servoy calcs, return value
if (// $NON-NLS-1$
o == null && "".equals(convertedValue) && isCalculation) {
mustRecalculate(dataProviderID, false);
return convertedValue;
}
if (!Utils.equalObjects(o, convertedValue)) {
boolean mustStop = false;
if (columnIndex != -1 && columnIndex < columndata.length) {
mustStop = !parent.getFoundsetManager().getEditRecordList().isEditing();
if (// if not yet existInDB, leave startEdit to Foundset new/duplicateRecord code!
src != null && existInDB && !wasUNINITIALIZED) {
src.startEditing(false);
}
createOldValuesIfNeeded();
columndata[columnIndex] = convertedValue;
} else if (isCalculation) {
unstoredCalcCache.put(dataProviderID, convertedValue);
}
lastException = null;
// Reset the mustRecalculate here, before setValue fires events, so if it is an every time changing calculation it will not be calculated again and again
if (isCalculation) {
mustRecalculate(dataProviderID, false);
threadCalculationComplete(dataProviderID);
}
handleCalculationDependencies(sheet.getTable().getColumn(dataProviderID), dataProviderID);
FireCollector collector = FireCollector.getFireCollector();
try {
fireNotifyChange(dataProviderID, convertedValue, collector);
} finally {
collector.done();
}
if (src != null && mustStop && existInDB && !wasUNINITIALIZED) {
try {
src.stopEditing();
} catch (Exception e) {
Debug.error(e);
}
}
return o;
} else if (isCalculation) {
// Reset the mustRecalculate here, before setValue fires events, so if it is an every time changing calculation it will not be calculated again and again
mustRecalculate(dataProviderID, false);
}
// is same so return
return convertedValue;
}
Aggregations