Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using alias on condition combined with AND or OR fails #834

Closed
TinchyIzzy opened this issue Mar 9, 2020 · 9 comments
Closed

Using alias on condition combined with AND or OR fails #834

TinchyIzzy opened this issue Mar 9, 2020 · 9 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@TinchyIzzy
Copy link

TinchyIzzy commented Mar 9, 2020

I want to find (i < 10 or i > 20) list

I have tried

less(param, 0).or().greater(param, 0) then setParameter(param, 10).setParameter(param, 20)
and
less(param, 0).parameterAlias("one").or().greater(param, 0).parameterAlias("two") then setParameter("one", 10).setParameter("two", 20)

none of them work

@greenrobot-team
Copy link
Member

Assuming this is pseudo-code this should be correct. Can you provide a small code example that reproduces this?

@greenrobot-team greenrobot-team added the more info required Further information is requested label Mar 9, 2020
@TinchyIzzy
Copy link
Author

TinchyIzzy commented Mar 11, 2020

Assuming this is pseudo-code this should be correct. Can you provide a small code example that reproduces this?

public class ObjectBoxActivity extends AppCompatActivity {
    private static final String TAG = "ObjectBoxActivity";

    BoxStore store;
    Box<TestEntity> box;
    Query<TestEntity> query;
    Query<TestEntity> query2;
    int size;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: ");

        store = MyObjectBox.builder()
                .androidContext(this)
                .build();
        boolean start = new AndroidObjectBrowser(store).start(this);
        box = store.boxFor(TestEntity.class);
        query = box.query().less(TestEntity_.value, 0).or().greater(TestEntity_.value, 0).build();
//        query2 = box.query().less(TestEntity_.value, 0).parameterAlias("one").or().greater(TestEntity_.value, 0).parameterAlias("two") .build();

        box.removeAll();
        box.put(new TestEntity(9));
        box.put(new TestEntity(7));
        box.put(new TestEntity(6));
        box.put(new TestEntity(16));
        box.put(new TestEntity(13));
        box.put(new TestEntity(23));
        box.put(new TestEntity(26));

        size = box.query().build().find().size();
        Log.e(TAG, "all size: " + size);
        size = query.setParameter(TestEntity_.value, 10).setParameter(TestEntity_.value, 20).find().size();
        Log.e(TAG, "size: " + size);
//        size = query2.setParameter("one", 10).setParameter("two", 20).find().size();
//        Log.e(TAG, "size: " + size);

    }
}

Log

size: 7
size: 7

if open query2

2020-03-11 11:48:31.661 4137-4137/com.xier.irexample E/ObjectBoxActivity: onCreate: 
2020-03-11 11:48:31.676 4137-4137/com.xier.irexample D/AndroidRuntime: Shutting down VM
2020-03-11 11:48:31.676 4137-4137/com.xier.irexample E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xier.irexample, PID: 4137
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xier.irexample/com.xier.irexample.ObjectBoxActivity}: java.lang.IllegalArgumentException: Argument condition "condition->withProperty()" not met (L392)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2671)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
     Caused by: java.lang.IllegalArgumentException: Argument condition "condition->withProperty()" not met (L392)
        at io.objectbox.query.QueryBuilder.nativeSetParameterAlias(Native Method)
        at io.objectbox.query.QueryBuilder.parameterAlias(QueryBuilder.java:313)
        at com.xier.irexample.ObjectBoxActivity.onCreate(ObjectBoxActivity.java:34)
        at android.app.Activity.performCreate(Activity.java:6709)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6141) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802) 
@Entity
public class TestEntity {
    @Id(assignable = false)
    public long id;
    private int value;

    public TestEntity() {
    }

    public TestEntity(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

@greenrobot-team
Copy link
Member

greenrobot-team commented Mar 16, 2020

Thanks, you have found a bug! Using parameterAlias(alias) on a condition combined with .and() or .or() causes an exception:

 Caused by: java.lang.IllegalArgumentException: Argument condition "condition->withProperty()" not met (L392)
    at io.objectbox.query.QueryBuilder.nativeSetParameterAlias(Native Method)
    at io.objectbox.query.QueryBuilder.parameterAlias(QueryBuilder.java:313)

Workaround: use the new Query API in 3.0.0-alpha1. It does not appear to have this issue as it combines queries differently internally (it creates the conditions first, then combines them).
https://docs.objectbox.io/queries#new-query-api

Example:

Query<TestEntity> query = box.query(
                TestEntity_.value.less(0).alias("less")
                        .or(TestEntity_.value.greater(0).alias("greater"))
        ).build();

@greenrobot-team greenrobot-team added bug Something isn't working and removed more info required Further information is requested labels Mar 16, 2020
@greenrobot-team greenrobot-team self-assigned this Mar 16, 2020
@greenrobot-team greenrobot-team changed the title How to create a reusing query with not between Using alias on condition combined with AND or OR fails Mar 16, 2020
@greenrobot-team greenrobot-team added this to the 3.0 milestone Mar 16, 2020
@TinchyIzzy
Copy link
Author

When will 3.0.0 be released

@greenrobot-team
Copy link
Member

When it's done, sorry.

Another workaround I forgot to mention: re-build the Query each time.

@TinchyIzzy
Copy link
Author

TinchyIzzy commented Mar 17, 2020

Thanks
I have re-build each time

@greenrobot-team
Copy link
Member

greenrobot-team commented Mar 24, 2020

A fix is available with 3.0.0-alpha2.
https://docs.objectbox.io/#v-3-0-0-alpha2-2020-03-24

@greenrobot-team greenrobot-team modified the milestones: 3.0, 2.6.0 Apr 28, 2020
@greenrobot-team
Copy link
Member

This fix was backported and is now also available with 2.6.0-RC.
https://docs.objectbox.io/#v-2-6-0-rc-2020-04-28

@greenrobot-team
Copy link
Member

Fixed with 2.6.0.
https://docs.objectbox.io/#v-2-6-0-2020-06-09

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants