Search in sources :

Example 16 with NotNull

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

the class JDBCTable method insertData.

/**
     * Inserts data row.
     * Note: if column value is NULL then it will be skipped (to let default value to be applied)
     * If ALL columns are null then explicit NULL values will be used for all of them (to let INSERT to execute - it won't work with empty column list)
     */
@NotNull
@Override
public ExecuteBatch insertData(@NotNull DBCSession session, @NotNull final DBSAttributeBase[] attributes, @Nullable DBDDataReceiver keysReceiver, @NotNull final DBCExecutionSource source) throws DBCException {
    readRequiredMeta(session.getProgressMonitor());
    return new ExecuteBatchImpl(attributes, keysReceiver, true) {

        private boolean allNulls;

        @NotNull
        @Override
        protected DBCStatement prepareStatement(@NotNull DBCSession session, Object[] attributeValues) throws DBCException {
            // Make query
            StringBuilder query = new StringBuilder(200);
            query.append(useUpsert(session) ? "UPSERT" : "INSERT").append(" INTO ").append(getFullyQualifiedName(DBPEvaluationContext.DML)).append(//$NON-NLS-1$ //$NON-NLS-2$
            " (");
            allNulls = true;
            for (int i = 0; i < attributes.length; i++) {
                if (!DBUtils.isNullValue(attributeValues[i])) {
                    allNulls = false;
                    break;
                }
            }
            boolean hasKey = false;
            for (int i = 0; i < attributes.length; i++) {
                DBSAttributeBase attribute = attributes[i];
                if (DBUtils.isPseudoAttribute(attribute) || (!allNulls && DBUtils.isNullValue(attributeValues[i]))) {
                    continue;
                }
                //$NON-NLS-1$
                if (hasKey)
                    query.append(",");
                hasKey = true;
                query.append(getAttributeName(attribute));
            }
            //$NON-NLS-1$
            query.append(")\nVALUES (");
            hasKey = false;
            for (int i = 0; i < attributes.length; i++) {
                DBSAttributeBase attribute = attributes[i];
                if (DBUtils.isPseudoAttribute(attribute) || (!allNulls && DBUtils.isNullValue(attributeValues[i]))) {
                    continue;
                }
                //$NON-NLS-1$
                if (hasKey)
                    query.append(",");
                hasKey = true;
                //$NON-NLS-1$
                query.append("?");
            }
            //$NON-NLS-1$
            query.append(")");
            // Execute
            DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, keysReceiver != null);
            dbStat.setStatementSource(source);
            return dbStat;
        }

        @Override
        protected void bindStatement(@NotNull DBDValueHandler[] handlers, @NotNull DBCStatement statement, Object[] attributeValues) throws DBCException {
            int paramIndex = 0;
            for (int k = 0; k < handlers.length; k++) {
                DBSAttributeBase attribute = attributes[k];
                if (DBUtils.isPseudoAttribute(attribute) || (!allNulls && DBUtils.isNullValue(attributeValues[k]))) {
                    continue;
                }
                handlers[k].bindValueObject(statement.getSession(), statement, attribute, paramIndex++, attributeValues[k]);
            }
        }
    };
}
Also used : ExecuteBatchImpl(org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl) NotNull(org.jkiss.code.NotNull) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) NotNull(org.jkiss.code.NotNull)

Example 17 with NotNull

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

the class JDBCCollection method makeCollectionFromResultSet.

@NotNull
private static JDBCCollection makeCollectionFromResultSet(@NotNull JDBCSession session, @NotNull Array array, @Nullable DBSDataType elementType) throws SQLException, DBException {
    ResultSet dbResult = array.getResultSet();
    if (dbResult == null) {
        throw new DBCException("JDBC array type was not resolved and result set was not provided by driver. Return NULL.");
    }
    DBDValueHandler valueHandler;
    if (elementType == null) {
        JDBCColumnMetaData itemMeta = new JDBCColumnMetaData(session.getDataSource(), dbResult.getMetaData(), 1);
        elementType = DBUtils.resolveDataType(session.getProgressMonitor(), session.getDataSource(), itemMeta.getTypeName());
        if (elementType == null) {
            elementType = new JDBCDataType(session.getDataSource(), itemMeta);
        }
        valueHandler = DBUtils.findValueHandler(session, itemMeta);
    } else {
        valueHandler = DBUtils.findValueHandler(session, elementType);
    }
    try {
        try (DBCResultSet resultSet = JDBCResultSetImpl.makeResultSet(session, null, dbResult, ModelMessages.model_jdbc_array_result_set, true)) {
            List<Object> data = new ArrayList<>();
            while (dbResult.next()) {
                // Fetch second column - it contains value
                data.add(valueHandler.fetchValueObject(session, resultSet, elementType, 1));
            }
            return new JDBCCollection(elementType, valueHandler, data.toArray());
        }
    } finally {
        try {
            dbResult.close();
        } catch (SQLException e) {
            //$NON-NLS-1$
            log.debug("Can't close array result set", e);
        }
    }
}
Also used : JDBCDataType(org.jkiss.dbeaver.model.impl.jdbc.struct.JDBCDataType) SQLException(java.sql.SQLException) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) JDBCColumnMetaData(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCColumnMetaData) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) NotNull(org.jkiss.code.NotNull)

Example 18 with NotNull

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

the class WebUtils method openConnection.

@NotNull
public static URLConnection openConnection(String urlString, DBAAuthInfo authInfo) throws IOException {
    log.debug("Open [" + urlString + "]");
    DBPPreferenceStore prefs = DBeaverCore.getGlobalPreferenceStore();
    String proxyHost = prefs.getString(DBeaverPreferences.UI_PROXY_HOST);
    Proxy proxy = null;
    if (!CommonUtils.isEmpty(proxyHost)) {
        int proxyPort = prefs.getInt(DBeaverPreferences.UI_PROXY_PORT);
        if (proxyPort <= 0) {
            log.warn("Invalid proxy port: " + proxyPort);
        }
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
    }
    URL url = new URL(urlString);
    final URLConnection connection = (proxy == null ? url.openConnection() : url.openConnection(proxy));
    connection.setReadTimeout(10000);
    connection.setConnectTimeout(10000);
    if (connection instanceof HttpURLConnection) {
        final HttpURLConnection httpConnection = (HttpURLConnection) connection;
        //$NON-NLS-1$
        httpConnection.setRequestMethod("GET");
        httpConnection.setInstanceFollowRedirects(true);
        connection.setRequestProperty(//$NON-NLS-1$
        "User-Agent", GeneralUtils.getProductTitle());
        if (authInfo != null && !CommonUtils.isEmpty(authInfo.getUserName())) {
            // Set auth info
            String encoded = Base64.getEncoder().encodeToString((authInfo.getUserName() + ":" + CommonUtils.notEmpty(authInfo.getUserPassword())).getBytes(GeneralUtils.UTF8_CHARSET));
            connection.setRequestProperty("Authorization", "Basic " + encoded);
        }
    }
    connection.connect();
    if (connection instanceof HttpURLConnection) {
        final HttpURLConnection httpConnection = (HttpURLConnection) connection;
        final int responseCode = httpConnection.getResponseCode();
        if (responseCode != 200) {
            throw new IOException("Can't open '" + urlString + "': " + httpConnection.getResponseMessage());
        }
    }
    return connection;
}
Also used : IOException(java.io.IOException) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore) NotNull(org.jkiss.code.NotNull)

Example 19 with NotNull

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

the class DB2StructureAssistant method findObjectsByMask.

@NotNull
@Override
public List<DBSObjectReference> findObjectsByMask(DBRProgressMonitor monitor, DBSObject parentObject, DBSObjectType[] objectTypes, String objectNameMask, boolean caseSensitive, boolean globalSearch, int maxResults) throws DBException {
    LOG.debug(objectNameMask);
    List<DB2ObjectType> db2ObjectTypes = new ArrayList<>(objectTypes.length);
    for (DBSObjectType dbsObjectType : objectTypes) {
        db2ObjectTypes.add((DB2ObjectType) dbsObjectType);
    }
    DB2Schema schema = parentObject instanceof DB2Schema ? (DB2Schema) parentObject : null;
    if (schema == null && !globalSearch) {
        schema = dataSource.getDefaultObject();
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Find objects by name")) {
        return searchAllObjects(session, schema, objectNameMask, db2ObjectTypes, caseSensitive, maxResults);
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBSObjectType(org.jkiss.dbeaver.model.struct.DBSObjectType) ArrayList(java.util.ArrayList) DB2Schema(org.jkiss.dbeaver.ext.db2.model.DB2Schema) NotNull(org.jkiss.code.NotNull)

Example 20 with NotNull

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

the class DB2IndexCache method prepareLookupStatement.

@NotNull
@Override
public JDBCStatement prepareLookupStatement(@NotNull JDBCSession session, @NotNull DB2Schema db2Schema, DB2Index db2Index, String db2IndexName) throws SQLException {
    if (db2Index != null || db2IndexName != null) {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_IND);
        dbStat.setString(1, db2Schema.getName());
        dbStat.setString(2, db2Index != null ? db2Index.getName() : db2IndexName);
        return dbStat;
    } else {
        final JDBCPreparedStatement dbStat = session.prepareStatement(SQL_IND_ALL);
        dbStat.setString(1, db2Schema.getName());
        return dbStat;
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) 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