Search in sources :

Example 11 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class ResultsetRowsCursor method next.

@Override
public Row next() {
    if (this.fetchedRows == null && this.currentPositionInEntireResult != BEFORE_START_OF_ROWS) {
        throw ExceptionFactory.createException(Messages.getString("ResultSet.Operation_not_allowed_after_ResultSet_closed_144"), this.protocol.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.fetchedRows == null) || (this.currentPositionInFetchedRows > (this.fetchedRows.size() - 1))) {
        fetchMoreRows();
        this.currentPositionInFetchedRows = 0;
    }
    Row row = this.fetchedRows.get(this.currentPositionInFetchedRows);
    row.setMetadata(this.metadata);
    return row;
}
Also used : Row(com.mysql.cj.result.Row) ResultsetRow(com.mysql.cj.protocol.ResultsetRow)

Example 12 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class ResultsetRowsCursor method fetchMoreRows.

private void fetchMoreRows() {
    if (this.lastRowFetched) {
        this.fetchedRows = new ArrayList<>(0);
        return;
    }
    synchronized (this.owner.getSyncMutex()) {
        try {
            boolean oldFirstFetchCompleted = this.firstFetchCompleted;
            if (!this.firstFetchCompleted) {
                this.firstFetchCompleted = true;
            }
            int numRowsToFetch = this.owner.getOwnerFetchSize();
            if (numRowsToFetch == 0) {
                numRowsToFetch = this.owner.getOwningStatementFetchSize();
            }
            if (numRowsToFetch == Integer.MIN_VALUE) {
                // Handle the case where the user used 'old' streaming result sets
                numRowsToFetch = 1;
            }
            if (this.fetchedRows == null) {
                this.fetchedRows = new ArrayList<>(numRowsToFetch);
            } else {
                this.fetchedRows.clear();
            }
            // TODO this is not the right place for this code, should be in protocol
            this.protocol.sendCommand(this.commandBuilder.buildComStmtFetch(this.protocol.getSharedSendPacket(), this.owner.getOwningStatementServerId(), numRowsToFetch), true, 0);
            Row row = null;
            while ((row = this.protocol.read(ResultsetRow.class, this.rowFactory)) != null) {
                this.fetchedRows.add(row);
            }
            this.currentPositionInFetchedRows = BEFORE_START_OF_ROWS;
            if (this.protocol.getServerSession().isLastRowSent()) {
                this.lastRowFetched = true;
                if (!oldFirstFetchCompleted && this.fetchedRows.size() == 0) {
                    this.wasEmpty = true;
                }
            }
        } catch (Exception ex) {
            throw ExceptionFactory.createException(ex.getMessage(), ex);
        }
    }
}
Also used : Row(com.mysql.cj.result.Row) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) ResultsetRow(com.mysql.cj.protocol.ResultsetRow)

Example 13 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class SessionImpl method getSchemas.

public List<Schema> getSchemas() {
    Function<Row, String> rowToName = r -> r.getValue(0, new StringValueFactory(this.session.getPropertySet()));
    Function<Row, Schema> rowToSchema = rowToName.andThen(n -> new SchemaImpl(this.session, this, n));
    return this.session.query(this.xbuilder.buildSqlStatement("select schema_name from information_schema.schemata"), null, rowToSchema, Collectors.toList());
}
Also used : MysqlErrorNumbers(com.mysql.cj.exceptions.MysqlErrorNumbers) PropertySet(com.mysql.cj.conf.PropertySet) Messages(com.mysql.cj.Messages) DefaultPropertySet(com.mysql.cj.conf.DefaultPropertySet) XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) HostInfo(com.mysql.cj.conf.HostInfo) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) MysqlxSession(com.mysql.cj.MysqlxSession) PropertyDefinitions(com.mysql.cj.conf.PropertyDefinitions) XProtocolError(com.mysql.cj.protocol.x.XProtocolError) Row(com.mysql.cj.result.Row) List(java.util.List) XProtocol(com.mysql.cj.protocol.x.XProtocol) XMessage(com.mysql.cj.protocol.x.XMessage) StringUtils(com.mysql.cj.util.StringUtils) StringValueFactory(com.mysql.cj.result.StringValueFactory) RuntimeProperty(com.mysql.cj.conf.RuntimeProperty) PropertyKey(com.mysql.cj.conf.PropertyKey) StringValueFactory(com.mysql.cj.result.StringValueFactory) Row(com.mysql.cj.result.Row)

Example 14 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class SqlResultBuilder method addProtocolEntity.

@Override
public boolean addProtocolEntity(ProtocolEntity entity) {
    if (entity instanceof Field) {
        this.fields.add((Field) entity);
        if (!this.isRowResult) {
            this.isRowResult = true;
        }
        this.prevEntity = entity;
        return false;
    } else if (entity instanceof Notice) {
        this.statementExecuteOkBuilder.addProtocolEntity(entity);
        return false;
    }
    if (this.isRowResult && this.metadata == null) {
        this.metadata = new DefaultColumnDefinition(this.fields.toArray(new Field[] {}));
    }
    if (entity instanceof Row) {
        this.rows.add(((Row) entity).setMetadata(this.metadata));
    } else if (entity instanceof FetchDoneMoreResults) {
        this.resultSets.add(new SqlSingleResult(this.metadata, this.defaultTimeZone, new BufferedRowList(this.rows), () -> this.statementExecuteOkBuilder.build(), this.pset));
        // clear variables to accept next result set
        this.fields = new ArrayList<>();
        this.metadata = null;
        this.rows = new ArrayList<>();
        this.statementExecuteOkBuilder = new StatementExecuteOkBuilder();
    } else if (entity instanceof FetchDoneEntity) {
        if (this.prevEntity instanceof FetchDoneMoreResults) {
        // no-op, possibly bug in xplugin sending FetchDone immediately following FetchDoneMoreResultsets
        } else {
            this.resultSets.add(new SqlSingleResult(this.metadata, this.defaultTimeZone, new BufferedRowList(this.rows), () -> this.statementExecuteOkBuilder.build(), this.pset));
        }
    } else if (entity instanceof StatementExecuteOk) {
        return true;
    }
    this.prevEntity = entity;
    return false;
}
Also used : BufferedRowList(com.mysql.cj.result.BufferedRowList) Field(com.mysql.cj.result.Field) Notice(com.mysql.cj.protocol.x.Notice) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) FetchDoneMoreResults(com.mysql.cj.protocol.x.FetchDoneMoreResults) ArrayList(java.util.ArrayList) Row(com.mysql.cj.result.Row) FetchDoneEntity(com.mysql.cj.protocol.x.FetchDoneEntity) StatementExecuteOk(com.mysql.cj.protocol.x.StatementExecuteOk)

Example 15 with Row

use of com.mysql.cj.result.Row in project ABC by RuiPinto96274.

the class DatabaseMetaData method getCrossReference.

@Override
public java.sql.ResultSet getCrossReference(final String primaryCatalog, final String primarySchema, final String primaryTable, final String foreignCatalog, final String foreignSchema, final String foreignTable) throws SQLException {
    if (primaryTable == null) {
        throw SQLError.createSQLException(Messages.getString("DatabaseMetaData.2"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
    }
    // String primaryDb = getDatabase(primaryCatalog, primarySchema); // TODO not needed?
    String foreignDb = getDatabase(foreignCatalog, foreignSchema);
    Field[] fields = createFkMetadataFields();
    final ArrayList<Row> tuples = new ArrayList<>();
    final Statement stmt = this.conn.getMetadataSafeStatement();
    final boolean dbMapsToSchema = DatabaseMetaData.this.databaseTerm.getValue() == DatabaseTerm.SCHEMA;
    try {
        new IterateBlock<String>(getDatabaseIterator(foreignDb)) {

            @Override
            void forEach(String dbStr) throws SQLException {
                ResultSet fkresults = null;
                try {
                    /*
                         * Get foreign key information for table
                         */
                    fkresults = extractForeignKeyFromCreateTable(dbStr, null);
                    String foreignTableWithCase = getTableNameWithCase(foreignTable);
                    String primaryTableWithCase = getTableNameWithCase(primaryTable);
                    /*
                         * Parse imported foreign key information
                         */
                    String dummy;
                    while (fkresults.next()) {
                        String tableType = fkresults.getString("Type");
                        if ((tableType != null) && (tableType.equalsIgnoreCase("innodb") || tableType.equalsIgnoreCase(SUPPORTS_FK))) {
                            String comment = fkresults.getString("Comment").trim();
                            if (comment != null) {
                                StringTokenizer commentTokens = new StringTokenizer(comment, ";", false);
                                if (commentTokens.hasMoreTokens()) {
                                    dummy = commentTokens.nextToken();
                                // Skip InnoDB comment
                                }
                                while (commentTokens.hasMoreTokens()) {
                                    String keys = commentTokens.nextToken();
                                    LocalAndReferencedColumns parsedInfo = parseTableStatusIntoLocalAndReferencedColumns(keys);
                                    int keySeq = 1;
                                    Iterator<String> referencingColumns = parsedInfo.localColumnsList.iterator();
                                    Iterator<String> referencedColumns = parsedInfo.referencedColumnsList.iterator();
                                    while (referencingColumns.hasNext()) {
                                        String referencingColumn = StringUtils.unQuoteIdentifier(referencingColumns.next(), DatabaseMetaData.this.quotedId);
                                        dummy = fkresults.getString("Name");
                                        if (dummy.compareTo(foreignTableWithCase) != 0) {
                                            continue;
                                        }
                                        // Skip foreign key if it doesn't refer to the right table
                                        if (parsedInfo.referencedTable.compareTo(primaryTableWithCase) != 0) {
                                            continue;
                                        }
                                        // one tuple for each table between parenthesis
                                        byte[][] tuple = new byte[14][];
                                        // PKTABLE_CAT
                                        tuple[0] = dbMapsToSchema ? s2b("def") : s2b(parsedInfo.referencedDatabase);
                                        // PKTABLE_SCHEM
                                        tuple[1] = dbMapsToSchema ? s2b(parsedInfo.referencedDatabase) : null;
                                        // PKTABLE_NAME
                                        tuple[2] = s2b(parsedInfo.referencedTable);
                                        // PKCOLUMN_NAME
                                        tuple[3] = s2b(StringUtils.unQuoteIdentifier(referencedColumns.next(), DatabaseMetaData.this.quotedId));
                                        // FKTABLE_CAT
                                        tuple[4] = dbMapsToSchema ? s2b("def") : s2b(dbStr);
                                        // FKTABLE_SCHEM
                                        tuple[5] = dbMapsToSchema ? s2b(dbStr) : null;
                                        // FKTABLE_NAME
                                        tuple[6] = s2b(dummy);
                                        // FKCOLUMN_NAME
                                        tuple[7] = s2b(referencingColumn);
                                        // KEY_SEQ
                                        tuple[8] = Integer.toString(keySeq).getBytes();
                                        int[] actions = getForeignKeyActions(keys);
                                        // UPDATE_RULE
                                        tuple[9] = Integer.toString(actions[1]).getBytes();
                                        // DELETE_RULE
                                        tuple[10] = Integer.toString(actions[0]).getBytes();
                                        // FK_NAME
                                        tuple[11] = s2b(parsedInfo.constraintName);
                                        // PK_NAME
                                        tuple[12] = null;
                                        // DEFERRABILITY
                                        tuple[13] = Integer.toString(java.sql.DatabaseMetaData.importedKeyNotDeferrable).getBytes();
                                        tuples.add(new ByteArrayRow(tuple, getExceptionInterceptor()));
                                        keySeq++;
                                    }
                                }
                            }
                        }
                    }
                } finally {
                    if (fkresults != null) {
                        try {
                            fkresults.close();
                        } catch (Exception sqlEx) {
                            AssertionFailedException.shouldNotHappen(sqlEx);
                        }
                        fkresults = null;
                    }
                }
            }
        }.doForAll();
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
    java.sql.ResultSet results = this.resultSetFactory.createFromResultsetRows(ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, new ResultsetRowsStatic(tuples, new DefaultColumnDefinition(fields)));
    return results;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) CJException(com.mysql.cj.exceptions.CJException) SQLException(java.sql.SQLException) AssertionFailedException(com.mysql.cj.exceptions.AssertionFailedException) Field(com.mysql.cj.result.Field) StringTokenizer(java.util.StringTokenizer) ResultSet(java.sql.ResultSet) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) ResultsetRowsStatic(com.mysql.cj.protocol.a.result.ResultsetRowsStatic) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) ByteArrayRow(com.mysql.cj.protocol.a.result.ByteArrayRow) Row(com.mysql.cj.result.Row) ByteArrayRow(com.mysql.cj.protocol.a.result.ByteArrayRow)

Aggregations

Row (com.mysql.cj.result.Row)144 ArrayList (java.util.ArrayList)81 ResultsetRow (com.mysql.cj.protocol.ResultsetRow)78 ByteArrayRow (com.mysql.cj.protocol.a.result.ByteArrayRow)72 DefaultColumnDefinition (com.mysql.cj.result.DefaultColumnDefinition)69 Field (com.mysql.cj.result.Field)69 ResultsetRowsStatic (com.mysql.cj.protocol.a.result.ResultsetRowsStatic)66 StringValueFactory (com.mysql.cj.result.StringValueFactory)48 ResultSet (java.sql.ResultSet)48 SQLException (java.sql.SQLException)45 PreparedStatement (java.sql.PreparedStatement)36 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)33 Statement (java.sql.Statement)33 CJException (com.mysql.cj.exceptions.CJException)30 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)30 AssertionFailedException (com.mysql.cj.exceptions.AssertionFailedException)27 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)27 Test (org.junit.jupiter.api.Test)27 Resultset (com.mysql.cj.protocol.Resultset)21 IOException (java.io.IOException)21