/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.storage.jdbc.aggregate;

import io.spine.server.storage.jdbc.query.AbstractSelectByIdQuery;

import static io.spine.server.storage.jdbc.aggregate.EventCountTable.Column.event_count;

/**
 * A query that selects event count by corresponding aggregate ID.
 *
 * @author Dmytro Grankin
 */
class SelectEventCountByIdQuery<I> extends AbstractSelectByIdQuery<I, Integer> {

    private SelectEventCountByIdQuery(Builder<I> builder) {
        super(builder);
    }

    @Override
    public Integer execute() {
        return factory().select(pathOf(event_count.name(), Integer.class))
                        .from(table())
                        .where(hasId())
                        .fetchOne();
    }

    static <I> Builder<I> newBuilder() {
        return new Builder<>();
    }

    @SuppressWarnings("ClassNameSameAsAncestorName")
    static class Builder<I> extends AbstractSelectByIdQuery.Builder<I,
                                                                    Builder<I>,
                                                                    SelectEventCountByIdQuery<I>> {
        @Override
        protected SelectEventCountByIdQuery<I> doBuild() {
            return new SelectEventCountByIdQuery<>(this);
        }

        @Override
        protected Builder<I> getThis() {
            return this;
        }
    }
}

@@ -1059,16 +1059,100 @@
 egate;%0A%0A
+import com.querydsl.core.Fetchable;%0Aimport com.querydsl.core.types.dsl.PathBuilder;%0A
 import i
@@ -1635,32 +1635,48 @@
     
-return factory().select(
+final PathBuilder%3CInteger%3E eventCount = 
 path
@@ -1712,18 +1712,114 @@
 r.class)
-)%0A
+;%0A        final Fetchable%3CInteger%3E query = factory().select(eventCount)%0A                          
         
@@ -1845,24 +1845,50 @@
 om(table())%0A
+                          
             
@@ -1914,16 +1914,17 @@
 hasId())
+;
 %0A       
@@ -1928,35 +1928,70 @@
     
-                .fetchOne()
+final Integer result = query.fetchOne();%0A        return result
 ;%0A  

Extract variables for a better readability