Search in sources :

Example 6 with QueryBuilder

use of io.divide.shared.transitory.query.QueryBuilder in project divide by HiddenStage.

the class MyActivity method getObjects.

@OnClick(R.id.getObjects)
public void getObjects() {
    final Query q = new QueryBuilder().select().from(BackendObject.class).limit(10).build();
    BackendServices.remote().query(BackendObject.class, q).subscribe(new Action1<Collection<BackendObject>>() {

        @Override
        public void call(Collection<BackendObject> objects) {
            objectList.clear();
            objectList.addAll(objects);
            adapter.notifyDataSetChanged();
        // BackendServices.local().save((BackendObject[]) objects.toArray());
        // List<BackendObject> col = BackendServices.local().query(q);
        // for (BackendObject o : col) logger.debug(o);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.debug("", throwable);
        }
    });
}
Also used : BackendObject(io.divide.client.BackendObject) Query(io.divide.shared.transitory.query.Query) Collection(java.util.Collection) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) OnClick(butterknife.OnClick)

Example 7 with QueryBuilder

use of io.divide.shared.transitory.query.QueryBuilder in project divide by HiddenStage.

the class DAOTest method testDelete.

@Test
public void testDelete() throws Exception {
    rawSave(object1, object2, object3, object4);
    dao.delete(testObject1);
    dao.delete(testObject4);
    Collection<TransientObject> results = dao.get(testObject1Table, testObject1.getObjectKey(), testObject2.getObjectKey());
    assertNotEquals(null, results);
    assertEquals(1, results.size());
    assertEquals(ObjectUtils.get1stOrNull(results).getObjectKey(), testObject2.getObjectKey());
    Query q = new QueryBuilder().select().from(TestObject1.class).build();
    results = dao.query(q);
    assertNotEquals("Select All", null, results);
    assertEquals("Select All", 2, results.size());
    q = new QueryBuilder().select().from(TestObject2.class).build();
    results = dao.query(q);
    assertNotEquals("Select All", null, results);
    assertEquals("Select All", 0, results.size());
}
Also used : Query(io.divide.shared.transitory.query.Query) TransientObject(io.divide.shared.transitory.TransientObject) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) Test(org.junit.Test)

Example 8 with QueryBuilder

use of io.divide.shared.transitory.query.QueryBuilder in project divide by HiddenStage.

the class MyActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DivideDrawer.attach(this, R.layout.main);
    // setContentView(R.layout.main);
    ButterKnife.inject(this);
    getActionBar().setHomeButtonEnabled(true);
    // au = AuthUtils.get(this, AuthManager.ACCOUNT_TYPE); TODO replace this
    app = (MyApplication) this.getApplication();
    BackendServices.addLoginListener(new LoginListener() {

        @Override
        public void onNext(BackendUser user) {
            System.out.println("loginListener: setUser: " + user);
            if (user != null) {
                setUser(user);
            }
        }
    });
    adapter = new BackendObjectAdaper(this, objectList);
    usersLV.setAdapter(adapter);
    usersLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            BackendObject o = adapter.getItem(position);
            Query q = new QueryBuilder().delete().from(BackendObject.class).where(TransientObject.OBJECT_KEY, OPERAND.EQ, o.getObjectKey()).build();
            BackendServices.remote().query(BackendObject.class, q).subscribe(new Action1<Collection<BackendObject>>() {

                @Override
                public void call(Collection<BackendObject> backendObjects) {
                    getObjects();
                }
            });
            BackendServices.local().delete(o);
        }
    });
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            if (BackendUser.isLoggedIn())
                getObjects();
            else
                logger.debug("Not signed in, not querying data.");
        }
    }, 1000);
    Storage s = new XmlStorage(new File(this.getFilesDir() + File.separator + "something.xml"), Storage.MODE_WORLD_WRITEABLE);
    String id = "something";
    System.out.println("Stored: " + s.getString(id, ""));
    System.out.println("Stored: " + s.contains(id));
    s.edit().putString(id, "something2").commit();
    s.edit().putInt("int", 55).commit();
}
Also used : BackendObject(io.divide.client.BackendObject) Action1(rx.functions.Action1) Query(io.divide.shared.transitory.query.Query) Handler(android.os.Handler) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) BackendUser(io.divide.client.BackendUser) InjectView(butterknife.InjectView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) XmlStorage(io.divide.shared.file.XmlStorage) LoginListener(io.divide.client.auth.LoginListener) Storage(io.divide.shared.file.Storage) XmlStorage(io.divide.shared.file.XmlStorage) Collection(java.util.Collection) AdapterView(android.widget.AdapterView) File(java.io.File)

Example 9 with QueryBuilder

use of io.divide.shared.transitory.query.QueryBuilder in project divide by HiddenStage.

the class OrientDBDao method delete.

@Override
public void delete(TransientObject... objects) throws DAOException {
    checkDb();
    if (objects.length == 0)
        return;
    QueryBuilder.WhereMoreBuilder builder = new QueryBuilder().select().from(objects[0].getClass()).where(TransientObject.OBJECT_KEY, OPERAND.EQ, objects[0].getObjectKey());
    for (int x = 1; x < objects.length; x++) {
        builder.or(TransientObject.OBJECT_KEY, OPERAND.EQ, objects[x].getObjectKey());
    }
    Query q = builder.build();
    // StringBuilder sb = new StringBuilder();
    // sb.append("SELECT * FROM " + objects[0].getClass().getName() + " WHERE ");
    // for(int x=0;x<objects.length;x++){
    // sb.append("meta_data.object_key = '" + objects[x].getObjectKey() +"'");
    // if(x+1<objects.length)
    // sb.append( " OR ");
    // }
    System.out.println("Delete: " + q.getSQL());
    List<ODocument> list = db.query(new OSQLSynchQuery<ODocument>(q.getSQL()));
    for (ODocument w : list) {
        System.out.println("Deleting: " + w);
        w.delete();
    }
}
Also used : Query(io.divide.shared.transitory.query.Query) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 10 with QueryBuilder

use of io.divide.shared.transitory.query.QueryBuilder in project divide by HiddenStage.

the class DAOTest method testQuery.

@Test
public void testQuery() throws Exception {
    rawSave(object1, object2, object3, object4);
    Query q;
    List<TransientObject> results;
    String key = testObject1.getObjectKey();
    // where
    q = new QueryBuilder().select().from(TestObject1.class).where(TransientObject.OBJECT_KEY, OPERAND.EQ, key).build();
    results = dao.query(q);
    assertNotNull("1.", results);
    assertEquals("2.", 1, results.size());
    TransientObject to = ObjectUtils.get1stOrNull(results);
    assertEquals(results + "", to.getObjectKey(), testObject1.getObjectKey());
    q = new QueryBuilder().select().from(TestObject1.class).build();
    results = dao.query(q);
    assertNotEquals("Select All", null, results);
    assertEquals("Select All", 3, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.EQ, "1").build();
    results = dao.query(q);
    assertNotEquals("key1=", null, results);
    assertEquals("key1=", 2, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.GREATER_THAN, "1").build();
    results = dao.query(q);
    assertNotEquals("key1>", null, results);
    assertEquals("key1>", 1, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.GREATER_THAN_EQ, "1").build();
    results = dao.query(q);
    assertNotEquals("key1>=", null, results);
    assertEquals("key1>=", 3, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.LESS_THAN, "3").build();
    results = dao.query(q);
    assertNotEquals("key1<", null, results);
    assertEquals("key1<", 2, results.size());
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.LESS_THAN_EQ, "3").build();
    results = dao.query(q);
    assertNotEquals("key1<=", null, results);
    assertEquals("key1<=", 3, results.size());
    q = new QueryBuilder().select().from(TestObject2.class).where("key1", OPERAND.EQ, "1").or("key2", OPERAND.EQ, "2").build();
    results = dao.query(q);
    assertNotEquals("2where", null, results);
    assertEquals("2where", 1, results.size());
    assertEquals("2where", testObject4.getObjectKey(), ObjectUtils.get1stOrNull(results).getObjectKey());
    // limit
    q = new QueryBuilder().select().from(TestObject1.class).where("key1", OPERAND.LESS_THAN_EQ, "3").limit(2).build();
    results = dao.query(q);
    assertNotEquals("limit test failed", null, results);
    assertEquals("limit test failed", 2, results.size());
// q = new QueryBuilder().delete().from(TestObject1.class).where("key1",OPERAND.LESS_THAN_EQ,"3").build();
// results = dao.query(q);
// assertNotEquals("delete", null, results);
// assertEquals("delete test failed", 1, results.size());
// assertEquals("delete test failed", 3, (int)results.get(0).get(Integer.class,"count"));
// 
// q = new QueryBuilder().delete().from(TestObject1.class).build();
// results = dao.query(q);
// assertNotEquals("delete test failed", null, results);
// assertEquals("delete test failed", 1, results.size());
// assertEquals("delete test failed", 0, (int)results.get(0).get(Integer.class,"count"));
// offset
}
Also used : Query(io.divide.shared.transitory.query.Query) TransientObject(io.divide.shared.transitory.TransientObject) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) Test(org.junit.Test)

Aggregations

QueryBuilder (io.divide.shared.transitory.query.QueryBuilder)14 Query (io.divide.shared.transitory.query.Query)12 TransientObject (io.divide.shared.transitory.TransientObject)10 Test (org.junit.Test)4 BackendObject (io.divide.client.BackendObject)2 ServerDAO (io.divide.dao.ServerDAO)2 DAOException (io.divide.shared.server.DAO.DAOException)2 AuthTokenUtils (io.divide.shared.util.AuthTokenUtils)2 Collection (java.util.Collection)2 Handler (android.os.Handler)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 InjectView (butterknife.InjectView)1 OnClick (butterknife.OnClick)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1