Here's what I came up with:
import groovy.sql.Sql
sql = Sql.newInstance("jdbc:mysql://localhost:3306/mydb", "mydbuser",
"mydbpass", "com.mysql.jdbc.Driver" )
def badIdList = []
sql.eachRow( "select id, weird_col, active from table1 where active = 1 and weird_col is not null" ) {
myId = it.id
checkCount =
sql.firstRow( "select count(*) as count from table2 where table1_id = ${myId} and is_right = 1").count
if ( checkCount == 0 )
{
println( "bad id: ${myId}")
badIdList.add( myId )
}
}
println( badIdList )
Now I guess I am just a really big nerd, but I felt oh so satisfied after writing that. For sh*!ts and grins, I wrote the same thing in java, and it was a lot of code, and it was annoying, and you had to do the while rs.hasNext thing, and get the current resultset, and then do the rs.getString() thing, and do the Statement = this that. It's a lot of code to do very little, and it kind of sucks. Now granted most people don't really do a ton of direct to db stuff anymore, with all the ORM solutions like Hibernate for Java and ActiveRecord for Ruby/Rails, but when you need something surgical like this, it just can't be beat. Man oh man it's awesome.
My name is Kirk, and I am a freakin nerd.
0 comments:
Post a Comment