Search in sources :

Example 51 with NotNull

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

the class SQLCompletionProcessor method makeTemplateProposals.

@NotNull
private ICompletionProposal[] makeTemplateProposals(ITextViewer viewer, SQLCompletionAnalyzer.CompletionRequest request) {
    String wordPart = request.wordPart.toLowerCase();
    final List<SQLTemplateCompletionProposal> templateProposals = new ArrayList<>();
    // Templates
    for (Template template : editor.getTemplatesPage().getTemplateStore().getTemplates()) {
        if (template.getName().toLowerCase().startsWith(wordPart)) {
            templateProposals.add(new SQLTemplateCompletionProposal(template, new SQLContext(SQLTemplatesRegistry.getInstance().getTemplateContextRegistry().getContextType(template.getContextTypeId()), viewer.getDocument(), new Position(request.wordDetector.getStartOffset(), request.wordDetector.getLength()), editor), new Region(request.documentOffset, 0), null));
        }
    }
    Collections.sort(templateProposals, new Comparator<SQLTemplateCompletionProposal>() {

        @Override
        public int compare(SQLTemplateCompletionProposal o1, SQLTemplateCompletionProposal o2) {
            return o1.getDisplayString().compareTo(o2.getDisplayString());
        }
    });
    return templateProposals.toArray(new ICompletionProposal[templateProposals.size()]);
}
Also used : Position(org.eclipse.jface.text.Position) SQLTemplateCompletionProposal(org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplateCompletionProposal) Region(org.eclipse.jface.text.Region) SQLContext(org.jkiss.dbeaver.ui.editors.sql.templates.SQLContext) Template(org.eclipse.jface.text.templates.Template) NotNull(org.jkiss.code.NotNull)

Example 52 with NotNull

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

the class JDBCCollection method makeCollectionFromString.

@NotNull
public static DBDCollection makeCollectionFromString(@NotNull JDBCSession session, String value) throws DBCException {
    String stringType = DBUtils.getDefaultDataTypeName(session.getDataSource(), DBPDataKind.STRING);
    if (stringType == null) {
        throw new DBCException("String data type not supported by database");
    }
    DBSDataType dataType = DBUtils.getLocalDataType(session.getDataSource(), stringType);
    if (dataType == null) {
        throw new DBCException("String data type '" + stringType + "' not supported by database");
    }
    DBDValueHandler valueHandler = DBUtils.findValueHandler(session, dataType);
    // Try to divide on string elements
    if (!CommonUtils.isEmpty(value)) {
        if (value.startsWith("[") && value.endsWith("]")) {
            // FIXME: use real parser (nested arrays, quotes escape, etc)
            String arrayString = value.substring(1, value.length() - 1);
            List<Object> items = new ArrayList<>();
            StringTokenizer st = new StringTokenizer(arrayString, ",", false);
            while (st.hasMoreTokens()) {
                String token = st.nextToken().trim();
                if (token.startsWith("\"") && token.endsWith("\"")) {
                    token = token.substring(1, token.length() - 1);
                }
                items.add(token);
            }
            return new JDBCCollectionString(dataType, valueHandler, value, items.toArray());
        }
    }
    return new JDBCCollectionString(dataType, valueHandler, value);
}
Also used : StringTokenizer(java.util.StringTokenizer) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) ArrayList(java.util.ArrayList) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) NotNull(org.jkiss.code.NotNull)

Example 53 with NotNull

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

the class JDBCCollection method makeCollectionFromJavaArray.

@NotNull
private static JDBCCollection makeCollectionFromJavaArray(@NotNull JDBCSession session, @NotNull DBSDataType elementType, @NotNull DBDValueHandler elementValueHandler, @Nullable Object arrObject) throws DBCException {
    int arrLength = arrObject == null ? 0 : java.lang.reflect.Array.getLength(arrObject);
    Object[] contents = new Object[arrLength];
    Object itemValue;
    for (int i = 0; i < arrLength; i++) {
        Object item = java.lang.reflect.Array.get(arrObject, i);
        if (item != null && item.getClass().isArray() && elementType.getDataKind() != DBPDataKind.ARRAY) {
            // This may happen in case of multidimensional array
            itemValue = makeCollectionFromJavaArray(session, elementType, elementValueHandler, item);
        } else {
            itemValue = elementValueHandler.getValueFromObject(session, elementType, item, false);
        }
        contents[i] = itemValue;
    }
    return new JDBCCollection(elementType, elementValueHandler, contents);
}
Also used : DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) NotNull(org.jkiss.code.NotNull)

Example 54 with NotNull

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

the class JDBCCollection method makeCollectionFromArray.

@NotNull
private static JDBCCollection makeCollectionFromArray(@NotNull JDBCSession session, @Nullable Array array, @NotNull DBSDataType elementType) throws SQLException, DBCException {
    final DBDValueHandler elementValueHandler = DBUtils.findValueHandler(session, elementType);
    if (array == null) {
        return new JDBCCollection(elementType, elementValueHandler, null);
    }
    Object arrObject = array.getArray();
    return makeCollectionFromJavaArray(session, elementType, elementValueHandler, arrObject);
}
Also used : DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) NotNull(org.jkiss.code.NotNull)

Example 55 with NotNull

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

the class JDBCCollection method makeCollectionFromArray.

/////////////////////////////////////////////////////////////////////////////////////
// Utilities
/////////////////////////////////////////////////////////////////////////////////////
@NotNull
public static JDBCCollection makeCollectionFromArray(@NotNull JDBCSession session, @NotNull DBSTypedObject column, Array array) throws DBCException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    DBSDataType elementType = null;
    if (column instanceof DBSTypedObjectEx) {
        DBSDataType arrayType = ((DBSTypedObjectEx) column).getDataType();
        if (arrayType != null) {
            elementType = arrayType.getComponentType(monitor);
        }
    }
    if (elementType == null) {
        try {
            if (array == null) {
                String arrayTypeName = column.getTypeName();
                DBSDataType arrayType = session.getDataSource().resolveDataType(monitor, arrayTypeName);
                if (arrayType != null) {
                    elementType = arrayType.getComponentType(monitor);
                }
            } else {
                String baseTypeName = array.getBaseTypeName();
                elementType = session.getDataSource().resolveDataType(monitor, baseTypeName);
            }
        } catch (Exception e) {
            throw new DBCException("Error resolving data type", e);
        }
    }
    try {
        if (elementType == null) {
            if (array == null) {
                throw new DBCException("Can't resolve NULL array data type");
            }
            try {
                return makeCollectionFromResultSet(session, array, null);
            } catch (SQLException e) {
                //$NON-NLS-1$
                throw new DBCException(e, session.getDataSource());
            }
        }
        try {
            return makeCollectionFromArray(session, array, elementType);
        } catch (SQLException e) {
            if (array == null) {
                //$NON-NLS-1$
                throw new DBCException(e, session.getDataSource());
            }
            try {
                return makeCollectionFromResultSet(session, array, elementType);
            } catch (SQLException e1) {
                //$NON-NLS-1$
                throw new DBCException(e1, session.getDataSource());
            }
        }
    } catch (DBException e) {
        //$NON-NLS-1$
        throw new DBCException("Can't extract array data from JDBC array", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) SQLException(java.sql.SQLException) DBSTypedObjectEx(org.jkiss.dbeaver.model.struct.DBSTypedObjectEx) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException) 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