All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iskrenyp.spockdbrepo.api.withviews.ViewsManager.groovy Maven / Gradle / Ivy

Go to download

A simple annotation driven local extension for Spock framework, which enables you to screen record your Specifications

The newest version!
package com.github.iskrenyp.spockdbrepo.api.withviews

import com.github.iskrenyp.spockdbrepo.api.repo.SqlDataStore
import com.github.iskrenyp.spockdbrepo.exception.DbRepoException
import static com.github.iskrenyp.spockdbrepo.api.repo.SqlDataStore.CONFIG_FILE_NAME

class ViewsManager {

    SqlDataStore dataStore
    String[] requiredViews

    ViewsManager(SqlDataStore dataStore, String[] requiredViews) {
        this.dataStore = dataStore
        this.requiredViews = requiredViews
    }

    Boolean isRequiredViews() {
        requiredViews && requiredViews.size() > 0
    }

    Map getConfiguredViews() throws DbRepoException {
        Map views = dataStore?.config?.views
        if (!views || views.isEmpty()) throw new DbRepoException("No views were configured for ${dataStore.config.url}")
        views
    }

    Map findMatchingViews() throws DbRepoException {
        Map configuredViews = getConfiguredViews()
        if (isRequiredViews()) {
            Map matchingViews = configuredViews.findAll { k, v -> k in requiredViews }
            if (!matchingViews || matchingViews.isEmpty()) throw new DbRepoException("Views for ${dataStore.repoName} were required: $requiredViews, but not found in $CONFIG_FILE_NAME")
            else matchingViews
        } else configuredViews
    }

    def createMatchingViews() throws DbRepoException {
        findMatchingViews().each { k, v ->
            dataStore.execute("""
                                          create view $k as
                                          $v""")
        }
    }

    def dropMatchingViews() throws DbRepoException {
        findMatchingViews().each { k,v ->
            dataStore.execute("drop view if exists $k;")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy