StartingGroovy
StartingGroovy

Reputation: 2860

Grails iterating through database tables

As I'm a bit new to Grails, I'm wondering how I can iterate through the current data i have saved in the database to check if the information already exists.

For instance, lets say I have a domain class for Books and I create an action that automatically adds more books but I want to check if the book.title already exists so I don't add it again.

Side note I'm just using the default database (whatever is used when the project is set to production mode)


Edit

I'll post my domains so it is a bit easier to understand. Instead of book.title, I changed it to where book belongs to author. So I only want the author added once but able to add many books for it. The issue happens in an action i created in the controller.

Author Domain:

class Author {

    static hasMany = [books:Book]

    String authorName
    String notes
    String age

    String toString() { authorName }


    static constraints = {
        authorName()
        notes(maxSize:500)
        age()
    }
} 

Book Domain:

class Book {

    static belongsTo = Author

    String toString() { bookNumber }

    Author bookAuthor
    String title
    String numberOfPages


    static constraints = {
        bookAuthor()
        title()
        numberOfPages()
    }
}

Book Controller (this is where I'm having issues):

class BookController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    //took out index, create, list, etc. to focus on the once that I'm concerned about


    //this action will simple read a text file and add books and authors
    def gather = {

        def parseData = new parseClient()        //parses text file line by line and puts it into a list  
        def dataHolder = parseData.information   //dataHolder will hold data from text file

        int linesOfData = dataHolder.size()      //get size to iterate and add authors & books

        linesOfData.times {
            def _author = dataHolder.author[it]  //get author - Author
            def _age = dataHolder.age[it]        //get age    - Author

            def _title = dataHolder.title[it]    //get title - Book
            def _pages = dataHolder.pages[it]    //get pages - Book

            def authorInstance                   //create new Author to add
            authorInstance = new Author()        //for some reason I have to create and save AuthorName (can't have other fields) before I can add my Book correctly
            authorInstance.setAuthorName(_author)
            authorInstance.save()

            def bookInstance
            bookInstance = new Book()           //create a new Book to add
            bookInstance.setBookAuthor(authorInstance)
            bookInstance.setTitle(_title)
            bookInstance.setNumberOfPages(_pages)
            bookInstance.save()                 //has to have access to the authorInstance to add correctly which is why i was wondering how to access the database to grab it if it existed

            authorInstance.setAge(_age)        //add whatever data is left for Author
            authorInstance.save()              //save again because cant save it with this information before I add authorInstance to the Book
        }

    }

}

Text File Content:

//You'll notice that author _Scott Davis_ is in here twice.
//I don't want to add two instances of Scott Davis but need to access it to add the book
//the unique constraint makes the value come up as not null but can't be added 

Scott Davis : Groovy Recipes
Bashar Abdul Jawad : Groovy and Grails Recipes
Fergal Dearle : Groovy for Domain-Specific Languages
Scott Davis : GIS for Web Developers: Adding 'Where' to Your Web Applications

So I'm basically looking for a way to add that information and haven't found a way that seems to work without running into random problems.

Hope this clears my question up a bit, as the original question was a bit broad

Upvotes: 0

Views: 1140

Answers (1)

hvgotcodes
hvgotcodes

Reputation: 120308

In this case, you can put a unique constraint on title and grails will do that for you.

You can iterate thru the data, but you probably don't want to load the db if you can avoid it. So you could write a custom query to select the number of books for the title, for example.

Edit: for your updates

  1. You don't need to use setters in your controller. Grails adds setters/getters for you dynamically at runtime. If you want to put some logic in your setters, then you can define your own and use them in that case

  2. Have you looked at the grails documentation? http://grails.org/doc/latest/

you have a static constraints block, but you haven't defined how you want each property to be constrained. for unique title it would be

 title(unique:true)

if you want to get a list of author names, you can do

List names = Author.executeQuery('select authorName from Author')

Upvotes: 3

Related Questions