Skip to content

Commit

Permalink
ledger-book: adding new source-connector for use pool-configured data…
Browse files Browse the repository at this point in the history
…source.
  • Loading branch information
itsoulltd committed May 24, 2022
1 parent f1b2bd2 commit d3bf426
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.itsoul.lab.ledgerbook.connector;

import com.it.soul.lab.sql.QueryExecutor;
import com.it.soul.lab.sql.SQLExecutor;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public final class SQLDataSourceConnector implements SourceConnector{

private DataSource dataSource;
private String driverClassName;
private String url;
private String username;
private String password;
private String schema;
private boolean shouldGenerateSchema;

public SQLDataSourceConnector(DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
public Connection getConnection() throws SQLException {
if (dataSource == null) throw new SQLException("DataSource must not be null!");
return dataSource.getConnection();
}

@Override
public QueryExecutor getExecutor() throws SQLException {
return new SQLExecutor(getConnection());
}

public SQLDataSourceConnector url(String url) {
this.url = url;
return this;
}

public SQLDataSourceConnector username(String username) {
this.username = username;
return this;
}

public SQLDataSourceConnector password(String password) {
this.password = password;
return this;
}

public SQLDataSourceConnector schema(String schema) {
this.schema = schema;
return this;
}

public SQLDataSourceConnector skipSchemaGeneration(boolean skip){
this.shouldGenerateSchema = !skip;
return this;
}

public SQLDataSourceConnector driverClassName(String driverClassName) {
this.driverClassName = driverClassName;
return this;
}

@Override
public boolean generateSchema() {
return shouldGenerateSchema;
}

public String driverClassName() {
return this.driverClassName;
}

public String url() {
return this.url;
}

public String username() {
return this.username;
}

public String password() {
return this.password;
}

public String schema() {
return schema;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum SourceConfig {
EMBEDDED_DERBY("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:dataSource;create=true",
"sa", "", "derby-schema.sql"),
JDBC_MYSQL("com.mysql.jdbc.Driver", "", "", "", "mysql-schema.sql"),
JDBC_POSTGRES("org.postgresql.Driver", "", "", "", "postgres-schema.sql");
JDBC_POSTGRES("org.postgresql.Driver", "", "", "", "postgres-schema.sql"),
JDBC_ORACLE("oracle.jdbc.OracleDriver", "", "", "", "oracle-schema.sql");

private final String driverClassName;
private final String url;
Expand Down

0 comments on commit d3bf426

Please sign in to comment.