Tim, Dinko, thank you both for your replies. I am sorry that my answer is so
delayed - I had too many first prio tasks last week. Today I experimented a
bit and I can confirm (for my case at least) - I cannot make GroovySQL work
correctly without the quotation marks in my full text search. I have
modified the code snippet that Tim kindly provided:
@GrabConfig(systemClassLoader=true)
@Grab('mysql:mysql-connector-java:5.1.6')
import groovy.sql.Sql
// Get a connection
def sql = Sql.newInstance("jdbc:mysql://localhost:3306/test", "root",
"root", "com.mysql.jdbc.Driver")
try{
sql.execute 'DROP table cpu_test'
}catch(ex){
}
sql.execute '''CREATE TABLE `cpu_test` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) ,
FULLTEXT INDEX `name_index` (`name` ASC) )
ENGINE = MyISAM;
'''.stripMargin()
// Populate it
[ 'amd athlon', 'amd athlon dual core 5000b', 'amd athlon', 'amd athlon ii
dual-core m320', 'amd athlon ii x3 440', 'amd athlon dual core 4450b', 'amd
athlon xp'].each {
sql.execute "INSERT INTO cpu_test( name ) VALUES ( $it )"
}
def name = 'amd athlon ii x3 440'
def query = "SELECT * FROM cpu_test WHERE MATCH name AGAINST ('\"$name\"' IN
BOOLEAN MODE);".toString();
println 'This works:'
println sql.firstRow(query)
println 'This does not:'
println sql.firstRow("SELECT * FROM cpu_test WHERE MATCH name AGAINST ($name
IN BOOLEAN MODE)")
// Drop our test table
sql.execute 'DROP table cpu_test'
Output:
This works:
[id:5, name:amd athlon ii x3 440]
This does not:
[id:1, name:amd athlon]
As you might notice, I switched from using LIKE to using MATCH AGAINST based
on Mysql MyISAM engine and FULLTEXT index, but I experience the same issues
with LIKE, when I use the full database, and not just a small sample value
set. Any ideas of how to make this work without leaving security holes?
--
View this message in context:
http://groovy.329449.n5.nabble.com/Dynamic-expressions-in-Groovy-SQL-tp5710851p5710881.html