com.orm.util.NamingHelper Java Examples

The following examples show how to use com.orm.util.NamingHelper. 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 File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 6 votes vote down vote up
private static void inflate(Cursor cursor, Object object, Map<Object, Long> entitiesMap) {
    List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
    if (!entitiesMap.containsKey(object)) {
        entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex(("ID"))));
    }

    for (Field field : columns) {
    	field.setAccessible(true);
        Class<?> fieldType = field.getType();
        if (isSugarEntity(fieldType)) {
            try {
                long id = cursor.getLong(cursor.getColumnIndex(NamingHelper.toSQLName(field)));
                field.set(object, (id > 0) ? findById(fieldType, id) : null);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            ReflectionUtil.setFieldValueFromCursor(cursor, field, object);
        }
    }
}
 
Example #2
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 6 votes vote down vote up
public static <T> long count(Class<?> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
    long result = -1;
    String filter = (!TextUtils.isEmpty(whereClause)) ? " where "  + whereClause : "";
    SQLiteStatement sqliteStatement;
    try {
        sqliteStatement = getSugarDataBase().compileStatement("SELECT count(*) FROM " + NamingHelper.toSQLName(type) + filter);
    } catch (SQLiteException e) {
        e.printStackTrace();
        return result;
    }

    if (whereArgs != null) {
        for (int i = whereArgs.length; i != 0; i--) {
            sqliteStatement.bindString(i, whereArgs[i - 1]);
        }
    }

    try {
        result = sqliteStatement.simpleQueryForLong();
    } finally {
        sqliteStatement.close();
    }

    return result;
}
 
Example #3
Source File: Select.java    From ApkTrack with GNU General Public License v3.0 6 votes vote down vote up
String toSql() {
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT * FROM ").append(NamingHelper.toSQLName(this.record)).append(" ");

    if (whereClause != null) {
        sql.append("WHERE ").append(whereClause).append(" ");
    }

    if (orderBy != null) {
        sql.append("ORDER BY ").append(orderBy).append(" ");
    }

    if (limit != null) {
        sql.append("LIMIT ").append(limit).append(" ");
    }

    if (offset != null) {
        sql.append("OFFSET ").append(offset).append(" ");
    }

    return sql.toString();
}
 
Example #4
Source File: AppsModel.java    From FastAccess with GNU General Public License v3.0 5 votes vote down vote up
public static int lastPosition() {
    AppsModel appsModel = Select.from(AppsModel.class).orderBy(NamingHelper.toSQLNameDefault("indexPosition") + " DESC").first();
    if (appsModel != null) {
        return appsModel.getIndexPosition();
    }
    return 0;
}
 
Example #5
Source File: SchemaGenerator.java    From ApkTrack with GNU General Public License v3.0 5 votes vote down vote up
public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    List<Class> domainClasses = getDomainClasses(context);
    String sql = "select count(*) from sqlite_master where type='table' and name='%s';";
    for (Class domain : domainClasses) {
        Cursor cursor = sqLiteDatabase.rawQuery(String.format(sql, NamingHelper.toSQLName(domain)), null);
        if (cursor.moveToFirst() && cursor.getInt(0) == 0) {
        	createTable(domain, sqLiteDatabase);
        }
    }
    executeSugarUpgrade(sqLiteDatabase, oldVersion, newVersion);
}
 
Example #6
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 5 votes vote down vote up
public boolean delete() {
    Long id = getId();
    Class<?> type = getClass();
    if (id != null && id > 0L) {
        Log.i(SUGAR, type.getSimpleName() + " deleted : " + id);
        return getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1;
    } else {
        Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved");
        return false;
    }
}
 
Example #7
Source File: AppsModel.java    From FastAccess with GNU General Public License v3.0 5 votes vote down vote up
public static boolean exists(@NonNull String activityInfoName, @NonNull String packageName) {
    return Select.from(AppsModel.class)
            .where(Condition.prop(NamingHelper.toSQLNameDefault("activityInfoName")).eq(activityInfoName))
            .and(Condition.prop(NamingHelper.toSQLNameDefault("packageName")).eq(packageName))
            .and(Condition.prop(NamingHelper.toSQLNameDefault("folderId")).eq(0))
            .first() != null;
}
 
Example #8
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 5 votes vote down vote up
static long save(SQLiteDatabase db, Object object) {
    Map<Object, Long> entitiesMap = getSugarContext().getEntitiesMap();
    List<Field> columns = ReflectionUtil.getTableFields(object.getClass());
    ContentValues values = new ContentValues(columns.size());
    Field idField = null;
    for (Field column : columns) {
        ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap);
        if (column.getName().equals("id")) {
            idField = column;
        }
    }

    boolean isSugarEntity = isSugarEntity(object.getClass());
    if (isSugarEntity && entitiesMap.containsKey(object)) {
            values.put("id", entitiesMap.get(object));
    }

    long id = db.insertWithOnConflict(NamingHelper.toSQLName(object.getClass()), null, values,
            SQLiteDatabase.CONFLICT_REPLACE);

    if (object.getClass().isAnnotationPresent(Table.class)) {
        if (idField != null) {
            idField.setAccessible(true);
            try {
                idField.set(object, new Long(id));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            entitiesMap.put(object, id);
        }
    } else if (SugarRecord.class.isAssignableFrom(object.getClass())) {
        ((SugarRecord) object).setId(id);
    }

    Log.i(SUGAR, object.getClass().getSimpleName() + " saved : " + id);

    return id;
}
 
Example #9
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 5 votes vote down vote up
public static <T> T first(Class<T>type) {
    List<T> list = findWithQuery(type,
            "SELECT * FROM " + NamingHelper.toSQLName(type) + " ORDER BY ID ASC LIMIT 1");
    if (list.isEmpty()) {
        return null;
    }
    return list.get(0);
}
 
Example #10
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 5 votes vote down vote up
public static <T> T last(Class<T>type) {
    List<T> list = findWithQuery(type,
            "SELECT * FROM " + NamingHelper.toSQLName(type) + " ORDER BY ID DESC LIMIT 1");
    if (list.isEmpty()) {
        return null;
    }
    return list.get(0);
}
 
Example #11
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 4 votes vote down vote up
public static <T> int deleteAll(Class<T> type, String whereClause, String... whereArgs) {
    return getSugarDataBase().delete(NamingHelper.toSQLName(type), whereClause, whereArgs);
}
 
Example #12
Source File: SchemaGenerator.java    From ApkTrack with GNU General Public License v3.0 4 votes vote down vote up
private void createTable(Class<?> table, SQLiteDatabase sqLiteDatabase) {
    Log.i(SUGAR, "Create table if not exists");
    List<Field> fields = ReflectionUtil.getTableFields(table);
    String tableName = NamingHelper.toSQLName(table);
    StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
    sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT ");

    for (Field column : fields) {
        String columnName = NamingHelper.toSQLName(column);
        String columnType = QueryBuilder.getColumnType(column.getType());

        if (columnType != null) {
            if (columnName.equalsIgnoreCase("Id")) {
                continue;
            }

            if (column.isAnnotationPresent(Column.class)) {
                Column columnAnnotation = column.getAnnotation(Column.class);
                columnName = columnAnnotation.name();

                sb.append(", ").append(columnName).append(" ").append(columnType);

                if (columnAnnotation.notNull()) {
                    if (columnType.endsWith(NULL)) {
                        sb.delete(sb.length() - 5, sb.length());
                    }
                    sb.append(NOT_NULL);
                }

                if (columnAnnotation.unique()) {
                    sb.append(UNIQUE);
                }

            } else {
                sb.append(", ").append(columnName).append(" ").append(columnType);

                if (column.isAnnotationPresent(NotNull.class)) {
                    if (columnType.endsWith(NULL)) {
                        sb.delete(sb.length() - 5, sb.length());
                    }
                    sb.append(NOT_NULL);
                }

                if (column.isAnnotationPresent(Unique.class)) {
                    sb.append(UNIQUE);
                }
            }
        }
    }

    sb.append(" ) ");
    Log.i(SUGAR, "Creating table " + tableName);

    if (!sb.toString().isEmpty()) {
        try {
            sqLiteDatabase.execSQL(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
 
Example #13
Source File: SchemaGenerator.java    From ApkTrack with GNU General Public License v3.0 4 votes vote down vote up
public void deleteTables(SQLiteDatabase sqLiteDatabase) {
    List<Class> tables = getDomainClasses(context);
    for (Class table : tables) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + NamingHelper.toSQLName(table));
    }
}
 
Example #14
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 4 votes vote down vote up
public static <T> List<T> find(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
    Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs,
            groupBy, null, orderBy, limit);

    return getEntitiesFromCursor(cursor, type);
}
 
Example #15
Source File: SugarRecord.java    From ApkTrack with GNU General Public License v3.0 4 votes vote down vote up
public static <T> Iterator<T> findAsIterator(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
    Cursor cursor = getSugarDataBase().query(NamingHelper.toSQLName(type), null, whereClause, whereArgs,
            groupBy, null, orderBy, limit);
    return new CursorIterator<T>(type, cursor);
}
 
Example #16
Source File: CommandHistory.java    From grblcontroller with GNU General Public License v3.0 4 votes vote down vote up
public static List<CommandHistory> getHistory(String offset, String limit){
    return Select.from(CommandHistory.class)
            .orderBy(NamingHelper.toSQLNameDefault("usageCount") + " DESC, " + NamingHelper.toSQLNameDefault("lastUsedOn") + " DESC")
            .limit(offset + ", " + limit).list();
}
 
Example #17
Source File: FolderModel.java    From FastAccess with GNU General Public License v3.0 4 votes vote down vote up
public static void deleteFolder(@NonNull FolderModel model) {
    model.delete();
    AppsModel.deleteAll(AppsModel.class, NamingHelper.toSQLNameDefault("folderId") + " = ?", String.valueOf(model.getId()));
}
 
Example #18
Source File: FolderModel.java    From FastAccess with GNU General Public License v3.0 4 votes vote down vote up
public static List<FolderModel> getFolders() {
    return Select.from(FolderModel.class)
            .orderBy(NamingHelper.toSQLNameDefault("createdDate") + " DESC")
            .list();
}
 
Example #19
Source File: FolderModel.java    From FastAccess with GNU General Public License v3.0 4 votes vote down vote up
@Nullable public static FolderModel getFolder(@NonNull String folderName) {
    return Select.from(FolderModel.class).where(NamingHelper.toSQLNameDefault("folderName") + " = ? COLLATE NOCASE", new String[]{folderName})
            .first();
}
 
Example #20
Source File: AppsModel.java    From FastAccess with GNU General Public License v3.0 4 votes vote down vote up
public static void deleteAllByFolder(@NonNull FolderModel folder) {
    deleteAll(AppsModel.class, NamingHelper.toSQLNameDefault("folderId") + " = ?", String.valueOf(folder.getId()));
}
 
Example #21
Source File: AppsModel.java    From FastAccess with GNU General Public License v3.0 4 votes vote down vote up
public static long countApps(long folderId) {
    return count(AppsModel.class, NamingHelper.toSQLNameDefault("folderId") + " = ?", new String[]{String.valueOf(folderId)});
}
 
Example #22
Source File: AppsModel.java    From FastAccess with GNU General Public License v3.0 4 votes vote down vote up
public static List<AppsModel> getApps(long folderId) {
    return Select.from(AppsModel.class)
            .where(Condition.prop(NamingHelper.toSQLNameDefault("folderId")).eq(folderId))
            .list();
}
 
Example #23
Source File: AppsModel.java    From FastAccess with GNU General Public License v3.0 4 votes vote down vote up
public static List<AppsModel> getApps() {
    return Select.from(AppsModel.class)
            .where(Condition.prop(NamingHelper.toSQLNameDefault("folderId")).eq(0))
            .orderBy(NamingHelper.toSQLNameDefault("indexPosition") + " ASC")
            .list();
}