Skip to content

Commit

Permalink
Add Oracle Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
palych-piter committed Jun 21, 2017
1 parent 5568dd5 commit c72ddba
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 75 deletions.
35 changes: 0 additions & 35 deletions src/main/java/excel2db/InsertDBRecord.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/java/excel2db/excel2db.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public void setInitConstants(InitConstants initConstants) {
}



public static void main(String[] args) {

try {
Expand All @@ -80,7 +79,7 @@ public static void main(String[] args) {
HashSet<String> fileList = app.generateFileList.generateFileList();
if (fileList == null){
throw new Exception("File list is empty in property file");
//logger.error("Empty file list");

}

//this way we call methods for objects that is already initialized by the Spring
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/excel2db/service/DBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ public interface DBConnection {

public void establishDBConnection();

interface CreateTable {
public void createTable();
}
}
29 changes: 29 additions & 0 deletions src/main/java/excel2db/service/impl/CreateTableDerbyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package excel2db.service.impl;

/**
* Created by Andrey on 6/21/2017.
*/

import java.sql.SQLException;

import excel2db.service.CreateTable;
import excel2db.service.PopulateTable;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class CreateTableDerbyImpl implements CreateTable {

public static final Logger logger = LoggerFactory.getLogger(PopulateTableOracleImpl.class);

private Integer numOfProcessedRows = 0;

public void createTable(Sheet sheet, String tableName)
throws SQLException {

logger.info("Create table method : Derby implementation is not covered yet");

}

}
53 changes: 52 additions & 1 deletion src/main/java/excel2db/service/impl/CreateTableOracleImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package excel2db.service.impl;

import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

import excel2db.excel2db;
import excel2db.service.CreateTable;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
Expand All @@ -12,8 +19,52 @@ public class CreateTableOracleImpl implements CreateTable {

public void createTable(Sheet sheet, String tableName) throws SQLException {

logger.info("Create table method : Oracle implementation is not covered yet");
Map<String, Integer> header = new LinkedHashMap<>();
header = InitInputFilesImpl.readSheetHeader(sheet);

//dropping statement
String sqlTableDropStatement = "DROP TABLE \"" +
tableName + "\"";

//creating statement
StringBuilder sqlTableCreateStatement = new StringBuilder();
sqlTableCreateStatement.append("CREATE TABLE \"" +
tableName + "\"(");
for (String headerColumnName : header.keySet()) {
sqlTableCreateStatement.append("\"" + headerColumnName + "\"" + " VARCHAR2(20), ");
}

sqlTableCreateStatement.setLength(sqlTableCreateStatement.length() - 2);
sqlTableCreateStatement.append(")");


//Oracle doesn't support the "DROP IF EXISTS" SQL statement
//Thus, checking the table and dropping if the table exists
logger.info("Checking if the table " + tableName + " already exists");
DatabaseMetaData dbm = excel2db.connection.getMetaData();
ResultSet tables = dbm.getTables(null, null, tableName, null);
if (tables.next()) {
// Table exists, dropping
logger.info("The table " + tableName + " exists, dropping ...");
PreparedStatement pstmtDrop = excel2db.connection.prepareStatement(sqlTableDropStatement);
pstmtDrop.execute();
logger.info("The table " + tableName + " has been dropped");
}
else {
// Table does not exist, skipp dropping
logger.info("The table " + tableName + " doesn't exist, skip dropping");
}


//creating
logger.info("The table " + tableName + " is being created ...");
PreparedStatement pstmtCreate = excel2db.connection.prepareStatement(sqlTableCreateStatement.toString());
pstmtCreate.execute();
logger.info("The table " + tableName + " has been created");


}

}


65 changes: 65 additions & 0 deletions src/main/java/excel2db/service/impl/DBConnectionDerbyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package excel2db.service.impl;

/**
* Created by Andrey on 6/21/2017.
*/

import java.sql.DriverManager;
import java.sql.SQLException;

import excel2db.excel2db;
import excel2db.service.DBConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;





public class DBConnectionDerbyImpl implements DBConnection {

// connection parameters
@Value("${db.derby.server}")
String dbServer;

@Value("${db.derby.user}")
String dbUser;

@Value("${db.derby.password}")
String dbPassword;

@Value("${db.derby.port}")
String dbPort;

@Value("${db.derby.database}")
String dbName;


public static final Logger logger = LoggerFactory.getLogger(DBConnectionDerbyImpl.class);

public void establishDBConnection() {
{

try {
excel2db.connection = DriverManager.getConnection(
//"jdbc:derby//" + dbServer + ":" + dbPort + "/" + dbName + ";create=true;user=" + dbUser + ";password=" + dbPassword
"jdbc:derby://localhost:1527/testDB;create=true"
);

} catch (SQLException e) {
logger.error("Derby Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (excel2db.connection != null) {
logger.info("Derby connection is established");
} else {
logger.error("Failed to make the Derby connection!");
}

}

}

}
41 changes: 33 additions & 8 deletions src/main/java/excel2db/service/impl/DBConnectionOracleImpl.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
package excel2db.service.impl;

import java.sql.DriverManager;
import java.sql.SQLException;

import excel2db.excel2db;
import excel2db.service.DBConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import oracle.jdbc.driver.OracleDriver;


public class DBConnectionOracleImpl implements DBConnection {

// connection parameters
@Value("${db.server}")
@Value("${db.oracle.server}")
String dbServer;

@Value("${db.user}")
@Value("${db.oracle.user}")
String dbUser;

@Value("${db.password}")
@Value("${db.oracle.password}")
String dbPassword;

@Value("${db.port}")
@Value("${db.oracle.port}")
String dbPort;

@Value("${db.database}")
String dbDatabase;
//it could be either sid or service name,
// depending what is provided to connect to the DB
@Value("${db.oracle.sid}")
String dbSid;

public static final Logger logger = LoggerFactory.getLogger(DBConnectionPostgresImpl.class);
public static final Logger logger = LoggerFactory.getLogger(DBConnectionOracleImpl.class);

public void establishDBConnection() {
{
logger.info("Oracle connection is not implemented yet");

try {
excel2db.connection = DriverManager.getConnection(
"jdbc:oracle:thin:@//" + dbServer + ":" + dbPort + "/" + dbSid, dbUser, dbPassword)
;

} catch (SQLException e) {
logger.error("Oracle Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (excel2db.connection != null) {
logger.info("Oracle connection is established");
} else {
logger.error("Failed to make the Oracle connection!");
}

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class PopulateTableOracleImpl implements PopulateTable {

public static final Logger logger = LoggerFactory.getLogger(PopulateTablePostgresImpl.class);
public static final Logger logger = LoggerFactory.getLogger(PopulateTableOracleImpl.class);

private Integer numOfProcessedRows = 0;

Expand Down
34 changes: 26 additions & 8 deletions src/main/resources/excel2db.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# names of input tables
input.files=test.xlsx

# db implementation classes
#db implementation: Postgres, Oracle
db.implementation=Postgres

# postgres section
# dbConnectionImplenentation=excel2db.service.impl.DBConnectionPostgresImpl
# oracle section
dbConnectionImplenentation=excel2db.service.impl.DBConnectionOracleImpl
# names of input tables
input.files=test.xlsx,test1.xlsx

# postgres connection parameters
db.server=localhost
Expand All @@ -15,3 +11,25 @@ db.port=5433
db.user=postgres
db.password=postgres

# oracle connection parameters
db.oracle.server=localhost
# specify sid or SERVICE NAME here
db.oracle.sid=xe
db.oracle.port=1521
db.oracle.user=oracle
db.oracle.password=oracle


# DON'T TOUCH :

# set db implementation classes based on the db.implementation
dbConnectionImplenentation=excel2db.service.impl.DBConnection${db.implementation}Impl
createTableImplenentation=excel2db.service.impl.CreateTable${db.implementation}Impl

# derby connection parameters
db.derby.server=localhost
db.derby.database=excel2db
db.derby.port=5433
db.derby.user=postgres
db.derby.password=postgres

7 changes: 4 additions & 3 deletions src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@

<!-- This way it is possible to get values from a properties file nd use the values -->
<!-- as paramaters in the spring configuration -->
<!--<bean id="dbConnection" class="${dbConnectionImplenentation}"></bean>-->

<bean id="dbConnection" class="excel2db.service.impl.DBConnectionPostgresImpl"></bean>
<bean id="dbConnection" class="${dbConnectionImplenentation}"></bean>
<!--<bean id="dbConnection" class="excel2db.service.impl.DBConnectionPostgresImpl"></bean>-->

<bean id="createTable" class="excel2db.service.impl.CreateTablePostgresImpl"></bean>
<bean id="createTable" class="${createTableImplenentation}"></bean>
<!--<bean id="createTable" class="excel2db.service.impl.CreateTablePostgresImpl"></bean>-->

<bean id="populateTable" class="excel2db.service.impl.PopulateTablePostgresImpl"></bean>

Expand Down
6 changes: 0 additions & 6 deletions src/test/java/InitInputFilesImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ public class InitInputFilesImplTest {
public void testInitInputFilesImpl() throws Exception {

//initializing application context
//need to check if we really need to get the spring.xml if the config is in "locations"
// AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// ApplicationContextUtils appcontext= (ApplicationContextUtils) context.getBean("applicationContextUtils");
// ApplicationContext appCon = appcontext.getApplicationContext();
// excel2db app = (excel2db) appCon.getBean("excel2db");

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
excel2db app = (excel2db) context.getBean("excel2db");

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/JUnitSuitePopulateTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import org.junit.runner.RunWith;
import org.junit.runners.Suite;


@RunWith(Categories.class)
@Categories.IncludeCategory(JUnitPopulateTableCategory.class)
@Suite.SuiteClasses({PopulateTablePostgresImplTest.class})
@Suite.SuiteClasses({PopulateTableImplTest.class})
public class JUnitSuitePopulateTable {

}
Loading

0 comments on commit c72ddba

Please sign in to comment.