Search in sources :

Example 1 with Post

use of sqlite.quickstart.model.Post in project kripton by xcesco.

the class TestQuickstartRuntime method testRunSqlite1.

@Test
public void testRunSqlite1() {
    BindQuickStartDataSource dataSource = BindQuickStartDataSource.instance();
    dataSource.execute(new BindQuickStartDataSource.Transaction() {

        @Override
        public TransactionResult onExecute(BindQuickStartDaoFactory daoFactory) {
            User user = new User();
            user.id = 1;
            user.name = "user";
            user.username = "username";
            daoFactory.getUserDao().insert(user);
            Post post = new Post();
            post.id = 2;
            post.title = "post";
            post.userId = user.id;
            daoFactory.getPostDao().insert(post);
            Comment comment = new Comment();
            comment.id = 3;
            comment.postId = post.id;
            daoFactory.getCommentDao().insert(comment);
            return TransactionResult.COMMIT;
        }
    });
}
Also used : Comment(sqlite.quickstart.model.Comment) BindQuickStartDataSource(sqlite.quickstart.persistence.BindQuickStartDataSource) TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) User(sqlite.quickstart.model.User) Post(sqlite.quickstart.model.Post) BindQuickStartDaoFactory(sqlite.quickstart.persistence.BindQuickStartDaoFactory) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 2 with Post

use of sqlite.quickstart.model.Post in project kripton by xcesco.

the class PostDaoImpl method selectOneByUserId.

/**
 * <h2>Select SQL:</h2>
 *
 * <pre>SELECT user_id, id, title, body FROM post WHERE id = ${value}</pre>
 *
 * <h2>Projected columns:</h2>
 * <dl>
 * 	<dt>user_id</dt><dd>is associated to bean's property <strong>userId</strong></dd>
 * 	<dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd>
 * 	<dt>title</dt><dd>is associated to bean's property <strong>title</strong></dd>
 * 	<dt>body</dt><dd>is associated to bean's property <strong>body</strong></dd>
 * </dl>
 *
 * <h2>Query's parameters:</h2>
 * <dl>
 * 	<dt>${value}</dt><dd>is binded to method's parameter <strong>userId</strong></dd>
 * </dl>
 *
 * @param userId
 * 	is binded to <code>${value}</code>
 * @return selected bean or <code>null</code>.
 */
@Override
public Post selectOneByUserId(long userId) {
    KriptonContentValues _contentValues = contentValues();
    // query SQL is statically defined
    String _sql = SELECT_ONE_BY_USER_ID_SQL4;
    // add where arguments
    _contentValues.addWhereArgs(String.valueOf(userId));
    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
        Post resultBean = null;
        if (_cursor.moveToFirst()) {
            int index0 = _cursor.getColumnIndex("user_id");
            int index1 = _cursor.getColumnIndex("id");
            int index2 = _cursor.getColumnIndex("title");
            int index3 = _cursor.getColumnIndex("body");
            resultBean = new Post();
            if (!_cursor.isNull(index0)) {
                resultBean.userId = _cursor.getLong(index0);
            }
            resultBean.id = _cursor.getLong(index1);
            if (!_cursor.isNull(index2)) {
                resultBean.title = _cursor.getString(index2);
            }
            if (!_cursor.isNull(index3)) {
                resultBean.body = _cursor.getString(index3);
            }
        }
        return resultBean;
    }
}
Also used : KriptonContentValues(com.abubusoft.kripton.android.sqlite.KriptonContentValues) Post(sqlite.quickstart.model.Post) Cursor(android.database.Cursor)

Example 3 with Post

use of sqlite.quickstart.model.Post in project kripton by xcesco.

the class PostDaoImpl method selectByUserId.

/**
 * <h2>Select SQL:</h2>
 *
 * <pre>SELECT user_id, id, title, body FROM post WHERE user_id = ${value}</pre>
 *
 * <h2>Projected columns:</h2>
 * <dl>
 * 	<dt>user_id</dt><dd>is associated to bean's property <strong>userId</strong></dd>
 * 	<dt>id</dt><dd>is associated to bean's property <strong>id</strong></dd>
 * 	<dt>title</dt><dd>is associated to bean's property <strong>title</strong></dd>
 * 	<dt>body</dt><dd>is associated to bean's property <strong>body</strong></dd>
 * </dl>
 *
 * <h2>Query's parameters:</h2>
 * <dl>
 * 	<dt>${value}</dt><dd>is binded to method's parameter <strong>userId</strong></dd>
 * </dl>
 *
 * @param userId
 * 	is binded to <code>${value}</code>
 * @return collection of bean or empty collection.
 */
@Override
public List<Post> selectByUserId(long userId) {
    KriptonContentValues _contentValues = contentValues();
    // query SQL is statically defined
    String _sql = SELECT_BY_USER_ID_SQL3;
    // add where arguments
    _contentValues.addWhereArgs(String.valueOf(userId));
    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<Post> resultList = new ArrayList<Post>(_cursor.getCount());
        Post resultBean = null;
        if (_cursor.moveToFirst()) {
            int index0 = _cursor.getColumnIndex("user_id");
            int index1 = _cursor.getColumnIndex("id");
            int index2 = _cursor.getColumnIndex("title");
            int index3 = _cursor.getColumnIndex("body");
            do {
                resultBean = new Post();
                if (!_cursor.isNull(index0)) {
                    resultBean.userId = _cursor.getLong(index0);
                }
                resultBean.id = _cursor.getLong(index1);
                if (!_cursor.isNull(index2)) {
                    resultBean.title = _cursor.getString(index2);
                }
                if (!_cursor.isNull(index3)) {
                    resultBean.body = _cursor.getString(index3);
                }
                resultList.add(resultBean);
            } while (_cursor.moveToNext());
        }
        return resultList;
    }
}
Also used : KriptonContentValues(com.abubusoft.kripton.android.sqlite.KriptonContentValues) Post(sqlite.quickstart.model.Post) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Aggregations

Post (sqlite.quickstart.model.Post)3 Cursor (android.database.Cursor)2 KriptonContentValues (com.abubusoft.kripton.android.sqlite.KriptonContentValues)2 BaseAndroidTest (base.BaseAndroidTest)1 TransactionResult (com.abubusoft.kripton.android.sqlite.TransactionResult)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 Comment (sqlite.quickstart.model.Comment)1 User (sqlite.quickstart.model.User)1 BindQuickStartDaoFactory (sqlite.quickstart.persistence.BindQuickStartDaoFactory)1 BindQuickStartDataSource (sqlite.quickstart.persistence.BindQuickStartDataSource)1