From 025f56961ef44b3e112571d60d6f08bffe4e1f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9E=D1=80=D1=83=D0=B4=D0=B6=D0=B5=D0=B2?= Date: Sat, 10 Oct 2020 20:29:56 +0400 Subject: [PATCH] db-problems-demo example added --- examples/db-problems-demo/.gitignore | 33 ++++++++ examples/db-problems-demo/pom.xml | 79 +++++++++++++++++++ examples/db-problems-demo/readme.md | 1 + .../java/db/migration/R__0002_Add_books.java | 60 ++++++++++++++ .../DbProblemsDemoApplication.java | 24 ++++++ .../src/main/resources/application.yml | 13 +++ .../1.0/V1_1__2020_10_10_Initial_schema.sql | 23 ++++++ .../data/R__0001_Add_catalogsl_data.sql | 15 ++++ 8 files changed, 248 insertions(+) create mode 100644 examples/db-problems-demo/.gitignore create mode 100644 examples/db-problems-demo/pom.xml create mode 100644 examples/db-problems-demo/readme.md create mode 100644 examples/db-problems-demo/src/main/java/db/migration/R__0002_Add_books.java create mode 100644 examples/db-problems-demo/src/main/java/ru/otus/dbproblemsdemo/DbProblemsDemoApplication.java create mode 100644 examples/db-problems-demo/src/main/resources/application.yml create mode 100644 examples/db-problems-demo/src/main/resources/db/migration/1.0/V1_1__2020_10_10_Initial_schema.sql create mode 100644 examples/db-problems-demo/src/main/resources/db/migration/data/R__0001_Add_catalogsl_data.sql diff --git a/examples/db-problems-demo/.gitignore b/examples/db-problems-demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/examples/db-problems-demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/examples/db-problems-demo/pom.xml b/examples/db-problems-demo/pom.xml new file mode 100644 index 00000000..eec2be97 --- /dev/null +++ b/examples/db-problems-demo/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.4.RELEASE + + + ru.otus + db-problems-demo + 0.0.1-SNAPSHOT + db-problems-demo + Demo project for Db usage problems + + + 11 + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-web + + + + org.flywaydb + flyway-core + + + + com.h2database + h2 + 1.4.200 + test + + + + org.postgresql + postgresql + 42.2.17 + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/examples/db-problems-demo/readme.md b/examples/db-problems-demo/readme.md new file mode 100644 index 00000000..d70fff70 --- /dev/null +++ b/examples/db-problems-demo/readme.md @@ -0,0 +1 @@ +Проект с миграцией для демонстрации того, как выглядит джойн нескольких таблиц при связи "многие-ко-многим" \ No newline at end of file diff --git a/examples/db-problems-demo/src/main/java/db/migration/R__0002_Add_books.java b/examples/db-problems-demo/src/main/java/db/migration/R__0002_Add_books.java new file mode 100644 index 00000000..84e9608b --- /dev/null +++ b/examples/db-problems-demo/src/main/java/db/migration/R__0002_Add_books.java @@ -0,0 +1,60 @@ +package db.migration; + +import org.flywaydb.core.api.migration.BaseJavaMigration; +import org.flywaydb.core.api.migration.Context; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.jdbc.datasource.SingleConnectionDataSource; + +import javax.sql.DataSource; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("unchecked") +public class R__0002_Add_books extends BaseJavaMigration { + + public static final int BOOKS_COUNT = 100_000; + + @Override + public void migrate(Context context) throws Exception { + DataSource ds = new SingleConnectionDataSource(context.getConnection(), true); + NamedParameterJdbcOperations jdbc = new NamedParameterJdbcTemplate(ds); + + insertBooks(jdbc); + insertBooksRelations(jdbc); + } + + private void insertBooks(NamedParameterJdbcOperations jdbc) { + List> params = new ArrayList<>(BOOKS_COUNT); + for (int i = 0; i < BOOKS_COUNT; i++) { + String bookTitle = String.format("Заголовок книги #%d", i); + String bookTextPart = String.format("Это текст книги #%d", i); + String bookText = bookTextPart.repeat( 8000 / bookTextPart.length()); + params.add(new MapSqlParameterSource("title", bookTitle) + .addValue("bookText", bookText) + .getValues() + ); + } + jdbc.batchUpdate("insert into books (title, book_text) values (:title, :bookText)", + params.toArray(new Map[0])); + } + + private void insertBooksRelations(NamedParameterJdbcOperations jdbc) { + List> paramsForAuthors = new ArrayList<>(BOOKS_COUNT); + List> paramsForGenres = new ArrayList<>(BOOKS_COUNT); + for (int i = 1; i <= BOOKS_COUNT; i++) { + for (int j = 1; j <= 3; j++) { + paramsForAuthors.add(new MapSqlParameterSource("bookId", i).addValue("authorId", j).getValues()); + paramsForGenres.add(new MapSqlParameterSource("bookId", i).addValue("genreId", j).getValues()); + } + } + jdbc.batchUpdate("insert into books_authors (book_id, author_id) values (:bookId, :authorId)", + paramsForAuthors.toArray(new Map[0])); + + jdbc.batchUpdate("insert into books_genres (book_id, genre_id) values (:bookId, :genreId)", + paramsForGenres.toArray(new Map[0])); + } + +} diff --git a/examples/db-problems-demo/src/main/java/ru/otus/dbproblemsdemo/DbProblemsDemoApplication.java b/examples/db-problems-demo/src/main/java/ru/otus/dbproblemsdemo/DbProblemsDemoApplication.java new file mode 100644 index 00000000..ed8a5ff4 --- /dev/null +++ b/examples/db-problems-demo/src/main/java/ru/otus/dbproblemsdemo/DbProblemsDemoApplication.java @@ -0,0 +1,24 @@ +package ru.otus.dbproblemsdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.sql.SQLException; + +/* +SELECT * FROM AUTHORS; +SELECT * FROM GENRES; +SELECT * FROM BOOKS_AUTHORS ; +SELECT * FROM BOOKS_GENRES; +SELECT * FROM BOOKS; +SELECT * FROM BOOKS_ALL_INFO ; + */ + +@SpringBootApplication +public class DbProblemsDemoApplication { + + public static void main(String[] args) throws SQLException { + SpringApplication.run(DbProblemsDemoApplication.class, args); + } + +} diff --git a/examples/db-problems-demo/src/main/resources/application.yml b/examples/db-problems-demo/src/main/resources/application.yml new file mode 100644 index 00000000..ea3a99d4 --- /dev/null +++ b/examples/db-problems-demo/src/main/resources/application.yml @@ -0,0 +1,13 @@ +spring: + datasource: + #url: jdbc:h2:mem:testdb + url: jdbc:postgresql://localhost:5432/problems + username: postgres + password: postgres + driver-class-name: org.postgresql.Driver + initialization-mode: never + h2: + console: + enabled: true + flyway: + enabled: true \ No newline at end of file diff --git a/examples/db-problems-demo/src/main/resources/db/migration/1.0/V1_1__2020_10_10_Initial_schema.sql b/examples/db-problems-demo/src/main/resources/db/migration/1.0/V1_1__2020_10_10_Initial_schema.sql new file mode 100644 index 00000000..dda2b660 --- /dev/null +++ b/examples/db-problems-demo/src/main/resources/db/migration/1.0/V1_1__2020_10_10_Initial_schema.sql @@ -0,0 +1,23 @@ +--date: 2002-10-10 +--author: stvort + +create table authors (id bigserial, name varchar(50), primary key (id)); +create table genres (id bigserial, name varchar(50), primary key (id)); +create table books (id bigserial, title varchar(50), book_text varchar(8000), primary key (id)); +create table books_authors (book_id bigint not null references books (id) on delete cascade , author_id bigint not null references authors (id) on delete cascade); +create table books_genres (book_id bigint not null references books (id) on delete cascade , genre_id bigint not null references genres (id) on delete cascade); + + +create view books_all_info as +select b.id as "ID книги", + b.title as "Заголовок книги", + b.book_text as "Текст книги", + a.id as "ID автора", + a.name as "Имя автора", + g.id as "ID жанра", + g.name as "Название жанра" +from books b left join + books_authors ba on b.id = ba.book_id left join + books_genres bg on b.id = bg.book_id left join + authors a on ba.author_id = a.id left join + genres g on bg.genre_id = g.id; diff --git a/examples/db-problems-demo/src/main/resources/db/migration/data/R__0001_Add_catalogsl_data.sql b/examples/db-problems-demo/src/main/resources/db/migration/data/R__0001_Add_catalogsl_data.sql new file mode 100644 index 00000000..ff10f291 --- /dev/null +++ b/examples/db-problems-demo/src/main/resources/db/migration/data/R__0001_Add_catalogsl_data.sql @@ -0,0 +1,15 @@ +insert into authors(id, name) +values (1, 'Админ Супервайзерович Рут'), + (2, 'Валентина Игоревна Априори'), + (3, 'Питуний Дельфинович Автостопов'), + (4, 'Инокентий Купертинович Жужа'); + +insert into genres(id, name) +values (1, 'Детектив'), + (2, 'Фантастика'), + (3, 'Фэнтези'), + (4, 'Техническая литература'); + + + +