use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.
the class PostgreSequence method loadAdditionalInfo.
private void loadAdditionalInfo(DBRProgressMonitor monitor) {
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load sequence additional info")) {
try (JDBCPreparedStatement dbSeqStat = session.prepareStatement("SELECT last_value,min_value,max_value,increment_by from " + getFullyQualifiedName(DBPEvaluationContext.DML))) {
try (JDBCResultSet seqResults = dbSeqStat.executeQuery()) {
if (seqResults.next()) {
additionalInfo.lastValue = JDBCUtils.safeGetLong(seqResults, 1);
additionalInfo.minValue = JDBCUtils.safeGetLong(seqResults, 2);
additionalInfo.maxValue = JDBCUtils.safeGetLong(seqResults, 3);
additionalInfo.incrementBy = JDBCUtils.safeGetLong(seqResults, 4);
}
}
}
additionalInfo.loaded = true;
} catch (Exception e) {
log.warn("Error reading sequence values", e);
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.
the class PostgreStructureAssistant method findTableColumnsByMask.
private void findTableColumnsByMask(JDBCSession session, @Nullable final List<PostgreSchema> schema, String columnNameMask, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
DBRProgressMonitor monitor = session.getProgressMonitor();
// Load constraints
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT x.attname,x.attrelid,x.atttypid,c.relnamespace " + "FROM pg_catalog.pg_attribute x, pg_catalog.pg_class c\n" + "WHERE c.oid=x.attrelid AND x.attname " + (caseSensitive ? "LIKE" : "ILIKE") + " ? " + (CommonUtils.isEmpty(schema) ? "" : " AND c.relnamespace IN (" + SQLUtils.generateParamList(schema.size()) + ")") + " ORDER BY x.attname LIMIT " + maxResults)) {
dbStat.setString(1, columnNameMask);
if (!CommonUtils.isEmpty(schema)) {
PostgreUtils.setArrayParameter(dbStat, 2, schema);
}
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
int tableNum = maxResults;
while (dbResult.next() && tableNum-- > 0) {
if (monitor.isCanceled()) {
break;
}
final long schemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
final long tableId = JDBCUtils.safeGetLong(dbResult, "attrelid");
final String attributeName = JDBCUtils.safeGetString(dbResult, "attname");
final PostgreSchema constrSchema = dataSource.getDefaultInstance().getSchema(session.getProgressMonitor(), schemaId);
if (constrSchema == null) {
log.debug("Schema '" + schemaId + "' not found");
continue;
}
objects.add(new AbstractObjectReference(attributeName, constrSchema, null, PostgreTableBase.class, RelationalObjectType.TYPE_TABLE) {
@Override
public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
final PostgreTableBase table = PostgreUtils.getObjectById(monitor, constrSchema.tableCache, constrSchema, tableId);
if (table == null) {
throw new DBException("Table '" + tableId + "' not found in schema '" + constrSchema.getName() + "'");
}
return table.getAttribute(monitor, attributeName);
}
});
}
}
}
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.
the class PostgreTable method getSubInheritance.
@NotNull
public List<PostgreTableInheritance> getSubInheritance(@NotNull DBRProgressMonitor monitor) throws DBException {
if (subTables == null) {
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table inheritance info")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT i.*,c.relnamespace " + "FROM pg_catalog.pg_inherits i,pg_catalog.pg_class c " + "WHERE i.inhparent=? AND c.oid=i.inhrelid")) {
dbStat.setLong(1, getObjectId());
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
final long subSchemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
final long subTableId = JDBCUtils.safeGetLong(dbResult, "inhrelid");
PostgreSchema schema = getDatabase().getSchema(monitor, subSchemaId);
if (schema == null) {
log.warn("Can't find sub-table's schema '" + subSchemaId + "'");
continue;
}
PostgreTableBase subTable = schema.getTable(monitor, subTableId);
if (subTable == null) {
log.warn("Can't find sub-table '" + subTableId + "' in '" + schema.getName() + "'");
continue;
}
if (subTables == null) {
subTables = new ArrayList<>();
}
subTables.add(new PostgreTableInheritance(subTable, this, JDBCUtils.safeGetInt(dbResult, "inhseqno"), true));
}
}
}
} catch (SQLException e) {
throw new DBCException(e, getDataSource());
}
if (subTables == null) {
subTables = Collections.emptyList();
}
}
return subTables;
}
use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.
the class PostgreTableForeign method readForeignInfo.
private void readForeignInfo(DBRProgressMonitor monitor) throws DBException {
if (foreignServerId > 0) {
return;
}
try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read foreign table info")) {
try (JDBCPreparedStatement stat = session.prepareStatement("SELECT * FROM pg_catalog.pg_foreign_table WHERE ftrelid=?")) {
stat.setLong(1, getObjectId());
try (JDBCResultSet result = stat.executeQuery()) {
if (result.next()) {
foreignServerId = JDBCUtils.safeGetLong(result, "ftserver");
foreignOptions = JDBCUtils.safeGetArray(result, "ftoptions");
}
}
}
} 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 MySQLDataSource method loadPrivileges.
private List<MySQLPrivilege> loadPrivileges(DBRProgressMonitor monitor) throws DBException {
try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load privileges")) {
try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW PRIVILEGES")) {
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
List<MySQLPrivilege> privileges = new ArrayList<>();
while (dbResult.next()) {
MySQLPrivilege user = new MySQLPrivilege(this, dbResult);
privileges.add(user);
}
return privileges;
}
}
} catch (SQLException ex) {
throw new DBException(ex, this);
}
}
Aggregations