use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewDataRepository method retrievePage.
@Override
public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection orderedSelection, final int firstRow, final int pageLength) throws DBException {
logger.debug("Getting page: firstRow={}, pageLength={} - {}", firstRow, pageLength, this);
logger.debug("Using: {}", orderedSelection);
final ViewId viewId = orderedSelection.getViewId();
final SqlAndParams sqlAndParams = sqlViewSelect.selectByPage().viewEvalCtx(viewEvalCtx).viewId(viewId).firstRowZeroBased(firstRow).pageLength(pageLength).build();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(pageLength);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());
rs = pstmt.executeQuery();
final List<IViewRow> page = loadViewRows(rs, viewEvalCtx, viewId, pageLength);
return page;
} catch (final SQLException | DBException e) {
throw DBException.wrapIfNeeded(e).setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
} finally {
DB.close(rs, pstmt);
}
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewDataRepository method retrieveById.
@Override
public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentId rowId) {
final SqlAndParams sqlAndParams = sqlViewSelect.selectById().viewEvalCtx(viewEvalCtx).viewId(viewId).rowId(rowId).build();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
final int limit = 2;
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(limit);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());
rs = pstmt.executeQuery();
final List<IViewRow> documents = loadViewRows(rs, viewEvalCtx, viewId, limit);
if (documents.isEmpty()) {
throw new EntityNotFoundException("No document found for rowId=" + rowId);
} else if (documents.size() > 1) {
logger.warn("More than one document found for rowId={} in {}. Returning only the first one from: {}", rowId, this, documents);
return documents.get(0);
} else {
return documents.get(0);
}
} catch (final SQLException | DBException e) {
throw DBException.wrapIfNeeded(e).setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
} finally {
DB.close(rs, pstmt);
}
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewRowIdsOrderedSelectionFactory method retrieveRowIdsForLineIds.
public static Set<DocumentId> retrieveRowIdsForLineIds(@NonNull SqlViewKeyColumnNamesMap keyColumnNamesMap, final ViewId viewId, final Set<Integer> lineIds) {
final SqlAndParams sqlAndParams = SqlViewSelectionQueryBuilder.buildSqlSelectRowIdsForLineIds(keyColumnNamesMap, viewId.getViewId(), lineIds);
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());
rs = pstmt.executeQuery();
final ImmutableSet.Builder<DocumentId> rowIds = ImmutableSet.builder();
while (rs.next()) {
final DocumentId rowId = keyColumnNamesMap.retrieveRowId(rs, "", false);
rowIds.add(rowId);
}
return rowIds.build();
} catch (final SQLException ex) {
throw new DBException(ex, sqlAndParams.getSql(), sqlAndParams.getSqlParams());
} finally {
DB.close(rs, pstmt);
}
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlHUEditorViewRepository method retrieveHUIdsPage.
@Override
public Page<Integer> retrieveHUIdsPage(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection selection, final int firstRow, final int maxRows) {
final SqlAndParams sqlAndParams = sqlViewSelect.selectByPage().viewEvalCtx(viewEvalCtx).viewId(selection.getViewId()).firstRowZeroBased(firstRow).pageLength(maxRows).build();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(maxRows);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());
rs = pstmt.executeQuery();
final Set<Integer> huIds = new LinkedHashSet<>();
int lastRowMax = -1;
while (rs.next()) {
final int huId = rs.getInt(I_M_HU.COLUMNNAME_M_HU_ID);
if (huId <= 0) {
continue;
}
huIds.add(huId);
final int lastRow = rs.getInt(SqlViewSelectData.COLUMNNAME_Paging_SeqNo_OneBased);
lastRowMax = Math.max(lastRowMax, lastRow);
}
if (huIds.isEmpty()) {
// shall not happen
return null;
} else {
final int lastRowZeroBased = lastRowMax - 1;
return Page.ofRowsAndLastRowIndex(ImmutableList.copyOf(huIds), lastRowZeroBased);
}
} catch (final SQLException ex) {
throw DBException.wrapIfNeeded(ex).setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
} finally {
DB.close(rs, pstmt);
}
}
use of de.metas.ui.web.view.descriptor.SqlAndParams in project metasfresh-webui-api by metasfresh.
the class SqlViewDataRepository method retrieveRowLines.
private List<IViewRow> retrieveRowLines(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentIdsSelection rowIds) {
logger.debug("Getting row lines: rowId={} - {}", rowIds, this);
logger.debug("Using: {}", viewId);
final SqlAndParams sqlAndParams = sqlViewSelect.selectIncludedLines().viewEvalCtx(viewEvalCtx).viewId(viewId).rowIds(rowIds).build();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());
rs = pstmt.executeQuery();
final List<IViewRow> lines = loadViewRows(rs, viewEvalCtx, viewId, -1);
return lines;
} catch (final SQLException | DBException e) {
throw DBException.wrapIfNeeded(e).setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
} finally {
DB.close(rs, pstmt);
}
}
Aggregations