use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class SQLServerMetaModel method loadTriggers.
@Override
public List<? extends GenericTrigger> loadTriggers(DBRProgressMonitor monitor, @NotNull GenericStructContainer container, @Nullable GenericTable table) throws DBException {
assert table != null;
try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read triggers")) {
String schema = getSystemSchema(getServerType(monitor, container.getDataSource()));
String catalog = DBUtils.getQuotedIdentifier(table.getCatalog());
String query = "SELECT triggers.name FROM " + catalog + "." + schema + ".sysobjects tables, " + catalog + "." + schema + ".sysobjects triggers\n" + "WHERE triggers.type = 'TR'\n" + "AND triggers.deltrig = tables.id\n" + "AND user_name(tables.uid) = ? AND tables.name = ?";
try (JDBCPreparedStatement dbStat = session.prepareStatement(query)) {
dbStat.setString(1, table.getSchema().getName());
dbStat.setString(2, table.getName());
List<GenericTrigger> result = new ArrayList<>();
try (JDBCResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
String name = JDBCUtils.safeGetString(dbResult, 1);
if (name == null) {
continue;
}
name = name.trim();
GenericTrigger trigger = new GenericTrigger(container, table, name, null);
result.add(trigger);
}
}
return result;
}
} catch (SQLException e) {
throw new DBException(e, container.getDataSource());
}
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class OraclePackageManager method createOrReplaceProcedureQuery.
private void createOrReplaceProcedureQuery(List<DBEPersistAction> actionList, OraclePackage pack) {
try {
String header = pack.getObjectDefinitionText(VoidProgressMonitor.INSTANCE);
if (!CommonUtils.isEmpty(header)) {
actionList.add(new OracleObjectValidateAction(pack, OracleObjectType.PACKAGE, "Create package header", //$NON-NLS-1$
header));
}
String body = pack.getExtendedDefinitionText(VoidProgressMonitor.INSTANCE);
if (!CommonUtils.isEmpty(body)) {
actionList.add(new OracleObjectValidateAction(pack, OracleObjectType.PACKAGE_BODY, "Create package body", body));
} else {
actionList.add(new SQLDatabasePersistAction("Drop package header", "DROP PACKAGE BODY " + pack.getFullyQualifiedName(DBPEvaluationContext.DDL), //$NON-NLS-1$
DBEPersistAction.ActionType.OPTIONAL));
}
} catch (DBException e) {
log.warn(e);
}
OracleUtils.addSchemaChangeActions(actionList, pack);
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class MySQLTableManager method createDatabaseObject.
@Override
protected MySQLTable createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, MySQLCatalog parent, Object copyFrom) throws DBException {
final MySQLTable table;
if (copyFrom instanceof DBSEntity) {
table = new MySQLTable(monitor, parent, (DBSEntity) copyFrom);
table.setName(getTableName(monitor, parent, ((DBSEntity) copyFrom).getName()));
} else if (copyFrom == null) {
table = new MySQLTable(parent);
setTableName(monitor, parent, table);
final MySQLTable.AdditionalInfo additionalInfo = table.getAdditionalInfo(monitor);
additionalInfo.setEngine(parent.getDataSource().getDefaultEngine());
additionalInfo.setCharset(parent.getDefaultCharset());
additionalInfo.setCollation(parent.getDefaultCollation());
} else {
throw new DBException("Can't create MySQL table from '" + copyFrom + "'");
}
return table;
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class SQLEditor method processSQL.
public void processSQL(boolean newTab, boolean script, SQLQueryTransformer transformer) {
IDocument document = getDocument();
if (document == null) {
setStatus(CoreMessages.editors_sql_status_cant_obtain_document, DBPMessageType.ERROR);
return;
}
List<SQLQuery> queries;
if (script) {
// Execute all SQL statements consequently
ITextSelection selection = (ITextSelection) getSelectionProvider().getSelection();
if (selection.getLength() > 1) {
queries = extractScriptQueries(selection.getOffset(), selection.getLength());
} else {
queries = extractScriptQueries(0, document.getLength());
}
} else {
// Execute statement under cursor or selected text (if selection present)
SQLQuery sqlQuery = extractActiveQuery();
if (sqlQuery == null) {
setStatus(CoreMessages.editors_sql_status_empty_query_string, DBPMessageType.ERROR);
return;
} else {
queries = Collections.singletonList(sqlQuery);
}
}
try {
if (transformer != null) {
DBPDataSource dataSource = getDataSource();
if (dataSource instanceof SQLDataSource) {
List<SQLQuery> xQueries = new ArrayList<>(queries.size());
for (int i = 0; i < queries.size(); i++) {
SQLQuery query = transformer.transformQuery((SQLDataSource) dataSource, queries.get(i));
if (query != null) {
xQueries.add(query);
}
}
queries = xQueries;
}
}
} catch (DBException e) {
UIUtils.showErrorDialog(getSite().getShell(), "Bad query", "Can't execute query", e);
return;
}
processQueries(queries, newTab, false, true);
}
use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.
the class DiagramLoader method load.
public static void load(DBRProgressMonitor monitor, IProject project, DiagramPart diagramPart, InputStream in) throws IOException, XMLException, DBException {
monitor.beginTask("Parse diagram", 1);
final EntityDiagram diagram = diagramPart.getDiagram();
final DataSourceRegistry dsRegistry = DBeaverCore.getInstance().getProjectRegistry().getDataSourceRegistry(project);
if (dsRegistry == null) {
throw new DBException("Cannot find datasource registry for project '" + project.getName() + "'");
}
final Document document = XMLUtils.parseDocument(in);
final Element diagramElem = document.getDocumentElement();
monitor.done();
// Check version
final String diagramVersion = diagramElem.getAttribute(ATTR_VERSION);
if (CommonUtils.isEmpty(diagramVersion)) {
throw new DBException("Diagram version not found");
}
if (!diagramVersion.equals(String.valueOf(ERD_VERSION_1))) {
throw new DBException("Unsupported diagram version: " + diagramVersion);
}
List<TableLoadInfo> tableInfos = new ArrayList<>();
List<RelationLoadInfo> relInfos = new ArrayList<>();
Map<String, TableLoadInfo> tableMap = new HashMap<>();
final Element entitiesElem = XMLUtils.getChildElement(diagramElem, TAG_ENTITIES);
if (entitiesElem != null) {
// Parse data source
for (Element dsElem : XMLUtils.getChildElementList(entitiesElem, TAG_DATA_SOURCE)) {
String dsId = dsElem.getAttribute(ATTR_ID);
if (CommonUtils.isEmpty(dsId)) {
log.warn("Missing datasource ID");
continue;
}
// Get connected datasource
final DataSourceDescriptor dataSourceContainer = dsRegistry.getDataSource(dsId);
if (dataSourceContainer == null) {
log.warn("Datasource '" + dsId + "' not found");
continue;
}
if (!dataSourceContainer.isConnected()) {
monitor.subTask("Connect to '" + dataSourceContainer.getName() + "'");
try {
dataSourceContainer.connect(monitor, true, true);
} catch (DBException e) {
diagram.addErrorMessage("Can't connect to '" + dataSourceContainer.getName() + "': " + e.getMessage());
continue;
}
}
final DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (!(dataSource instanceof DBSObjectContainer)) {
diagram.addErrorMessage("Datasource '" + dataSourceContainer.getName() + "' entities cannot be loaded - no entity container found");
continue;
}
DBSObjectContainer rootContainer = (DBSObjectContainer) dataSource;
// Parse entities
Collection<Element> entityElemList = XMLUtils.getChildElementList(dsElem, TAG_ENTITY);
monitor.beginTask("Parse entities", entityElemList.size());
for (Element entityElem : entityElemList) {
String tableId = entityElem.getAttribute(ATTR_ID);
String tableName = entityElem.getAttribute(ATTR_NAME);
monitor.subTask("Load " + tableName);
List<String> path = new ArrayList<>();
for (Element pathElem : XMLUtils.getChildElementList(entityElem, TAG_PATH)) {
path.add(0, pathElem.getAttribute(ATTR_NAME));
}
DBSObjectContainer container = rootContainer;
for (String conName : path) {
final DBSObject child = container.getChild(monitor, conName);
if (child == null) {
diagram.addErrorMessage("Object '" + conName + "' not found within '" + container.getName() + "'");
container = null;
break;
}
if (child instanceof DBSObjectContainer) {
container = (DBSObjectContainer) child;
} else {
diagram.addErrorMessage("Object '" + child.getName() + "' is not a container");
container = null;
break;
}
}
if (container == null) {
continue;
}
final DBSObject child = container.getChild(monitor, tableName);
if (!(child instanceof DBSEntity)) {
diagram.addErrorMessage("Cannot find table '" + tableName + "' in '" + container.getName() + "'");
continue;
}
String locX = entityElem.getAttribute(ATTR_X);
String locY = entityElem.getAttribute(ATTR_Y);
DBSEntity table = (DBSEntity) child;
Rectangle bounds = new Rectangle();
if (CommonUtils.isEmpty(locX) || CommonUtils.isEmpty(locY)) {
diagram.setNeedsAutoLayout(true);
} else {
bounds.x = Integer.parseInt(locX);
bounds.y = Integer.parseInt(locY);
}
TableLoadInfo info = new TableLoadInfo(tableId, table, bounds);
tableInfos.add(info);
tableMap.put(info.objectId, info);
monitor.worked(1);
}
monitor.done();
}
}
final Element relationsElem = XMLUtils.getChildElement(diagramElem, TAG_RELATIONS);
if (relationsElem != null) {
// Parse relations
Collection<Element> relElemList = XMLUtils.getChildElementList(relationsElem, TAG_RELATION);
monitor.beginTask("Parse relations", relElemList.size());
for (Element relElem : relElemList) {
String relName = relElem.getAttribute(ATTR_NAME);
monitor.subTask("Load " + relName);
String relType = relElem.getAttribute(ATTR_TYPE);
String pkRefId = relElem.getAttribute(ATTR_PK_REF);
String fkRefId = relElem.getAttribute(ATTR_FK_REF);
if (CommonUtils.isEmpty(relName) || CommonUtils.isEmpty(pkRefId) || CommonUtils.isEmpty(fkRefId)) {
log.warn("Missing relation ID");
continue;
}
TableLoadInfo pkTable = tableMap.get(pkRefId);
TableLoadInfo fkTable = tableMap.get(fkRefId);
if (pkTable == null || fkTable == null) {
log.debug("PK (" + pkRefId + ") or FK (" + fkRefId + ") table(s) not found for relation " + relName);
continue;
}
RelationLoadInfo relationLoadInfo = new RelationLoadInfo(relName, relType, pkTable, fkTable);
relInfos.add(relationLoadInfo);
// Load columns (present only in logical relations)
for (Element columnElem : XMLUtils.getChildElementList(relElem, TAG_COLUMN)) {
String name = columnElem.getAttribute(ATTR_NAME);
String refName = columnElem.getAttribute(ATTR_REF_NAME);
relationLoadInfo.columns.put(name, refName);
}
// Load bends
for (Element bendElem : XMLUtils.getChildElementList(relElem, TAG_BEND)) {
String type = bendElem.getAttribute(ATTR_TYPE);
if (!BEND_RELATIVE.equals(type)) {
String locX = bendElem.getAttribute(ATTR_X);
String locY = bendElem.getAttribute(ATTR_Y);
if (!CommonUtils.isEmpty(locX) && !CommonUtils.isEmpty(locY)) {
relationLoadInfo.bends.add(new Point(Integer.parseInt(locX), Integer.parseInt(locY)));
}
}
}
monitor.worked(1);
}
monitor.done();
}
// Load notes
final Element notesElem = XMLUtils.getChildElement(diagramElem, TAG_NOTES);
if (notesElem != null) {
// Parse relations
Collection<Element> noteElemList = XMLUtils.getChildElementList(notesElem, TAG_NOTE);
monitor.beginTask("Parse notes", noteElemList.size());
for (Element noteElem : noteElemList) {
final String noteText = XMLUtils.getElementBody(noteElem);
ERDNote note = new ERDNote(noteText);
diagram.addNote(note, false);
String locX = noteElem.getAttribute(ATTR_X);
String locY = noteElem.getAttribute(ATTR_Y);
String locW = noteElem.getAttribute(ATTR_W);
String locH = noteElem.getAttribute(ATTR_H);
if (!CommonUtils.isEmpty(locX) && !CommonUtils.isEmpty(locY) && !CommonUtils.isEmpty(locW) && !CommonUtils.isEmpty(locH)) {
Rectangle bounds = new Rectangle(Integer.parseInt(locX), Integer.parseInt(locY), Integer.parseInt(locW), Integer.parseInt(locH));
diagram.addInitBounds(note, bounds);
}
}
}
// Fill entities
List<DBSEntity> tableList = new ArrayList<>();
for (TableLoadInfo info : tableInfos) {
tableList.add(info.table);
}
diagram.fillTables(monitor, tableList, null);
// Set initial bounds
for (TableLoadInfo info : tableInfos) {
final ERDEntity erdEntity = diagram.getERDTable(info.table);
if (erdEntity != null) {
diagram.addInitBounds(erdEntity, info.bounds);
}
}
// Add logical relations
for (RelationLoadInfo info : relInfos) {
if (info.type.equals(ERDConstants.CONSTRAINT_LOGICAL_FK.getId())) {
final ERDEntity sourceEntity = diagram.getERDTable(info.pkTable.table);
final ERDEntity targetEntity = diagram.getERDTable(info.fkTable.table);
if (sourceEntity != null && targetEntity != null) {
new ERDAssociation(targetEntity, sourceEntity, false);
}
}
}
// Set relations' bends
for (RelationLoadInfo info : relInfos) {
if (!CommonUtils.isEmpty(info.bends)) {
final ERDEntity sourceEntity = diagram.getERDTable(info.pkTable.table);
if (sourceEntity == null) {
log.warn("Source table " + info.pkTable.table.getName() + " not found");
continue;
}
final ERDEntity targetEntity = diagram.getERDTable(info.fkTable.table);
if (targetEntity == null) {
log.warn("Target table " + info.pkTable.table.getName() + " not found");
continue;
}
diagram.addInitRelationBends(sourceEntity, targetEntity, info.name, info.bends);
}
}
}
Aggregations