Skip to content
This repository was archived by the owner on Jun 11, 2022. It is now read-only.

Commit 183b48e

Browse files
authored
Merge pull request #1 from jakub-balinski/develop
Implement DAO
2 parents 7e873fb + c1ad066 commit 183b48e

22 files changed

+2202
-48736
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
*.iml
33
/webapp/target
44
/server/target
5+
server/src/main/resources/database.trace.db
6+
server/src/main/resources/database.h2.db

sakila-min.sql

Lines changed: 1245 additions & 0 deletions
Large diffs are not rendered by default.

sakila-script.sql

Lines changed: 0 additions & 48588 deletions
This file was deleted.

server/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>h2</artifactId>
4040
<version>1.4.196</version>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.apache.commons</groupId>
44+
<artifactId>commons-dbcp2</artifactId>
45+
<version>2.7.0</version>
46+
</dependency>
4247
</dependencies>
4348

4449
<build>
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
package com.balinski.api_project;
2-
import com.balinski.api_project.database.DatabaseProxy;
2+
3+
import com.balinski.api_project.database.DaoManager;
4+
5+
import com.balinski.api_project.database.dao.DaoException;
6+
import com.balinski.api_project.database.model.Film;
37
import com.balinski.api_project.server.JettyServer;
48

5-
import java.sql.ResultSet;
6-
import java.sql.SQLException;
9+
import java.util.List;
10+
711

812
public class Main {
913

10-
public static void testDB() throws SQLException {
14+
public static void main(String[] args) {
15+
testDatabase();
16+
// runApplication();
17+
}
1118

19+
static void runApplication() {
20+
try {
21+
JettyServer.start(8080);
22+
} catch (Exception e) {
23+
System.err.println("A critical error occurred when trying to run the server.");
24+
System.err.println("The application will be stopped.");
25+
System.exit(-1);
26+
}
1227
}
1328

14-
public static void main(String[] args) throws Exception {
15-
testDB();
16-
JettyServer.start(8080);
29+
static void testDatabase() {
30+
try {
31+
List<Film> list = new DaoManager().getFilmDao().getAvailableInLanguage("jaPANESE");
32+
for(var actor : list)
33+
System.out.println(actor.getTitle());
34+
} catch (DaoException e) {
35+
e.printStackTrace();
36+
}
1737
}
1838
}
Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,115 @@
11
package com.balinski.api_project.database;
22

3-
import java.sql.Connection;
4-
import java.sql.ResultSet;
5-
import java.sql.SQLException;
6-
import java.sql.Statement;
7-
import java.util.HashSet;
8-
import java.util.Properties;
9-
import java.util.Set;
3+
import com.balinski.api_project.database.dao.ActorDao;
4+
import com.balinski.api_project.database.dao.DaoException;
5+
import com.balinski.api_project.database.dao.FilmDao;
6+
import com.balinski.api_project.database.dao.LanguageDao;
7+
import com.balinski.api_project.util.FilePropertiesLoader;
8+
import com.balinski.api_project.util.SqlExceptionPrinter;
9+
10+
import java.io.IOException;
11+
import java.sql.*;
12+
import java.util.*;
1013

1114
public class DaoManager {
12-
Connection connection;
15+
protected DataSourceWrapper dataSource;
16+
17+
public DaoManager() throws DaoException {
18+
try {
19+
initDataSource("server/src/main/resources/database.properties");
20+
} catch (IOException e) {
21+
throw new DaoException(e);
22+
}
23+
}
24+
25+
public ActorDao getActorDao() {
26+
return new ActorDao(this, false);
27+
}
28+
29+
public FilmDao getFilmDao() {
30+
return new FilmDao(this, false);
31+
}
32+
33+
public LanguageDao getLanguageDao() {
34+
return new LanguageDao(this, false);
35+
}
1336

14-
public Set<Properties> query(String sql, String[] selectedFields) {
15-
try(Connection connection = DatabaseProxy.getDatabaseConnection()) {
37+
public ActorDao getActorDao(boolean transaction) {
38+
return new ActorDao(this, transaction);
39+
}
40+
41+
public FilmDao getFilmDao(boolean transaction) {
42+
return new FilmDao(this, transaction);
43+
}
44+
45+
public LanguageDao getLanguageDao (boolean transaction) {
46+
return new LanguageDao(this, transaction);
47+
}
48+
49+
public List<Map<String, Object>> queryGetData(String sql) {
50+
List<Map<String, Object>> data = null;
51+
52+
try(Connection connection = getConnection()) {
1653
try(Statement statement = connection.createStatement()) {
17-
try(ResultSet result = statement.executeQuery(sql)) {
18-
Set<Properties> data = new HashSet<>();
19-
Properties record = new Properties();
20-
21-
while(result.next()) {
22-
for(var field : selectedFields) {
23-
record.put(field, result.getString(field));
24-
}
25-
data.add(record);
26-
record.clear();
27-
}
54+
try(ResultSet rs = statement.executeQuery(sql)) {
55+
ResultSetMetaData md = rs.getMetaData();
56+
int columns = md.getColumnCount();
57+
data = new LinkedList<>();
58+
59+
while(rs.next()) {
60+
Map<String, Object> row = new HashMap<>(columns);
61+
62+
for(int i = 1; i <= columns; i++)
63+
row.put(md.getColumnName(i), rs.getObject(i));
2864

29-
return data;
65+
data.add(row);
66+
}
3067
}
3168
}
3269
} catch (SQLException e) {
33-
DatabaseProxy.handleSqlException(e, "An error occurred when trying to query the database");
70+
SqlExceptionPrinter.print("An error occurred when trying to query the database", e);
71+
} finally {
72+
closeConnection();
3473
}
3574

36-
return null;
75+
return data;
3776
}
77+
78+
public int queryModify(String sql, boolean transaction) {
79+
int rowsAffected = 0;
80+
81+
try(Connection connection = getConnection()) {
82+
if(transaction)
83+
connection.setAutoCommit(false);
84+
85+
try(Statement statement = connection.createStatement()) {
86+
rowsAffected = statement.executeUpdate(sql);
87+
}
88+
89+
if(transaction) {
90+
connection.setAutoCommit(true);
91+
connection.commit();
92+
}
93+
} catch (SQLException e) {
94+
SqlExceptionPrinter.print("An error occurred when trying to query the database", e);
95+
} finally {
96+
closeConnection();
97+
}
98+
99+
return rowsAffected;
100+
}
101+
102+
public Connection getConnection() {
103+
return dataSource.getConnection();
104+
}
105+
106+
protected void closeConnection() {
107+
dataSource.closeConnection();
108+
}
109+
110+
protected void initDataSource(String path) throws IOException {
111+
Properties dbProps = FilePropertiesLoader.load(path);
112+
this.dataSource = new DataSourceWrapper(dbProps);
113+
}
114+
38115
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.balinski.api_project.database;
2+
3+
import com.balinski.api_project.util.SqlExceptionPrinter;
4+
import org.apache.commons.dbcp2.BasicDataSource;
5+
6+
import java.sql.*;
7+
import java.util.*;
8+
import java.util.stream.Stream;
9+
10+
public class DataSourceWrapper {
11+
private BasicDataSource dataSource;
12+
private Connection connection;
13+
private String url;
14+
private String username;
15+
private String password;
16+
17+
public DataSourceWrapper(Properties props) {
18+
try {
19+
loadDatabaseProperties(props);
20+
initDataSource();
21+
} catch (Exception e) {
22+
System.err.println("Cannot initialize database data source: " + e.getMessage());
23+
}
24+
}
25+
26+
public Connection getConnection() {
27+
try {
28+
this.connection = dataSource.getConnection();
29+
} catch (SQLException e) {
30+
SqlExceptionPrinter.print("Could not obtain an instance of connection from given data source.", e);
31+
}
32+
33+
return connection;
34+
}
35+
36+
public void closeConnection() {
37+
if(connection == null)
38+
return;
39+
40+
try {
41+
this.connection.close();
42+
} catch (SQLException e) {
43+
SqlExceptionPrinter.print("Cannot close the connection.", e);
44+
}
45+
}
46+
47+
private void initDataSource() {
48+
dataSource = new BasicDataSource();
49+
dataSource.setUrl(url);
50+
dataSource.setUsername(username);
51+
dataSource.setPassword(password);
52+
dataSource.setMinIdle(5);
53+
dataSource.setMaxIdle(10);
54+
dataSource.setMaxOpenPreparedStatements(100);
55+
}
56+
57+
private void loadDatabaseProperties(Properties props) throws NullPointerException, ClassNotFoundException {
58+
url = props.getProperty("url");
59+
String driver = props.getProperty("driver");
60+
username = props.getProperty("username");
61+
password = props.getProperty("password");
62+
63+
if(Stream.of(url, driver, username, password).anyMatch(Objects::isNull)) {
64+
throw new NullPointerException("One or more of properties (url, driver, username, password) is missing");
65+
}
66+
67+
try {
68+
Class.forName(driver);
69+
} catch (ClassNotFoundException e) {
70+
throw new ClassNotFoundException("Driver class " + driver + " not found.", e);
71+
}
72+
}
73+
}

server/src/main/java/com/balinski/api_project/database/DatabaseProxy.java

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)