How can I prevent sql injection with groovy?












1















I have a sql like:



String sql = """SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental
FROM statistics_stin WHERE 1=1
${p.team_num == 0 ? "" : "AND team_num = ${p.team_num}"}
${p.zone == 0 ? "" : "AND team_id = ${p.zone}"}
${p.is_hbp == 2 ? "" : "AND is_hbp = ${p.is_hbp}"}
${p.is_dm == 2 ? "" : "AND is_dm = ${p.is_dm}"}
${p.is_chd == 2 ? "" : "AND is_chd = ${p.is_chd}"}
${p.is_cva == 2 ? "" : "AND is_cva = ${p.is_cva}"}
${p.is_copd == 2 ? "" : "AND is_copd = ${p.is_copd}"}
${p.is_cancer == 2 ? "" : "AND is_cancer = ${p.is_cancer}"}
${p.is_floating == 2 ? "" : "AND is_floating = ${p.is_floating}"}
${p.is_poor == 2 ? "" : "AND is_poor = ${p.is_poor}"}
${p.is_disability == 2 ? "" : "AND is_disability = ${p.is_disability}"}
${p.is_mental == 2 ? "" : "AND is_mental = ${p.is_mental}"}
${p.is_aged == 2 ? "" : (p.is_aged == 1 ? " AND age >= 65" : " AND age < 65")}
${p.is_prep_aged == 2 ? "" : (p.is_prep_aged == 1 ? "AND (age BETWEEN 60 AND 64)" : "AND (age < 60 OR age > 64)")}
${p.is_young == 2 ? "" : (p.is_young == 1 ? " AND age < 60" : " AND age >= 60")}
ORDER BY team_id ASC, id ASC
LIMIT ${start}, ${page_size}
""";


Then use :



def datasource = ctx.lookup("jdbc/mysql");
def executer = Sql.newInstance(datasource);
def rows = executer.rows(sql);


Here the p is a json object like:



p = {is_aged=2, is_cancer=2, is_chd=1, is_copd=2, is_cva=2, is_disability=2, is_dm=2, is_floating=2, is_hbp=1, is_mental=2, is_poor=2, pn=1, team_num=0, zone=0}


This way has sql injection. I know I can use params type like:



executer.rows('SELECT * from statistics_stin WHERE is_chd=:is_chd', [is_chd: 1]);


But this case has to many AND conditions, use or not will be decided by the json p.



How to do this please?










share|improve this question























  • stackoverflow.com/questions/43782702/…

    – metters
    Nov 23 '18 at 12:02











  • @metters Thank you, but I do not think it can prevent sql injection.

    – xunitc
    Nov 23 '18 at 12:17
















1















I have a sql like:



String sql = """SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental
FROM statistics_stin WHERE 1=1
${p.team_num == 0 ? "" : "AND team_num = ${p.team_num}"}
${p.zone == 0 ? "" : "AND team_id = ${p.zone}"}
${p.is_hbp == 2 ? "" : "AND is_hbp = ${p.is_hbp}"}
${p.is_dm == 2 ? "" : "AND is_dm = ${p.is_dm}"}
${p.is_chd == 2 ? "" : "AND is_chd = ${p.is_chd}"}
${p.is_cva == 2 ? "" : "AND is_cva = ${p.is_cva}"}
${p.is_copd == 2 ? "" : "AND is_copd = ${p.is_copd}"}
${p.is_cancer == 2 ? "" : "AND is_cancer = ${p.is_cancer}"}
${p.is_floating == 2 ? "" : "AND is_floating = ${p.is_floating}"}
${p.is_poor == 2 ? "" : "AND is_poor = ${p.is_poor}"}
${p.is_disability == 2 ? "" : "AND is_disability = ${p.is_disability}"}
${p.is_mental == 2 ? "" : "AND is_mental = ${p.is_mental}"}
${p.is_aged == 2 ? "" : (p.is_aged == 1 ? " AND age >= 65" : " AND age < 65")}
${p.is_prep_aged == 2 ? "" : (p.is_prep_aged == 1 ? "AND (age BETWEEN 60 AND 64)" : "AND (age < 60 OR age > 64)")}
${p.is_young == 2 ? "" : (p.is_young == 1 ? " AND age < 60" : " AND age >= 60")}
ORDER BY team_id ASC, id ASC
LIMIT ${start}, ${page_size}
""";


Then use :



def datasource = ctx.lookup("jdbc/mysql");
def executer = Sql.newInstance(datasource);
def rows = executer.rows(sql);


Here the p is a json object like:



p = {is_aged=2, is_cancer=2, is_chd=1, is_copd=2, is_cva=2, is_disability=2, is_dm=2, is_floating=2, is_hbp=1, is_mental=2, is_poor=2, pn=1, team_num=0, zone=0}


This way has sql injection. I know I can use params type like:



executer.rows('SELECT * from statistics_stin WHERE is_chd=:is_chd', [is_chd: 1]);


But this case has to many AND conditions, use or not will be decided by the json p.



How to do this please?










share|improve this question























  • stackoverflow.com/questions/43782702/…

    – metters
    Nov 23 '18 at 12:02











  • @metters Thank you, but I do not think it can prevent sql injection.

    – xunitc
    Nov 23 '18 at 12:17














1












1








1








I have a sql like:



String sql = """SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental
FROM statistics_stin WHERE 1=1
${p.team_num == 0 ? "" : "AND team_num = ${p.team_num}"}
${p.zone == 0 ? "" : "AND team_id = ${p.zone}"}
${p.is_hbp == 2 ? "" : "AND is_hbp = ${p.is_hbp}"}
${p.is_dm == 2 ? "" : "AND is_dm = ${p.is_dm}"}
${p.is_chd == 2 ? "" : "AND is_chd = ${p.is_chd}"}
${p.is_cva == 2 ? "" : "AND is_cva = ${p.is_cva}"}
${p.is_copd == 2 ? "" : "AND is_copd = ${p.is_copd}"}
${p.is_cancer == 2 ? "" : "AND is_cancer = ${p.is_cancer}"}
${p.is_floating == 2 ? "" : "AND is_floating = ${p.is_floating}"}
${p.is_poor == 2 ? "" : "AND is_poor = ${p.is_poor}"}
${p.is_disability == 2 ? "" : "AND is_disability = ${p.is_disability}"}
${p.is_mental == 2 ? "" : "AND is_mental = ${p.is_mental}"}
${p.is_aged == 2 ? "" : (p.is_aged == 1 ? " AND age >= 65" : " AND age < 65")}
${p.is_prep_aged == 2 ? "" : (p.is_prep_aged == 1 ? "AND (age BETWEEN 60 AND 64)" : "AND (age < 60 OR age > 64)")}
${p.is_young == 2 ? "" : (p.is_young == 1 ? " AND age < 60" : " AND age >= 60")}
ORDER BY team_id ASC, id ASC
LIMIT ${start}, ${page_size}
""";


Then use :



def datasource = ctx.lookup("jdbc/mysql");
def executer = Sql.newInstance(datasource);
def rows = executer.rows(sql);


Here the p is a json object like:



p = {is_aged=2, is_cancer=2, is_chd=1, is_copd=2, is_cva=2, is_disability=2, is_dm=2, is_floating=2, is_hbp=1, is_mental=2, is_poor=2, pn=1, team_num=0, zone=0}


This way has sql injection. I know I can use params type like:



executer.rows('SELECT * from statistics_stin WHERE is_chd=:is_chd', [is_chd: 1]);


But this case has to many AND conditions, use or not will be decided by the json p.



How to do this please?










share|improve this question














I have a sql like:



String sql = """SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental
FROM statistics_stin WHERE 1=1
${p.team_num == 0 ? "" : "AND team_num = ${p.team_num}"}
${p.zone == 0 ? "" : "AND team_id = ${p.zone}"}
${p.is_hbp == 2 ? "" : "AND is_hbp = ${p.is_hbp}"}
${p.is_dm == 2 ? "" : "AND is_dm = ${p.is_dm}"}
${p.is_chd == 2 ? "" : "AND is_chd = ${p.is_chd}"}
${p.is_cva == 2 ? "" : "AND is_cva = ${p.is_cva}"}
${p.is_copd == 2 ? "" : "AND is_copd = ${p.is_copd}"}
${p.is_cancer == 2 ? "" : "AND is_cancer = ${p.is_cancer}"}
${p.is_floating == 2 ? "" : "AND is_floating = ${p.is_floating}"}
${p.is_poor == 2 ? "" : "AND is_poor = ${p.is_poor}"}
${p.is_disability == 2 ? "" : "AND is_disability = ${p.is_disability}"}
${p.is_mental == 2 ? "" : "AND is_mental = ${p.is_mental}"}
${p.is_aged == 2 ? "" : (p.is_aged == 1 ? " AND age >= 65" : " AND age < 65")}
${p.is_prep_aged == 2 ? "" : (p.is_prep_aged == 1 ? "AND (age BETWEEN 60 AND 64)" : "AND (age < 60 OR age > 64)")}
${p.is_young == 2 ? "" : (p.is_young == 1 ? " AND age < 60" : " AND age >= 60")}
ORDER BY team_id ASC, id ASC
LIMIT ${start}, ${page_size}
""";


Then use :



def datasource = ctx.lookup("jdbc/mysql");
def executer = Sql.newInstance(datasource);
def rows = executer.rows(sql);


Here the p is a json object like:



p = {is_aged=2, is_cancer=2, is_chd=1, is_copd=2, is_cva=2, is_disability=2, is_dm=2, is_floating=2, is_hbp=1, is_mental=2, is_poor=2, pn=1, team_num=0, zone=0}


This way has sql injection. I know I can use params type like:



executer.rows('SELECT * from statistics_stin WHERE is_chd=:is_chd', [is_chd: 1]);


But this case has to many AND conditions, use or not will be decided by the json p.



How to do this please?







groovy sql-injection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 11:01









xunitcxunitc

437




437













  • stackoverflow.com/questions/43782702/…

    – metters
    Nov 23 '18 at 12:02











  • @metters Thank you, but I do not think it can prevent sql injection.

    – xunitc
    Nov 23 '18 at 12:17



















  • stackoverflow.com/questions/43782702/…

    – metters
    Nov 23 '18 at 12:02











  • @metters Thank you, but I do not think it can prevent sql injection.

    – xunitc
    Nov 23 '18 at 12:17

















stackoverflow.com/questions/43782702/…

– metters
Nov 23 '18 at 12:02





stackoverflow.com/questions/43782702/…

– metters
Nov 23 '18 at 12:02













@metters Thank you, but I do not think it can prevent sql injection.

– xunitc
Nov 23 '18 at 12:17





@metters Thank you, but I do not think it can prevent sql injection.

– xunitc
Nov 23 '18 at 12:17












2 Answers
2






active

oldest

votes


















2














You have a problem of dynamic SQL binding, i.e. the number of bind parameters is not constant, but depend on the input.



There is an elegant solution from Tom Kyte
which has even more elegant implementation in Groovy



The basic idea is simple bind all variables, the variables that have an input value and should be used are processed normally, e.g



 col1 = :col1


the variables that have no input (and shall be ignored) are binded with a dummy construct:



 (1=1 or :col2 is NULL)


i.e. they are effectively ignored by shortcut evaluation.



Here two examples for three columns



 def p = ["col1" : 1, "col2" : 2, "col3" : 3]


This input leads to a full query



SELECT col1, col2, col3
FROM tab WHERE
col1 = :col1 AND
col2 = :col2 AND
col3 = :col3
ORDER by col1,col2,col3


For limited input



 p = [ "col3" : 3] 


you get this query



SELECT col1, col2, col3
FROM tab WHERE
(1=1 or :col1 is NULL) AND
(1=1 or :col2 is NULL) AND
col3 = :col3
ORDER by col1,col2,col3


Here the Groovy creation of the SQL Statement



String sql = """SELECT col1, col2, col3
FROM tab WHERE
${(!p.col1) ? "(1=1 or :col1 is NULL)" : "col1 = :col1"} AND
${(!p.col2) ? "(1=1 or :col2 is NULL)" : "col2 = :col2"} AND
${(!p.col3) ? "(1=1 or :col3 is NULL)" : "col3 = :col3"}
ORDER by col1,col2,col3
"""


You can even get rid of the ugly 1=1 predicate;)






share|improve this answer


























  • Thank you. by this way, it can work, even sql is not clear.

    – xunitc
    Nov 25 '18 at 1:12



















1














Another option is to build your bindings as you build the query then execute the appropriate implementation of rows



def query = new StringBuilder( "SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental FROM statistics_stin WHERE 1=1" )
def binds =
if ( p.team_num == 0 ) {
query.append( ' AND team_num = ? ' )
binds << p.team_num
}
if ( p.zone == 0 ) {
query.append( ' AND team_id = ? ' )
binds << p.zone == 0
}
...
executer.rows(query.toString(), binds);





share|improve this answer
























  • Thank you. it can work, even the if code so many.

    – xunitc
    Nov 25 '18 at 1:16











  • Nice alternative with building the bind array.

    – Marmite Bomber
    Nov 25 '18 at 9:46












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445438%2fhow-can-i-prevent-sql-injection-with-groovy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You have a problem of dynamic SQL binding, i.e. the number of bind parameters is not constant, but depend on the input.



There is an elegant solution from Tom Kyte
which has even more elegant implementation in Groovy



The basic idea is simple bind all variables, the variables that have an input value and should be used are processed normally, e.g



 col1 = :col1


the variables that have no input (and shall be ignored) are binded with a dummy construct:



 (1=1 or :col2 is NULL)


i.e. they are effectively ignored by shortcut evaluation.



Here two examples for three columns



 def p = ["col1" : 1, "col2" : 2, "col3" : 3]


This input leads to a full query



SELECT col1, col2, col3
FROM tab WHERE
col1 = :col1 AND
col2 = :col2 AND
col3 = :col3
ORDER by col1,col2,col3


For limited input



 p = [ "col3" : 3] 


you get this query



SELECT col1, col2, col3
FROM tab WHERE
(1=1 or :col1 is NULL) AND
(1=1 or :col2 is NULL) AND
col3 = :col3
ORDER by col1,col2,col3


Here the Groovy creation of the SQL Statement



String sql = """SELECT col1, col2, col3
FROM tab WHERE
${(!p.col1) ? "(1=1 or :col1 is NULL)" : "col1 = :col1"} AND
${(!p.col2) ? "(1=1 or :col2 is NULL)" : "col2 = :col2"} AND
${(!p.col3) ? "(1=1 or :col3 is NULL)" : "col3 = :col3"}
ORDER by col1,col2,col3
"""


You can even get rid of the ugly 1=1 predicate;)






share|improve this answer


























  • Thank you. by this way, it can work, even sql is not clear.

    – xunitc
    Nov 25 '18 at 1:12
















2














You have a problem of dynamic SQL binding, i.e. the number of bind parameters is not constant, but depend on the input.



There is an elegant solution from Tom Kyte
which has even more elegant implementation in Groovy



The basic idea is simple bind all variables, the variables that have an input value and should be used are processed normally, e.g



 col1 = :col1


the variables that have no input (and shall be ignored) are binded with a dummy construct:



 (1=1 or :col2 is NULL)


i.e. they are effectively ignored by shortcut evaluation.



Here two examples for three columns



 def p = ["col1" : 1, "col2" : 2, "col3" : 3]


This input leads to a full query



SELECT col1, col2, col3
FROM tab WHERE
col1 = :col1 AND
col2 = :col2 AND
col3 = :col3
ORDER by col1,col2,col3


For limited input



 p = [ "col3" : 3] 


you get this query



SELECT col1, col2, col3
FROM tab WHERE
(1=1 or :col1 is NULL) AND
(1=1 or :col2 is NULL) AND
col3 = :col3
ORDER by col1,col2,col3


Here the Groovy creation of the SQL Statement



String sql = """SELECT col1, col2, col3
FROM tab WHERE
${(!p.col1) ? "(1=1 or :col1 is NULL)" : "col1 = :col1"} AND
${(!p.col2) ? "(1=1 or :col2 is NULL)" : "col2 = :col2"} AND
${(!p.col3) ? "(1=1 or :col3 is NULL)" : "col3 = :col3"}
ORDER by col1,col2,col3
"""


You can even get rid of the ugly 1=1 predicate;)






share|improve this answer


























  • Thank you. by this way, it can work, even sql is not clear.

    – xunitc
    Nov 25 '18 at 1:12














2












2








2







You have a problem of dynamic SQL binding, i.e. the number of bind parameters is not constant, but depend on the input.



There is an elegant solution from Tom Kyte
which has even more elegant implementation in Groovy



The basic idea is simple bind all variables, the variables that have an input value and should be used are processed normally, e.g



 col1 = :col1


the variables that have no input (and shall be ignored) are binded with a dummy construct:



 (1=1 or :col2 is NULL)


i.e. they are effectively ignored by shortcut evaluation.



Here two examples for three columns



 def p = ["col1" : 1, "col2" : 2, "col3" : 3]


This input leads to a full query



SELECT col1, col2, col3
FROM tab WHERE
col1 = :col1 AND
col2 = :col2 AND
col3 = :col3
ORDER by col1,col2,col3


For limited input



 p = [ "col3" : 3] 


you get this query



SELECT col1, col2, col3
FROM tab WHERE
(1=1 or :col1 is NULL) AND
(1=1 or :col2 is NULL) AND
col3 = :col3
ORDER by col1,col2,col3


Here the Groovy creation of the SQL Statement



String sql = """SELECT col1, col2, col3
FROM tab WHERE
${(!p.col1) ? "(1=1 or :col1 is NULL)" : "col1 = :col1"} AND
${(!p.col2) ? "(1=1 or :col2 is NULL)" : "col2 = :col2"} AND
${(!p.col3) ? "(1=1 or :col3 is NULL)" : "col3 = :col3"}
ORDER by col1,col2,col3
"""


You can even get rid of the ugly 1=1 predicate;)






share|improve this answer















You have a problem of dynamic SQL binding, i.e. the number of bind parameters is not constant, but depend on the input.



There is an elegant solution from Tom Kyte
which has even more elegant implementation in Groovy



The basic idea is simple bind all variables, the variables that have an input value and should be used are processed normally, e.g



 col1 = :col1


the variables that have no input (and shall be ignored) are binded with a dummy construct:



 (1=1 or :col2 is NULL)


i.e. they are effectively ignored by shortcut evaluation.



Here two examples for three columns



 def p = ["col1" : 1, "col2" : 2, "col3" : 3]


This input leads to a full query



SELECT col1, col2, col3
FROM tab WHERE
col1 = :col1 AND
col2 = :col2 AND
col3 = :col3
ORDER by col1,col2,col3


For limited input



 p = [ "col3" : 3] 


you get this query



SELECT col1, col2, col3
FROM tab WHERE
(1=1 or :col1 is NULL) AND
(1=1 or :col2 is NULL) AND
col3 = :col3
ORDER by col1,col2,col3


Here the Groovy creation of the SQL Statement



String sql = """SELECT col1, col2, col3
FROM tab WHERE
${(!p.col1) ? "(1=1 or :col1 is NULL)" : "col1 = :col1"} AND
${(!p.col2) ? "(1=1 or :col2 is NULL)" : "col2 = :col2"} AND
${(!p.col3) ? "(1=1 or :col3 is NULL)" : "col3 = :col3"}
ORDER by col1,col2,col3
"""


You can even get rid of the ugly 1=1 predicate;)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 9:46

























answered Nov 23 '18 at 13:06









Marmite BomberMarmite Bomber

8,27731135




8,27731135













  • Thank you. by this way, it can work, even sql is not clear.

    – xunitc
    Nov 25 '18 at 1:12



















  • Thank you. by this way, it can work, even sql is not clear.

    – xunitc
    Nov 25 '18 at 1:12

















Thank you. by this way, it can work, even sql is not clear.

– xunitc
Nov 25 '18 at 1:12





Thank you. by this way, it can work, even sql is not clear.

– xunitc
Nov 25 '18 at 1:12













1














Another option is to build your bindings as you build the query then execute the appropriate implementation of rows



def query = new StringBuilder( "SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental FROM statistics_stin WHERE 1=1" )
def binds =
if ( p.team_num == 0 ) {
query.append( ' AND team_num = ? ' )
binds << p.team_num
}
if ( p.zone == 0 ) {
query.append( ' AND team_id = ? ' )
binds << p.zone == 0
}
...
executer.rows(query.toString(), binds);





share|improve this answer
























  • Thank you. it can work, even the if code so many.

    – xunitc
    Nov 25 '18 at 1:16











  • Nice alternative with building the bind array.

    – Marmite Bomber
    Nov 25 '18 at 9:46
















1














Another option is to build your bindings as you build the query then execute the appropriate implementation of rows



def query = new StringBuilder( "SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental FROM statistics_stin WHERE 1=1" )
def binds =
if ( p.team_num == 0 ) {
query.append( ' AND team_num = ? ' )
binds << p.team_num
}
if ( p.zone == 0 ) {
query.append( ' AND team_id = ? ' )
binds << p.zone == 0
}
...
executer.rows(query.toString(), binds);





share|improve this answer
























  • Thank you. it can work, even the if code so many.

    – xunitc
    Nov 25 '18 at 1:16











  • Nice alternative with building the bind array.

    – Marmite Bomber
    Nov 25 '18 at 9:46














1












1








1







Another option is to build your bindings as you build the query then execute the appropriate implementation of rows



def query = new StringBuilder( "SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental FROM statistics_stin WHERE 1=1" )
def binds =
if ( p.team_num == 0 ) {
query.append( ' AND team_num = ? ' )
binds << p.team_num
}
if ( p.zone == 0 ) {
query.append( ' AND team_id = ? ' )
binds << p.zone == 0
}
...
executer.rows(query.toString(), binds);





share|improve this answer













Another option is to build your bindings as you build the query then execute the appropriate implementation of rows



def query = new StringBuilder( "SELECT id, name, sex, age, bron_year, address, phone, state, comment, is_hbp, is_dm, is_cva, is_copd, is_chd, is_cancer, is_floating, is_poor, is_disability, is_mental FROM statistics_stin WHERE 1=1" )
def binds =
if ( p.team_num == 0 ) {
query.append( ' AND team_num = ? ' )
binds << p.team_num
}
if ( p.zone == 0 ) {
query.append( ' AND team_id = ? ' )
binds << p.zone == 0
}
...
executer.rows(query.toString(), binds);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 16:32









Mike WMike W

2,8522712




2,8522712













  • Thank you. it can work, even the if code so many.

    – xunitc
    Nov 25 '18 at 1:16











  • Nice alternative with building the bind array.

    – Marmite Bomber
    Nov 25 '18 at 9:46



















  • Thank you. it can work, even the if code so many.

    – xunitc
    Nov 25 '18 at 1:16











  • Nice alternative with building the bind array.

    – Marmite Bomber
    Nov 25 '18 at 9:46

















Thank you. it can work, even the if code so many.

– xunitc
Nov 25 '18 at 1:16





Thank you. it can work, even the if code so many.

– xunitc
Nov 25 '18 at 1:16













Nice alternative with building the bind array.

– Marmite Bomber
Nov 25 '18 at 9:46





Nice alternative with building the bind array.

– Marmite Bomber
Nov 25 '18 at 9:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445438%2fhow-can-i-prevent-sql-injection-with-groovy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud