-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Need help for a SQL building example. #2186
-
Hello,
In the 'Extending a simple insert' in this page: https://github.com/JSQLParser/JSqlParser/wiki/Examples-of-SQL-building, the 'Insert' type doesn't have a getItemsList() method. It does have a getWithItemsList() method. Is there a change of the API since the publication of the page? I also can't find the ‘ExpressionList’ class in your API. The version I am using is jsqlparser-5.1-SNAPSHOT.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
Greetings.
void insertDemo() { Insert insert = new Insert() .withTable( new Table("test")) .withSelect( new Values() .addExpressions( new StringValue("A") , new StringValue("B") ) ); TestUtils.assertStatementCanBeDeparsedAs( insert , "INSERT INTO test VALUES ('A', 'B')" );
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks for helping me out. I've been going through your examples lately. I found out that some of the examples don't compile any more and some use deprecated methods. I supply at the end codes for some of them that actually compile. If you like, please use them to replace the outdated examples. It could be easier for a new guy like me.
Extract table names from SQL
statement = CCJSqlParserUtil.parse("SELECT * FROM MY_TABLE1 JOIN MY_TABLE1");
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
Set tables = tablesNamesFinder.getTables(statement);
Apply aliases to all expressions
select = (Select) CCJSqlParserUtil.parse("select a,b,c from test");
final AddAliasesVisitor instance = new AddAliasesVisitor();
select.getPlainSelect().accept(instance, null);
Extending a simple insert
insert = (Insert)CCJSqlParserUtil.parse("insert into mytable (col1) values (1)");
System.out.println(insert.toString());
insert.getColumns().add(new Column("col2"));
ExpressionVisitorAdapter expressionVisitorAdapter = new ExpressionVisitorAdapter<>() {
@Override
public <S> LongValue visit(ExpressionList<? extends Expression> expressionList, S context) {
LongValue r = visitHelper(expressionList);
return r;
}
private LongValue visitHelper(ExpressionList expressionList) {
LongValue l = new LongValue(5);
expressionList.addExpression(l);
return l;
}
};
((ExpressionList)insert.getValues().getExpressions()).accept(expressionVisitorAdapter, null);
System.out.println(insert.toString());
insert.getColumns().add(new Column("col3"));
((ExpressionList)insert.getValues().getExpressions()).addExpression(new LongValue(10));
System.out.println(insert.toString());
Replacing String values
String sql ="SELECT NAME, ADDRESS, COL1 FROM USER WHERE SSN IN ('11111111111111', '22222222222222');";
select = (Select) CCJSqlParserUtil.parse(sql);
StringBuilder buffer = new StringBuilder();
ExpressionDeParser expressionDeParser = new ExpressionDeParser() {
@Override
public <S> StringBuilder visit(StringValue stringValue, S context) {
this.getBuffer().append("XXXX");
return null;
}
};
SelectDeParser deparser = new SelectDeParser(expressionDeParser, buffer);
expressionDeParser.setSelectVisitor(deparser);
expressionDeParser.setBuffer(buffer);
select.getPlainSelect().accept(deparser, null);
System.out.println(buffer.toString());
More general replacing of String values in Statements
public class ReplaceColumnValues {
static class ReplaceColumnAndLongValues extends ExpressionDeParser {
@Override
public <S> StringBuilder visit(StringValue stringValue, S context) {
this.getBuffer().append("?");
return null;
}
@Override
public <S> StringBuilder visit(LongValue longValue, S context) {
this.getBuffer().append("*");
return null;
}
}
public static String cleanStatement(String sql) throws JSQLParserException {
StringBuilder buffer = new StringBuilder();
ExpressionDeParser expr = new ReplaceColumnAndLongValues();
SelectDeParser selectDeparser = new SelectDeParser(expr, buffer);
expr.setSelectVisitor(selectDeparser);
expr.setBuffer(buffer);
StatementDeParser stmtDeparser = new StatementDeParser(expr, selectDeparser, buffer);
Statement stmt = CCJSqlParserUtil.parse(sql);
stmt.accept(stmtDeparser);
return stmtDeparser.getBuffer().toString();
}
public static void main(String[] args) throws JSQLParserException {
System.out.println(cleanStatement("SELECT 'abc', 5 FROM mytable WHERE col='test'"));
System.out.println(cleanStatement("UPDATE table1 A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY'"));
System.out.println(cleanStatement("INSERT INTO example (num, name, address, tel) VALUES (1, 'name', 'test ', '1234-1234')"));
System.out.println(cleanStatement("DELETE FROM table1 where col=5 and col2=4"));
}
}
Retrieve all selected items of a SQL statement
stmt = (Select) CCJSqlParserUtil.parse("SELECT col1 AS a, col2 AS b, col3 AS c FROM table WHERE col_1 = 10 AND col_2 = 20 AND col_3 = 30");
System.out.println("before " + stmt.toString());
stmt.getPlainSelect().getWhere().accept(new ExpressionVisitorAdapter<>() {
@Override
public <S> Column visit(Column column, S context) {
column.setColumnName(column.getColumnName().replace("_", ""));
return null;
}
}, null);
System.out.println("after " + stmt.toString());
Beta Was this translation helpful? Give feedback.