Sunday, 19 June 2011

Grails + SQLFire QuickStart

SQLFire is now is beta and I am sure I am like many of you out there and the first thing I want to do is take it for a spin. So how do I get started? Let’s build a quick CRUD app.

There is some great documentation available that explains the benefits of SQLFire and Grails but here all I am going to do I walkthrough the raw steps I used to create a Grails application that uses SQLFire.  

Installing SQLFire Beta

The SQLFire Beta comes as a jar file so the installation process is straightforward:

java -jar SQLFire10Beta_Installer.jar

Starting SQLFire Beta

Having installed the binary file, the next step is to start a server instance. The easiest way to do this is to create a directory (server1) within the install. This will contain your server data and configuration.

cd ~/SQLFire10Beta

mkdir server1

SQLFire Beta has a command called “sqlf” that is used mange the lifecycle of the server. To start the server simply issue an server start command and specify the server directory and ports. Here is the command I used:

export PATH=$PATH:~/SQLFire10Beta/bin

sqlf server start -dir=server1 -client-port=1527 -mcast-port=12333 –bind-address=127.0.0.1 &

You now have a very basic SQLFire instance running,

Building a CRUD application

There are many ways you could build a basic CRUD application. As SQLFire has a SQL instance I am free to use any framework I wish. In this case I decided to use Grials as I want to build an application as quickly as possible. 

Lets create a quick domain class that represents a Person. Grails has a CLI to help here:

grails create-domain-class org.springsource.example.sqlfire.Person


I want to keep this simple so lets just add a name field to the Person that must not be null and be between 3 and 80 in length.

package org.springsource.example.sqlfire

class Person {

    String name
           
    static constraints = {
        name(blank:false, nullable: false, size:3..80)
    }
}


Now we have a domain class, the next step is to create a simple controller that allows CRUD operations on my Person entity. The grails CLI can also help here:

grails create-controller org.springsource.example.sqlfire.Person

Grails will generate a controller for you but lets also get Grails to generate the reset of the application component for us. We can do this by adding the “scaffold” property to the controller:

package org.springsource.example.sqlfire

class PersonController {
            
     def scaffold = true 
}

JDBC Driver

SQLFire has a JDBC driver called sqlfireclient.jar. You can find this jar within the lib folder of our SQLFire Beta installation. Copy this jar file into the Grails application lib directory.

DataSource Configuration

The next step is to point the Grails application to your SQLFire server.  Open the file DataSource.groovy and update the driver class use the SQLFire driver and the url to point your SQLFire server instance.


    driverClassName = "com.vmware.sqlfire.jdbc.ClientDriver"

    url = "jdbc:sqlfire://127.0.0.1:1527/"

  
Starting the Application

We are now ready to start the application:

grails run-app

No comments:

Post a Comment