use of com.mysql.cj.protocol.a.result.ResultsetRowsStatic in project ABC by RuiPinto96274.
the class TextResultsetReader method read.
@Override
public Resultset read(int maxRows, boolean streamResults, NativePacketPayload resultPacket, ColumnDefinition metadata, ProtocolEntityFactory<Resultset, NativePacketPayload> resultSetFactory) throws IOException {
Resultset rs = null;
// try {
long columnCount = resultPacket.readInteger(IntegerDataType.INT_LENENC);
if (columnCount > 0) {
// Build a result set with rows.
// Read in the column information
ColumnDefinition cdef = this.protocol.read(ColumnDefinition.class, new ColumnDefinitionFactory(columnCount, metadata));
// There is no EOF packet after fields when CLIENT_DEPRECATE_EOF is set
if (!this.protocol.getServerSession().isEOFDeprecated()) {
this.protocol.skipPacket();
// this.protocol.readServerStatusForResultSets(this.protocol.readPacket(this.protocol.getReusablePacket()), true);
}
ResultsetRows rows = null;
if (!streamResults) {
TextRowFactory trf = new TextRowFactory(this.protocol, cdef, resultSetFactory.getResultSetConcurrency(), false);
ArrayList<ResultsetRow> rowList = new ArrayList<>();
ResultsetRow row = this.protocol.read(ResultsetRow.class, trf);
while (row != null) {
if ((maxRows == -1) || (rowList.size() < maxRows)) {
rowList.add(row);
}
row = this.protocol.read(ResultsetRow.class, trf);
}
rows = new ResultsetRowsStatic(rowList, cdef);
} else {
rows = new ResultsetRowsStreaming<>(this.protocol, cdef, false, resultSetFactory);
this.protocol.setStreamingData(rows);
}
/*
* Build ResultSet from ResultsetRows
*/
rs = resultSetFactory.createFromProtocolEntity(rows);
} else {
// check for file request
if (columnCount == NativePacketPayload.NULL_LENGTH) {
String charEncoding = this.protocol.getPropertySet().getStringProperty(PropertyKey.characterEncoding).getValue();
String fileName = resultPacket.readString(StringSelfDataType.STRING_TERM, this.protocol.getServerSession().getCharsetSettings().doesPlatformDbCharsetMatches() ? charEncoding : null);
resultPacket = this.protocol.sendFileToServer(fileName);
}
/*
* Build ResultSet with no ResultsetRows
*/
// read and parse OK packet
// oldStatus set in sendCommand()
OkPacket ok = this.protocol.readServerStatusForResultSets(resultPacket, false);
rs = resultSetFactory.createFromProtocolEntity(ok);
}
return rs;
// } catch (IOException ioEx) {
// throw SQLError.createCommunicationsException(this.protocol.getConnection(), this.protocol.getPacketSentTimeHolder().getLastPacketSentTime(),
// this.protocol.getPacketReceivedTimeHolder().getLastPacketReceivedTime(), ioEx, this.protocol.getExceptionInterceptor());
// }
}
use of com.mysql.cj.protocol.a.result.ResultsetRowsStatic 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.protocol.a.result.ResultsetRowsStatic 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.protocol.a.result.ResultsetRowsStatic 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;
}
use of com.mysql.cj.protocol.a.result.ResultsetRowsStatic in project ABC by RuiPinto96274.
the class DatabaseMetaData method getTablePrivileges.
@Override
public java.sql.ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
Field[] fields = new Field[7];
fields[0] = new Field("", "TABLE_CAT", this.metadataCollationIndex, this.metadataEncoding, MysqlType.CHAR, 64);
fields[1] = new Field("", "TABLE_SCHEM", this.metadataCollationIndex, this.metadataEncoding, MysqlType.CHAR, 1);
fields[2] = new Field("", "TABLE_NAME", this.metadataCollationIndex, this.metadataEncoding, MysqlType.CHAR, 64);
fields[3] = new Field("", "GRANTOR", this.metadataCollationIndex, this.metadataEncoding, MysqlType.CHAR, 77);
fields[4] = new Field("", "GRANTEE", this.metadataCollationIndex, this.metadataEncoding, MysqlType.CHAR, 77);
fields[5] = new Field("", "PRIVILEGE", this.metadataCollationIndex, this.metadataEncoding, MysqlType.CHAR, 64);
fields[6] = new Field("", "IS_GRANTABLE", this.metadataCollationIndex, this.metadataEncoding, MysqlType.CHAR, 3);
String dbPattern = getDatabase(catalog, schemaPattern);
StringBuilder grantQueryBuf = new StringBuilder("SELECT host,db,table_name,grantor,user,table_priv FROM mysql.tables_priv");
StringBuilder conditionBuf = new StringBuilder();
if (dbPattern != null) {
conditionBuf.append(this.databaseTerm.getValue() == DatabaseTerm.SCHEMA ? " db LIKE ?" : " db = ?");
}
if (tableNamePattern != null) {
if (conditionBuf.length() > 0) {
conditionBuf.append(" AND");
}
conditionBuf.append(" table_name LIKE ?");
}
if (conditionBuf.length() > 0) {
grantQueryBuf.append(" WHERE");
grantQueryBuf.append(conditionBuf);
}
ResultSet results = null;
ArrayList<Row> grantRows = new ArrayList<>();
PreparedStatement pStmt = null;
try {
pStmt = prepareMetaDataSafeStatement(grantQueryBuf.toString());
int nextId = 1;
if (dbPattern != null) {
pStmt.setString(nextId++, dbPattern);
}
if (tableNamePattern != null) {
pStmt.setString(nextId, tableNamePattern);
}
results = pStmt.executeQuery();
while (results.next()) {
String host = results.getString(1);
String db = results.getString(2);
String table = results.getString(3);
String grantor = results.getString(4);
String user = results.getString(5);
if ((user == null) || (user.length() == 0)) {
user = "%";
}
StringBuilder fullUser = new StringBuilder(user);
if ((host != null) && this.useHostsInPrivileges) {
fullUser.append("@");
fullUser.append(host);
}
String allPrivileges = results.getString(6);
if (allPrivileges != null) {
allPrivileges = allPrivileges.toUpperCase(Locale.ENGLISH);
StringTokenizer st = new StringTokenizer(allPrivileges, ",");
while (st.hasMoreTokens()) {
String privilege = st.nextToken().trim();
// Loop through every column in the table
java.sql.ResultSet columnResults = null;
try {
columnResults = getColumns(catalog, schemaPattern, table, null);
while (columnResults.next()) {
byte[][] tuple = new byte[8][];
// PKTABLE_CAT
tuple[0] = this.databaseTerm.getValue() == DatabaseTerm.SCHEMA ? s2b("def") : s2b(db);
// PKTABLE_SCHEM
tuple[1] = this.databaseTerm.getValue() == DatabaseTerm.SCHEMA ? s2b(db) : null;
tuple[2] = s2b(table);
tuple[3] = grantor != null ? s2b(grantor) : null;
tuple[4] = s2b(fullUser.toString());
tuple[5] = s2b(privilege);
tuple[6] = null;
grantRows.add(new ByteArrayRow(tuple, getExceptionInterceptor()));
}
} finally {
if (columnResults != null) {
try {
columnResults.close();
} catch (Exception ex) {
}
}
}
}
}
}
} finally {
if (results != null) {
try {
results.close();
} catch (Exception ex) {
}
results = null;
}
if (pStmt != null) {
try {
pStmt.close();
} catch (Exception ex) {
}
pStmt = null;
}
}
return this.resultSetFactory.createFromResultsetRows(ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, new ResultsetRowsStatic(grantRows, new DefaultColumnDefinition(fields)));
}
Aggregations