com.mongodb.jdbc.MongoConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongodb-jdbc Show documentation
Show all versions of mongodb-jdbc Show documentation
JDBC Driver for MongoDB Atlas SQL interface
package com.mongodb.jdbc;
import com.google.common.base.Preconditions;
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.Document;
public class MongoConnection implements Connection {
private MongoClient mongoClient;
private String currentDB;
private String url;
private String user;
private boolean isClosed;
private boolean relaxed;
public MongoConnection(ConnectionString cs, String database, String conversionMode) {
Preconditions.checkNotNull(cs);
this.url = cs.getConnectionString();
this.user = cs.getUsername();
this.currentDB = database;
mongoClient = MongoClients.create(cs);
relaxed = conversionMode == null || !conversionMode.equals("strict");
isClosed = false;
}
private void checkConnection() throws SQLException {
if (isClosed()) {
throw new SQLException("Connection is closed.");
}
}
String getURL() {
return url;
}
String getUser() {
return user;
}
String getServerVersion() throws SQLException {
checkConnection();
BsonDocument command = new BsonDocument();
command.put("buildInfo", new BsonInt32(1));
try {
Document result = mongoClient.getDatabase("admin").runCommand(command);
return (String) result.get("version");
} catch (Exception e) {
throw new SQLException(e);
}
}
@Override
public Statement createStatement() throws SQLException {
checkConnection();
try {
return new MongoStatement(this, currentDB, relaxed);
} catch (IllegalArgumentException e) {
throw new SQLException(e);
}
}
protected MongoDatabase getDatabase(String DBName) {
return mongoClient.getDatabase(DBName);
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
checkConnection();
try {
return new MongoPreparedStatement(sql, this, currentDB, relaxed);
} catch (IllegalArgumentException e) {
throw new SQLException(e);
}
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public String nativeSQL(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
checkConnection();
}
@Override
public boolean getAutoCommit() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public void commit() throws SQLException {
checkConnection();
}
@Override
public void rollback() throws SQLException {
checkConnection();
}
@Override
public void close() {
if (isClosed()) {
return;
}
mongoClient.close();
isClosed = true;
}
@Override
public boolean isClosed() {
return isClosed;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return new MongoDatabaseMetaData(this);
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
checkConnection();
}
@Override
public boolean isReadOnly() throws SQLException {
checkConnection();
return true;
}
@Override
public void setCatalog(String catalog) throws SQLException {
checkConnection();
currentDB = catalog;
}
@Override
public String getCatalog() throws SQLException {
checkConnection();
return currentDB;
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
checkConnection();
}
@Override
public int getTransactionIsolation() throws SQLException {
checkConnection();
return Connection.TRANSACTION_NONE;
}
@Override
public SQLWarning getWarnings() throws SQLException {
checkConnection();
return null;
}
@Override
public void clearWarnings() throws SQLException {
checkConnection();
}
// --------------------------JDBC 2.0-----------------------------
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
if (resultSetType == ResultSet.TYPE_FORWARD_ONLY
&& resultSetConcurrency == ResultSet.CONCUR_READ_ONLY) {
return createStatement();
} else {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace().toString());
}
}
@Override
public PreparedStatement prepareStatement(
String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public java.util.Map> getTypeMap() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public void setTypeMap(java.util.Map> map) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
// --------------------------JDBC 3.0-----------------------------
@Override
public void setHoldability(int holdability) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public int getHoldability() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public Savepoint setSavepoint() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
checkConnection();
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public Statement createStatement(
int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
if (resultSetType == ResultSet.TYPE_FORWARD_ONLY
&& resultSetConcurrency == ResultSet.CONCUR_READ_ONLY) {
return createStatement();
} else {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
}
@Override
public PreparedStatement prepareStatement(
String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public CallableStatement prepareCall(
String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public PreparedStatement prepareStatement(String sql, String columnNames[])
throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public Clob createClob() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public Blob createBlob() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public NClob createNClob() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
@Override
public SQLXML createSQLXML() throws SQLException {
throw new SQLFeatureNotSupportedException(
Thread.currentThread().getStackTrace()[1].toString());
}
private void validateConn() throws SQLException {
Statement statement = createStatement();
boolean resultExists = statement.execute("SELECT 1 from DUAL");
if (!resultExists) {
// no resultSet returned
throw new SQLException("Connection error");
}
}
class ConnValidation implements Callable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy