Java Code Examples for com.j256.ormlite.dao.RuntimeExceptionDao

The following examples show how to use com.j256.ormlite.dao.RuntimeExceptionDao. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: android-project-wo2b   Source File: UserDatabaseHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 数据初始化
 */
private void initDatabase()
{
	RuntimeExceptionDao<FocusItemInfo, Long> koItemInfoDao = getFocusItemInfoDao();
	int length = BusinessData.LOCAL_ALBUM_FOCUS.length;
	int itemCount = BusinessData.LOCAL_ALBUM_FOCUS[0].length;
	
	FocusItemInfo itemInfo = null;
	for (int i = 0; i < length; i++)
	{
		itemInfo = new FocusItemInfo();
		itemInfo.setData(BusinessData.LOCAL_ALBUM_FOCUS[i][0]);
		itemInfo.setBucket_display_name(BusinessData.LOCAL_ALBUM_FOCUS[i][1]);
		itemInfo.setBeautiful_name(BusinessData.LOCAL_ALBUM_FOCUS[i][2]);
		itemInfo.setIcon(BusinessData.LOCAL_ALBUM_FOCUS[i][3]);
		
		itemInfo.setSystem(true);
		// 系统自带索引1000开始, 用户添加的应当排在最前面.
		itemInfo.setOrder_by(1000 + i);
		
		koItemInfoDao.create(itemInfo);
	}
}
 
Example 2
Source Project: zulip-android   Source File: Stream.java    License: Apache License 2.0 6 votes vote down vote up
public static Stream getByName(ZulipApp app, String name) {
    Stream stream = null;
    try {
        RuntimeExceptionDao<Stream, Object> streams = app.getDao(Stream.class);
        stream = streams.queryBuilder().where()
                .eq(Stream.NAME_FIELD, new SelectArg(name)).queryForFirst();

        if (stream == null) {
            Log.w("Stream.getByName",
                    "We received a stream message for a stream we don't have data for. Fake it until you make it.");
            stream = new Stream(name);
            app.getDao(Stream.class).createIfNotExists(stream);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    return stream;
}
 
Example 3
Source Project: zulip-android   Source File: Message.java    License: Apache License 2.0 6 votes vote down vote up
public static void createMessages(final ZulipApp app,
                                  final List<Message> messages) {
    try {
        TransactionManager.callInTransaction(app.getDatabaseHelper()
                .getConnectionSource(), new Callable<Void>() {
            public Void call() throws Exception {
                RuntimeExceptionDao<Message, Object> messageDao = app.getDao(Message.class);

                for (Message m : messages) {
                    Person person = Person.getOrUpdate(app, m.getSenderEmail(), m.getSenderFullName(), m.getAvatarUrl(), m.getSenderId());
                    m.setSender(person);
                    Stream stream = null;
                    if (m.getType() == MessageType.STREAM_MESSAGE) {
                        stream = Stream.getByName(app, m.getRecipients());
                    }
                    m.setStream(stream);
                    messageDao.createOrUpdate(m);
                }
                return null;
            }
        });
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source Project: zulip-android   Source File: MessageRange.java    License: Apache License 2.0 6 votes vote down vote up
public static MessageRange getRangeContaining(int value,
                                              RuntimeExceptionDao<MessageRange, Integer> messageRangeDao) {
    List<MessageRange> ranges;
    try {
        ranges = messageRangeDao.queryBuilder().where().le("low", value)
                .and().ge("high", value).query();
        if (ranges.size() == 1) {
            return ranges.get(0);
        } else if (!ranges.isEmpty()) {
            Log.wtf("rangecheck",
                    "Expected one range, got " + ranges.size()
                            + " when looking for ID " + value);
        }
    } catch (SQLException e) {
        // This is nonfatal.
        ZLog.logException(e);
    }

    return null;
}
 
Example 5
Source Project: zulip-android   Source File: MessageRange.java    License: Apache License 2.0 6 votes vote down vote up
public static void updateNewMessagesRange(ZulipApp app, int maxId) {
    synchronized (app.updateRangeLock) {
        RuntimeExceptionDao<MessageRange, Integer> rangeDao = app
                .getDao(MessageRange.class);

        MessageRange currentRange = MessageRange.getRangeContaining(
                app.getMaxMessageId(), rangeDao);
        if (currentRange == null) {
            currentRange = new MessageRange(app.getMaxMessageId(),
                    app.getMaxMessageId());
        }

        if (currentRange.high <= maxId) {
            currentRange.high = maxId;
            rangeDao.createOrUpdate(currentRange);
        }
    }

    app.setMaxMessageId(maxId);
}
 
Example 6
Source Project: zulip-android   Source File: ZulipApp.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Fills the Emoji Table with the existing emoticons saved in the assets folder.
 */
private void setupEmoji() {
    try {
        final RuntimeExceptionDao<Emoji, Object> dao = getDao(Emoji.class);
        if (dao.queryForAll().size() != 0) return;
        final String emojis[] = getAssets().list("emoji");
        TransactionManager.callInTransaction(getDatabaseHelper()
                        .getConnectionSource(),
                new Callable<Void>() {
                    public Void call() throws Exception {
                        for (String newEmoji : emojis) {
                            //currently emojis are in png format
                            newEmoji = newEmoji.replace(".png", "");
                            dao.create(new Emoji(newEmoji));
                        }
                        return null;
                    }
                });
    } catch (SQLException | IOException e) {
        ZLog.logException(e);
    }
}
 
Example 7
/**
 * This is called when the database is first created. Usually you should call createTable statements here to create
 * the tables that will store your data.
 */
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    try {
        Log.i(DatabaseHelper.class.getName(), "onCreate");
        TableUtils.createTable(connectionSource, SimpleData.class);
    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
        throw new RuntimeException(e);
    }

    // here we try inserting data in the on-create as a test
    RuntimeExceptionDao<SimpleData, Integer> dao = getSimpleDataDao();
    long millis = System.currentTimeMillis();
    // create some entries in the onCreate
    SimpleData simple = new SimpleData(millis);
    dao.create(simple);
    simple = new SimpleData(millis + 1);
    dao.create(simple);
    Log.i(DatabaseHelper.class.getName(), "created new entries in onCreate: " + millis);
}
 
Example 8
Source Project: Walrus   Source File: DatabaseHelper.java    License: GNU General Public License v3.0 5 votes vote down vote up
public RuntimeExceptionDao<Card, Integer> getCardDao() {
    if (cardDao == null) {
        try {
            cardDao = RuntimeExceptionDao.createDao(getConnectionSource(), Card.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    return cardDao;
}
 
Example 9
Source Project: android-project-wo2b   Source File: UserDatabaseHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 数据初始化
 */
private void initDatabase_BAK()
{
	RuntimeExceptionDao<FocusItemInfo, Long> koItemInfoDao = getFocusItemInfoDao();
	int length = BusinessData.LOCAL_FOCUS_PARENT_DIRECTORY.length;
	int itemCount = BusinessData.LOCAL_FOCUS_PARENT_DIRECTORY[0].length;

	String sdcard_root = null;

	if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
	{
		File sdcard_dir = Environment.getExternalStorageDirectory();
		sdcard_root = sdcard_dir.getPath();
	}
	
	FocusItemInfo itemInfo = null;
	for (int i = 0; i < length; i++)
	{
		itemInfo = new FocusItemInfo();
		itemInfo.setData(sdcard_root + BusinessData.LOCAL_ALBUM_FOCUS[i][0]);
		itemInfo.setBucket_display_name(BusinessData.LOCAL_ALBUM_FOCUS[i][1]);
		itemInfo.setBeautiful_name(BusinessData.LOCAL_ALBUM_FOCUS[i][1]);
		itemInfo.setIcon(BusinessData.LOCAL_ALBUM_FOCUS[i][2]);

		itemInfo.setSystem(true);
		// 系统自带索引1000开始, 用户添加的应当排在最前面.
		itemInfo.setOrder_by(1000 + i);

		koItemInfoDao.create(itemInfo);
	}
}
 
Example 10
Source Project: android-project-wo2b   Source File: UserDatabaseHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 返回MyFavorites Dao
 * 
 * @return
 */
public RuntimeExceptionDao<MyFavorites, Long> getMyFavoritesDao()
{
	if (myFavoritesDao == null)
	{
		myFavoritesDao = getRuntimeExceptionDao(MyFavorites.class);
	}
	
	return myFavoritesDao;
}
 
Example 11
Source Project: android-project-wo2b   Source File: UserDatabaseHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 返回KoItemInfo Dao
 * 
 * @return
 */
public RuntimeExceptionDao<FocusItemInfo, Long> getFocusItemInfoDao()
{
	if (focusItemInfo == null)
	{
		focusItemInfo = getRuntimeExceptionDao(FocusItemInfo.class);
	}
	
	return focusItemInfo;
}
 
Example 12
Source Project: android-project-wo2b   Source File: DatabaseHelper.java    License: Apache License 2.0 5 votes vote down vote up
public RuntimeExceptionDao<AlbumInfo, Long> getAlbumDao()
{
	if (albumDao == null)
	{
		albumDao = getRuntimeExceptionDao(AlbumInfo.class);
	}
	
	return albumDao;
}
 
Example 13
Source Project: android-project-wo2b   Source File: DatabaseHelper.java    License: Apache License 2.0 5 votes vote down vote up
public RuntimeExceptionDao<PhotoInfo, Long> getPhotoDao()
{
	if (mPhotoDao == null)
	{
		mPhotoDao = getRuntimeExceptionDao(PhotoInfo.class);
	}
	
	return mPhotoDao;
}
 
Example 14
Source Project: zulip-android   Source File: ZulipApp.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <C, T> RuntimeExceptionDao<C, T> getDao(Class<C> cls, boolean useCache) {
    try {
        RuntimeExceptionDao<C, T> ret = new RuntimeExceptionDao<>(
                (Dao<C, T>) databaseHelper.getDao(cls));
        if (useCache) {
            ret.setObjectCache(getObjectCache());
        }
        return ret;
    } catch (SQLException e) {
        // Well that's sort of awkward.
        throw new RuntimeException(e);
    }
}
 
Example 15
Source Project: zulip-android   Source File: UnsortedTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testMessageTrim() throws SQLException {
    prepTests();
    TransactionManager.callInTransaction(app.getDatabaseHelper()
            .getConnectionSource(), new Callable<Void>() {
        public Void call() throws Exception {
            for (int i = 1; i <= 300; i++) {
                sampleMessage(app, i);
            }

            for (int i = 501; i <= 800; i++) {
                sampleMessage(app, i);
            }

            app.getDao(MessageRange.class).create(new MessageRange(1, 300));
            app.getDao(MessageRange.class).create(
                    new MessageRange(501, 800));
            return null;
        }
    });

    RuntimeExceptionDao<MessageRange, Integer> messageRangeDao = app
            .getDao(MessageRange.class);

    assertEquals(600, messageDao.countOf());
    Message.trim(100, app);
    this.messageDao.queryForAll();
    assertEquals(100, messageDao.countOf());
    assertEquals(1, messageRangeDao.countOf());
    MessageRange r = messageRangeDao.queryBuilder().queryForFirst();
    // We have messages 701 through 800, which is 100 messages.
    assertEquals(800, r.high);
    assertEquals(800 - 99, r.low);

}
 
Example 16
Source Project: xpra-client   Source File: DatabaseHelper.java    License: GNU General Public License v3.0 5 votes vote down vote up
public synchronized RuntimeExceptionDao<Connection, Integer> getConnectionDao() {
	if (connectionDao == null)
		try {
			Dao<Connection, Integer> dao = getDao(Connection.class);
			connectionDao = new RuntimeExceptionDao<Connection, Integer>(dao);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	return connectionDao;
}
 
Example 17
/**
 * Returns the RuntimeExceptionDao (Database Access Object) version of a Dao for our SimpleData class. It will
 * create it or just give the cached value. RuntimeExceptionDao only through RuntimeExceptions.
 */
public RuntimeExceptionDao<SimpleData, Integer> getSimpleDataDao() {
    if (simpleRuntimeDao == null) {
        simpleRuntimeDao = getRuntimeExceptionDao(SimpleData.class);
    }
    return simpleRuntimeDao;
}
 
Example 18
Source Project: Man-Man   Source File: PersistManager.java    License: GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public RuntimeExceptionDao<ManSectionItem, String> getManChaptersDao() {
    if (manChaptersDao == null) {
        manChaptersDao = getRuntimeExceptionDao(ManSectionItem.class);
    }
    return manChaptersDao;
}
 
Example 19
Source Project: Man-Man   Source File: PersistManager.java    License: GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public RuntimeExceptionDao<ManPage, String> getManPagesDao() {
    if (manPagesDao == null) {
        manPagesDao = getRuntimeExceptionDao(ManPage.class);
    }
    return manPagesDao;
}
 
Example 20
Source Project: Man-Man   Source File: PersistManager.java    License: GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public RuntimeExceptionDao<ManSectionIndex, String> getManChapterIndexesDao() {
    if (manChapterIndexesDao == null) {
        manChapterIndexesDao = getRuntimeExceptionDao(ManSectionIndex.class);
    }
    return manChapterIndexesDao;
}
 
Example 21
Source Project: ormlite-android   Source File: OrmLiteSqliteOpenHelper.java    License: ISC License 5 votes vote down vote up
/**
 * Get a RuntimeExceptionDao for our class. This uses the {@link DaoManager} to cache the DAO for future gets.
 * 
 * <p>
 * NOTE: This routing does not return RuntimeExceptionDao&lt;T, ID&gt; because of casting issues if we are assigning it to
 * a custom DAO. Grumble.
 * </p>
 */
public <D extends RuntimeExceptionDao<T, ?>, T> D getRuntimeExceptionDao(Class<T> clazz) {
	try {
		Dao<T, ?> dao = getDao(clazz);
		@SuppressWarnings({ "unchecked", "rawtypes" })
		D castDao = (D) new RuntimeExceptionDao(dao);
		return castDao;
	} catch (SQLException e) {
		throw new RuntimeException("Could not create RuntimeExcepitionDao for class " + clazz, e);
	}
}
 
Example 22
Source Project: zulip-android   Source File: Person.java    License: Apache License 2.0 4 votes vote down vote up
public static Person getById(ZulipApp app, int id) {
    RuntimeExceptionDao<Person, Object> dao = app.getDao(Person.class);
    return dao.queryForId(id);
}
 
Example 23
Source Project: zulip-android   Source File: Person.java    License: Apache License 2.0 4 votes vote down vote up
public static List<Person> getAllPeople(ZulipApp app) throws SQLException {
    RuntimeExceptionDao<Person, Object> dao = app.getDao(Person.class);
    return dao.queryBuilder().where().eq(Person.ISBOT_FIELD, false).query();
}
 
Example 24
Source Project: zulip-android   Source File: Message.java    License: Apache License 2.0 4 votes vote down vote up
public static void trim(final int olderThan, final ZulipApp app) {
    final RuntimeExceptionDao<Message, Integer> messageDao = app
            .<Message, Integer>getDao(Message.class);

    if (messageDao.countOf() <= olderThan) {
        return;
    }

    try {
        synchronized (app.updateRangeLock) {
            TransactionManager.callInTransaction(app.getDatabaseHelper()
                    .getConnectionSource(), new Callable<Void>() {
                public Void call() throws Exception {

                    int topID = messageDao.queryBuilder()
                            .orderBy(Message.ID_FIELD, false)
                            .offset((long) olderThan).limit((long) 1)
                            .queryForFirst().getID();

                    DeleteBuilder<Message, Integer> messageDeleter = messageDao
                            .deleteBuilder();
                    messageDeleter.where().le(ID_FIELD, topID);
                    messageDeleter.delete();

                    MessageRange rng = MessageRange.getRangeContaining(
                            topID,
                            app.<MessageRange, Integer>getDao(MessageRange.class));
                    if (rng == null) {
                        Log.wtf("trim",
                                "Message in database but not in range!");
                        return null;
                    }
                    if (rng.high == topID) {
                        rng.delete();
                    } else {
                        rng.low = topID + 1;
                        rng.update();
                    }
                    DeleteBuilder<MessageRange, Integer> dB2 = app
                            .<MessageRange, Integer>getDao(
                                    MessageRange.class).deleteBuilder();

                    dB2.where().le("high", topID);
                    dB2.delete();

                    return null;
                }
            });
        }
    } catch (SQLException e) {
        ZLog.logException(e);
    }

}
 
Example 25
Source Project: zulip-android   Source File: MessageRange.java    License: Apache License 2.0 4 votes vote down vote up
public static void markRange(ZulipApp app, final int low, final int high) {
    final RuntimeExceptionDao<MessageRange, Integer> messageRangeDao = app
            .getDao(MessageRange.class);
    try {
        synchronized (app.updateRangeLock) {
            TransactionManager.callInTransaction(app.getDatabaseHelper()
                    .getConnectionSource(), new Callable<Void>() {
                public Void call() throws Exception {
                    Where<MessageRange, Integer> where = messageRangeDao
                            .queryBuilder().orderBy("low", true).where();
                    @SuppressWarnings("unchecked")
                    List<MessageRange> ranges = where.or(
                            where.and(where.ge("high", low - 1),
                                    where.le("high", high + 1)),
                            where.and(where.ge("low", low - 1),
                                    where.le("low", high + 1))).query();

                    MessageRange rng = new MessageRange(low, high);
                    if (!ranges.isEmpty()) {
                        Log.i("", "our low: " + rng.low + ", our high: "
                                + rng.high);
                        int dbLow = ranges.get(0).low;
                        int dbHigh = ranges.get(ranges.size() - 1).high;
                        Log.i("", "their low: " + dbLow + ", their high: "
                                + dbHigh);
                        if (dbLow < rng.low) {
                            rng.low = dbLow;
                        }
                        if (dbHigh > rng.high) {
                            rng.high = dbHigh;
                        }
                        messageRangeDao.delete(ranges);
                    }
                    messageRangeDao.createOrUpdate(rng);
                    return null;
                }
            });
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
 
Example 26
Source Project: zulip-android   Source File: ZulipApp.java    License: Apache License 2.0 4 votes vote down vote up
public <C, T> RuntimeExceptionDao<C, T> getDao(Class<C> cls) {
    return getDao(cls, false);
}
 
Example 27
public ChapterContentsCursorAdapter(Activity context, RuntimeExceptionDao<ManSectionItem, String> dao, PreparedQuery<ManSectionItem> query, String chapter) {
    super(context, dao, query);
    indexes = DbProvider.getHelper().getManChapterIndexesDao().queryForEq("parentChapter", chapter);
}
 
Example 28
Source Project: Man-Man   Source File: OrmLiteCursorAdapter.java    License: GNU General Public License v3.0 4 votes vote down vote up
public OrmLiteCursorAdapter(Activity context, RuntimeExceptionDao<T, ?> dao, PreparedQuery<T> query) {
    mContext = context;
    mDao = dao;
    mQuery = query;
    updateQuery();
}
 
Example 29
Source Project: Man-Man   Source File: ManChaptersFragment.java    License: GNU General Public License v3.0 4 votes vote down vote up
private ManPageContentsResult(@NonNull RuntimeExceptionDao<ManSectionItem, String> dao, @NonNull PreparedQuery<ManSectionItem> query, @NonNull String chapter) {
    this.choiceDbCache = Pair.create(dao, query);
    this.choiceList = null;
    this.chapter = chapter;
}
 
Example 30
Source Project: mOrgAnd   Source File: OrgFile.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static RuntimeExceptionDao<OrgFile, String> getDao() {
    Context context = Application.getInstace();
    return OpenHelperManager.getHelper(context, DatabaseHelper.class).getRuntimeExceptionDao(OrgFile.class);
}