我在提供程序包中有一个ContentProvider,其中声明了JOIN查询:
final String ALL_DATA_FOR_AN_ANIMAL = "SELECT " +
"animals._id, "
+ COLUMN_NAME + " AS _name, "
+ COLUMN_CLASSE + " AS _class, "
+ "IFNULL(" + COLUMN_OVERRIDE_INDIVIDU_NUMBER + ", " + COLUMN_INDIVIDU_NUMBER + ")" + " AS _ind_nb, "
+ "FROM " + ANIMALS_TABLE_NAME + " LEFT JOIN " + ANIMALS_OVERRIDE_TABLE_NAME
+ " ON " + COLUMN_NAME + " = " + COLUMN_OVERRIDE_NAME
+ " WHERE animals._id = ?";
使用AS定义的所有sql别名都在ContentResolver(在 operations 包中)中使用,以定义传递给ContentProvider中的查询的投影和选择
String[] projection = new String[]{"_name", "_class", "_ind_nb", "_in_danger"};
String selection = "_id";
String[] selectionArgs = {selectionArg};
return mCr.query(uri, projection, selection, selectionArgs, sortOrder);
最后,别名用于在视图包中显示查询结果
tv_Name.setText(cursor.getString(cursor.getColumnIndex("_name")));
tv_Class.setText(cursor.getString(cursor.getColumnIndex("_class")));
//...
我认为最好在 Constants 类中定义别名而不是使用硬编码的字符串,但我已经在包 ui中为ui声明了一些常量。 utils的
最佳做法是什么?编写一个常量类专业包或为整个应用程序编写单个常量类更好吗? (..或通过不同的包使用这些别名是架构不好的标志?)
更新
下面是我为gui实现的 Constants 类,这个类在包 view.utils
中public class Constants {
public static final int ANIMAL_PAGE_POS = 0;
public static final int CARACTERISTICS_PAGE_POS = 1;
//...
}
我会对别名做同样的事情,例如:
public class Constants {
public static final String ALIAS_ID = "_id";
public static final String ALIAS_NAME = "_name";
//...
}
问题是:
答案 0 :(得分:-1)
有一件事我忘了说我定义了一个类合约,其中定义了所有表。正如@Sourabh建议我使用这个类来定义JOIN查询所需的常量
var map = new Map([[1, 2], ['foo', 3], [[23, 43], 'bar']])
var data = { number: 23, myKey: map };
var serialized = JSON
.stringify(data, (k, v) => (v instanceof Map) ? [...v] : v);
console.log(serialized);