Skip to content

Commit

Permalink
Now we can know if a column has index or not with DBUtility.findPragm…
Browse files Browse the repository at this point in the history
…aTableInfo method. Show this function in the demo.
  • Loading branch information
zcc1234567 committed Jul 29, 2020
1 parent 5106df2 commit 014b708
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 23 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/litepal/LitePalBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ private ColumnModel convertFieldToColumnModel(Field field) {
columnModel.setNullable(nullable);
columnModel.setUnique(unique);
columnModel.setDefaultValue(defaultValue);
columnModel.setIndex(hasIndex);
columnModel.setHasIndex(hasIndex);
return columnModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public boolean hasIndex() {
return hasIndex;
}

public void setIndex(boolean hasIndex) {
public void setHasIndex(boolean hasIndex) {
this.hasIndex = hasIndex;
}

Expand Down
46 changes: 26 additions & 20 deletions core/src/main/java/org/litepal/util/DBUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Pair;

import org.litepal.exceptions.DatabaseGenerateException;
import org.litepal.tablemanager.model.ColumnModel;
Expand Down Expand Up @@ -397,7 +398,9 @@ public static List<String> findAllTableNames(SQLiteDatabase db) {
*/
public static TableModel findPragmaTableInfo(String tableName, SQLiteDatabase db) {
if (isTableExists(tableName, db)) {
Set<String> uniqueColumns = findUniqueColumns(tableName, db);
Pair<Set<String>, Set<String>> indexPair = findIndexedColumns(tableName, db);
Set<String> indexColumns = indexPair.first;
Set<String> uniqueColumns = indexPair.second;
TableModel tableModelDB = new TableModel();
tableModelDB.setTableName(tableName);
String checkingColumnSQL = "pragma table_info(" + tableName + ")";
Expand All @@ -411,11 +414,13 @@ public static TableModel findPragmaTableInfo(String tableName, SQLiteDatabase db
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
boolean nullable = cursor.getInt(cursor.getColumnIndexOrThrow("notnull")) != 1;
boolean unique = uniqueColumns.contains(name);
boolean hasIndex = indexColumns.contains(name);
String defaultValue = cursor.getString(cursor.getColumnIndexOrThrow("dflt_value"));
columnModel.setColumnName(name);
columnModel.setColumnType(type);
columnModel.setNullable(nullable);
columnModel.setUnique(unique);
columnModel.setHasIndex(hasIndex);
if (defaultValue != null) {
defaultValue = defaultValue.replace("'", "");
} else {
Expand All @@ -441,30 +446,33 @@ public static TableModel findPragmaTableInfo(String tableName, SQLiteDatabase db
}

/**
* Find all unique column names of specified table.
* Find all columns with index, including normal index and unique index of specified table.
* @param tableName
* The table to find unique columns.
* @param db
* Instance of SQLiteDatabase.
* @return A set with all unique column names of specified table.
* @return A pair contains two types of index columns. First is normal index columns. Second is unique index columns.
*/
public static Set<String> findUniqueColumns(String tableName, SQLiteDatabase db) {
Set<String> columns = new HashSet<>();
public static Pair<Set<String>, Set<String>> findIndexedColumns(String tableName, SQLiteDatabase db) {
Set<String> indexColumns = new HashSet<>();
Set<String> uniqueColumns = new HashSet<>();
Cursor cursor = null;
Cursor innerCursor = null;
try {
cursor = db.rawQuery("pragma index_list(" + tableName +")", null);
if (cursor.moveToFirst()) {
do {
int unique = cursor.getInt(cursor.getColumnIndexOrThrow("unique"));
if (unique == 1) {
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
innerCursor = db.rawQuery("pragma index_info(" + name + ")", null);
if (innerCursor.moveToFirst()) {
String columnName = innerCursor.getString(innerCursor.getColumnIndexOrThrow("name"));
columns.add(columnName);
}
}
boolean unique = cursor.getInt(cursor.getColumnIndexOrThrow("unique")) == 1;
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
innerCursor = db.rawQuery("pragma index_info(" + name + ")", null);
if (innerCursor.moveToFirst()) {
String columnName = innerCursor.getString(innerCursor.getColumnIndexOrThrow("name"));
if (unique) {
uniqueColumns.add(columnName);
} else {
indexColumns.add(columnName);
}
}
} while (cursor.moveToNext());
}
} catch (Exception e) {
Expand All @@ -478,7 +486,7 @@ public static Set<String> findUniqueColumns(String tableName, SQLiteDatabase db)
innerCursor.close();
}
}
return columns;
return new Pair<>(indexColumns, uniqueColumns);
}

/**
Expand All @@ -491,9 +499,7 @@ public static Set<String> findUniqueColumns(String tableName, SQLiteDatabase db)
public static boolean isFieldNameConflictWithSQLiteKeywords(String fieldName) {
if (!TextUtils.isEmpty(fieldName)) {
String fieldNameWithComma = "," + fieldName.toLowerCase(Locale.US) + ",";
if (SQLITE_KEYWORDS.contains(fieldNameWithComma)) {
return true;
}
return SQLITE_KEYWORDS.contains(fieldNameWithComma);
}
return false;
}
Expand Down Expand Up @@ -595,8 +601,8 @@ public static String convertOrderByClauseToValidName(String orderBy) {
* @return Converted order by item with valid column name.
*/
private static String convertOrderByItem(String orderByItem) {
String column = null;
String append = null;
String column;
String append;
if (orderByItem.endsWith("asc")) {
column = orderByItem.replace("asc", "").trim();
append = " asc";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
String columnType = columnModel.getColumnType();
boolean nullable = columnModel.isNullable();
boolean unique = columnModel.isUnique();
boolean hasIndex = columnModel.hasIndex();
String defaultValue = columnModel.getDefaultValue();
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.table_structure_item, null);
Expand All @@ -100,6 +101,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
text4.setText(String.valueOf(unique));
TextView text5 = view.findViewById(R.id.text_5);
text5.setText(defaultValue);
TextView text6 = view.findViewById(R.id.text_6);
text6.setText(String.valueOf(hasIndex));
return view;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Song extends LitePalSupport {
@Column(index = true)
private String name;

@Column(index = true)
@Column(unique = true, index = true)
private String lyric;

private String duration;
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/res/layout/table_structure_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@
android:gravity="center_vertical"
android:paddingLeft="6dip" />

<TextView
android:id="@+id/text_6"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="6dip" />

</LinearLayout>
10 changes: 10 additions & 0 deletions sample/src/main/res/layout/table_structure_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
android:paddingLeft="6dip"
android:text="@string/column_default"
android:textColor="#fff" />

<TextView
android:id="@+id/text_6"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:text="@string/column_index"
android:textColor="#fff" />
</LinearLayout>

<ListView
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<string name="column_nullable">Nullable</string>
<string name="column_unique">Unique</string>
<string name="column_default">Default</string>
<string name="column_index">Index</string>
<string name="sample_codes">Sample codes:</string>
<string name="result">Result:</string>
<string name="save_sample">Save Sample</string>
Expand Down

0 comments on commit 014b708

Please sign in to comment.