简体   繁体   English

如何在Groovy中将PL / SQL to_date与变量一起使用?

[英]How do I use PL/SQL to_date with a variable in Groovy?

I've got the following small Groovy script that just does a count of rows in the database for a specific date. 我有以下小的Groovy脚本,该脚本只对特定日期的数据库中的行进行计数。

import groovy.sql.Sql

def today= new GregorianCalendar()
def dateString = "${today.get(Calendar.MONTH)+1}/${today.get(Calendar.DAY_OF_MONTH)-1}/${today.get(Calendar.YEAR)}"

def sql = Sql.newInstance("jdbc:oracle:thin:bc/bc@nemesis:1521:billctr", "bc","bc", "oracle.jdbc.OracleDriver")

def sqlLine = "select count(id) as count from bc_payment where trunc(paymentdate) = to_date(${dateString}, \'MM/DD/YYYY\')"
println(sqlLine)
def payCount = sql.execute(sqlLine)
println payCount

to_date requires single-quotes around the date you pass in. If I leave them off, I get SQLException: Invalid column type but if I put \\' around the variable, I get a warning from Groovy to_date要求在传递日期前后加上单引号。如果我不使用单引号,则会得到SQLException: Invalid column type但是如果在变量周围加上\\',则会收到Groovy的警告。

WARNING: In Groovy SQL please do not use quotes around dynamic expressions (which start with $) as this means we cannot use a JDBC PreparedStatement and so is a security hole. Groovy has worked around your mistake but the security hole is still there. The expression so far is: select count(id) as count from bc_payment where trunc(paymentdate) = to_date('?', 'MM/DD/YYYY')

Is there a better way of doing this without to_date or formatting the variable differently? 有没有没有to_date或以不同的方式设置变量格式的更好方法? I'm new to Groovy so any suggestions are welcome. 我是Groovy的新手,欢迎任何建议。 Thanks in advance! 提前致谢!

Try the following (I hope I haven't introduced a syntax error, no Groovy here...) 尝试以下操作(我希望我没有引入语法错误,这里没有Groovy ...)

import groovy.sql.Sql

def today= new java.sql.Date(new java.util.Date().getTime())

def sql = Sql.newInstance("jdbc:oracle:thin:bc/bc@nemesis:1521:billctr", "bc","bc", "oracle.jdbc.OracleDriver")

def sqlLine = "select count(id) as count from bc_payment where trunc(paymentdate) = ?"
println(sqlLine)
def payCount = sql.execute(sqlLine, [today])
println payCount

Edit: replaced 编辑:替换

def today = new Date()

with

def today= new java.sql.Date(new java.util.Date().getTime())

Late answer by a developer with similar problems. 具有类似问题的开发人员的最新答案。

I found that the problem was could be fixed by changing the declaration: 我发现可以通过更改声明来解决此问题:

def sqlLine = "... ${yourString} ..."

... which creates sqlLine as a GStringImpl-object. ...将sqlLine创建为GStringImpl对象。 If instead you declare sqlLine like this: 相反,如果您这样声明sqlLine:

String sqlLine = "... ${yourString} ..."

... we resolve the variables inline and receive a String-object. ...我们内联解析变量,并接收一个String对象。 This way groovy.sql.Sql never know that we created the sql dynamically. 这样groovy.sql.Sql永远不会知道我们动态创建了sql。

Actually you can read the sql instance parameters from the DataSource doing the followng: 实际上,您可以执行以下操作从DataSource中读取sql实例参数:

def _url      = ConfigurationHolder.config.dataSource.url
def _username = ConfigurationHolder.config.dataSource.username
def _password = ConfigurationHolder.config.dataSource.password
def _driver   = ConfigurationHolder.config.dataSource.driverClassName

def sql = Sql.newInstance(_url, _username, _password, _driver)

// For the paging
def int max    = Math.min(params.max ? params.max.toInteger() : 25,  100)
def int offset = params.offset.toInteger()
def int last   = offset + max

def month= params.month_value

I use Oracle TO_DATE and TO_TIMESTAMP function. 我使用Oracle TO_DATETO_TIMESTAMP函数。 In my case it is as follows: 就我而言,如下所示:

query = "select * from " +
          "(SELECT  reporting.id, " +
          "company_id as comp, " +      
          "to_date(TO_CHAR(invoice,'dd.mm.YYYY')) as invoice, " +
          "TO_CHAR(last_updated,'dd.mm.YYYY HH:MI') as erstelltAm, " +
          "row_number() over (" + sortByStr + ") as row_num FROM reporting, company " +
          "WHERE reporting.company_id = company.id) " +
              "reporting.month = TO_TIMESTAMP(" + month + ", 'dd.mm.yy')""
        "where ROW_NUM between " + offset + " and " + last;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM