use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class TestJqlChecker method testDelet1.
@Test
public void testDelet1() {
String sql = "DELETE FROM channel WHERE ownerUid=${field1} and ownerUid=${bean.field2} and ownerUid=${bean.field3} and ownerUid=${field5}";
JQL jql = new JQL();
jql.value = sql;
JQLChecker checker = JQLChecker.getInstance();
// verify sql
checker.verify(new JQLContext() {
@Override
public String getContextDescription() {
return "test context";
}
}, jql);
// check bind parameters
{
List<JQLPlaceHolder> aspected = new ArrayList<>();
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field1"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field2"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field3"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field5"));
List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
checkCollectionExactly(actual, aspected);
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class SqlSelectBuilder method generateSQL.
static SplittedSql generateSQL(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder, final boolean replaceProjectedColumns) {
JQLChecker jqlChecker = JQLChecker.getInstance();
final SplittedSql splittedSql = new SplittedSql();
String sql = convertJQL2SQL(method, false);
// parameters extracted from JQL converted in SQL
splittedSql.sqlBasic = jqlChecker.replaceVariableStatements(method, sql, new JQLReplaceVariableStatementListenerImpl() {
@Override
public String onWhere(String statement) {
splittedSql.sqlWhereStatement = statement;
return "";
}
@Override
public String onOrderBy(String statement) {
splittedSql.sqlOrderByStatement = statement;
return "";
}
@Override
public String onOffset(String statement) {
splittedSql.sqlOffsetStatement = statement;
return "";
}
@Override
public String onLimit(String statement) {
splittedSql.sqlLimitStatement = statement;
return "";
}
@Override
public String onHaving(String statement) {
splittedSql.sqlHavingStatement = statement;
return "";
}
@Override
public String onGroup(String statement) {
splittedSql.sqlGroupStatement = statement;
return "";
}
@Override
public String onProjectedColumns(String statement) {
if (replaceProjectedColumns)
return "%s";
else
return null;
}
});
return splittedSql;
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class TestJqlChecker method testSelect01.
/**
* extract projections from a select
*/
@Test
public void testSelect01() {
// String sql="SELECT count(*) FROM channel WHERE
// updateTime=${bean.updateTime}";
String sql = "SELECT count(*) as alias1, field2, field3 as alias3, table1.field3 as alias3, table2.field4 as alias4 FROM channel WHERE updateTime=${ bean.field1 } and field=${ field2 } and #{" + JQLDynamicStatementType.DYNAMIC_WHERE + "}";
String logSql = "SELECT count(*) as alias1, field2, field3 as alias3, table1.field3 as alias3, table2.field4 as alias4 FROM channel WHERE updateTime=? and field=? and \"+DYNAMIC_WHERE+\"";
// String usedSql = "SELECT count(*) FROM channel WHERE
// updateTime=${bean.updateTime}";
JQL jql = new JQL();
jql.value = sql;
JQLChecker checker = JQLChecker.getInstance();
// verify sql
checker.verify(dummyContext, jql);
Finder<SQLProperty> entityMock = new Finder<SQLProperty>() {
@Override
public String getSimpleName() {
// TODO Auto-generated method stub
return null;
}
@Override
public List<SQLProperty> getCollection() {
// TODO Auto-generated method stub
return null;
}
@Override
public SQLProperty findPropertyByName(String name) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getTableName() {
// TODO Auto-generated method stub
return null;
}
};
// check projections
Set<JQLProjection> projections = checker.extractProjections(dummyContext, jql.value, entityMock);
{
LinkedHashSet<JQLProjection> aspected = new LinkedHashSet<>();
aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COMPLEX).expression("count(*)").alias("alias1").build());
aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).column("field2").build());
aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).column("field3").alias("alias3").build());
aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).table("table1").column("field3").alias("alias3").build());
aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).table("table2").column("field4").alias("alias4").build());
checkCollectionExactly(projections, aspected);
}
// check bind parameters
{
List<JQLPlaceHolder> aspected = new ArrayList<>();
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field2"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.DYNAMIC_SQL, JQLDynamicStatementType.DYNAMIC_WHERE.toString()));
List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
checkCollectionExactly(actual, aspected);
}
// prepare for log
String sqlLogResult = checker.replace(dummyContext, jql, new JQLReplacerListenerImpl(null) {
@Override
public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
return String.format("\"+%s+\"", dynamicStatement);
}
@Override
public String onBindParameter(String bindParameterName) {
return "?";
}
@Override
public String onColumnFullyQualifiedName(String tableName, String columnName) {
return null;
}
});
assertEquals("sql for log generation failed", logSql, sqlLogResult);
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class TestJqlChecker method testInsert01.
@Test
public void testInsert01() {
String sql = "INSERT INTO channel (uid, owner_uid, update_time, name, field) VALUES (${bean.field1}, ${bean.field2}, ${bean.field3}, ${bean.field4}, ${field5})";
JQL jql = new JQL();
jql.value = sql;
JQLChecker checker = JQLChecker.getInstance();
// verify sql
checker.verify(new JQLContext() {
@Override
public String getContextDescription() {
return "test context";
}
}, jql);
// check bind parameters
{
List<JQLPlaceHolder> aspected = new ArrayList<>();
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field2"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field3"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field4"));
aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field5"));
List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
checkCollectionExactly(actual, aspected);
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker in project kripton by xcesco.
the class TestJqlChecker method testBindParametersFromVariableStatements.
@Test
public void testBindParametersFromVariableStatements() {
final One<String> where = new One<>();
final One<String> orderBy = new One<>();
final One<String> limit = new One<>();
final One<String> offset = new One<>();
final One<String> group = new One<>();
final One<String> having = new One<>();
String sql = "SELECT id, parentId, birthCity, birthDay, value, name, surname FROM Person WHERE name like ${nameTemp} || '%' GROUP BY id HAVING id=2 ORDER BY id, #{" + JQLDynamicStatementType.DYNAMIC_ORDER_BY + "} LIMIT #{" + JQLDynamicStatementType.DYNAMIC_PAGE_SIZE + "} OFFSET #{" + JQLDynamicStatementType.DYNAMIC_PAGE_OFFSET + "}";
JQL jql = new JQL();
jql.value = sql;
JQLChecker checker = JQLChecker.getInstance();
// verify sql
checker.verify(dummyContext, jql);
String finalSql = checker.replaceVariableStatements(dummyContext, jql.value, new JQLReplaceVariableStatementListenerImpl() {
@Override
public String onWhere(String whereStatement) {
where.value0 = whereStatement;
return "";
}
@Override
public String onOrderBy(String orderByStatement) {
orderBy.value0 = orderByStatement;
return "";
}
@Override
public String onLimit(String statement) {
limit.value0 = statement;
return "";
}
@Override
public String onOffset(String statement) {
offset.value0 = statement;
return "";
}
@Override
public String onGroup(String statement) {
group.value0 = statement;
return "";
}
@Override
public String onHaving(String statement) {
having.value0 = statement;
return "";
}
@Override
public String onProjectedColumns(String statement) {
return null;
}
});
log(where.value0);
log(orderBy.value0);
log(group.value0);
log(having.value0);
log(offset.value0);
log(limit.value0);
log(finalSql);
// assertEquals(where, " id = ${dummy} and a=${dummy2}");
{
List<JQLPlaceHolder> list = checker.extractFromVariableStatement(new JQLContext() {
@Override
public String getContextDescription() {
return "Test context";
}
}, where.value0);
for (JQLPlaceHolder item : list) {
log(item.value);
}
String replacedSql = checker.replaceFromVariableStatement(new JQLContext() {
@Override
public String getContextDescription() {
return "Test context";
}
}, where.value0, new JQLReplacerListenerImpl(null) {
@Override
public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
return "*";
}
@Override
public String onBindParameter(String bindParameterName) {
return "?";
}
@Override
public String onColumnFullyQualifiedName(String tableName, String columnName) {
// TODO Auto-generated method stub
return null;
}
});
log(replacedSql);
}
}
Aggregations