Skip to content
Auto-generate HQL query based on DSL in domain classes
Java Groovy
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
grails-app
src/java/org/grails/plugins/search
test/unit/org/grails/plugins
.classpath
.gitignore
.project
README.md
SearchFieldsGrailsPlugin.groovy
application.properties

README.md

SearchFields

Auto-generate HQL query based on DSL in domain classes

Usage

Definition of domain class:

class Author {
    String name
    String lastName
    Date birthday

    static searchFields = [
        fields : [
            'name',
            'lastName',
            'birthday'
        ],
        defaultField : 'name', //default filter field
        defaultSort : 'name'
    ]
}

Example of use in action "list":

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    params.sort = 'name'
    params.order = 'asc'
    params.field = 'lastName'
    params.value = 'test'

    def search = Author.createSearch()
    search.setSort(params.sort, params.order == 'asc')
    search = search.execute(params.field, params.value)

    //generate: select count(author) from Author author where (lower(author.lastName) like 'test%)
    def count = Author.executeQuery(search.count)

    //generate: from Author author where(lower(author.lastName) like 'test%') order by author.name asc
    def list = Author.findAll(search.query, [max: params.max, offset: params.offset])

    [authorInstanceList: list, authorInstanceTotal: count]
}
Something went wrong with that request. Please try again.