use of com.developmentontheedge.sql.model.SqlParser 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.SqlParser in project be5 by DevelopmentOnTheEdge.
the class Project method enterSQL.
public void enterSQL() {
if (++beSQL == 1) {
reconfigureFreemarker();
SqlParser parser = new SqlParser();
for (String sqlMacro : sqlMacros) {
parser.parse(sqlMacro);
List<String> messages = parser.getMessages();
if (!messages.isEmpty()) {
throw new IllegalArgumentException(("SQL Macro cannot be parsed:\nMacro:" + sqlMacro + "\nErrors: " + String.join("\n", messages)).replace("\r", "").replace("\n", System.lineSeparator()));
}
}
this.sqlParser = parser;
}
}
use of com.developmentontheedge.sql.model.SqlParser in project be5 by DevelopmentOnTheEdge.
the class TestMain method testModule5DB.
public static StatContext testModule5DB(String name, List<String> okQueries) throws ProjectLoadException, ReadException {
LoadContext ctx = new LoadContext();
Project project = ModuleUtils.loadModule(name, ctx);
ModuleUtils.addModuleScripts(project);
ctx.check();
SqlParser parser = new SqlParser();
Formatter formatter = new Formatter();
DefaultParserContext parserContext = new DefaultParserContext();
parser.setContext(parserContext);
Rdbms[] dbms = { Rdbms.MYSQL, Rdbms.DB2, Rdbms.ORACLE, Rdbms.SQLSERVER, Rdbms.POSTGRESQL };
StatContext stat = new StatContext(name + "/5DB", StreamEx.of(dbms).map(Rdbms::name).prepend("Count", "PF").append("Success").toArray(String[]::new));
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;
project.setDatabaseSystem(Rdbms.BESQL);
String queryText;
try {
queryText = query.getQueryCompiled().validate();
} catch (ProjectElementException e) {
continue;
}
stat.inc("Count");
parser.parse(queryText);
if (!parser.getMessages().isEmpty()) {
stat.inc("PF");
continue;
}
try {
formatter.format(parser.getStartNode(), new Context(Dbms.POSTGRESQL), parserContext);
} catch (IllegalArgumentException e) {
System.out.println(entityName + "." + query.getName() + ": " + e.getMessage());
stat.inc("PF");
continue;
}
boolean success = true;
for (Rdbms db : dbms) {
project.setDatabaseSystem(db);
String expected;
try {
expected = sanitizeValue(query.getQueryCompiled().validate());
} catch (ProjectElementException e) {
continue;
}
String formatted;
try {
formatted = formatter.format(parser.getStartNode(), new Context(Dbms.valueOf(db.name())), parserContext);
} catch (IllegalArgumentException e) {
System.out.println(entityName + "." + query.getName() + ": " + e.getMessage());
stat.inc(db.name());
success = false;
continue;
}
String actual = sanitizeValue(formatted);
if (!expected.equals(actual)) {
if (db == Rdbms.ORACLE) {
System.out.println(entityName + "." + query.getName());
System.out.println("BESQL: " + queryText);
System.out.println("Actual: " + actual);
System.out.println("Expected: " + expected);
}
stat.inc(db.name());
success = false;
}
}
if (success) {
stat.inc("Success");
// okQueries.add( entityName+"."+query.getName() );
}
}
}
return stat;
}
use of com.developmentontheedge.sql.model.SqlParser in project be5 by DevelopmentOnTheEdge.
the class MacroTest method testMacro.
@Test
public void testMacro() {
SqlParser parser = new SqlParser();
String input = "MACRO A(arg1=default, arg2=NOW()) \'<!--\' || CAST((arg2) AS CHAR) || \'-->\' || \'<a href=\"...\">\' || arg1 || \'</a>\' END";
parser.parse(input);
if (!parser.getMessages().isEmpty()) {
throw new IllegalArgumentException(String.join("\n", parser.getMessages()));
}
ParserContext context = parser.getContext();
AstStart start = SqlQuery.parse("SELECT A(a, b) FROM table t", context);
new MacroExpander().expandMacros(start);
assertEquals("SELECT \'<!--\' || TO_CHAR(( b))|| \'-->\' || \'<a href=\"...\">\' || a || \'</a>\' FROM table t", new Formatter().format(start, new Context(Dbms.ORACLE), context));
start = SqlQuery.parse("SELECT A(a) FROM table t", context);
new MacroExpander().expandMacros(start);
assertEquals("SELECT \'<!--\' || TO_CHAR((SYSDATE))|| \'-->\' || \'<a href=\"...\">\' || a || \'</a>\' FROM table t", new Formatter().format(start, new Context(Dbms.ORACLE), context));
SqlParser newParser = new SqlParser();
input = "MACRO B(arg1, arg2, arg3) arg1 || arg2 || A(arg3) END";
newParser.setContext(context);
newParser.parse(input);
if (!newParser.getMessages().isEmpty()) {
throw new IllegalArgumentException(String.join("\n", newParser.getMessages()));
}
start = SqlQuery.parse("SELECT B(a, b, c) FROM table t", context);
new MacroExpander().expandMacros(start);
assertEquals("SELECT a || b || '<!--' || TO_CHAR((SYSDATE))|| \'-->\' || '<a href=\"...\">\' || c || \'</a>\' FROM table t", new Formatter().format(start, new Context(Dbms.ORACLE), context));
}
Aggregations