com.mongodb.jdbc.MySQLStatement 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.mongodb.MongoExecutionTimeoutException;
import com.mongodb.client.MongoIterable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import java.sql.Statement;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
public class MySQLStatement extends MongoStatement implements Statement {
private boolean relaxed;
private final BsonInt32 formatVersion = new BsonInt32(2);
public MySQLStatement(MongoConnection conn, String databaseName, boolean relaxed)
throws SQLException {
super(conn, databaseName);
this.relaxed = relaxed;
}
@SuppressWarnings("unchecked")
public ResultSet executeQuery(String sql) throws SQLException {
checkClosed();
closeExistingResultSet();
BsonDocument stage = constructQueryDocument(sql, "mysql", formatVersion);
try {
MongoIterable iterable =
currentDB
.withCodecRegistry(MongoDriver.registry)
.aggregate(Collections.singletonList(stage), MySQLResultDoc.class)
.maxTime(maxQuerySec, TimeUnit.SECONDS);
if (fetchSize != 0) {
iterable = iterable.batchSize(fetchSize);
}
resultSet = new MySQLResultSet(this, iterable.cursor(), relaxed);
return resultSet;
} catch (MongoExecutionTimeoutException e) {
throw new SQLTimeoutException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy