JSqlParser
JSqlParser copied to clipboard
Statement deep cloning
Hello. I can't seem to find a way to deep-clone an already parsed statement. Does such functionality exist?
At the moment there is nothing like it. Maybe a CopyVisitor? But then all parts have somehow to be clonable. Quite frankly it seems to be a complex tasks. A workaround now would be to parse the text output of the parsed statement.
Today, I do re-parse the statement to get its clone.
I can think of two solutions for implementing Statement's clone() method:
- Recursive deep state copying.
- Use Jackson to marshall/unmarshall the Statement. In both cases, back references (if exist) should be handled. No other obstacles, i can see at the moment.
@wheredevel
This is a really silly idea but, could you theoretically do a clone by doing this
Statement s1 = JsqlParseUtil.parse("SELECT * FROM BLAH");
Statement clone = JsqlParseUtil.parse(s1.toString());
It should give you a whole new cloned object right?
Unless you want to modify some state on the statement directly, then I guess the new clone could potentially not be correct.
@AnEmortalKid
... clone by doing this:
Statement s1 = JsqlParseUtil.parse("SELECT * FROM BLAH"); Statement clone = JsqlParseUtil.parse(s1.toString());
@wheredevel
... Today, I do re-parse the statement to get its clone.
deep clone it as a prototype, for sql instrument. JsqlParseUtil.parse() is not a good idea, but has not a better idea
Is there progress on this issue, because I am currently working on a project where this copy functionality could optimize query string operations.
Statement s1 = JsqlParseUtil.parse("SELECT * FROM BLAH"); Statement clone = JsqlParseUtil.parse(s1.toString());
Re-parsing is actually way too expensive, so this is unfortunately not an option.
No its not. Maybe something here helps: https://programmerall.com/article/9960949396/ ?
Make the Entities Serializable and use SerializationUtils.clone() (which we depend on already).
I am working on this step by step as I touch any Entity Class.