001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2020-2022 Agorapulse.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package com.agorapulse.micronaut.bigquery.tck;
019
020 import com.agorapulse.micronaut.bigquery.BigQueryService;
021 import com.agorapulse.micronaut.bigquery.RowResult;
022 import io.micronaut.context.annotation.Value;
023 import io.reactivex.Flowable;
024
025 import javax.inject.Singleton;
026 import java.util.Collections;
027 import java.util.HashMap;
028 import java.util.Map;
029 import java.util.Optional;
030
031 @Singleton
032 public class JavaPersonService implements PersonService {
033
034 // tag::service[]
035 private final String schema;
036 private final String table;
037 private final BigQueryService bq;
038
039 public JavaPersonService(
040 @Value("${person.schema:persons}") String schema,
041 @Value("${person.table:persons}") String table,
042 BigQueryService bq
043 ) {
044 this.schema = schema;
045 this.table = table;
046 this.bq = bq;
047 }
048 // end::service[]
049
050 @Override
051 // tag::new-person[]
052 public Person createPerson(String firstName, String lastName, String email, Role role) {
053 Person person = new Person();
054 person.setId(System.currentTimeMillis());
055 person.setFirstName(firstName);
056 person.setLastName(lastName);
057 person.setEmail(email);
058 person.setRole(role);
059
060 return bq.insert(person, schema, table);
061 }
062 // end::new-person[]
063
064 @Override
065 // tag::query-single[]
066 public Optional<Person> get(long id) {
067 return bq.querySingle(
068 Collections.singletonMap("id", id), // <1>
069 String.format("select * from %s.%s where id = @id", schema, table), // <2>
070 JavaPersonService::buildPerson // <3>
071 );
072 }
073 // end::query-single[]
074
075 @Override
076 public Optional<Person> getUnsafe(long id) {
077 return bq.querySingle(
078 String.format("select * from %s.%s where id = %d", schema, table, id),
079 JavaPersonService::buildPerson
080 );
081 }
082
083 @Override
084 public Flowable<Person> findByLastNameUnsafe(String lastName) {
085 return bq.query(
086 String.format("select * from %s.%s where last_name = '%s'", schema, table, lastName),
087 JavaPersonService::buildPerson
088 );
089 }
090
091 @Override
092 // tag::query-many[]
093 public Flowable<Person> findByLastName(String lastName) {
094 return bq.query(
095 Collections.singletonMap("last_name", lastName),
096 String.format("select * from %s.%s where last_name = @last_name", schema, table),
097 JavaPersonService::buildPerson
098 );
099 }
100 // end::query-many[]
101
102 @Override
103 // tag::execute-update[]
104 public void updateRole(long id, Role role) {
105 Map<String, Object> parameters = new HashMap<>();
106 parameters.put("id", id);
107 parameters.put("role", role);
108
109 bq.execute(
110 parameters, // <1>
111 String.format("update %s.%s set role = @role where id = @id", schema, table)// <2>
112 );
113 }
114 // end::execute-update[]
115
116 @Override
117 // tag::execute-delete[]
118 public void deletePerson(long id) {
119 bq.execute(
120 Collections.singletonMap("id", id), // <1>
121 String.format("delete from %s.%s where id = @id", schema, table) // <2>
122 );
123 }
124 // end::execute-delete[]
125
126 @Override
127 public void deleteEverything() {
128 bq.execute(
129 String.format("delete from %s.%s where 1 = 1", schema, table)
130 );
131 }
132
133 // tag::build-person[]
134 private static Person buildPerson(RowResult result) {
135 Person person = new Person();
136 person.setId(result.getLongValue("id"));
137 person.setFirstName(result.getStringValue("first_name"));
138 person.setLastName(result.getStringValue("last_name"));
139 person.setEmail(result.getStringValue("email"));
140 person.setRole(result.getEnumValue("role", Role.class));
141 person.setScore(result.getDoubleValue("score"));
142 person.setCreated(result.getTimestampValue("created"));
143 person.setEnabled(result.getBooleanValue("enabled"));
144 return person;
145 }
146 // end::build-person[]
147 }
|