0

我有一个包含内容的文本文件:

ACTION = NEW        // (NEW, CHANGE)
NEW_NUMBER = '123456'
NUMBER_FOR_CHANGE = '789456'
TEXT_NAME1 = 'TEST1'
TEXT_NAME2 = 'TEST2'
TEXT_NAME3 = 'TEST3'

我想在 Groovy 脚本上编写一个代码来解析这个文件并进行下一个更改:

如果行动=新:

`sql.executeInsert "INSERT INTO TABLE.DB (NUMBER, NAME1, NAME2, NAME3) VALUES ($NEW_NUMBER, $TEXT_NAME1, $TEXT_NAME2, $TEXT_NAME3)" `    

如果行动=改变:

    sql.executeUpdate "UPDATE TABLE.DB SET NAME1 = $TEXT_NAME1, NAME2 = $TEXT_NAME2, NAME3 = $TEXT_NAME3 WHERE NUMBER LIKE $NUMBER_FOR_CHANGE"

如果动作 = 删除:

    sql.execute "DELETE FROM TABLE.DB WHERE NUMBER = $NUMBER_FOR_CHANGE"

我将不胜感激任何帮助

谢谢

4

1 回答 1

2

这样的事情应该这样做吗?

def sqlstr = new Properties().with { props ->
  new File( '/path/to/file' ).withReader { r ->
    load( r )
    switch( props.ACTION ) {
      case 'NEW':
        "INSERT INTO TABLE.DB (NUMBER, NAME1, NAME2, NAME3) VALUES ($NEW_NUMBER, $TEXT_NAME1, $TEXT_NAME2, $TEXT_NAME3)"
        break
      case 'CHANGE':
        "UPDATE TABLE.DB SET NAME1=$TEXT_NAME1, NAME2=$TEXT_NAME2, NAME3=$TEXT_NAME3 WHERE NUMBER = $NUMBER_FOR_CHANGE"
        break
      case 'DELETE':
        "DELETE FROM TABLE.DB WHERE NUMBER = $NUMBER_FOR_CHANGE"
        break
      default:
        throw new RuntimeException( "Unknown ACTION $props.ACTION" )
    }
  }
}

// Assuming sql is set up right:
sql.execute sqlstr
于 2013-01-28T13:44:56.483 回答