下面列出了java.sql.ResultSet#TYPE_SCROLL_SENSITIVE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
CassandraStatement(CassandraConnection con, String cql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException
{
this.connection = con;
this.cql = cql;
this.batchQueries = Lists.newArrayList();
this.consistencyLevel = con.defaultConsistencyLevel;
if (!(resultSetType == ResultSet.TYPE_FORWARD_ONLY
|| resultSetType == ResultSet.TYPE_SCROLL_INSENSITIVE
|| resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE)) throw new SQLSyntaxErrorException(BAD_TYPE_RSET);
this.resultSetType = resultSetType;
if (!(resultSetConcurrency == ResultSet.CONCUR_READ_ONLY
|| resultSetConcurrency == ResultSet.CONCUR_UPDATABLE)) throw new SQLSyntaxErrorException(BAD_TYPE_RSET);
this.resultSetConcurrency = resultSetConcurrency;
if (!(resultSetHoldability == ResultSet.HOLD_CURSORS_OVER_COMMIT
|| resultSetHoldability == ResultSet.CLOSE_CURSORS_AT_COMMIT))
throw new SQLSyntaxErrorException(BAD_HOLD_RSET);
this.resultSetHoldability = resultSetHoldability;
}
/**
* Returns the resultType if set if not set returns the default
* java.sql.ResultSet.TYPE_FORWARD_ONLY
*/
static int getResultType(StatementAttrs attrs) {
int rsType;
if (attrs != null && attrs.isSetResultSetType()) {
rsType = attrs.getResultSetType();
}
else {
rsType = gfxdConstants.DEFAULT_RESULTSET_TYPE;
}
switch (rsType) {
case gfxdConstants.RESULTSET_TYPE_FORWARD_ONLY:
return ResultSet.TYPE_FORWARD_ONLY;
case gfxdConstants.RESULTSET_TYPE_INSENSITIVE:
return ResultSet.TYPE_SCROLL_INSENSITIVE;
case gfxdConstants.RESULTSET_TYPE_SENSITIVE:
return ResultSet.TYPE_SCROLL_SENSITIVE;
default:
throw new InternalGemFireError("unknown resultSet type "
+ attrs.getResultSetType());
}
}
/**
* JDBC 2.0
*
* Does the database support the concurrency type in combination
* with the given result set type?
*
* @param type defined in java.sql.ResultSet
* @param concurrency type defined in java.sql.ResultSet
* @return true if so
* @see Connection
*/
public boolean supportsResultSetConcurrency(int type, int concurrency) {
if (type == ResultSet.TYPE_SCROLL_SENSITIVE) {
// (TYPE_SCROLL_SENSITIVE, *)
return false;
} else {
// (FORWARD_ONLY, CONCUR_UPDATABLE)
// (FORWARD_ONLY, CONCUR_READ_ONLY)
// (TYPE_SCROLL_INSENSITIVE, CONCUR_UPDATABLE)
// (TYPE_SCROLL_INSENSITIVE, READ_ONLY)
return true;
}
}
private int setResultSetType(int resultSetType) {
/* Add warning if scroll sensitive cursor
* and downgrade to scroll insensitive cursor.
*/
if (resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE)
{
addWarning(SQLWarningFactory.newSQLWarning(SQLState.NO_SCROLL_SENSITIVE_CURSORS));
resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
}
return resultSetType;
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY) {
throw new SQLException(
String.format("Statements with result set concurrency %d are not supported",
resultSetConcurrency));
}
if (resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE) {
throw new SQLException(String.format("Statements with ResultSet type %d are not supported",
resultSetType));
}
return new IoTDBStatement(this, getClient(), sessionId, zoneId);
}
@DataProvider(name = "rowSetScrollTypes")
protected Object[][] rowSetScrollTypes() throws Exception {
RowSet rs = newInstance();
return new Object[][]{
{rs, ResultSet.TYPE_FORWARD_ONLY},
{rs, ResultSet.TYPE_SCROLL_INSENSITIVE},
{rs, ResultSet.TYPE_SCROLL_SENSITIVE}
};
}
/**
* Determine if DBMS/Driver supports scrollable resultsets to be able to
* determine complete row count for a given SQL.
*
* @param conn established JDBC connection
* @param dc connection information
* @param sql the sql to be executed
*/
private void updateScrollableSupport(Connection conn, DatabaseConnection dc,
String sql) {
useScrollableCursors = dc.isUseScrollableCursors();
if (!useScrollableCursors) {
return;
}
String driverName = dc.getDriverClass();
/* Derby fails to support scrollable cursors when invoking 'stored procedures'
which return resultsets - it fails hard: not throwing a SQLException,
but terminating the connection - so don't try to use scrollable cursor
on derby, for "non"-selects */
if (driverName != null && driverName.startsWith("org.apache.derby")) { //NOI18N
if (!isSelectStatement(sql)) {
resultSetScrollType = ResultSet.TYPE_FORWARD_ONLY;
return;
}
}
/* Try to get a "good" scrollable ResultSet and follow the DBs support */
try {
if (conn.getMetaData().supportsResultSetType(
ResultSet.TYPE_SCROLL_INSENSITIVE)) {
resultSetScrollType = ResultSet.TYPE_SCROLL_INSENSITIVE;
} else if (conn.getMetaData().supportsResultSetType(
ResultSet.TYPE_SCROLL_SENSITIVE)) {
resultSetScrollType = ResultSet.TYPE_SCROLL_SENSITIVE;
}
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Exception while querying" //NOI18N
+ " database for scrollable resultset support"); //NOI18N
}
}
@DataProvider(name = "rowSetScrollTypes")
protected Object[][] rowSetScrollTypes() throws Exception {
RowSet rs = newInstance();
return new Object[][]{
{rs, ResultSet.TYPE_FORWARD_ONLY},
{rs, ResultSet.TYPE_SCROLL_INSENSITIVE},
{rs, ResultSet.TYPE_SCROLL_SENSITIVE}
};
}
@DataProvider(name = "rowSetScrollTypes")
protected Object[][] rowSetScrollTypes() throws Exception {
RowSet rs = newInstance();
return new Object[][]{
{rs, ResultSet.TYPE_FORWARD_ONLY},
{rs, ResultSet.TYPE_SCROLL_INSENSITIVE},
{rs, ResultSet.TYPE_SCROLL_SENSITIVE}
};
}
public static int getJdbcResultSetType(byte thriftType) {
switch (thriftType) {
case gfxdConstants.RESULTSET_TYPE_FORWARD_ONLY:
return ResultSet.TYPE_FORWARD_ONLY;
case gfxdConstants.RESULTSET_TYPE_INSENSITIVE:
return ResultSet.TYPE_SCROLL_INSENSITIVE;
case gfxdConstants.RESULTSET_TYPE_SENSITIVE:
return ResultSet.TYPE_SCROLL_SENSITIVE;
default:
throw new IllegalArgumentException("Thrift ResultSet type="
+ thriftType);
}
}
private int setResultSetType(int resultSetType) {
/* Add warning if scroll sensitive cursor
* and downgrade to scroll insensitive cursor.
*/
if (resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE)
{
addWarning(SQLWarningFactory.newSQLWarning(SQLState.NO_SCROLL_SENSITIVE_CURSORS));
resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
}
return resultSetType;
}
/**
* JDBC 2.0
*
* Does the database support the concurrency type in combination
* with the given result set type?
*
* @param type defined in java.sql.ResultSet
* @param concurrency type defined in java.sql.ResultSet
* @return true if so
* @see Connection
*/
public boolean supportsResultSetConcurrency(int type, int concurrency) {
if (type == ResultSet.TYPE_SCROLL_SENSITIVE) {
// (TYPE_SCROLL_SENSITIVE, *)
return false;
} else {
// (FORWARD_ONLY, CONCUR_UPDATABLE)
// (FORWARD_ONLY, CONCUR_READ_ONLY)
// (TYPE_SCROLL_INSENSITIVE, CONCUR_UPDATABLE)
// (TYPE_SCROLL_INSENSITIVE, READ_ONLY)
return true;
}
}
private int setResultSetType(int resultSetType) {
/* Add warning if scroll sensitive cursor
* and downgrade to scroll insensitive cursor.
*/
if (resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE)
{
addWarning(SQLWarningFactory.newSQLWarning(SQLState.NO_SCROLL_SENSITIVE_CURSORS));
resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
}
return resultSetType;
}
@Override
public ClickHouseStatement createStatement(int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
if (resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE || resultSetConcurrency != ResultSet.CONCUR_READ_ONLY
|| resultSetHoldability != ResultSet.CLOSE_CURSORS_AT_COMMIT) {
throw new SQLFeatureNotSupportedException();
}
return createStatement(resultSetType);
}
private static int getResultSetType(RuntimeProperties props) throws BarleyDBQueryException {
if (props.getScrollType() == null) {
return ResultSet.TYPE_FORWARD_ONLY;
}
switch(props.getScrollType()) {
case FORWARD_ONLY:
return ResultSet.TYPE_FORWARD_ONLY;
case SCROLL_INSENSITIVE:
return ResultSet.TYPE_SCROLL_INSENSITIVE;
case SCROLL_SENSITIVE:
return ResultSet.TYPE_SCROLL_SENSITIVE;
default:
throw new IllegalQueryStateException("Unknown scroll type '" + props.getScrollType() + "'");
}
}
private void loadDataFrom(final DataViewPageContext pageContext, ResultSet rs) throws SQLException, InterruptedException {
if (rs == null) {
return;
}
int pageSize = pageContext.getPageSize();
int startFrom;
if (useScrollableCursors ) {
startFrom = pageContext.getCurrentPos(); // will use rs.absolute
} else if (!limitSupported || isLimitUsedInSelect(dataView.getSQLString())) {
startFrom = pageContext.getCurrentPos(); // need to use slow skip
} else {
startFrom = 0; // limit added to select, can start from first item
}
final List<Object[]> rows = new ArrayList<>();
int colCnt = pageContext.getTableMetaData().getColumnCount();
int curRowPos = 0;
try {
long start = System.currentTimeMillis();
boolean hasNext = false;
boolean needSlowSkip = true;
if (useScrollableCursors
&& (rs.getType() == ResultSet.TYPE_SCROLL_INSENSITIVE
|| rs.getType() == ResultSet.TYPE_SCROLL_SENSITIVE)) {
try {
hasNext = rs.absolute(startFrom);
curRowPos = rs.getRow();
needSlowSkip = false;
} catch (SQLException ex) {
LOGGER.log(Level.FINE, "Absolute positioning failed", ex); // NOI18N
}
}
if (needSlowSkip) {
// Skip till current position
hasNext = rs.next();
curRowPos++;
while (hasNext && curRowPos < startFrom) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
hasNext = rs.next();
curRowPos++;
}
}
// Get next page
int rowCnt = 0;
while (((pageSize <= 0) || (pageSize > rowCnt)) && (hasNext)) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
Object[] row = new Object[colCnt];
for (int i = 0; i < colCnt; i++) {
row[i] = DBReadWriteHelper.readResultSet(rs,
pageContext.getTableMetaData().getColumn(i), i + 1);
}
rows.add(row);
rowCnt++;
try {
hasNext = rs.next();
curRowPos++;
} catch (SQLException x) {
LOGGER.log(Level.INFO, "Failed to forward to next record, cause: " + x.getLocalizedMessage(), x);
hasNext = false;
}
}
long end = System.currentTimeMillis();
dataView.addFetchTime(end - start);
} catch (SQLException e) {
LOGGER.log(Level.INFO, "Failed to set up table model.", e); // NOI18N
throw e;
} finally {
Mutex.EVENT.writeAccess(new Runnable() {
@Override
public void run() {
pageContext.getModel().setData(rows);
pageContext.getModel().setRowOffset(pageContext.getCurrentPos() - 1);
}
});
}
}
/**
* Default to {@link ResultSet#TYPE_SCROLL_SENSITIVE}, meaning that a change in the underlying
* database may affect the result set.
*/
@Override
public int getResultSetType() throws SQLException {
return ResultSet.TYPE_SCROLL_SENSITIVE;
}
/**
* See if a VTI modification statement should be deferred.
*
* @param statementType DeferModification.INSERT_STATEMENT, UPDATE_STATEMENT, or DELETE_STATEMENT
* @param targetVTI The target VTI
* @param updateColumnNames The list of columns being updated, null if this is not an update statement
* @param source
*/
public static boolean deferIt( int statementType,
FromVTI targetVTI,
String[] updateColumnNames,
QueryTreeNode source)
throws StandardException
{
try
{
DeferModification deferralControl;
int resultSetType = targetVTI.getResultSetType( );
/* Deferred updates and deletes are implemented by scrolling the result set. So, if
* the statement is an update or delete but the result set is not scrollable then do
* not attempt to defer the statement.
*/
if( (statementType == DeferModification.UPDATE_STATEMENT ||statementType == DeferModification.DELETE_STATEMENT)
&& resultSetType == ResultSet.TYPE_FORWARD_ONLY)
return false;
deferralControl = targetVTI.getDeferralControl();
if( deferralControl == null)
{
String VTIClassName = targetVTI.getMethodCall().getJavaClassName();
deferralControl = new DefaultVTIModDeferPolicy( VTIClassName,
ResultSet.TYPE_SCROLL_SENSITIVE == resultSetType);
}
if( deferralControl.alwaysDefer( statementType))
return true;
if( source == null && statementType != DeferModification.UPDATE_STATEMENT)
return false;
VTIDeferModPolicy deferralSearch = new VTIDeferModPolicy( targetVTI,
updateColumnNames,
deferralControl,
statementType);
if( source != null)
source.accept( deferralSearch);
if( statementType == DeferModification.UPDATE_STATEMENT)
{
// Apply the columnRequiresDefer method to updated columns not in the where clause.
Enumeration columns = deferralSearch.columns.keys();
while( columns.hasMoreElements())
{
if( deferralControl.columnRequiresDefer( statementType,
(String) columns.nextElement(),
false))
return true;
}
}
return deferralSearch.deferred;
}
catch( SQLException sqle)
{
throw StandardException.unexpectedUserException(sqle);
}
}
/**
* See if a VTI modification statement should be deferred.
*
* @param statementType DeferModification.INSERT_STATEMENT, UPDATE_STATEMENT, or DELETE_STATEMENT
* @param targetVTI The target VTI
* @param updateColumnNames The list of columns being updated, null if this is not an update statement
* @param source
*/
public static boolean deferIt( int statementType,
FromVTI targetVTI,
String[] updateColumnNames,
QueryTreeNode source)
throws StandardException
{
try
{
DeferModification deferralControl;
int resultSetType = targetVTI.getResultSetType( );
/* Deferred updates and deletes are implemented by scrolling the result set. So, if
* the statement is an update or delete but the result set is not scrollable then do
* not attempt to defer the statement.
*/
if( (statementType == DeferModification.UPDATE_STATEMENT ||statementType == DeferModification.DELETE_STATEMENT)
&& resultSetType == ResultSet.TYPE_FORWARD_ONLY)
return false;
deferralControl = targetVTI.getDeferralControl();
if( deferralControl == null)
{
String VTIClassName = targetVTI.getMethodCall().getJavaClassName();
deferralControl = new DefaultVTIModDeferPolicy( VTIClassName,
ResultSet.TYPE_SCROLL_SENSITIVE == resultSetType);
}
if( deferralControl.alwaysDefer( statementType))
return true;
if( source == null && statementType != DeferModification.UPDATE_STATEMENT)
return false;
VTIDeferModPolicy deferralSearch = new VTIDeferModPolicy( targetVTI,
updateColumnNames,
deferralControl,
statementType);
if( source != null)
source.accept( deferralSearch);
if( statementType == DeferModification.UPDATE_STATEMENT)
{
// Apply the columnRequiresDefer method to updated columns not in the where clause.
Enumeration columns = deferralSearch.columns.keys();
while( columns.hasMoreElements())
{
if( deferralControl.columnRequiresDefer( statementType,
(String) columns.nextElement(),
false))
return true;
}
}
return deferralSearch.deferred;
}
catch( SQLException sqle)
{
throw StandardException.unexpectedUserException(sqle);
}
}
private boolean isScrollable() {
return (this.resultSetType == ResultSet.TYPE_SCROLL_INSENSITIVE ||
this.resultSetType == ResultSet.TYPE_SCROLL_SENSITIVE);
}