Search in sources :

Example 1 with AbstractMacro

use of org.sagacity.sqltoy.plugins.id.macro.AbstractMacro in project sagacity-sqltoy by chenrenfei.

the class MacroUtilsTest method testReplaceMacros1.

@Test
public void testReplaceMacros1() {
    Map<String, AbstractMacro> macros = new HashMap<String, AbstractMacro>();
    macros.put("@loop", new SqlLoop());
    String sql = "select * from table where 1=1 @loop(:startDates,' (bizDate between str_to_date(':startDates[i]','%Y-%m-%d') and str_to_date(':endDates[i]','%Y-%m-%d')','or')";
    String[] paramsNamed = { "startDates", "endDates" };
    String[][] paramsValue = { { "2020-10-01", "2020-11-01", "2020-12-01" }, { "2020-10-30", "2020-11-30", "2020-12-30" } };
    IgnoreKeyCaseMap<String, Object> keyValues = new IgnoreKeyCaseMap<String, Object>();
    for (int i = 0; i < paramsNamed.length; i++) {
        keyValues.put(paramsNamed[i], paramsValue[i]);
    }
    String result = MacroUtils.replaceMacros(sql, keyValues, false, macros);
    System.err.println(result);
}
Also used : AbstractMacro(org.sagacity.sqltoy.plugins.id.macro.AbstractMacro) HashMap(java.util.HashMap) SqlLoop(org.sagacity.sqltoy.plugins.id.macro.impl.SqlLoop) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) Test(org.junit.jupiter.api.Test)

Example 2 with AbstractMacro

use of org.sagacity.sqltoy.plugins.id.macro.AbstractMacro in project sagacity-sqltoy by chenrenfei.

the class MacroUtilsTest method testReplaceMacros.

@Test
public void testReplaceMacros() {
    Map<String, AbstractMacro> macros = new HashMap<String, AbstractMacro>();
    macros.put("@loop", new SqlLoop());
    String sql = "select * from table where 1=1 @loop(:startDates,'(bizDate between :startDates[i] and :endDates[i])',or)";
    String[] paramsNamed = { "startDates", "endDates" };
    String[][] paramsValue = { { "2020-10-01", "2020-11-01", "2020-12-01" }, { "2020-10-30", "2020-11-30", "2020-12-30" } };
    IgnoreKeyCaseMap<String, Object> keyValues = new IgnoreKeyCaseMap<String, Object>();
    for (int i = 0; i < paramsNamed.length; i++) {
        keyValues.put(paramsNamed[i], paramsValue[i]);
    }
    String result = MacroUtils.replaceMacros(sql, keyValues, false, macros);
    System.err.println(result);
}
Also used : AbstractMacro(org.sagacity.sqltoy.plugins.id.macro.AbstractMacro) HashMap(java.util.HashMap) SqlLoop(org.sagacity.sqltoy.plugins.id.macro.impl.SqlLoop) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)2 Test (org.junit.jupiter.api.Test)2 IgnoreKeyCaseMap (org.sagacity.sqltoy.model.IgnoreKeyCaseMap)2 AbstractMacro (org.sagacity.sqltoy.plugins.id.macro.AbstractMacro)2 SqlLoop (org.sagacity.sqltoy.plugins.id.macro.impl.SqlLoop)2