Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

GSQL

Excerpt

supports easier access to databases using Groovy

.

Groovy has built-in SQL support. If that doesn't meet your needs, consider this module.

The source code can be found here :

Examples

Code Block
titleSqlGeneratorTest.groovy
/**
 * Test to verify valid construction of default DDL
 *
 * @author <a href="mailto:jeremy.rayner@bigfoot.com">Jeremy Rayner</a>
 * @version $Revision: 1.2 $
 */
package org.javanicus.gsql

import java.io.*

class SqlGeneratorTest extends GroovyTestCase {
    def database
    def sqlGenerator

    void setUp() {
        def typeMap = new TypeMap()
        def build = new RelationalBuilder(typeMap)
        def sqlGenerator = new SqlGenerator(typeMap,System.getProperty( "line.separator", "\n" ))

        def database = build.database(name:'genealogy') {
          table(name:'event') {
              column(name:'event_id', type:'integer', size:10, primaryKey:true, required:true)
              column(name:'description', type:'varchar', size:30)
          }
          table(name:'individual') {
            column(name:'individual_id', type:'integer', size:10, required:true, primaryKey:true, autoIncrement:true)
            column(name:'surname', type:'varchar', size:15, required:true)
            column(name:'event_id', type:'integer', size:10)
            foreignKey(foreignTable:'event') {
                reference(local:'event_id',foreign:'event_id')
            }
            index(name:'surname_index') {
                indexColumn(name:'surname')
            }
          }
        }
    }

    void testGenerateDDL() {
        def testWriter = new PrintWriter(new FileOutputStream("SqlGeneratorTest.sql"))
        sqlGenerator.writer = testWriter
        sqlGenerator.createDatabase(database,true)
        testWriter.flush()
   }

}