in src/main/user-impl/java/com/mysql/cj/jdbc/DatabaseMetaDataMysqlSchema.java [1913:2013]
public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
final String dbFromTerm = chooseDatabaseTerm(catalog, schemaPattern);
final String dbFilter = normalizeIdentifierQuoting(dbFromTerm);
final String tableNameFilter = normalizeIdentifierQuoting(tableNamePattern);
StringBuilder query = new StringBuilder("SELECT host, db, table_name, grantor, user, table_priv FROM mysql.tables_priv");
StringBuilder condition = new StringBuilder();
if (dbFilter != null) {
condition.append(chooseBasedOnDatabaseTerm(() -> " db = ?", () -> " db LIKE ?"));
}
if (tableNameFilter != null) {
if (condition.length() > 0) {
condition.append(" AND");
}
condition.append(" table_name LIKE ?");
}
if (condition.length() > 0) {
query.append(" WHERE");
query.append(condition);
}
ResultSet rs = null;
ArrayList<Row> rows = new ArrayList<>();
try (PreparedStatement pStmt = prepareMetaDataSafeStatement(query.toString())) {
int nextIdx = 1;
if (dbFilter != null) {
pStmt.setString(nextIdx++, storesLowerCaseIdentifiers() ? dbFilter.toLowerCase(Locale.ROOT) : dbFilter);
}
if (tableNameFilter != null) {
pStmt.setString(nextIdx, storesLowerCaseIdentifiers() ? tableNameFilter.toLowerCase(Locale.ROOT) : tableNameFilter);
}
try {
rs = pStmt.executeQuery();
while (rs.next()) {
String host = rs.getString(1);
String db = rs.getString(2);
String table = rs.getString(3);
String grantor = rs.getString(4);
String user = rs.getString(5);
if (user == null || user.length() == 0) {
user = "%";
}
StringBuilder fullUser = new StringBuilder(user);
if (host != null && useHostsInPrivilegesValue()) {
fullUser.append("@");
fullUser.append(host);
}
String allPrivileges = rs.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.
ResultSet columnRs = null;
try {
columnRs = getColumns(catalog, schemaPattern, StringUtils.quoteIdentifier(table, getQuoteId(), !pedanticValue()), null);
while (columnRs.next()) {
byte[][] row = new byte[8][];
row[0] = chooseBasedOnDatabaseTerm(() -> s2b(db), () -> s2b("def")); // TABLE_CAT
row[1] = chooseBasedOnDatabaseTerm(() -> null, () -> s2b(db)); // TABLE_SCHEM
row[2] = s2b(table); // TABLE_NAME
row[3] = grantor != null ? s2b(grantor) : null; // GRANTOR
row[4] = s2b(fullUser.toString()); // GRANTEE
row[5] = s2b(privilege); // PRIVILEGE
row[6] = null; // IS_GRANTABLE
rows.add(new ByteArrayRow(row, getExceptionInterceptor()));
}
} finally {
if (columnRs != null) {
try {
columnRs.close();
} catch (Exception e) {
AssertionFailedException.shouldNotHappen(e);
}
columnRs = null;
}
}
}
}
}
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
AssertionFailedException.shouldNotHappen(e);
}
rs = null;
}
}
}
return getResultSetFactory().createFromResultsetRows(ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE,
new ResultsetRowsStatic(rows, new DefaultColumnDefinition(createTablePrivilegesFields())));
}