Search in sources :

Example 6 with Country

use of sqlite.feature.rx.model.Country in project kripton by xcesco.

the class TestRx method testRunSync.

@Test
public void testRunSync() {
    BindXenoDataSource dataSource = BindXenoDataSource.instance();
    dataSource.execute(new BindXenoDataSource.Transaction() {

        @Override
        public TransactionResult onExecute(BindXenoDaoFactory daoFactory) {
            CountryDaoImpl dao = daoFactory.getCountryDao();
            for (int i = 0; i < COUNTER; i++) {
                Country bean = new Country();
                bean.code = "code" + i;
                bean.callingCode = "" + i;
                bean.name = "name" + i;
                // Object bean = new
                dao.insert(bean);
            }
            dao.selectAll();
            return TransactionResult.COMMIT;
        }
    });
    dataSource.execute(new BindXenoDataSource.Transaction() {

        @Override
        public TransactionResult onExecute(BindXenoDaoFactory daoFactory) {
            System.out.println("onSubscribe");
            CountryDaoImpl dao = daoFactory.getCountryDao();
            List<Country> list = dao.selectAll();
            for (Country item : list) {
                System.out.println("onNext" + item);
            }
            System.out.println("onComplete");
            return TransactionResult.COMMIT;
        }
    });
}
Also used : CountryDaoImpl(sqlite.feature.rx.persistence.CountryDaoImpl) BindXenoDataSource(sqlite.feature.rx.persistence.BindXenoDataSource) TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) Country(sqlite.feature.rx.model.Country) List(java.util.List) BindXenoDaoFactory(sqlite.feature.rx.persistence.BindXenoDaoFactory) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 7 with Country

use of sqlite.feature.rx.model.Country in project kripton by xcesco.

the class TestRx method testDatabase.

@Test
public void testDatabase() {
    final BindXenoDataSource ds = prepareDataSource();
    Disposable s1 = ds.getCountryDao().subject().subscribe(new Consumer<SQLiteEvent>() {

        @Override
        public void accept(SQLiteEvent t) throws Exception {
            log("---->  MAP " + Thread.currentThread().getName());
            log("S1 ---------------------- receive country %s %s", t.operationType, t.value);
        }
    });
    /*
		 * ds.execute(new ObservableTransaction<Country>() {
		 * 
		 * @Override public TransactionResult onExecute(BindXenoDaoFactory
		 * daoFactory, ObservableEmitter<Country> emitter) { log("onExecute " +
		 * Thread.currentThread().getName()); CountryDaoImpl dao =
		 * daoFactory.getCountryDao();
		 * 
		 * List<Country> list = dao.selectAll();
		 * 
		 * for (Country item : list) { emitter.onNext(item); }
		 * 
		 * return TransactionResult.COMMIT; }
		 * }).subscribeOn(Schedulers.computation()).observeOn(Schedulers.io()).
		 * subscribe(new Consumer<Country>() {
		 * 
		 * @Override public void accept(Country t) throws Exception {
		 * log("accept " + Thread.currentThread().getName()); log(" country " +
		 * t.name);
		 * 
		 * } });
		 */
    ds.executeBatch(new BindXenoDataSource.Batch<Void>() {

        @Override
        public Void onExecute(BindXenoDaoFactory daoFactory) {
            for (int i = 100; i < 102; i++) {
                Country bean = new Country();
                bean.code = "code" + i;
                bean.callingCode = "" + i;
                bean.name = "name" + i;
                daoFactory.getCountryDao().insert(bean);
            }
            return null;
        }
    });
    Disposable s2 = ds.countrySubject().observeOn(Schedulers.io()).map(new Function<SQLiteEvent, List<Country>>() {

        @Override
        public List<Country> apply(SQLiteEvent t) throws Exception {
            log("---->  MAP " + Thread.currentThread().getName());
            return ds.executeBatch(new BindXenoDataSource.Batch<List<Country>>() {

                @Override
                public List<Country> onExecute(BindXenoDaoFactory daoFactory) {
                    return daoFactory.getCountryDao().selectAll();
                }
            });
        }
    }).subscribe(new Consumer<List<Country>>() {

        @Override
        public void accept(List<Country> t) throws Exception {
            log("---->  S2 " + Thread.currentThread().getName());
        // log("S2 ---------------------- receive country %s
        // %s",t.operationType , t.value);
        // ds.getCountryDao().selectAll();
        }
    });
    ds.executeBatch(new BindXenoDataSource.Batch<Void>() {

        @Override
        public Void onExecute(BindXenoDaoFactory daoFactory) {
            for (int i = 200; i < 202; i++) {
                Country bean = new Country();
                bean.code = "code" + i;
                bean.callingCode = "" + i;
                bean.name = "name" + i;
                daoFactory.getCountryDao().insert(bean);
            }
            return null;
        }
    });
    try {
        Thread.currentThread().sleep(5000);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    s1.dispose();
    s2.dispose();
}
Also used : Disposable(io.reactivex.disposables.Disposable) BindXenoDaoFactory(sqlite.feature.rx.persistence.BindXenoDaoFactory) SQLiteEvent(com.abubusoft.kripton.android.sqlite.SQLiteEvent) Function(io.reactivex.functions.Function) BindXenoDataSource(sqlite.feature.rx.persistence.BindXenoDataSource) Country(sqlite.feature.rx.model.Country) List(java.util.List) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 8 with Country

use of sqlite.feature.rx.model.Country in project kripton by xcesco.

the class CountryDaoImpl method selectById.

/**
 * <h2>Select SQL:</h2>
 *
 * <pre>SELECT id, area, code, calling_code, region, name, translated_name FROM country WHERE id = ${id}</pre>
 *
 * <h2>Projected columns:</h2>
 * <dl>
 * 	<dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd>
 * 	<dt>area</dt><dd>is associated to bean's property <strong>area</strong></dd>
 * 	<dt>code</dt><dd>is associated to bean's property <strong>code</strong></dd>
 * 	<dt>calling_code</dt><dd>is associated to bean's property <strong>callingCode</strong></dd>
 * 	<dt>region</dt><dd>is associated to bean's property <strong>region</strong></dd>
 * 	<dt>name</dt><dd>is associated to bean's property <strong>name</strong></dd>
 * 	<dt>translated_name</dt><dd>is associated to bean's property <strong>translatedName</strong></dd>
 * </dl>
 *
 * <h2>Query's parameters:</h2>
 * <dl>
 * 	<dt>${id}</dt><dd>is binded to method's parameter <strong>id</strong></dd>
 * </dl>
 *
 * @param id
 * 	is binded to <code>${id}</code>
 * @return selected bean or <code>null</code>.
 */
@Override
public Country selectById(long id) {
    KriptonContentValues _contentValues = contentValues();
    // query SQL is statically defined
    String _sql = SELECT_BY_ID_SQL6;
    // add where arguments
    _contentValues.addWhereArgs(String.valueOf(id));
    String[] _sqlArgs = _contentValues.whereArgsAsArray();
    // log section BEGIN
    if (_context.isLogEnabled()) {
        // manage log
        Logger.info(_sql);
        // log for where parameters -- BEGIN
        int _whereParamCounter = 0;
        for (String _whereParamItem : _contentValues.whereArgs()) {
            Logger.info("==> param%s: '%s'", (_whereParamCounter++), StringUtils.checkSize(_whereParamItem));
        }
    // log for where parameters -- END
    }
    // log section END
    try (Cursor _cursor = database().rawQuery(_sql, _sqlArgs)) {
        // log section BEGIN
        if (_context.isLogEnabled()) {
            Logger.info("Rows found: %s", _cursor.getCount());
        }
        // log section END
        Country resultBean = null;
        if (_cursor.moveToFirst()) {
            int index0 = _cursor.getColumnIndex("id");
            int index1 = _cursor.getColumnIndex("area");
            int index2 = _cursor.getColumnIndex("code");
            int index3 = _cursor.getColumnIndex("calling_code");
            int index4 = _cursor.getColumnIndex("region");
            int index5 = _cursor.getColumnIndex("name");
            int index6 = _cursor.getColumnIndex("translated_name");
            resultBean = new Country();
            resultBean.id = _cursor.getLong(index0);
            if (!_cursor.isNull(index1)) {
                resultBean.area = _cursor.getLong(index1);
            }
            resultBean.code = _cursor.getString(index2);
            resultBean.callingCode = _cursor.getString(index3);
            if (!_cursor.isNull(index4)) {
                resultBean.region = _cursor.getString(index4);
            }
            resultBean.name = _cursor.getString(index5);
            if (!_cursor.isNull(index6)) {
                resultBean.translatedName = CountryTable.parseTranslatedName(_cursor.getBlob(index6));
            }
        }
        return resultBean;
    }
}
Also used : KriptonContentValues(com.abubusoft.kripton.android.sqlite.KriptonContentValues) Country(sqlite.feature.rx.model.Country) Cursor(android.database.Cursor)

Example 9 with Country

use of sqlite.feature.rx.model.Country in project kripton by xcesco.

the class CountryDaoImpl method selectAll.

/**
 * <h2>Select SQL:</h2>
 *
 * <pre>SELECT id, area, code, calling_code, region, name, translated_name FROM country ORDER BY name asc</pre>
 *
 * <h2>Projected columns:</h2>
 * <dl>
 * 	<dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd>
 * 	<dt>area</dt><dd>is associated to bean's property <strong>area</strong></dd>
 * 	<dt>code</dt><dd>is associated to bean's property <strong>code</strong></dd>
 * 	<dt>calling_code</dt><dd>is associated to bean's property <strong>callingCode</strong></dd>
 * 	<dt>region</dt><dd>is associated to bean's property <strong>region</strong></dd>
 * 	<dt>name</dt><dd>is associated to bean's property <strong>name</strong></dd>
 * 	<dt>translated_name</dt><dd>is associated to bean's property <strong>translatedName</strong></dd>
 * </dl>
 *
 * @param listener
 * 	is the Country listener
 */
@Override
public void selectAll(OnReadBeanListener<Country> listener) {
    KriptonContentValues _contentValues = contentValues();
    // query SQL is statically defined
    String _sql = SELECT_ALL_SQL8;
    // add where arguments
    String[] _sqlArgs = _contentValues.whereArgsAsArray();
    // log section BEGIN
    if (_context.isLogEnabled()) {
        // manage log
        Logger.info(_sql);
        // log for where parameters -- BEGIN
        int _whereParamCounter = 0;
        for (String _whereParamItem : _contentValues.whereArgs()) {
            Logger.info("==> param%s: '%s'", (_whereParamCounter++), StringUtils.checkSize(_whereParamItem));
        }
    // log for where parameters -- END
    }
    // log section END
    try (Cursor _cursor = database().rawQuery(_sql, _sqlArgs)) {
        // log section BEGIN
        if (_context.isLogEnabled()) {
            Logger.info("Rows found: %s", _cursor.getCount());
        }
        // log section END
        Country resultBean = new Country();
        if (_cursor.moveToFirst()) {
            int index0 = _cursor.getColumnIndex("id");
            int index1 = _cursor.getColumnIndex("area");
            int index2 = _cursor.getColumnIndex("code");
            int index3 = _cursor.getColumnIndex("calling_code");
            int index4 = _cursor.getColumnIndex("region");
            int index5 = _cursor.getColumnIndex("name");
            int index6 = _cursor.getColumnIndex("translated_name");
            int rowCount = _cursor.getCount();
            do {
                // reset mapping
                // id does not need reset
                resultBean.area = 0L;
                // code does not need reset
                // callingCode does not need reset
                resultBean.region = null;
                // name does not need reset
                resultBean.translatedName = null;
                // generate mapping
                resultBean.id = _cursor.getLong(index0);
                if (!_cursor.isNull(index1)) {
                    resultBean.area = _cursor.getLong(index1);
                }
                resultBean.code = _cursor.getString(index2);
                resultBean.callingCode = _cursor.getString(index3);
                if (!_cursor.isNull(index4)) {
                    resultBean.region = _cursor.getString(index4);
                }
                resultBean.name = _cursor.getString(index5);
                if (!_cursor.isNull(index6)) {
                    resultBean.translatedName = CountryTable.parseTranslatedName(_cursor.getBlob(index6));
                }
                listener.onRead(resultBean, _cursor.getPosition(), rowCount);
            } while (_cursor.moveToNext());
        }
    }
}
Also used : KriptonContentValues(com.abubusoft.kripton.android.sqlite.KriptonContentValues) Country(sqlite.feature.rx.model.Country) Cursor(android.database.Cursor)

Example 10 with Country

use of sqlite.feature.rx.model.Country in project kripton by xcesco.

the class CountryDaoImpl method selectAll.

/**
 * <h2>Select SQL:</h2>
 *
 * <pre>SELECT id, area, code, calling_code, region, name, translated_name FROM country ORDER BY name asc</pre>
 *
 * <h2>Projected columns:</h2>
 * <dl>
 * 	<dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd>
 * 	<dt>area</dt><dd>is associated to bean's property <strong>area</strong></dd>
 * 	<dt>code</dt><dd>is associated to bean's property <strong>code</strong></dd>
 * 	<dt>calling_code</dt><dd>is associated to bean's property <strong>callingCode</strong></dd>
 * 	<dt>region</dt><dd>is associated to bean's property <strong>region</strong></dd>
 * 	<dt>name</dt><dd>is associated to bean's property <strong>name</strong></dd>
 * 	<dt>translated_name</dt><dd>is associated to bean's property <strong>translatedName</strong></dd>
 * </dl>
 *
 * @return collection of bean or empty collection.
 */
@Override
public List<Country> selectAll() {
    KriptonContentValues _contentValues = contentValues();
    // query SQL is statically defined
    String _sql = SELECT_ALL_SQL7;
    // add where arguments
    String[] _sqlArgs = _contentValues.whereArgsAsArray();
    // log section BEGIN
    if (_context.isLogEnabled()) {
        // manage log
        Logger.info(_sql);
        // log for where parameters -- BEGIN
        int _whereParamCounter = 0;
        for (String _whereParamItem : _contentValues.whereArgs()) {
            Logger.info("==> param%s: '%s'", (_whereParamCounter++), StringUtils.checkSize(_whereParamItem));
        }
    // log for where parameters -- END
    }
    // log section END
    try (Cursor _cursor = database().rawQuery(_sql, _sqlArgs)) {
        // log section BEGIN
        if (_context.isLogEnabled()) {
            Logger.info("Rows found: %s", _cursor.getCount());
        }
        // log section END
        ArrayList<Country> resultList = new ArrayList<Country>(_cursor.getCount());
        Country resultBean = null;
        if (_cursor.moveToFirst()) {
            int index0 = _cursor.getColumnIndex("id");
            int index1 = _cursor.getColumnIndex("area");
            int index2 = _cursor.getColumnIndex("code");
            int index3 = _cursor.getColumnIndex("calling_code");
            int index4 = _cursor.getColumnIndex("region");
            int index5 = _cursor.getColumnIndex("name");
            int index6 = _cursor.getColumnIndex("translated_name");
            do {
                resultBean = new Country();
                resultBean.id = _cursor.getLong(index0);
                if (!_cursor.isNull(index1)) {
                    resultBean.area = _cursor.getLong(index1);
                }
                resultBean.code = _cursor.getString(index2);
                resultBean.callingCode = _cursor.getString(index3);
                if (!_cursor.isNull(index4)) {
                    resultBean.region = _cursor.getString(index4);
                }
                resultBean.name = _cursor.getString(index5);
                if (!_cursor.isNull(index6)) {
                    resultBean.translatedName = CountryTable.parseTranslatedName(_cursor.getBlob(index6));
                }
                resultList.add(resultBean);
            } while (_cursor.moveToNext());
        }
        return resultList;
    }
}
Also used : KriptonContentValues(com.abubusoft.kripton.android.sqlite.KriptonContentValues) ArrayList(java.util.ArrayList) Country(sqlite.feature.rx.model.Country) Cursor(android.database.Cursor)

Aggregations

Country (sqlite.feature.rx.model.Country)10 Cursor (android.database.Cursor)5 KriptonContentValues (com.abubusoft.kripton.android.sqlite.KriptonContentValues)5 BindXenoDaoFactory (sqlite.feature.rx.persistence.BindXenoDaoFactory)5 BindXenoDataSource (sqlite.feature.rx.persistence.BindXenoDataSource)5 BaseAndroidTest (base.BaseAndroidTest)4 Test (org.junit.Test)4 CountryDaoImpl (sqlite.feature.rx.persistence.CountryDaoImpl)4 TransactionResult (com.abubusoft.kripton.android.sqlite.TransactionResult)3 List (java.util.List)3 Disposable (io.reactivex.disposables.Disposable)2 OnReadBeanListener (com.abubusoft.kripton.android.sqlite.OnReadBeanListener)1 SQLiteEvent (com.abubusoft.kripton.android.sqlite.SQLiteEvent)1 Function (io.reactivex.functions.Function)1 ArrayList (java.util.ArrayList)1