Search in sources :

Example 1 with LitePalAttr

use of org.litepal.parser.LitePalAttr in project LitePal by LitePalFramework.

the class BaseUtility method changeCase.

/**
 * It will change the case of the passing parameter into the case defined in
 * litepal.xml file.
 *
 * @param string
 *            The string want to change case.
 * @return The string after changing case. If the name is null, then simply
 *         return null.
 */
public static String changeCase(String string) {
    if (string != null) {
        LitePalAttr litePalAttr = LitePalAttr.getInstance();
        String cases = litePalAttr.getCases();
        if (Const.Config.CASES_KEEP.equals(cases)) {
            return string;
        } else if (Const.Config.CASES_UPPER.equals(cases)) {
            return string.toUpperCase(Locale.US);
        }
        return string.toLowerCase(Locale.US);
    }
    return null;
}
Also used : LitePalAttr(org.litepal.parser.LitePalAttr)

Example 2 with LitePalAttr

use of org.litepal.parser.LitePalAttr in project LitePal by LitePalFramework.

the class LitePal method use.

/**
 * Switch the using database to the one specified by parameter.
 * @param litePalDB
 *          The database to switch to.
 */
public static void use(LitePalDB litePalDB) {
    LitePalAttr litePalAttr = LitePalAttr.getInstance();
    litePalAttr.setDbName(litePalDB.getDbName());
    litePalAttr.setVersion(litePalDB.getVersion());
    litePalAttr.setStorage(litePalDB.getStorage());
    litePalAttr.setClassNames(litePalDB.getClassNames());
    // set the extra key name only when use database other than default or litepal.xml not exists
    if (!isDefaultDatabase(litePalDB.getDbName())) {
        litePalAttr.setExtraKeyName(litePalDB.getDbName());
        litePalAttr.setCases("lower");
    }
    Connector.clearLitePalOpenHelperInstance();
}
Also used : LitePalAttr(org.litepal.parser.LitePalAttr)

Example 3 with LitePalAttr

use of org.litepal.parser.LitePalAttr in project LitePal by LitePalFramework.

the class Connector method buildConnection.

/**
 * Build a connection to the database. This progress will analysis the
 * litepal.xml file, and will check if the fields in LitePalAttr are valid,
 * and it will open a SQLiteOpenHelper to decide to create tables or update
 * tables or doing nothing depends on the version attributes.
 *
 * After all the stuffs above are finished. This method will return a
 * LitePalHelper object.Notes this method could throw a lot of exceptions.
 *
 * @return LitePalHelper object.
 *
 * @throws org.litepal.exceptions.InvalidAttributesException
 */
private static LitePalOpenHelper buildConnection() {
    LitePalAttr litePalAttr = LitePalAttr.getInstance();
    litePalAttr.checkSelfValid();
    if (mLitePalHelper == null) {
        String dbName = litePalAttr.getDbName();
        if ("external".equalsIgnoreCase(litePalAttr.getStorage())) {
            dbName = LitePalApplication.getContext().getExternalFilesDir("") + "/databases/" + dbName;
        } else if (!"internal".equalsIgnoreCase(litePalAttr.getStorage()) && !TextUtils.isEmpty(litePalAttr.getStorage())) {
            // internal or empty means internal storage, neither or them means sdcard storage
            String dbPath = Environment.getExternalStorageDirectory().getPath() + "/" + litePalAttr.getStorage();
            dbPath = dbPath.replace("//", "/");
            if (BaseUtility.isClassAndMethodExist("android.support.v4.content.ContextCompat", "checkSelfPermission") && ContextCompat.checkSelfPermission(LitePalApplication.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                throw new DatabaseGenerateException(String.format(DatabaseGenerateException.EXTERNAL_STORAGE_PERMISSION_DENIED, dbPath));
            }
            File path = new File(dbPath);
            if (!path.exists()) {
                path.mkdirs();
            }
            dbName = dbPath + "/" + dbName;
        }
        mLitePalHelper = new LitePalOpenHelper(dbName, litePalAttr.getVersion());
    }
    return mLitePalHelper;
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) LitePalAttr(org.litepal.parser.LitePalAttr) File(java.io.File)

Aggregations

LitePalAttr (org.litepal.parser.LitePalAttr)3 File (java.io.File)1 DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)1