Aravind Yarram
Aravind Yarram

Reputation: 80194

what is the easiest way to check for the equality of two db rows using groovy sql

Is there a simple and concise way to check that two rows of a given Table contains the same data in all columns?

Upvotes: 0

Views: 246

Answers (2)

Dónal
Dónal

Reputation: 187399

I haven't tested this, but it seems the most obvious solution:

// get an Sql instance
def db = [url:'jdbc:hsqldb:mem:testDB', user:'sa', password:'',
    driver:'org.hsqldb.jdbcDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)

// Get 2 rows
GroovyRowResults row1 = sql.firstRow("select * from user where id = 4")
GroovyRowResults row2 = sql.firstRow("select * from user where email = '[email protected]'")

// compare them
boolean identical = row1.equals(row2)

Upvotes: 1

Andrew
Andrew

Reputation: 4624

Not especally Groovy, but I'd make SQL do the lifting a la something like:

db.firstRow("SELECT COUNT(DISTINCT CONCAT(city,state,zip)) FROM Candidates WHERE id IN (1,2)")[0] == 0

Upvotes: 0

Related Questions