use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement 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.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.
the class MySQLView method loadAdditionalInfo.
private void loadAdditionalInfo(DBRProgressMonitor monitor) throws DBCException {
if (!isPersisted() || getContainer().isSystem()) {
additionalInfo.loaded = true;
return;
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table status")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM " + MySQLConstants.META_TABLE_VIEWS + " WHERE " + MySQLConstants.COL_TABLE_SCHEMA + "=? AND " + MySQLConstants.COL_TABLE_NAME + "=?")) {
dbStat.setString(1, getContainer().getName());
dbStat.setString(2, getName());
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
if (dbResult.next()) {
try {
additionalInfo.setCheckOption(CheckOption.valueOf(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CHECK_OPTION)));
} catch (IllegalArgumentException e) {
log.warn(e);
}
additionalInfo.setDefiner(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_DEFINER));
additionalInfo.setDefinition(SQLUtils.formatSQL(getDataSource(), JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_VIEW_DEFINITION)));
additionalInfo.setUpdatable("YES".equals(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_IS_UPDATABLE)));
}
additionalInfo.loaded = true;
}
}
} catch (SQLException e) {
throw new DBCException(e, getDataSource());
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.
the class MySQLPlanAnalyser method explain.
public void explain(DBCSession session) throws DBCException {
String plainQuery = SQLUtils.stripComments(SQLUtils.getDialectFromObject(session.getDataSource()), query).toUpperCase();
if (!plainQuery.startsWith("SELECT")) {
throw new DBCException("Only SELECT statements could produce execution plan");
}
JDBCSession connection = (JDBCSession) session;
try {
try (JDBCPreparedStatement dbStat = connection.prepareStatement("EXPLAIN EXTENDED " + query)) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
rootNodes = new ArrayList<>();
while (dbResult.next()) {
MySQLPlanNode node = new MySQLPlanNode(null, dbResult);
rootNodes.add(node);
}
}
}
} catch (SQLException e) {
throw new DBCException(e, session.getDataSource());
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement 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.model.exec.jdbc.JDBCPreparedStatement 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