Skip to content

Commit

Permalink
fix: build in arcPreparedStatement with subquery
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Oct 24, 2024
1 parent dff421b commit 50fd3b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,5 @@ public ArcPreparedStatementBuilder append(ColumnEnum column) {
public String quoteText(ColumnEnum column) {
return quoteText(column.getColumnName());
}

@Override
public ArcPreparedStatementBuilder build(Object... queryElements) {
for (Object queryElement : queryElements) {
getQuery().append(queryElement);
}
return this;
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package fr.insee.arc.core.dataobjects;

import static org.junit.Assert.assertEquals;

import org.junit.Assert;
import org.junit.Test;

import fr.insee.arc.utils.dao.SQL;
import fr.insee.arc.utils.exception.ArcException;

public class ArcPreparedStatementBuilderTest {

@Test
Expand All @@ -21,4 +26,28 @@ public void sqlListeColumnsByView() {
query.sqlListeOfColumnsFromModel(ViewEnum.TABLE_TEST_IN_TEMPORARY).toString());
}

@Test
public void arcPreparedStatementAppenderTest() throws ArcException {

ArcPreparedStatementBuilder sousRequeteAvant = new ArcPreparedStatementBuilder();
sousRequeteAvant.build("d=", sousRequeteAvant.quoteText("valeur_de_d"));

ArcPreparedStatementBuilder sousRequeteApres = new ArcPreparedStatementBuilder();
sousRequeteApres.build("b=", sousRequeteApres.quoteText("valeur_de_b"));

ArcPreparedStatementBuilder query = new ArcPreparedStatementBuilder();
query.build(SQL.SELECT, "*", SQL.FROM, ViewEnum.CONTROLE_REGLE, SQL.WHERE);
query.build(sousRequeteAvant);
query.build(SQL.AND, "a=", query.quoteText("valeur_de_a"), SQL.AND);
query.build(sousRequeteApres);
query.build(SQL.ORDER_BY, ColumnEnum.ARCHIVE_DATE);

// check that query have the bind variables in the right order
assertEquals(" SELECT * FROM controle_regle WHERE d= ? AND a= ? AND b= ? ORDER BY archive_date",
query.getQuery().toString());
assertEquals("valeur_de_d", query.getParameters().get(0).getValue());
assertEquals("valeur_de_a", query.getParameters().get(1).getValue());
assertEquals("valeur_de_b", query.getParameters().get(2).getValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public GenericPreparedStatementBuilder(StringBuilder query) {

public GenericPreparedStatementBuilder build(Object... queryElements) {
for (Object queryElement : queryElements) {
if (queryElement instanceof GenericPreparedStatementBuilder q)
{
this.append(q);
continue;
}
query.append(queryElement);
}
return this;
Expand Down

0 comments on commit 50fd3b6

Please sign in to comment.