use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class JSTableObject method js_createNewColumn.
/**
* Creates a new column in this table. The name, type and length of the new column must be specified. For specifying the
* type of the column, use the JSColumn constants. The column is not actually created in the database until this
* table is synchronized with the database using the JSServer.synchronizeWithDB method.
*
* The method returns a JSColumn instance that corresponds to the newly created column. If any error occurs and the column cannot be created, then the method
* returns null.
* @see JSColumnObject
*
* @sample
* var server = plugins.maintenance.getServer("example_data");
* if (server)
* {
* var table = server.createNewTable("users");
* if (table)
* {
* var pk = table.createNewColumn("id", JSColumn.MEDIA, 16); // can also use (JSColumn.TEXT, 36) for UUIDs
* pk.rowIdentifierType = JSColumn.PK_COLUMN;
* pk.setFlag(JSColumn.UUID_COLUMN, true)
* pk.sequenceType = JSColumn.UUID_GENERATOR
* var c = table.createNewColumn("name", JSColumn.TEXT, 100);
* c.allowNull = false
* table.createNewColumn("age", JSColumn.INTEGER, 0);
* table.createNewColumn("last_login", JSColumn.DATETIME, 0);
* var result = server.synchronizeWithDB(table);
* if (result) application.output("Table successfully created.");
* else application.output("Table not created.");
* }
* }
*
* @param columnName
* @param type
* @param length
* @param allowNull
* @param pkColumn
*/
public JSColumnObject js_createNewColumn(String columnName, Number type, Number length, Boolean allowNull, Boolean pkColumn) {
int _type = Utils.getAsInteger(type);
int _length = Utils.getAsInteger(length);
boolean _allowNull = Utils.getAsBoolean(allowNull);
boolean _pkColumn = Utils.getAsBoolean(pkColumn);
try {
Column c = ((Table) getTable()).createNewColumn(DummyValidator.INSTANCE, columnName, _type, _length, _allowNull, _pkColumn);
return new JSColumnObject(c, getServer(), getTable());
} catch (RepositoryException e) {
// $NON-NLS-1$
Debug.error("Exception while creating new column.", e);
return null;
}
}
use of com.servoy.j2db.persistence.Column 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;
}
use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class JSDatabaseManager method js_getFoundSetDataProviderAsArray.
/**
* This method is deprecated, use databaseManager.convertToDataSet(foundset, pkNames) instead.
*
* @sample
* var dataSet = databaseManager.convertToDataSet(foundset,['order_id']);
*
* @deprecated As of release 6.0, replaced by {@link #convertToDataSet(Object[])}.
*
* @param foundset The foundset
* @param dataprovider The dataprovider for the values of the array.
*
* @return An Array with the column values.
*/
@Deprecated
public Object[] js_getFoundSetDataProviderAsArray(Object foundset, String dataprovider) throws ServoyException {
checkAuthorized();
if (foundset instanceof FoundSet && ((FoundSet) foundset).getSQLSheet().getTable() != null) {
FoundSet fs = (FoundSet) foundset;
FoundSetManager fsm = (FoundSetManager) application.getFoundSetManager();
SQLSheet sheet = fs.getSQLSheet();
Column column = sheet.getTable().getColumn(dataprovider);
if (column != null) {
IDataSet dataSet = null;
if ((fs.hadMoreRows() || fs.getSize() > fsm.config.pkChunkSize()) && !fsm.getEditRecordList().hasEditedRecords(fs)) {
// large foundset, query the column in 1 go
QuerySelect sqlSelect = AbstractBaseQuery.deepClone(fs.getQuerySelectForReading());
ArrayList<IQuerySelectValue> cols = new ArrayList<IQuerySelectValue>(1);
cols.add(column.queryColumn(sqlSelect.getTable()));
sqlSelect.setColumns(cols);
SQLStatement trackingInfo = null;
if (fsm.getEditRecordList().hasAccess(sheet.getTable(), IRepository.TRACKING_VIEWS)) {
trackingInfo = new SQLStatement(ISQLActionTypes.SELECT_ACTION, sheet.getServerName(), sheet.getTable().getName(), null, null);
trackingInfo.setTrackingData(new String[] { column.getSQLName() }, new Object[][] {}, new Object[][] {}, fsm.getApplication().getUserUID(), fsm.getTrackingInfo(), fsm.getApplication().getClientID());
}
try {
dataSet = fsm.getDataServer().performQuery(fsm.getApplication().getClientID(), sheet.getServerName(), fsm.getTransactionID(sheet), sqlSelect, null, fsm.getTableFilterParams(sheet.getServerName(), sqlSelect), false, 0, -1, IDataServer.FOUNDSET_LOAD_QUERY, trackingInfo);
} catch (RemoteException e) {
Debug.error(e);
return new Object[0];
} catch (ServoyException e) {
Debug.error(e);
return new Object[0];
}
} else {
// small foundset or there are edited records
List<Column> pks = fs.getSQLSheet().getTable().getRowIdentColumns();
if (// if is pk optimize
pks.size() == 1 && pks.get(0).equals(column)) {
PksAndRecordsHolder pksAndRecordsCopy;
PKDataSet pkds;
boolean queryForMore;
int rowCount;
synchronized (fs.getPksAndRecords()) {
pksAndRecordsCopy = fs.getPksAndRecords().shallowCopy();
pkds = pksAndRecordsCopy.getPks();
queryForMore = pkds == null || pkds.hadMoreRows();
rowCount = pkds == null ? 0 : pkds.getRowCount();
}
if (queryForMore) {
fs.queryForMorePKs(pksAndRecordsCopy, rowCount, -1, true);
}
dataSet = pkds;
}
}
if (dataSet != null) {
Object[] retval = new Object[dataSet.getRowCount()];
for (int i = 0; i < retval.length; i++) {
Object[] dataSetRow = dataSet.getRow(i);
if (dataSetRow == null) {
Debug.warn(// $NON-NLS-1$//$NON-NLS-2$
"js_getFoundSetDataProviderAsArray - null row at index: " + i + " when getting dataprovider: " + dataprovider + " from foundset: " + // $NON-NLS-1$
foundset);
retval[i] = null;
} else {
Object value = dataSetRow[0];
if (column.hasFlag(IBaseColumn.UUID_COLUMN)) {
// this is a UUID column, first convert to UUID (could be string or byte array (media)) - so we can get/use it as a valid uuid string
value = Utils.getAsUUID(value, false);
}
retval[i] = value;
}
}
return retval;
}
}
// cannot het the data via a dataset, use the records (could be slow)
List<Object> lst = new ArrayList<Object>();
for (int i = 0; i < fs.getSize(); i++) {
IRecordInternal r = fs.getRecord(i);
Object value = r.getValue(dataprovider);
if (value instanceof Date) {
value = new Date(((Date) value).getTime());
}
lst.add(value);
}
return lst.toArray();
}
return new Object[0];
}
use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class JSDatabaseManager method js_getSQLParameters.
/**
* Returns the internal SQL parameters, as an array, that are used to define the specified (related)foundset.
* When the founset is in find mode, the arguments for the find conditions are included in the result.
*
* @sample var sqlParameterArray = databaseManager.getSQLParameters(foundset,false)
*
* @param foundsetOrQBSelect The JSFoundset or QBSelect to get the sql parameters for.
* @param includeFilters include the parameters for the filters.
*
* @return An Array with the sql parameter values.
*/
public Object[] js_getSQLParameters(Object foundsetOrQBSelect, boolean includeFilters) throws ServoyException {
checkAuthorized();
if (foundsetOrQBSelect instanceof IFoundSetInternal && ((IFoundSetInternal) foundsetOrQBSelect).getTable() != null) {
try {
// TODO parameters from updates and cleanups
QuerySet querySet = getQuerySet(((IFoundSetInternal) foundsetOrQBSelect).getCurrentStateQuery(true, false), includeFilters);
Object[][] qsParams = querySet.getSelect().getParameters();
if (qsParams == null || qsParams.length == 0) {
return null;
}
return qsParams[0];
} 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);
Object[][] qsParams = querySet.getSelect().getParameters();
if (qsParams == null || qsParams.length == 0) {
return null;
}
return qsParams[0];
} catch (RemoteException e) {
Debug.error(e);
}
}
return null;
}
use of com.servoy.j2db.persistence.Column in project servoy-client by Servoy.
the class JSDatabaseManager method copyMatchingFields.
public boolean copyMatchingFields(Object src, IRecordInternal dest, boolean overwrite, Object[] names) throws ServoyException {
checkAuthorized();
if (dest.getParentFoundSet().getSQLSheet() == null) {
return false;
}
List<Object> al = new ArrayList<Object>();
if (names != null) {
al = Arrays.asList(names);
}
try {
SQLSheet destSheet = dest.getParentFoundSet().getSQLSheet();
Table dest_table = destSheet.getTable();
boolean wasEditing = dest.isEditing();
Map<String, Method> getters = new HashMap<String, Method>();
if (dest.startEditing()) {
Iterator<Column> it = dest_table.getColumns().iterator();
while (it.hasNext()) {
Column c = it.next();
ColumnInfo ci = c.getColumnInfo();
if (ci != null && ci.isExcluded()) {
continue;
}
if (al.contains(c.getDataProviderID())) {
// skip, also if value in dest_rec is null
continue;
}
Object dval = dest.getValue(c.getDataProviderID());
if (dval == null || (!dest_table.getRowIdentColumns().contains(c) && (overwrite || (al.size() > 0 && !al.contains(c.getDataProviderID()))))) {
if (src instanceof IRecordInternal) {
IRecordInternal src_rec = (IRecordInternal) src;
int index = src_rec.getParentFoundSet().getColumnIndex(c.getDataProviderID());
if (index != -1) {
Object sval = src_rec.getValue(c.getDataProviderID());
try {
int type = ((FoundSetManager) application.getFoundSetManager()).getConvertedTypeForColumn(c, false);
dest.setValue(c.getDataProviderID(), Column.getAsRightType(type, c.getFlags(), sval, c.getLength(), true, true));
} catch (Exception e) {
application.reportJSError("Could not copy matching field to " + dest_table.getName() + "." + c.getDataProviderID() + ". The value: '" + sval + "' does not match the type of the destination.", e);
}
}
} else if (src instanceof NativeObject) {
NativeObject no = ((NativeObject) src);
if (no.has(c.getDataProviderID(), no)) {
Object raw_val = no.get(c.getDataProviderID(), no);
Object val = c.getAsRightType(raw_val);
dest.setValue(c.getDataProviderID(), val);
}
} else if (src != null) {
Method m = getMethod(src, c.getDataProviderID(), getters);
if (m != null) {
Object raw_val = m.invoke(src, (Object[]) null);
Object val = c.getAsRightType(raw_val);
dest.setValue(c.getDataProviderID(), val);
}
}
}
}
if (!wasEditing) {
dest.stopEditing();
}
return true;
}
} catch (Exception e) {
Debug.error(e);
}
return false;
}
Aggregations