Created
August 9, 2013 07:32
-
-
Save aya-eiya/6191790 to your computer and use it in GitHub Desktop.
ちょっとしたことがGroovy「PostgreSQLのXML型のカラムのデータを修正する」 ref: http://qiita.com/aya_eiya/items/7e4d126757e684f0ff0a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='postgresql', module='postgresql', version='9.1-901.jdbc4') | |
@Grab(group='org.codehaus.gpars', module='gpars', version='1.0.0') | |
import org.postgresql.ds.PGSimpleDataSource; | |
import groovy.sql.Sql; | |
/* meta */ | |
String.metaClass.queryTo = { con -> | |
def qry = delegate | |
new Object(){ | |
def leftShift = {c -> con.eachRow(qry,c)} | |
def forList = new Object(){ | |
def leftShift = {c -> try{c(con.rows(qry).collect{it[0]})}catch(Exception ex){println ex} } | |
} | |
} | |
} | |
String.metaClass.updateBy = { con,params -> | |
def qry = delegate | |
con.executeInsert(qry,params) | |
} | |
PGSimpleDataSource.metaClass.set = { srv,port,db,usr,pass -> | |
delegate.setServerName(srv) ; delegate.setPortNumber(port) | |
delegate.setDatabaseName(db) | |
delegate.setUser(usr) ; delegate.setPassword(pass) | |
delegate.getConnection() | |
} | |
/* datasource */ | |
def cons = { | |
[pg01 : new Sql(new PGSimpleDataSource().set("localhost" , 5432 , "postgres" , "myUser" , "******"))] | |
}() | |
/* query */ | |
""" | |
select xml_data | |
from xml_data_store | |
where | |
document_number = '20130801' | |
""".queryTo(cons.pg01) << { | |
def xml = it.description.getString().replaceAll('不適切な文言','(不適切な表現につき検閲されました)') | |
try{ | |
def xmlobj = new org.postgresql.jdbc4.Jdbc4SQLXML(cons.publication.connection,xml) | |
""" | |
update xml_data_store | |
set xml_data_fixed = ? | |
where | |
document_number = '20130801' | |
""".updateBy(cons.publication,[xmlobj]) | |
}catch(ex){ | |
println ex | |
} | |
} | |
/* close */ | |
cons.each {try{it.value.close()}catch(e){}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment