JSqlParser icon indicating copy to clipboard operation
JSqlParser copied to clipboard

Statement deep cloning

Open wheredevel opened this issue 8 years ago • 8 comments

Hello. I can't seem to find a way to deep-clone an already parsed statement. Does such functionality exist?

wheredevel avatar Apr 04 '17 07:04 wheredevel

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.

wumpz avatar Apr 05 '17 06:04 wumpz

Today, I do re-parse the statement to get its clone.

I can think of two solutions for implementing Statement's clone() method:

  1. Recursive deep state copying.
  2. 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 avatar Apr 05 '17 07:04 wheredevel

@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 avatar May 05 '17 04:05 AnEmortalKid

@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.

wheredevel avatar May 05 '17 05:05 wheredevel

deep clone it as a prototype, for sql instrument. JsqlParseUtil.parse() is not a good idea, but has not a better idea

qhsword avatar Aug 05 '19 00:08 qhsword

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.

DiegoKrupitza avatar Jan 28 '22 18:01 DiegoKrupitza

No its not. Maybe something here helps: https://programmerall.com/article/9960949396/ ?

wumpz avatar Feb 06 '22 21:02 wumpz

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.

manticore-projects avatar Apr 30 '23 12:04 manticore-projects