use of com.servoy.j2db.query.ColumnType in project servoy-client by Servoy.
the class JSDatabaseManager method js_convertToDataSet.
// why did we add this method ?
// @JSFunction
// public Collection<Procedure> js_getProcedures(String serverName) throws Exception
// {
// Collection<Procedure> procedures = application.getSolution().getServer(serverName).getProcedures();
// return procedures;
// }
/**
* @clonedesc js_convertToDataSet(IFoundSetInternal)
*
* @sampleas js_convertToDataSet(IFoundSetInternal)
*
* @param ids Concatenated values to be put into dataset.
*
* @return JSDataSet with the data.
*/
public JSDataSet js_convertToDataSet(String ids) {
if (ids == null) {
return null;
}
// $NON-NLS-1$
String[] dpnames = { "id" };
ColumnType[] dptypes = { ColumnType.getInstance(IColumnTypes.INTEGER, Integer.MAX_VALUE, 0) };
List<Object[]> lst = new ArrayList<Object[]>();
// $NON-NLS-1$
StringTokenizer st = new StringTokenizer(ids, ",;\n\r\t ");
while (st.hasMoreElements()) {
Object o = st.nextElement();
if (o instanceof Double && ((Double) o).doubleValue() == ((Double) o).intValue()) {
o = new Integer(((Double) o).intValue());
}
lst.add(new Object[] { o });
}
return new JSDataSet(application, BufferedDataSetInternal.createBufferedDataSet(dpnames, dptypes, lst, false));
}
use of com.servoy.j2db.query.ColumnType in project servoy-client by Servoy.
the class XMLUtils method parseColumnTypeArray.
/**
* Parse an array string '[[tp,len,scale], [tp,len,scale], ...]' as ColumnType list
*/
public static List<ColumnType> parseColumnTypeArray(String s) {
if (s == null)
return null;
List<ColumnType> list = null;
JSONSerializer serializer = new JSONSerializer();
try {
serializer.registerDefaultSerializers();
Integer[][] array = (Integer[][]) serializer.fromJSON(s);
if (array != null && array.length > 0) {
list = new ArrayList<ColumnType>(array.length);
for (Integer[] elem : array) {
list.add(ColumnType.getInstance(elem[0].intValue(), elem[1].intValue(), elem[2].intValue()));
}
}
} catch (Exception e) {
Debug.error(e);
}
return list;
}
use of com.servoy.j2db.query.ColumnType in project servoy-client by Servoy.
the class JSDatabaseManager method js_convertToDataSet.
/**
* @clonedesc js_convertToDataSet(IFoundSetInternal)
*
* @sampleas js_convertToDataSet(IFoundSetInternal)
*
* @param values The values array.
* @param dataproviderNames The property names array.
*
* @return JSDataSet with the data.
*/
public JSDataSet js_convertToDataSet(Object[] values, String[] dataproviderNames) {
if (values == null) {
return null;
}
// $NON-NLS-1$
String[] dpnames = { "id" };
ColumnType[] dptypes = { ColumnType.getInstance(IColumnTypes.INTEGER, Integer.MAX_VALUE, 0) };
List<Object[]> lst = new ArrayList<Object[]>();
Object[] array = values;
if (dataproviderNames != null) {
dpnames = dataproviderNames;
}
Map<String, Method> getters = new HashMap<String, Method>();
for (Object o : array) {
if (o instanceof Number || o instanceof String || o instanceof UUID || o instanceof Date) {
if (o instanceof Double && ((Double) o).doubleValue() == ((Double) o).intValue()) {
o = new Integer(((Double) o).intValue());
}
lst.add(new Object[] { o });
} else if (o instanceof Scriptable) {
List<Object> row = new ArrayList<Object>();
for (String dpname : dpnames) {
if (((Scriptable) o).has(dpname, (Scriptable) o))
row.add(ScriptVariableScope.unwrap(((Scriptable) o).get(dpname, (Scriptable) o)));
}
if (dpnames.length != row.size() || dpnames.length == 0) {
// for backward compatibility
lst.add(new Object[] { o });
} else {
lst.add(row.toArray());
}
} else if (o != null) {
// try reflection
List<Object> row = new ArrayList<Object>();
for (String dpname : dpnames) {
Method m = getMethod(o, dpname, getters);
if (m != null) {
try {
row.add(m.invoke(o, (Object[]) null));
} catch (Exception e) {
Debug.error(e);
}
}
}
if (dpnames.length != row.size() || dpnames.length == 0) {
// for backward compatibility
lst.add(new Object[] { o });
} else {
lst.add(row.toArray());
}
}
}
return new JSDataSet(application, BufferedDataSetInternal.createBufferedDataSet(dpnames, dptypes, lst, false));
}
use of com.servoy.j2db.query.ColumnType in project servoy-client by Servoy.
the class JSDatabaseManager method js_convertToDataSet.
/**
* @clonedesc js_convertToDataSet(IFoundSetInternal)
*
* @sampleas js_convertToDataSet(IFoundSetInternal)
*
* @param foundset The foundset to be converted.
* @param dataproviderNames Array with column names.
*
* @return JSDataSet with the data.
*/
public JSDataSet js_convertToDataSet(IFoundSetInternal foundset, String[] dataproviderNames) throws RepositoryException {
if (foundset == null) {
return null;
}
// $NON-NLS-1$
String[] dpnames = { "id" };
ColumnType[] dptypes = { ColumnType.getInstance(IColumnTypes.INTEGER, Integer.MAX_VALUE, 0) };
List<Object[]> lst = new ArrayList<Object[]>();
FoundSet fs = (FoundSet) foundset;
if (fs.getTable() != null) {
if (dataproviderNames != null) {
dpnames = dataproviderNames;
} else {
dpnames = fs.getSQLSheet().getPKColumnDataProvidersAsArray();
}
FoundSetManager fsm = (FoundSetManager) application.getFoundSetManager();
boolean getInOneQuery = !fs.isInFindMode() && (fs.hadMoreRows() || fs.getSize() > fsm.config.pkChunkSize()) && !fsm.getEditRecordList().hasEditedRecords(fs);
dptypes = new ColumnType[dpnames.length];
Table table = fs.getSQLSheet().getTable();
Map<String, Column> columnMap = new HashMap<String, Column>();
for (int i = 0; i < dpnames.length; i++) {
IDataProvider dp = application.getFlattenedSolution().getDataProviderForTable(table, dpnames[i]);
dptypes[i] = dp == null ? ColumnType.getInstance(0, 0, 0) : ColumnType.getInstance(dp instanceof Column ? ((Column) dp).getType() : dp.getDataProviderType(), dp.getLength(), dp instanceof Column ? ((Column) dp).getScale() : 0);
if (getInOneQuery) {
// only columns and data we can get from the foundset (calculations only when stored)
if (dp instanceof Column) {
columnMap.put(dpnames[i], (Column) dp);
// Blobs require special resultset handling
getInOneQuery = !SQLGenerator.isBlobColumn((Column) dp);
} else {
// aggregates, globals
getInOneQuery = fs.containsDataProvider(dpnames[i]);
}
}
}
if (getInOneQuery && columnMap.size() > 0) {
// large foundset, query the columns in 1 go
QuerySelect sqlSelect = AbstractBaseQuery.deepClone(fs.getQuerySelectForReading());
ArrayList<IQuerySelectValue> cols = new ArrayList<IQuerySelectValue>(columnMap.size());
ArrayList<String> distinctColumns = new ArrayList<String>(columnMap.size());
for (String dpname : dpnames) {
Column column = columnMap.get(dpname);
if (column != null && !distinctColumns.contains(dpname)) {
distinctColumns.add(dpname);
cols.add(column.queryColumn(sqlSelect.getTable()));
}
}
boolean hasJoins = sqlSelect.getJoins() != null;
if (hasJoins) {
// add pk columns so distinct-in-memory can be used
List<Column> rowIdentColumns = ((Table) fs.getTable()).getRowIdentColumns();
for (Column column : rowIdentColumns) {
if (!columnMap.containsKey(column.getDataProviderID())) {
cols.add(column.queryColumn(sqlSelect.getTable()));
}
}
}
sqlSelect.setColumns(cols);
try {
SQLSheet sheet = fs.getSQLSheet();
IConverterManager<IColumnConverter> columnConverterManager = ((FoundSetManager) fs.getFoundSetManager()).getColumnConverterManager();
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(sqlSelect.getColumnNames(), new Object[][] {}, new Object[][] {}, fsm.getApplication().getUserUID(), fsm.getTrackingInfo(), fsm.getApplication().getClientID());
}
IDataSet dataSet = fsm.getDataServer().performQuery(fsm.getApplication().getClientID(), sheet.getServerName(), fsm.getTransactionID(sheet), sqlSelect, null, fsm.getTableFilterParams(sheet.getServerName(), sqlSelect), hasJoins, 0, -1, IDataServer.FOUNDSET_LOAD_QUERY, trackingInfo);
lst = new ArrayList<Object[]>(dataSet.getRowCount());
for (int i = 0; i < dataSet.getRowCount(); i++) {
Object[] row = new Object[dpnames.length];
// may contain more data: pk columns for distinct-in-memory
Object[] dataseRow = dataSet.getRow(i);
for (int j = 0; j < dpnames.length; j++) {
Column column = columnMap.get(dpnames[j]);
if (column == null) {
// fs.containsDataProvider returned true for this dpname
row[j] = fs.getDataProviderValue(dpnames[j]);
} else {
row[j] = sheet.convertValueToObject(dataseRow[distinctColumns.indexOf(dpnames[j])], sheet.getColumnIndex(dpnames[j]), columnConverterManager);
}
}
lst.add(row);
}
} catch (RepositoryException e) {
throw e;
} catch (Exception e) {
Debug.error(e);
throw new RepositoryException(e.getMessage());
}
} else {
// loop over the records
for (int i = 0; i < fs.getSize(); i++) {
IRecordInternal record = fs.getRecord(i);
Object[] pk = new Object[dpnames.length];
for (int j = 0; j < dpnames.length; j++) {
pk[j] = record.getValue(dpnames[j]);
}
lst.add(pk);
}
}
}
return new JSDataSet(application, BufferedDataSetInternal.createBufferedDataSet(dpnames, dptypes, lst, false));
}
Aggregations