question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proper way to create a duplicate Expression object

See original GitHub issue

I recently faced a situation where I needed to set the same criteria expression in all the SELECTs in a UNION query. Though the criteria is same, I would want distinct objects that represent the same criteria i.e., changing the criteria in one of the SELECT queries should not affect the criteria in the other SELECT queries. Let me demonstrate what I was trying to achieve:

		Select selectStatement = (Select) CCJSqlParserUtil.parse(
                "SELECT id FROM table1 UNION SELECT id FROM table2"
        );
        Expression criteriaExpr = CCJSqlParserUtil.parseCondExpression("NAME LIKE '%M%'");

        selectStatement.getSelectBody().accept(new SelectVisitorAdapter() {
            @Override
            public void visit(PlainSelect plainSelect) {
                plainSelect.setWhere(criteriaExpr);
            }

            @Override
            public void visit(SetOperationList setOpList) {
                setOpList.getSelects().forEach(
                        selectBody -> selectBody.accept(this)
                );
            }
        });

The program properly sets the criteria for both the queries. But as it uses the same criteriaExpr object for both the queries in the UNION, changing the criteria in one of the queries also changes the criteria in the other query. This is an issue in my case.

One way to overcome this is by moving the creation of the criteriaExpr into the visit(PlainSelect plainSelect) method. But it has the limitation that there is no direct way to communicate to the caller, the case in which the criteria could not be parsed (i.e., when an JSQLParserException occurs) as the signature of the visit method cannot be changed.

Coming to the question, is there a way to create a clone of the creteriaExpr without having to parse the expression again so that a unique object can be assigned to all the queries in the UNION?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
wumpzcommented, Dec 1, 2019

With my proposal #901 this would be more simple.

0reactions
sivaraamcommented, Feb 4, 2020

Sure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Duplicate Object (CRTDUPOBJ) - IBM
The Create Duplicate Object (CRTDUPOBJ) command copies a single object or a group of objects. It does not create an exact duplicate of...
Read more >
TIME-SAVING AE Copy + Paste Expression! | Adobe After ...
This After Effects Quick Tip teaches you how to COPY + PASTE expressions, which is an ESSENTIAL tip that every motion graphic designer...
Read more >
3 Ways to Copy objects in JavaScript, Shallow vs. Deep Copy
First, create a new object named person . Second, clone the person object using the Object.assign() method. Third, change the first name and...
Read more >
Object references and copying - The Modern JavaScript Tutorial
To make a “real copy” (a clone) we can use Object.assign for the so-called “shallow copy” (nested objects are copied by reference) or...
Read more >
Creating a copy of an object in C# [duplicate] - Stack Overflow
The easiest way to do this is writing a copy constructor in the MyClass class. Something like this: namespace Example { class MyClass ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found