Page 1 of 1

Groovy - java scripting language

PostPosted: Tue Nov 29, 2005 12:44 pm
by neilg
For the Groovy home page see: http://groovy.codehaus.org

This sample script reads a text file and outputs some SQL DDL statements to rename columns.

Most of it is regular Java although it follows normal script practices (loosely typed, etc):

Code: Select all
myFileDirectory = "/path/to/my/textfile/"
myFileName = "mytextfile.txt"
tableName = "mytable"
myFile = new File(myFileDirectory + myFileName)

i = 1
qte = "\""

processLine =
{
   if (it.indexOf("ADD FIELD") == 0)
   {
      sql = ""
      oldColumnName = qte + "Column #".concat("" + i++) + qte
      //splits the input according to some regular expression
      newColumnName = qte + it.split(qte)[1] + qte
      //
      sql = "alter table " + tableName
      sql = sql + " rename column " + oldColumnName
      sql = sql + " to " + newColumnName
      //
      println sql
      println "GO"
   }
}

myFile.eachLine(
   processLine
)