Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy / Postgres "No signature of method: java.lang.String.positive()" [duplicate]

Tags:

groovy

Trying to write some basic PostgreSQL code using JDBC to eventually integrate into an application written in Groovy. I have written this Groovy code to connect to the DB and then execute statements; but, I'm getting an error that I have tried to find a solution to, and could not. Here is the relevant part of the Groovy code, along with a comment as to where the error appears to be happening:

def sql = Sql.newInstance(dbUrl, dbUser, dbPassword, dbDriver)

println "Sql Instance: " + sql

sql.execute(
        " DROP TABLE IF EXISTS test;"
        + "CREATE TABLE test ("
            + "id SERIAL,"
            + "word TEXT,"
            + "number INTEGER,"
            + "decimal NUMERIC,"
            + "datetime TIMESTAMP"
            + ");"
 )

def params = ['Hello, World!', 42, 3.14159, null]

sql.execute("INSERT INTO test (word, number, decimal, datetime)"
            + "VALUES (?,?,?,?);", params)

sql.eachRow("SELECT * FROM test;") { row ->
    println "The row Id is: ${row.id}"
        // HERE??
        + "The word is: ${row.word}"
        + "The number is: ${row.number}"
        + "The decimal is: ${row.decimal}"
        + "The date-time is: ${row.datetime}"
}
sql.close()

The console log says:

    Sql Instance: groovy.sql.Sql@5aa9e4eb
    The row Id is: 1
    Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.positive() is applicable for argument types: () values: []
    Possible solutions: notify(), tokenize(), size(), size()
    groovy.lang.MissingMethodException: No signature of method: java.lang.String.positive() is applicable for argument types: () values: []
    Possible solutions: notify(), tokenize(), size(), size()
        at DatabaseTest$_run_closure1.doCall(DatabaseTest.groovy:34)
        at DatabaseTest.run(DatabaseTest.groovy:31)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

    Process finished with exit code 1

Any idea what I'm doing wrong?

like image 563
Phrancis Avatar asked Jun 25 '15 08:06

Phrancis


Video Answer


2 Answers

The other answer gave you the solution I also would recommend, but it can be good to still know the reason for why this is happening.

Groovy uses operator overloading quite excessively. This means that if you were to write your own class, you can overload the + operator to do many things.

However, there is a difference between using + at the end of a line and at the beginning of a line.

At the end of a line, + is treated as the binary operator append (a + b), but at the start of a line, it is treated as the unary operator positive (+6 is treated as "positive six").

If you were to write this, it would work better:

println "The row Id is: ${row.id}" +
    "The word is: ${row.word}" +
    "The number is: ${row.number}" +
    "The decimal is: ${row.decimal}" +
    "The date-time is: ${row.datetime}"

If you would do this, however, you would get the output on one line, that's because you haven't added new line characters, \n

println "The row Id is: ${row.id}\n" +
    "The word is: ${row.word}\n" +
    "The number is: ${row.number}\n" +
    "The decimal is: ${row.decimal}\n" +
    "The date-time is: ${row.datetime}"

And now things are starting to look uglier, which is why Groovy's multi-line String functionality can come in handy, as shown in the other answer.

like image 107
Simon Forsberg Avatar answered Oct 09 '22 00:10

Simon Forsberg


don't use the dreaded string-concatenation! In Groovy there's a nice replacement:

println """The row Id is: ${row.id}
    The word is: ${row.word}
    The number is: ${row.number}
    The decimal is: ${row.decimal}
    The date-time is: ${row.datetime}"""
like image 20
injecteer Avatar answered Oct 09 '22 00:10

injecteer