Search in sources :

Example 6 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class JDBCTableColumn method getValueEnumeration.

@NotNull
@Override
public Collection<DBDLabelValuePair> getValueEnumeration(@NotNull DBCSession session, @Nullable Object valuePattern, int maxResults) throws DBException {
    DBDValueHandler valueHandler = DBUtils.findValueHandler(session, this);
    StringBuilder query = new StringBuilder();
    query.append("SELECT ").append(DBUtils.getQuotedIdentifier(this)).append(", count(*)");
    // Do not use description columns because they duplicate distinct value
    //        String descColumns = DBVUtils.getDictionaryDescriptionColumns(session.getProgressMonitor(), this);
    //        if (descColumns != null) {
    //            query.append(", ").append(descColumns);
    //        }
    query.append("\nFROM ").append(DBUtils.getObjectFullName(getTable(), DBPEvaluationContext.DML));
    if (valuePattern instanceof String) {
        query.append("\nWHERE ").append(DBUtils.getQuotedIdentifier(this)).append(" LIKE ?");
    }
    query.append("\nGROUP BY ").append(DBUtils.getQuotedIdentifier(this));
    try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false)) {
        if (valuePattern instanceof String) {
            valueHandler.bindValueObject(session, dbStat, this, 0, "%" + valuePattern + "%");
        }
        dbStat.setLimit(0, maxResults);
        if (dbStat.executeStatement()) {
            try (DBCResultSet dbResult = dbStat.openResultSet()) {
                return DBVUtils.readDictionaryRows(session, this, valueHandler, dbResult);
            }
        } else {
            return Collections.emptyList();
        }
    }
}
Also used : DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) NotNull(org.jkiss.code.NotNull)

Example 7 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class DBVUtils method readDictionaryRows.

@NotNull
public static List<DBDLabelValuePair> readDictionaryRows(DBCSession session, DBSEntityAttribute valueAttribute, DBDValueHandler valueHandler, DBCResultSet dbResult) throws DBCException {
    List<DBDLabelValuePair> values = new ArrayList<>();
    List<DBCAttributeMetaData> metaColumns = dbResult.getMeta().getAttributes();
    List<DBDValueHandler> colHandlers = new ArrayList<>(metaColumns.size());
    for (DBCAttributeMetaData col : metaColumns) {
        colHandlers.add(DBUtils.findValueHandler(session, col));
    }
    // Extract enumeration values and (optionally) their descriptions
    while (dbResult.nextRow()) {
        // Check monitor
        if (session.getProgressMonitor().isCanceled()) {
            break;
        }
        // Get value and description
        Object keyValue = valueHandler.fetchValueObject(session, dbResult, valueAttribute, 0);
        if (keyValue == null) {
            continue;
        }
        String keyLabel = valueHandler.getValueDisplayString(valueAttribute, keyValue, DBDDisplayFormat.NATIVE);
        if (metaColumns.size() > 1) {
            keyLabel = "";
            for (int i = 1; i < colHandlers.size(); i++) {
                Object descValue = colHandlers.get(i).fetchValueObject(session, dbResult, metaColumns.get(i), i);
                if (!keyLabel.isEmpty()) {
                    keyLabel += " ";
                }
                keyLabel += colHandlers.get(i).getValueDisplayString(metaColumns.get(i), descValue, DBDDisplayFormat.NATIVE);
            }
        }
        values.add(new DBDLabelValuePair(keyLabel, keyValue));
    }
    return values;
}
Also used : DBCAttributeMetaData(org.jkiss.dbeaver.model.exec.DBCAttributeMetaData) ArrayList(java.util.ArrayList) NotNull(org.jkiss.code.NotNull)

Example 8 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class SQLUtils method generateScript.

@NotNull
public static String generateScript(DBPDataSource dataSource, DBEPersistAction[] persistActions, boolean addComments) {
    SQLDialect sqlDialect = dataSource instanceof SQLDataSource ? ((SQLDataSource) dataSource).getSQLDialect() : null;
    String lineSeparator = GeneralUtils.getDefaultLineSeparator();
    StringBuilder script = new StringBuilder(64);
    if (addComments) {
        script.append(DBEAVER_DDL_COMMENT).append(Platform.getProduct().getName()).append(lineSeparator).append(DBEAVER_DDL_WARNING).append(lineSeparator);
    }
    if (persistActions != null) {
        String redefiner = null;
        if (sqlDialect != null) {
            redefiner = sqlDialect.getScriptDelimiterRedefiner();
        }
        for (DBEPersistAction action : persistActions) {
            String scriptLine = action.getScript();
            if (CommonUtils.isEmpty(scriptLine)) {
                continue;
            }
            String delimiter = sqlDialect == null ? SQLConstants.DEFAULT_STATEMENT_DELIMITER : sqlDialect.getScriptDelimiter();
            if (action.isComplex() && redefiner != null) {
                script.append(lineSeparator).append(redefiner).append(" ").append(DBEAVER_SCRIPT_DELIMITER).append(lineSeparator);
                delimiter = DBEAVER_SCRIPT_DELIMITER;
                script.append(delimiter).append(lineSeparator);
            }
            script.append(scriptLine);
            script.append(" ").append(delimiter).append(lineSeparator);
            if (action.isComplex() && redefiner != null) {
                script.append(redefiner).append(" ").append(sqlDialect.getScriptDelimiter()).append(lineSeparator);
            }
        }
    }
    return script.toString();
}
Also used : BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) NotNull(org.jkiss.code.NotNull)

Example 9 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class GeneralUtils method getProductReleaseDate.

@NotNull
public static Date getProductReleaseDate() {
    final IProduct product = Platform.getProduct();
    if (product != null) {
        Bundle definingBundle = product.getDefiningBundle();
        final Dictionary<String, String> headers = definingBundle.getHeaders();
        final String releaseDate = headers.get("Bundle-Release-Date");
        if (releaseDate != null) {
            try {
                return new SimpleDateFormat(DEFAULT_DATE_PATTERN).parse(releaseDate);
            } catch (ParseException e) {
                log.debug(e);
            }
        }
        final String buildTime = headers.get("Build-Time");
        if (buildTime != null) {
            try {
                return new SimpleDateFormat(DEFAULT_TIMESTAMP_PATTERN).parse(buildTime);
            } catch (ParseException e) {
                log.debug(e);
            }
        }
    }
    // Failed to get valid date from product bundle
    final Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 2017);
    calendar.set(Calendar.MONTH, 0);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    return calendar.getTime();
}
Also used : Bundle(org.osgi.framework.Bundle) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) NotNull(org.jkiss.code.NotNull)

Example 10 with NotNull

use of org.jkiss.code.NotNull in project dbeaver by serge-rider.

the class SharedTextColors method getColor.

@NotNull
public Color getColor(String rgbString) {
    RGB rgb;
    synchronized (rgbMap) {
        rgb = rgbMap.get(rgbString);
        if (rgb == null) {
            rgb = StringConverter.asRGB(rgbString);
            rgbMap.put(rgbString, rgb);
        }
    }
    return getColor(rgb);
}
Also used : RGB(org.eclipse.swt.graphics.RGB) NotNull(org.jkiss.code.NotNull)

Aggregations

NotNull (org.jkiss.code.NotNull)58 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)11 SQLException (java.sql.SQLException)9 DBCException (org.jkiss.dbeaver.model.exec.DBCException)9 DBException (org.jkiss.dbeaver.DBException)7 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)7 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)6 ArrayList (java.util.ArrayList)5 Tree (org.eclipse.swt.widgets.Tree)5 TreeColumn (org.eclipse.swt.widgets.TreeColumn)5 TreeItem (org.eclipse.swt.widgets.TreeItem)5 Nullable (org.jkiss.code.Nullable)5 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)5 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)5 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)5 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)5 SQLScriptStatusDialog (org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog)4 DBDValueHandler (org.jkiss.dbeaver.model.data.DBDValueHandler)3 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)3 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)3