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);
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations