Search in sources :

Example 1 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class DbBase method dropDb.

@Override
public void dropDb() throws DbException {
    Cursor cursor = execQuery("SELECT name FROM sqlite_master WHERE type='table' AND name<>'sqlite_sequence'");
    if (cursor != null) {
        try {
            while (cursor.moveToNext()) {
                try {
                    String tableName = cursor.getString(0);
                    execNonQuery("DROP TABLE " + tableName);
                } catch (Throwable e) {
                    LogUtil.e(e.getMessage(), e);
                }
            }
            synchronized (tableMap) {
                for (TableEntity<?> table : tableMap.values()) {
                    table.setCheckedDatabase(false);
                }
                tableMap.clear();
            }
        } catch (Throwable e) {
            throw new DbException(e);
        } finally {
            IOUtil.closeQuietly(cursor);
        }
    }
}
Also used : Cursor(android.database.Cursor) DbException(org.xutils.ex.DbException)

Example 2 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class SqlInfoBuilder method buildDeleteSqlInfo.

//*********************************************** delete sql ***********************************************
public static SqlInfo buildDeleteSqlInfo(TableEntity<?> table, Object entity) throws DbException {
    SqlInfo result = new SqlInfo();
    ColumnEntity id = table.getId();
    Object idValue = id.getColumnValue(entity);
    if (idValue == null) {
        throw new DbException("this entity[" + table.getEntityType() + "]'s id value is null");
    }
    StringBuilder builder = new StringBuilder("DELETE FROM ");
    builder.append("\"").append(table.getName()).append("\"");
    builder.append(" WHERE ").append(WhereBuilder.b(id.getName(), "=", idValue));
    result.setSql(builder.toString());
    return result;
}
Also used : ColumnEntity(org.xutils.db.table.ColumnEntity) DbException(org.xutils.ex.DbException)

Example 3 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class SqlInfoBuilder method buildDeleteSqlInfoById.

public static SqlInfo buildDeleteSqlInfoById(TableEntity<?> table, Object idValue) throws DbException {
    SqlInfo result = new SqlInfo();
    ColumnEntity id = table.getId();
    if (idValue == null) {
        throw new DbException("this entity[" + table.getEntityType() + "]'s id value is null");
    }
    StringBuilder builder = new StringBuilder("DELETE FROM ");
    builder.append("\"").append(table.getName()).append("\"");
    builder.append(" WHERE ").append(WhereBuilder.b(id.getName(), "=", idValue));
    result.setSql(builder.toString());
    return result;
}
Also used : ColumnEntity(org.xutils.db.table.ColumnEntity) DbException(org.xutils.ex.DbException)

Example 4 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class LruDiskCache method getDiskCacheFile.

public DiskCacheFile getDiskCacheFile(String key) throws InterruptedException {
    if (!available || TextUtils.isEmpty(key)) {
        return null;
    }
    DiskCacheFile result = null;
    DiskCacheEntity entity = get(key);
    if (entity != null && new File(entity.getPath()).exists()) {
        ProcessLock processLock = ProcessLock.tryLock(entity.getPath(), false, LOCK_WAIT);
        if (processLock != null && processLock.isValid()) {
            result = new DiskCacheFile(entity, entity.getPath(), processLock);
            if (!result.exists()) {
                try {
                    cacheDb.delete(entity);
                } catch (DbException ex) {
                    LogUtil.e(ex.getMessage(), ex);
                }
                result = null;
            }
        }
    }
    return result;
}
Also used : ProcessLock(org.xutils.common.util.ProcessLock) File(java.io.File) DbException(org.xutils.ex.DbException)

Example 5 with DbException

use of org.xutils.ex.DbException in project xUtils3 by wyouflf.

the class LruDiskCache method commitDiskCacheFile.

/**
     * 添加缓存文件
     *
     * @param cacheFile
     */
/*package*/
DiskCacheFile commitDiskCacheFile(DiskCacheFile cacheFile) throws IOException {
    if (cacheFile != null && cacheFile.length() < 1L) {
        IOUtil.closeQuietly(cacheFile);
        return null;
    }
    if (!available || cacheFile == null) {
        return null;
    }
    DiskCacheFile result = null;
    DiskCacheEntity cacheEntity = cacheFile.cacheEntity;
    if (cacheFile.getName().endsWith(TEMP_FILE_SUFFIX)) {
        // is temp file
        ProcessLock processLock = null;
        DiskCacheFile destFile = null;
        try {
            String destPath = cacheEntity.getPath();
            processLock = ProcessLock.tryLock(destPath, true, LOCK_WAIT);
            if (processLock != null && processLock.isValid()) {
                // lock
                destFile = new DiskCacheFile(cacheEntity, destPath, processLock);
                if (cacheFile.renameTo(destFile)) {
                    try {
                        result = destFile;
                        cacheDb.replace(cacheEntity);
                    } catch (DbException ex) {
                        LogUtil.e(ex.getMessage(), ex);
                    }
                    trimSize();
                } else {
                    throw new IOException("rename:" + cacheFile.getAbsolutePath());
                }
            } else {
                throw new FileLockedException(destPath);
            }
        } catch (InterruptedException ex) {
            result = cacheFile;
            LogUtil.e(ex.getMessage(), ex);
        } finally {
            if (result == null) {
                result = cacheFile;
                IOUtil.closeQuietly(destFile);
                IOUtil.closeQuietly(processLock);
                IOUtil.deleteFileOrDir(destFile);
            } else {
                IOUtil.closeQuietly(cacheFile);
                IOUtil.deleteFileOrDir(cacheFile);
            }
        }
    } else {
        result = cacheFile;
    }
    return result;
}
Also used : FileLockedException(org.xutils.ex.FileLockedException) ProcessLock(org.xutils.common.util.ProcessLock) IOException(java.io.IOException) DbException(org.xutils.ex.DbException)

Aggregations

DbException (org.xutils.ex.DbException)16 Cursor (android.database.Cursor)8 ColumnEntity (org.xutils.db.table.ColumnEntity)3 ArrayList (java.util.ArrayList)2 ProcessLock (org.xutils.common.util.ProcessLock)2 DbModel (org.xutils.db.table.DbModel)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1 DbManager (org.xutils.DbManager)1 KeyValue (org.xutils.common.util.KeyValue)1 FileLockedException (org.xutils.ex.FileLockedException)1 Parent (org.xutils.sample.db.Parent)1 Event (org.xutils.view.annotation.Event)1