Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 2e0a447

Browse files
committed
Add new exercise UserProfileDbInitializer.java
1 parent c495ac6 commit 2e0a447

File tree

6 files changed

+409
-0
lines changed

6 files changed

+409
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>product-dao</module>
1616
<module>account-db-initializer</module>
1717
<module>wall-street-db-initializer</module>
18+
<module>user-profile-db-initializer</module>
1819
</modules>
1920

2021
<properties>

user-profile-db-initializer/README.MD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>User profiles db initializer exercise :muscle:
2+
Improve your database design and SQL skills
3+
### Task
4+
`UserProfilesDbInitializer` provides an API that allows to create(initialize) a database tables to store user information,
5+
their work profiles and its relations. Each user can have one and only one profile. A profile is optional and is related
6+
to a single user. The job of initializer is to crate proper database tables.
7+
8+
`UserProfilesDbInitializer` contains a *javadoc* that specifies database requirements. **It already contains all required Java
9+
implementation.** Your job is to **implement SQL file** `table_initialization.sql`.
10+
11+
The purpose of the task is to **design a database table following docs and naming convention and implement it using SQL**
12+
It helps you to **learn the 1 to 1 relations**, and how to design it it the database in **the most efficient way.**
13+
14+
To verify your implementation, run `UserProfilesDbInitializerTest.java`
15+
16+
### Pre-conditions :heavy_exclamation_mark:
17+
You're supposed to be familiar with database design and naming convention, *JDBC API* and *SQL*
18+
19+
### How to start :question:
20+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
21+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
22+
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
23+
24+
### Related materials :information_source:
25+
* [JDBC API basics tutorial](https://github.com/bobocode-projects/jdbc-api-tutorial/tree/master/jdbc-basics) <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>
26+
27+

user-profile-db-initializer/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>jdbc-api-exercises</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>user-profile-db-initializer</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.bobocode</groupId>
17+
<artifactId>jdbc-util</artifactId>
18+
<version>1.0-SNAPSHOT</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.util.FileReader;
4+
5+
import javax.sql.DataSource;
6+
import java.sql.Connection;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
10+
/**
11+
* {@link UserProfileDbInitializer} is an API that has only one method. It allow to create a database tables to store
12+
* information about users and their profiles.
13+
*/
14+
public class UserProfileDbInitializer {
15+
private final static String TABLE_INITIALIZATION_SQL_FILE = "db/migration/table_initialization.sql"; // todo: see the file
16+
private DataSource dataSource;
17+
18+
public UserProfileDbInitializer(DataSource dataSource) {
19+
this.dataSource = dataSource;
20+
}
21+
22+
/**
23+
* Reads the SQL script form the file and executes it
24+
*
25+
* @throws SQLException
26+
*/
27+
public void init() throws SQLException {
28+
String createTablesSql = FileReader.readWholeFileFromResources(TABLE_INITIALIZATION_SQL_FILE);
29+
30+
try (Connection connection = dataSource.getConnection()) {
31+
Statement statement = connection.createStatement();
32+
statement.execute(createTablesSql);
33+
}
34+
}
35+
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
3+
User profile database stores information about users and their work profiles.
4+
5+
Each user has one and only one work profile.
6+
7+
Each user has stored first and last names, email and birthday which are mandatory. Email is a unique value.
8+
A profile for each user is optional, and consists of optional information: city, job_position, company and education.
9+
All these fields are regular strings without restrictions.
10+
11+
TECH NOTES AND NAMING CONVENTION
12+
- All tables, columns and constraints are named using "snake case" naming convention
13+
- All table names must be plural (e.g. "companies", not "company")
14+
- All tables (except link tables) should have a single-value identifier of type BIGINT, which is a primary key
15+
- All primary key, foreign key, and unique constraint should be named according to the naming convention.
16+
- All "1 - optional 1" relations should be handled using the same primary key value for both tables. E.g. child table
17+
should have a column that stores primary key from a parent table, which is a foreign key and primary key at the same time
18+
19+
- All primary keys should be named according to the following rule "table_name_PK"
20+
- All foreign keys should be named according to the following rule "table_name_reference_table_name_FK"
21+
- All alternative keys (unique) should be named according to the following rule "table_name_column_name_AK"
22+
23+
*/
24+
25+
-- TODO: implement the SQL according to the description

0 commit comments

Comments
 (0)