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]);
}
}
};
}
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);
}
}
}
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;
}
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);
}
}
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;
}
}
Aggregations