GroovyPersonService.groovy
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 groovy.transform.CompileStatic
023 import io.micronaut.context.annotation.Value
024 import io.reactivex.Flowable
025 
026 import javax.inject.Singleton
027 
028 @Singleton
029 @CompileStatic
030 class GroovyPersonService implements PersonService {
031 
032     // tag::service[]
033     private final String schema
034     private final String table
035     private final BigQueryService bq
036 
037     GroovyPersonService(
038         @Value('${person.schema:persons}') String schema,
039         @Value('${person.table:persons}') String table,
040         BigQueryService bq
041     ) {
042         this.schema = schema
043         this.table = table
044         this.bq = bq
045     }
046     // end::service[]
047 
048     @Override
049     // tag::new-person[]
050     Person createPerson(String firstName, String lastName, String email, Role role) {
051         return bq.insert(new Person(
052             id: System.currentTimeMillis(),
053             firstName: firstName,
054             lastName: lastName,
055             role: role,
056             email: email
057         ), schema, table)
058     }
059     // end::new-person[]
060 
061     @Override
062     // tag::query-single[]
063     Optional<Person> get(long id) {
064         return bq.querySingle("select * from ${schema}.${table} where id = $id") {      // <1>
065             return buildPerson(it)                                                      // <2>
066         }
067     }
068     // end::query-single[]
069 
070     @Override
071     Optional<Person> getUnsafe(long id) {
072         return bq.querySingle("select * from ${schema}.${table} where id = @id", id: id) {
073             return buildPerson(it)
074         }
075     }
076 
077     @Override
078     Flowable<Person> findByLastNameUnsafe(String lastName) {
079         return bq.query(lastName: lastName, "select * from ${schema}.${table} where last_name = @lastName") {
080             return buildPerson(it)
081         }
082     }
083 
084     @Override
085     // tag::query-many[]
086     Flowable<Person> findByLastName(String lastName) {
087         return bq.query("select * from ${schema}.${table} where last_name = $lastName") {
088             return buildPerson(it)
089         }
090     }
091     // end::query-many[]
092 
093     @Override
094     // tag::execute-update[]
095     void updateRole(long id, Role role) {
096         bq.execute """
097             update ${schema}.${table}
098             set
099                 role = $role
100             where
101                 id = $id
102         """
103     }
104     // end::execute-update[]
105 
106     @Override
107     // tag::execute-delete[]
108     void deletePerson(long id) {
109         bq.execute "delete from ${schema}.${table} where id = $id"
110     }
111     // end::execute-delete[]
112 
113     @Override
114     void deleteEverything() {
115         bq.execute"delete from ${schema}.${table} where 1 = 1"
116     }
117 
118     // tag::build-person[]
119     private static Person buildPerson(RowResult result) {
120         return new Person(
121             id: result.getLongValue('id'),
122             firstName: result.getStringValue('first_name'),
123             lastName: result.getStringValue('last_name'),
124             email: result.getStringValue('email'),
125             role: result.getEnumValue('role', Role),
126             score: result.getDoubleValue('score'),
127             created: result.getTimestampValue('created'),
128             enabled: result.getBooleanValue('enabled')
129         )
130     }
131     // end::build-person[]
132 
133 }