|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner.sample; |
| 18 | + |
| 19 | +import com.google.cloud.spanner.sample.entities.Album; |
| 20 | +import com.google.cloud.spanner.sample.entities.Singer; |
| 21 | +import com.google.cloud.spanner.sample.entities.Track; |
| 22 | +import com.google.cloud.spanner.sample.repositories.AlbumRepository; |
| 23 | +import com.google.cloud.spanner.sample.repositories.SingerRepository; |
| 24 | +import com.google.cloud.spanner.sample.repositories.TrackRepository; |
| 25 | +import com.google.cloud.spanner.sample.service.SingerService; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.springframework.boot.CommandLineRunner; |
| 29 | +import org.springframework.boot.SpringApplication; |
| 30 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 31 | + |
| 32 | +@SpringBootApplication |
| 33 | +public class Application implements CommandLineRunner { |
| 34 | + private static final Logger logger = LoggerFactory.getLogger(Application.class); |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + SpringApplication.run(Application.class, args).close(); |
| 38 | + } |
| 39 | + |
| 40 | + private final DatabaseSeeder databaseSeeder; |
| 41 | + |
| 42 | + private final SingerService singerService; |
| 43 | + |
| 44 | + private final SingerRepository singerRepository; |
| 45 | + |
| 46 | + private final AlbumRepository albumRepository; |
| 47 | + |
| 48 | + private final TrackRepository trackRepository; |
| 49 | + |
| 50 | + public Application( |
| 51 | + SingerService singerService, |
| 52 | + DatabaseSeeder databaseSeeder, |
| 53 | + SingerRepository singerRepository, |
| 54 | + AlbumRepository albumRepository, |
| 55 | + TrackRepository trackRepository) { |
| 56 | + this.databaseSeeder = databaseSeeder; |
| 57 | + this.singerService = singerService; |
| 58 | + this.singerRepository = singerRepository; |
| 59 | + this.albumRepository = albumRepository; |
| 60 | + this.trackRepository = trackRepository; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void run(String... args) { |
| 65 | + |
| 66 | + // Set the system property 'drop_schema' to true to drop any existing database |
| 67 | + // schema when the application is executed. |
| 68 | + if (Boolean.parseBoolean(System.getProperty("drop_schema", "false"))) { |
| 69 | + logger.info("Dropping existing schema if it exists"); |
| 70 | + databaseSeeder.dropDatabaseSchemaIfExists(); |
| 71 | + } |
| 72 | + |
| 73 | + logger.info("Creating database schema if it does not already exist"); |
| 74 | + databaseSeeder.createDatabaseSchemaIfNotExists(); |
| 75 | + logger.info("Deleting existing test data"); |
| 76 | + databaseSeeder.deleteTestData(); |
| 77 | + logger.info("Inserting fresh test data"); |
| 78 | + databaseSeeder.insertTestData(); |
| 79 | + |
| 80 | + Iterable<Singer> allSingers = singerRepository.findAll(); |
| 81 | + for (Singer singer : allSingers) { |
| 82 | + logger.info( |
| 83 | + "Found singer: {} with {} albums", |
| 84 | + singer, |
| 85 | + albumRepository.countAlbumsBySingerId(singer.getId())); |
| 86 | + for (Album album : albumRepository.findAlbumsBySingerId(singer.getId())) { |
| 87 | + logger.info("\tAlbum: {}, released at {}", album, album.getReleaseDate()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Create a new singer and three albums in a transaction. |
| 92 | + Singer insertedSinger = |
| 93 | + singerService.createSingerAndAlbums( |
| 94 | + new Singer("Amethyst", "Jiang"), |
| 95 | + new Album(databaseSeeder.randomTitle()), |
| 96 | + new Album(databaseSeeder.randomTitle()), |
| 97 | + new Album(databaseSeeder.randomTitle())); |
| 98 | + logger.info( |
| 99 | + "Inserted singer {} {} {}", |
| 100 | + insertedSinger.getId(), |
| 101 | + insertedSinger.getFirstName(), |
| 102 | + insertedSinger.getLastName()); |
| 103 | + |
| 104 | + // Create a new track record and insert it into the database. |
| 105 | + Album album = albumRepository.getFirst().orElseThrow(); |
| 106 | + Track track = new Track(album, 1, databaseSeeder.randomTitle()); |
| 107 | + track.setSampleRate(3.14d); |
| 108 | + // Spring Data JDBC supports the same base CRUD operations on entities as for example |
| 109 | + // Spring Data JPA. |
| 110 | + trackRepository.save(track); |
| 111 | + |
| 112 | + // List all singers that have a last name starting with an 'J'. |
| 113 | + logger.info("All singers with a last name starting with an 'J':"); |
| 114 | + for (Singer singer : singerRepository.findSingersByLastNameStartingWith("J")) { |
| 115 | + logger.info("\t{}", singer.getFullName()); |
| 116 | + } |
| 117 | + |
| 118 | + // The singerService.listSingersWithLastNameStartingWith(..) method uses a read-only |
| 119 | + // transaction. You should prefer read-only transactions to read/write transactions whenever |
| 120 | + // possible, as read-only transactions do not take locks. |
| 121 | + logger.info("All singers with a last name starting with an 'A', 'B', or 'C'."); |
| 122 | + for (Singer singer : singerService.listSingersWithLastNameStartingWith("A", "B", "C")) { |
| 123 | + logger.info("\t{}", singer.getFullName()); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments