use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class TestMain method testProject.
private static StatContext testProject(Project project, FailData failedQueries) {
String name = project.getName();
Rdbms dbms = project.getDatabaseSystem();
StatContext stat = new StatContext(name + "/" + dbms, "Count", "FMF", "PE", "FE", "PF", "RFE", "RFM", "Success");
SqlParser parser = new SqlParser();
parser.setContext(new DefaultParserContext());
Formatter formatter = new Formatter();
Context context = new Context(Arrays.stream(Dbms.values()).filter(d -> d.getType() == dbms.getType()).findFirst().get());
for (String entityName : project.getEntityNames()) {
Entity entity = project.getEntity(entityName);
for (Query query : entity.getQueries()) {
if (!allowedTypes.contains(query.getType()))
continue;
if (query.getName().equals("Table definition"))
continue;
if (query.getQuery().trim().isEmpty())
continue;
stat.inc("Count");
String queryText;
try {
queryText = query.getQueryCompiled().validate();
} catch (ProjectElementException e) {
stat.inc("FMF");
continue;
}
parser.parse(queryText);
if (!parser.getMessages().isEmpty()) {
if (knownNonExpanded.stream().anyMatch(queryText::contains)) {
stat.inc("FMF");
continue;
}
failedQueries.add(query, queryText);
// System.out.println("----------------------\nQuery: "+entity.getName()+"."+query.getName()+"\n"+queryText+"\n\n"+String.join("\n", parser.getMessages()));
stat.inc("PE");
continue;
}
AstStart astStart = parser.getStartNode();
String format;
try {
format = formatter.format(astStart, context, parser.getContext());
Objects.requireNonNull(format);
} catch (Exception e) {
failedQueries.add(query, queryText);
System.out.println("Err: " + entity.getName() + "." + query.getName() + ": " + e.getMessage());
stat.inc("FE");
continue;
}
parser.parse(format);
if (!parser.getMessages().isEmpty()) {
failedQueries.add(query, queryText);
// System.out.println("PF: "+queryText+"\n"+"Formatted: "+format+"\n"+parser.getMessages());
stat.inc("PF");
continue;
}
astStart = parser.getStartNode();
String format2;
try {
format2 = formatter.format(astStart, context, parser.getContext());
Objects.requireNonNull(format2);
} catch (Exception e) {
stat.inc("RFF");
continue;
}
if (!format.equals(format2)) {
failedQueries.add(query, queryText);
// System.out.println("Orig : "+queryText);
// System.out.println("Form1: "+format);
// System.out.println("Form2: "+format2);
stat.inc("RFM");
continue;
}
stat.inc("Success");
}
}
return stat;
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class CategoryFilterTest method testCategoryFilter.
@Test
public void testCategoryFilter() {
AstStart start = SqlQuery.parse("SELECT t.a, q.b FROM myTable t, otherTable q JOIN oneMoreTable a ON (a.ID=q.ID) WHERE t.b > 2");
new CategoryFilter("myTable", "ID", 123).apply(start);
assertEquals("SELECT t.a, q.b FROM myTable t " + "INNER JOIN classifications ON classifications.categoryID = 123 " + "AND classifications.recordID = t.ID, otherTable q " + "INNER JOIN oneMoreTable a ON (a.ID = q.ID) " + "WHERE t.b > 2", new Formatter().format(start, new Context(Dbms.MYSQL), new DefaultParserContext()));
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class CloneTest method testAstClone.
@Test
public void testAstClone() {
AstStart start = SqlQuery.parse("SELECT * FROM table1, table2 WHERE col = 'value'");
AstStart clone = start.clone();
assertEquals(start.jjtGetNumChildren(), clone.jjtGetNumChildren());
assertEquals(start.dump(), clone.dump());
((AstSelect) clone.getQuery().child(0)).getFrom().removeChild(1);
// TODO: Current expected value is incorrect and should be fixed when formatter will be improved
assertEquals("SELECT * FROM table1 WHERE col = 'value'", new Formatter().format(clone, new Context(Dbms.ORACLE), new DefaultParserContext()));
assertEquals("SELECT * FROM table1, table2 WHERE col = 'value'", new Formatter().format(start, new Context(Dbms.ORACLE), new DefaultParserContext()));
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class ContextApplierApplyParametersTest method digitString.
@Test
public void digitString() {
AstStart start = SqlQuery.parse("SELECT * FROM table WHERE name = '<parameter:name />'");
ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("name", "123").build());
contextApplier.applyContext(start);
assertEquals("SELECT * FROM table WHERE name = '123'", start.getQuery().toString());
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class ContextApplierApplyParametersTest method useSafeStr.
@Test
public void useSafeStr() {
AstStart start = SqlQuery.parse("SELECT * FROM table WHERE name = <parameter:name safestr=\"yes\" />");
ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("name", "12's").build());
contextApplier.applyContext(start);
assertEquals("SELECT * FROM table WHERE name = '12''s'", start.getQuery().toString());
}
Aggregations