/** * Returns the next row. * * @return the next row value * @throws SQLException if a database error occurs */ public ResultSetRow next() throws SQLException { if (this.fetchedRows == null && this.currentPositionInEntireResult != BEFORE_START_OF_ROWS) { throw SQLError.createSQLException( Messages.getString( "ResultSet.Operation_not_allowed_after_ResultSet_closed_144"), //$NON-NLS-1$ SQLError.SQL_STATE_GENERAL_ERROR, mysql.getExceptionInterceptor()); } if (!hasNext()) { return null; } this.currentPositionInEntireResult++; this.currentPositionInFetchedRows++; // Catch the forced scroll-passed-end if (this.fetchedRows != null && this.fetchedRows.size() == 0) { return null; } if (this.currentPositionInFetchedRows > (this.fetchedRows.size() - 1)) { fetchMoreRows(); this.currentPositionInFetchedRows = 0; } ResultSetRow row = this.fetchedRows.get(this.currentPositionInFetchedRows); row.setMetadata(this.metadata); return row; }
/** * Creates a new RowDataDynamic object. * * @param io the connection to MySQL that this data is coming from * @param metadata the metadata that describe this data * @param isBinaryEncoded is this data in native format? * @param colCount the number of columns * @throws SQLException if the next record can not be found */ public RowDataDynamic(MysqlIO io, int colCount, Field[] fields, boolean isBinaryEncoded) throws SQLException { this.io = io; this.columnCount = colCount; this.isBinaryEncoded = isBinaryEncoded; this.metadata = fields; this.exceptionInterceptor = this.io.getExceptionInterceptor(); this.useBufferRowExplicit = MysqlIO.useBufferRowExplicit(this.metadata); }
/** * Creates a new cursor-backed row provider. * * @param ioChannel connection to the server. * @param creatingStatement statement that opened the cursor. * @param metadata field-level metadata for the results that this cursor covers. */ public RowDataCursor( MysqlIO ioChannel, ServerPreparedStatement creatingStatement, Field[] metadata) { this.currentPositionInEntireResult = BEFORE_START_OF_ROWS; this.metadata = metadata; this.mysql = ioChannel; this.statementIdOnServer = creatingStatement.getServerStatementId(); this.prepStmt = creatingStatement; this.useBufferRowExplicit = MysqlIO.useBufferRowExplicit(this.metadata); }
/** * Get a description of table columns available in a catalog. * * <p>Only column descriptions matching the catalog, schema, table and column name criteria are * returned. They are ordered by TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION. * * <p>Each column description has the following columns: * * <OL> * <li><B>TABLE_CAT</B> String => table catalog (may be null) * <li><B>TABLE_SCHEM</B> String => table schema (may be null) * <li><B>TABLE_NAME</B> String => table name * <li><B>COLUMN_NAME</B> String => column name * <li><B>DATA_TYPE</B> short => SQL type from java.sql.Types * <li><B>TYPE_NAME</B> String => Data source dependent type name * <li><B>COLUMN_SIZE</B> int => column size. For char or date types this is the maximum number * of characters, for numeric or decimal types this is precision. * <li><B>BUFFER_LENGTH</B> is not used. * <li><B>DECIMAL_DIGITS</B> int => the number of fractional digits * <li><B>NUM_PREC_RADIX</B> int => Radix (typically either 10 or 2) * <li><B>NULLABLE</B> int => is NULL allowed? * <UL> * <li>columnNoNulls - might not allow NULL values * <li>columnNullable - definitely allows NULL values * <li>columnNullableUnknown - nullability unknown * </ul> * <li><B>REMARKS</B> String => comment describing column (may be null) * <li><B>COLUMN_DEF</B> String => default value (may be null) * <li><B>SQL_DATA_TYPE</B> int => unused * <li><B>SQL_DATETIME_SUB</B> int => unused * <li><B>CHAR_OCTET_LENGTH</B> int => for char types the maximum number of bytes in the column * <li><B>ORDINAL_POSITION</B> int => index of column in table (starting at 1) * <li><B>IS_NULLABLE</B> String => "NO" means column definitely does not allow NULL values; * "YES" means the column might allow NULL values. An empty string means nobody knows. * </ol> */ public ResultSet getColumns( String catalog, String schemaPattern, String tableName, String columnNamePattern) throws SQLException { if (columnNamePattern == null) { if (this.conn.getNullNamePatternMatchesAll()) { columnNamePattern = "%"; } else { throw SQLError.createSQLException( "Column name pattern can not be NULL or empty.", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } } if (catalog == null) { if (this.conn.getNullCatalogMeansCurrent()) { catalog = this.database; } } StringBuffer sqlBuf = new StringBuffer( "SELECT " + "TABLE_SCHEMA AS TABLE_CAT, " + "NULL AS TABLE_SCHEM," + "TABLE_NAME," + "COLUMN_NAME,"); MysqlDefs.appendJdbcTypeMappingQuery(sqlBuf, "DATA_TYPE"); sqlBuf.append(" AS DATA_TYPE, "); if (conn.getCapitalizeTypeNames()) { sqlBuf.append( "UPPER(CASE WHEN LOCATE('unsigned', COLUMN_TYPE) != 0 AND LOCATE('unsigned', DATA_TYPE) = 0 THEN CONCAT(DATA_TYPE, ' unsigned') ELSE DATA_TYPE END) AS TYPE_NAME,"); } else { sqlBuf.append( "CASE WHEN LOCATE('unsigned', COLUMN_TYPE) != 0 AND LOCATE('unsigned', DATA_TYPE) = 0 THEN CONCAT(DATA_TYPE, ' unsigned') ELSE DATA_TYPE END AS TYPE_NAME,"); } sqlBuf.append( "CASE WHEN CHARACTER_MAXIMUM_LENGTH IS NULL THEN NUMERIC_PRECISION ELSE CASE WHEN CHARACTER_MAXIMUM_LENGTH > " + Integer.MAX_VALUE + " THEN " + Integer.MAX_VALUE + " ELSE CHARACTER_MAXIMUM_LENGTH END END AS COLUMN_SIZE, " + MysqlIO.getMaxBuf() + " AS BUFFER_LENGTH," + "NUMERIC_SCALE AS DECIMAL_DIGITS," + "10 AS NUM_PREC_RADIX," + "CASE WHEN IS_NULLABLE='NO' THEN " + columnNoNulls + " ELSE CASE WHEN IS_NULLABLE='YES' THEN " + columnNullable + " ELSE " + columnNullableUnknown + " END END AS NULLABLE," + "COLUMN_COMMENT AS REMARKS," + "COLUMN_DEFAULT AS COLUMN_DEF," + "0 AS SQL_DATA_TYPE," + "0 AS SQL_DATETIME_SUB," + "CASE WHEN CHARACTER_OCTET_LENGTH > " + Integer.MAX_VALUE + " THEN " + Integer.MAX_VALUE + " ELSE CHARACTER_OCTET_LENGTH END AS CHAR_OCTET_LENGTH," + "ORDINAL_POSITION," + "IS_NULLABLE " + "FROM INFORMATION_SCHEMA.COLUMNS WHERE " + "TABLE_SCHEMA LIKE ? AND " + "TABLE_NAME LIKE ? AND COLUMN_NAME LIKE ? " + "ORDER BY TABLE_SCHEMA, TABLE_NAME, ORDINAL_POSITION"); PreparedStatement pStmt = null; try { pStmt = prepareMetaDataSafeStatement(sqlBuf.toString()); if (catalog != null) { pStmt.setString(1, catalog); } else { pStmt.setString(1, "%"); } pStmt.setString(2, tableName); pStmt.setString(3, columnNamePattern); ResultSet rs = executeMetadataQuery(pStmt); ((com.mysql.jdbc.ResultSet) rs) .redefineFieldsForDBMD( new Field[] { new Field("", "TABLE_CAT", Types.CHAR, 255), new Field("", "TABLE_SCHEM", Types.CHAR, 0), new Field("", "TABLE_NAME", Types.CHAR, 255), new Field("", "COLUMN_NAME", Types.CHAR, 32), new Field("", "DATA_TYPE", Types.SMALLINT, 5), new Field("", "TYPE_NAME", Types.CHAR, 16), new Field( "", "COLUMN_SIZE", Types.INTEGER, Integer.toString(Integer.MAX_VALUE).length()), new Field("", "BUFFER_LENGTH", Types.INTEGER, 10), new Field("", "DECIMAL_DIGITS", Types.INTEGER, 10), new Field("", "NUM_PREC_RADIX", Types.INTEGER, 10), new Field("", "NULLABLE", Types.INTEGER, 10), new Field("", "REMARKS", Types.CHAR, 0), new Field("", "COLUMN_DEF", Types.CHAR, 0), new Field("", "SQL_DATA_TYPE", Types.INTEGER, 10), new Field("", "SQL_DATETIME_SUB", Types.INTEGER, 10), new Field( "", "CHAR_OCTET_LENGTH", Types.INTEGER, Integer.toString(Integer.MAX_VALUE).length()), new Field("", "ORDINAL_POSITION", Types.INTEGER, 10), new Field("", "IS_NULLABLE", Types.CHAR, 3) }); return rs; } finally { if (pStmt != null) { pStmt.close(); } } }