fun.langel.cql.datasource.support.JdbcDataSource Maven / Gradle / Ivy
The newest version!
package fun.langel.cql.datasource.support;
import com.mysql.cj.jdbc.Driver;
import fun.langel.cql.Language;
import fun.langel.cql.constant.Const;
import fun.langel.cql.datasource.*;
import fun.langel.cql.datasource.Connection;
import fun.langel.cql.parameter.Parameter;
import fun.langel.cql.resolve.RvResolver;
import fun.langel.cql.resolve.rv.JdbcRvResolver;
import fun.langel.cql.rv.ReturnValue;
import fun.langel.cql.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.List;
import java.util.Properties;
/**
* @author [email protected](GuHan)
* @since 2022/3/18 17:06
**/
public class JdbcDataSource implements DataSource {
private static final Logger LOG = LoggerFactory.getLogger(JdbcDataSource.class);
static {
try {
String driverKlass = System.getProperty(Const.CAVE_JDBC_DRIVER);
if (StringUtil.isEmpty(driverKlass)) {
DriverManager.registerDriver(new Driver());
} else {
Class.forName(driverKlass);
}
} catch (SQLException | ClassNotFoundException e) {
LOG.error(e.getMessage(), e);
}
}
@Override
public DataSourceType type() {
return DataSourceType.MYSQL;
}
@Override
public Connection getConnection() {
final String host = System.getProperty(Const.CAVE_MYSQL_HOST);
final String dbName = System.getProperty(Const.CAVE_MYSQL_DBNAME);
final String userName = System.getProperty(Const.CAVE_MYSQL_USERNAME);
final String password = System.getProperty(Const.CAVE_MYSQL_PASSWORD);
Properties props = new Properties();
props.setProperty(Const.CAVE_MYSQL_HOST, host);
props.setProperty(Const.CAVE_MYSQL_DBNAME, dbName);
props.setProperty(Const.CAVE_MYSQL_USERNAME, userName);
props.setProperty(Const.CAVE_MYSQL_PASSWORD, password);
try {
java.sql.Connection connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbName, userName, password);
return new JdbcConnection(connection);
} catch (SQLException ex) {
LOG.error(ex.getMessage(), ex);
}
return null;
}
}
class JdbcConnection implements Connection {
private static final Logger LOG = LoggerFactory.getLogger(JdbcConnection.class);
private java.sql.Connection connection;
JdbcConnection(java.sql.Connection connection) {
this.connection = connection;
}
@Override
public Session getSession() {
return new JdbcPreparedSession(this.connection);
}
}
class JdbcSession implements Session {
private static final Logger LOG = LoggerFactory.getLogger(JdbcSession.class);
private final RvResolver
© 2015 - 2025 Weber Informatics LLC | Privacy Policy