use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class MySQLStructureAssistant method findTableColumnsByMask.
private void findTableColumnsByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String constrNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
DBRProgressMonitor monitor = session.getProgressMonitor();
// Load columns
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + "," + MySQLConstants.COL_COLUMN_NAME + " FROM " + MySQLConstants.META_TABLE_COLUMNS + " WHERE " + MySQLConstants.COL_COLUMN_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_COLUMN_NAME + " LIMIT " + maxResults)) {
dbStat.setString(1, constrNameMask.toLowerCase(Locale.ENGLISH));
if (catalog != null) {
dbStat.setString(2, catalog.getName());
}
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
int tableNum = maxResults;
while (dbResult.next() && tableNum-- > 0) {
if (monitor.isCanceled()) {
break;
}
final String catalogName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_SCHEMA);
final String tableName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_NAME);
final String columnName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_NAME);
objects.add(new AbstractObjectReference(columnName, dataSource.getCatalog(catalogName), null, MySQLTableColumn.class, RelationalObjectType.TYPE_TABLE_COLUMN) {
@NotNull
@Override
public String getFullyQualifiedName(DBPEvaluationContext context) {
return DBUtils.getQuotedIdentifier(dataSource, catalogName) + '.' + DBUtils.getQuotedIdentifier(dataSource, tableName) + '.' + DBUtils.getQuotedIdentifier(dataSource, columnName);
}
@Override
public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
if (tableCatalog == null) {
throw new DBException("Column catalog '" + catalogName + "' not found");
}
MySQLTableBase table = tableCatalog.getTableCache().getObject(monitor, tableCatalog, tableName);
if (table == null) {
throw new DBException("Column table '" + tableName + "' not found in catalog '" + tableCatalog.getName() + "'");
}
MySQLTableColumn column = table.getAttribute(monitor, columnName);
if (column == null) {
throw new DBException("Column '" + columnName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
}
return column;
}
});
}
}
}
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class MySQLUser method getGrants.
public List<MySQLGrant> getGrants(DBRProgressMonitor monitor) throws DBException {
if (this.grants != null) {
return this.grants;
}
if (!isPersisted()) {
this.grants = new ArrayList<>();
return this.grants;
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read catalog privileges")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW GRANTS FOR " + getFullName())) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
List<MySQLGrant> grants = new ArrayList<>();
while (dbResult.next()) {
List<MySQLPrivilege> privileges = new ArrayList<>();
boolean allPrivilegesFlag = false;
boolean grantOption = false;
String catalog = null;
String table = null;
String grantString = CommonUtils.notEmpty(JDBCUtils.safeGetString(dbResult, 1)).trim().toUpperCase(Locale.ENGLISH);
if (grantString.endsWith(" WITH GRANT OPTION")) {
//privileges.add(getDataSource().getPrivilege(monitor, MySQLPrivilege.GRANT_PRIVILEGE));
grantOption = true;
}
String privString;
Matcher matcher = MySQLGrant.TABLE_GRANT_PATTERN.matcher(grantString);
if (matcher.find()) {
privString = matcher.group(1);
catalog = matcher.group(2);
table = matcher.group(3);
} else {
matcher = MySQLGrant.GLOBAL_GRANT_PATTERN.matcher(grantString);
if (matcher.find()) {
privString = matcher.group(1);
} else {
log.warn("Can't parse GRANT string: " + grantString);
continue;
}
}
StringTokenizer st = new StringTokenizer(privString, ",");
while (st.hasMoreTokens()) {
String privName = st.nextToken().trim();
if (privName.equalsIgnoreCase(MySQLPrivilege.ALL_PRIVILEGES)) {
allPrivilegesFlag = true;
continue;
}
MySQLPrivilege priv = getDataSource().getPrivilege(monitor, privName);
if (priv == null) {
log.warn("Can't find privilege '" + privName + "'");
} else {
privileges.add(priv);
}
}
grants.add(new MySQLGrant(this, privileges, catalog, table, allPrivilegesFlag, grantOption));
}
this.grants = grants;
return this.grants;
}
}
} catch (SQLException e) {
throw new DBException(e, getDataSource());
}
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class MySQLUserEditorPrivileges method activatePart.
@Override
public synchronized void activatePart() {
if (isLoaded) {
return;
}
isLoaded = true;
LoadingJob.createService(new DatabaseLoadService<java.util.List<MySQLPrivilege>>(MySQLMessages.editors_user_editor_privileges_service_load_privileges, getExecutionContext()) {
@Override
public java.util.List<MySQLPrivilege> evaluate(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
return getDatabaseObject().getDataSource().getPrivileges(monitor);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
}
}, pageControl.createPrivilegesLoadVisualizer()).schedule();
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class MySQLDataSource method loadParameters.
private List<MySQLParameter> loadParameters(DBRProgressMonitor monitor, boolean status, boolean global) throws DBException {
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load status")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW " + (global ? "GLOBAL " : "") + (status ? "STATUS" : "VARIABLES"))) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
List<MySQLParameter> parameters = new ArrayList<>();
while (dbResult.next()) {
MySQLParameter parameter = new MySQLParameter(this, JDBCUtils.safeGetString(dbResult, "variable_name"), JDBCUtils.safeGetString(dbResult, "value"));
parameters.add(parameter);
}
return parameters;
}
}
} catch (SQLException ex) {
throw new DBException(ex, this);
}
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class MySQLDataSource method loadUsers.
private List<MySQLUser> loadUsers(DBRProgressMonitor monitor) throws DBException {
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load users")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM mysql.user ORDER BY user")) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
List<MySQLUser> userList = new ArrayList<>();
while (dbResult.next()) {
MySQLUser user = new MySQLUser(this, dbResult);
userList.add(user);
}
return userList;
}
}
} catch (SQLException ex) {
throw new DBException(ex, this);
}
}
Aggregations