use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext in project kripton by xcesco.
the class TestJqlChecker method testOK.
/**
* <p>
* OK
* </p>
*
* @throws Throwable
*/
@Test
public void testOK() throws Throwable {
String sql = "SELECT id, action, number, countryCode, contactName, contactId FROM phone_number WHERE number = ${bean.number} and number like ${bean.number} || '%' and #{" + JQLDynamicStatementType.DYNAMIC_WHERE + "}";
log(sql);
JQL jql = new JQL();
jql.value = sql;
JQLChecker jsqChecker = JQLChecker.getInstance();
jsqChecker.analyze(dummyContext, jql, new JqlBaseListener() {
@Override
public void enterBind_parameter(Bind_parameterContext ctx) {
TestJqlChecker.this.log("xx parameter name %s", ctx.bind_parameter_name().getText());
}
@Override
public void enterBind_dynamic_sql(Bind_dynamic_sqlContext ctx) {
TestJqlChecker.this.log("xx dynamic %s", ctx.bind_parameter_name().getText());
}
@Override
public void enterColumn_name(Column_nameContext ctx) {
super.enterColumn_name(ctx);
log("column " + ctx.getText());
}
});
jsqChecker.extractPlaceHoldersAsList(new JQLContext() {
@Override
public String getContextDescription() {
return "test context";
}
}, jql.value);
log("replaced " + jsqChecker.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) {
// TODO Auto-generated method stub
return null;
}
}));
log("aa");
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext 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.JQLContext 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.JQLContext 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);
}
}
use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext in project kripton by xcesco.
the class BindTableGenerator method buldIndexes.
public static Pair<String, String> buldIndexes(final SQLiteEntity entity, boolean unique, int counter) {
Pair<String, String> result = new Pair<>();
result.value0 = "";
result.value1 = "";
ModelAnnotation annotationTable = entity.getAnnotation(BindTable.class);
if (annotationTable == null)
return result;
List<String> indexes = null;
String uniqueString;
if (unique) {
uniqueString = "UNIQUE ";
indexes = annotationTable.getAttributeAsArray(AnnotationAttributeType.UNIQUE_INDEXES);
} else {
uniqueString = "";
indexes = annotationTable.getAttributeAsArray(AnnotationAttributeType.INDEXES);
}
if (indexes == null || indexes.size() == 0)
return result;
// CREATE INDEX index_name ON tab_name (column1, column2)
// Matcher matcher = patternIndex.matcher(rawIndexes);
List<String> listCreateIndex = new ArrayList<>();
List<String> listDropIndex = new ArrayList<>();
for (String index : indexes) {
String createIndex = String.format(" CREATE %sINDEX idx_%s_%s on %s (%s)", uniqueString, entity.getTableName(), counter++, entity.getTableName(), index);
String dropIndex = String.format(" DROP INDEX IF EXISTS idx_%s_%s", entity.getTableName(), counter);
final One<Integer> fieldCounter = new One<Integer>(0);
createIndex = JQLChecker.getInstance().replace(new JQLContext() {
@Override
public String getContextDescription() {
return "While table definition generation for entity " + entity.getName();
}
}, createIndex, new JQLReplacerListenerImpl(null) {
@Override
public String onColumnName(String columnName) {
fieldCounter.value0++;
SQLProperty property = entity.findPropertyByName(columnName);
AssertKripton.assertTrue(property != null, "class '%s' in @%s(indexes) use unknown property '%s'", entity.getName(), BindTable.class.getSimpleName(), columnName);
return property.columnName;
}
@Override
public String onColumnFullyQualifiedName(String tableName, String columnName) {
AssertKripton.fail("Inconsistent state");
return null;
}
});
AssertKripton.assertTrue(fieldCounter.value0 > 0, "class '%s' have @%s(indexes) with no well formed indexes", entity.getName(), BindTable.class.getSimpleName());
listCreateIndex.add(createIndex);
listDropIndex.add(dropIndex);
}
result.value0 = StringUtils.join(listCreateIndex, ";");
result.value1 = StringUtils.join(listDropIndex, ";");
return result;
}
Aggregations