Search in sources :

Example 1 with DefaultColumnDefinition

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

the class XProtocolAsyncTest method simpleSuccessfulQuery.

@Test
public void simpleSuccessfulQuery() throws Exception {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    try {
        String collName = createTempTestCollection(this.protocol);
        String json = "{'_id': '85983efc2a9a11e5b345feff819cdc9f', 'testVal': 1, 'insertedBy': 'Jess'}".replaceAll("'", "\"");
        this.protocol.send(this.messageBuilder.buildDocInsert(getTestDatabase(), collName, Arrays.asList(new String[] { json }), false), 0);
        this.protocol.readQueryResult(new StatementExecuteOkBuilder());
        final ValueHolder<ColumnDefinition> metadataHolder = new ValueHolder<>();
        final ValueHolder<ArrayList<Row>> rowHolder = new ValueHolder<>();
        rowHolder.accept(new ArrayList<>());
        final ValueHolder<StatementExecuteOk> okHolder = new ValueHolder<>();
        final ValueHolder<Throwable> excHolder = new ValueHolder<>();
        this.protocol.queryAsync(this.messageBuilder.buildFind(new DocFilterParams(getTestDatabase(), collName)), new ResultBuilder<RowResult>() {

            private ArrayList<Field> fields = new ArrayList<>();

            private ColumnDefinition metadata;

            @Override
            public boolean addProtocolEntity(ProtocolEntity entity) {
                if (entity instanceof Field) {
                    this.fields.add((Field) entity);
                } else if (entity instanceof ColumnDefinition) {
                    this.metadata = (ColumnDefinition) entity;
                    metadataHolder.accept(this.metadata);
                } else if (entity instanceof Row) {
                    if (this.metadata == null) {
                        this.metadata = new DefaultColumnDefinition(this.fields.toArray(new Field[] {}));
                        metadataHolder.accept(this.metadata);
                    }
                    rowHolder.get().add((Row) entity);
                } else if (entity instanceof StatementExecuteOk) {
                    okHolder.accept((StatementExecuteOk) entity);
                    synchronized (XProtocolAsyncTest.this) {
                        XProtocolAsyncTest.this.notify();
                    }
                    return true;
                }
                return false;
            }

            @Override
            public RowResult build() {
                return null;
            }
        });
        synchronized (this) {
            // timeout in case we get stuck
            this.wait(5000);
        }
        assertEquals(1, metadataHolder.get().getFields().length);
        assertEquals(1, rowHolder.get().size());
        assertNotNull(okHolder.get());
        assertNull(excHolder.get());
    } finally {
        dropTempTestCollection(this.protocol);
    }
}
Also used : StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) ArrayList(java.util.ArrayList) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) StatementExecuteOk(com.mysql.cj.protocol.x.StatementExecuteOk) RowResult(com.mysql.cj.xdevapi.RowResult) Field(com.mysql.cj.result.Field) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) Row(com.mysql.cj.result.Row) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) ProtocolEntity(com.mysql.cj.protocol.ProtocolEntity) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultColumnDefinition

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

the class CallableStatement method fakeParameterTypes.

/**
 * Used to fake up some metadata when we don't have access to
 * SHOW CREATE PROCEDURE or mysql.proc.
 *
 * @param isReallyProcedure
 *            is it a procedure or function
 *
 * @throws SQLException
 *             if we can't build the metadata.
 */
private void fakeParameterTypes(boolean isReallyProcedure) throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        String encoding = this.connection.getSession().getServerSession().getCharsetSettings().getMetadataEncoding();
        int collationIndex = this.connection.getSession().getServerSession().getCharsetSettings().getMetadataCollationIndex();
        Field[] fields = new Field[13];
        fields[0] = new Field("", "PROCEDURE_CAT", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[1] = new Field("", "PROCEDURE_SCHEM", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[2] = new Field("", "PROCEDURE_NAME", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[3] = new Field("", "COLUMN_NAME", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[4] = new Field("", "COLUMN_TYPE", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[5] = new Field("", "DATA_TYPE", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[6] = new Field("", "TYPE_NAME", collationIndex, encoding, MysqlType.CHAR, 0);
        fields[7] = new Field("", "PRECISION", collationIndex, encoding, MysqlType.INT, 0);
        fields[8] = new Field("", "LENGTH", collationIndex, encoding, MysqlType.INT, 0);
        fields[9] = new Field("", "SCALE", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[10] = new Field("", "RADIX", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[11] = new Field("", "NULLABLE", collationIndex, encoding, MysqlType.SMALLINT, 0);
        fields[12] = new Field("", "REMARKS", collationIndex, encoding, MysqlType.CHAR, 0);
        String procName = isReallyProcedure ? extractProcedureName() : null;
        byte[] procNameAsBytes = null;
        procNameAsBytes = procName == null ? null : StringUtils.getBytes(procName, "UTF-8");
        ArrayList<Row> resultRows = new ArrayList<>();
        for (int i = 0; i < ((PreparedQuery<?>) this.query).getParameterCount(); i++) {
            byte[][] row = new byte[13][];
            // PROCEDURE_CAT
            row[0] = null;
            // PROCEDURE_SCHEM
            row[1] = null;
            // PROCEDURE/NAME
            row[2] = procNameAsBytes;
            // COLUMN_NAME
            row[3] = s2b(String.valueOf(i));
            row[4] = s2b(String.valueOf(java.sql.DatabaseMetaData.procedureColumnIn));
            // DATA_TYPE
            row[5] = s2b(String.valueOf(MysqlType.VARCHAR.getJdbcType()));
            // TYPE_NAME
            row[6] = s2b(MysqlType.VARCHAR.getName());
            // PRECISION
            row[7] = s2b(Integer.toString(65535));
            // LENGTH
            row[8] = s2b(Integer.toString(65535));
            // SCALE
            row[9] = s2b(Integer.toString(0));
            // RADIX
            row[10] = s2b(Integer.toString(10));
            // nullable
            row[11] = s2b(Integer.toString(java.sql.DatabaseMetaData.procedureNullableUnknown));
            row[12] = null;
            resultRows.add(new ByteArrayRow(row, getExceptionInterceptor()));
        }
        java.sql.ResultSet paramTypesRs = this.resultSetFactory.createFromResultsetRows(ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, new ResultsetRowsStatic(resultRows, new DefaultColumnDefinition(fields)));
        convertGetProcedureColumnsToInternalDescriptors(paramTypesRs);
    }
}
Also used : ArrayList(java.util.ArrayList) Field(com.mysql.cj.result.Field) ResultSet(java.sql.ResultSet) DefaultColumnDefinition(com.mysql.cj.result.DefaultColumnDefinition) ResultsetRowsStatic(com.mysql.cj.protocol.a.result.ResultsetRowsStatic) ByteArrayRow(com.mysql.cj.protocol.a.result.ByteArrayRow) Row(com.mysql.cj.result.Row) ByteArrayRow(com.mysql.cj.protocol.a.result.ByteArrayRow)

Example 3 with DefaultColumnDefinition

use of com.mysql.cj.result.DefaultColumnDefinition 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 4 with DefaultColumnDefinition

use of com.mysql.cj.result.DefaultColumnDefinition 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)

Example 5 with DefaultColumnDefinition

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

the class DatabaseMetaData method getImportedKeys.

@Override
public java.sql.ResultSet getImportedKeys(String catalog, String schema, final String table) throws SQLException {
    if (table == null) {
        throw SQLError.createSQLException(Messages.getString("DatabaseMetaData.2"), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, getExceptionInterceptor());
    }
    Field[] fields = createFkMetadataFields();
    final ArrayList<Row> rows = new ArrayList<>();
    final Statement stmt = this.conn.getMetadataSafeStatement();
    String db = getDatabase(catalog, schema);
    try {
        new IterateBlock<String>(getDatabaseIterator(db)) {

            @Override
            void forEach(String dbStr) throws SQLException {
                ResultSet fkresults = null;
                try {
                    /*
                         * Get foreign key information for table
                         */
                    // we can use 'SHOW CREATE TABLE'
                    fkresults = extractForeignKeyFromCreateTable(dbStr, table);
                    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()) {
                                    // Skip InnoDB comment
                                    commentTokens.nextToken();
                                    while (commentTokens.hasMoreTokens()) {
                                        String keysComment = commentTokens.nextToken();
                                        populateKeyResults(dbStr, table, keysComment, rows, null, false);
                                    }
                                }
                            }
                        }
                    }
                } finally {
                    if (fkresults != null) {
                        try {
                            fkresults.close();
                        } catch (SQLException 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(rows, new DefaultColumnDefinition(fields)));
    return results;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) 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) ResultsetRow(com.mysql.cj.protocol.ResultsetRow) ByteArrayRow(com.mysql.cj.protocol.a.result.ByteArrayRow) Row(com.mysql.cj.result.Row)

Aggregations

DefaultColumnDefinition (com.mysql.cj.result.DefaultColumnDefinition)87 ArrayList (java.util.ArrayList)81 ResultsetRowsStatic (com.mysql.cj.protocol.a.result.ResultsetRowsStatic)78 Field (com.mysql.cj.result.Field)72 Row (com.mysql.cj.result.Row)69 ByteArrayRow (com.mysql.cj.protocol.a.result.ByteArrayRow)63 ResultsetRow (com.mysql.cj.protocol.ResultsetRow)54 ResultSet (java.sql.ResultSet)42 SQLException (java.sql.SQLException)39 PreparedStatement (java.sql.PreparedStatement)33 Statement (java.sql.Statement)27 AssertionFailedException (com.mysql.cj.exceptions.AssertionFailedException)24 CJException (com.mysql.cj.exceptions.CJException)24 StringTokenizer (java.util.StringTokenizer)18 TreeMap (java.util.TreeMap)9 StatementExecuteOk (com.mysql.cj.protocol.x.StatementExecuteOk)6 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)6 Iterator (java.util.Iterator)6 MysqlType (com.mysql.cj.MysqlType)3 ResultSetImpl (com.mysql.cj.jdbc.result.ResultSetImpl)3