1 |
|
package com.dreamcatcher.bicycle.dataset; |
2 |
|
|
3 |
|
import java.util.ArrayList; |
4 |
|
import java.util.HashMap; |
5 |
|
|
6 |
|
import android.app.SearchManager; |
7 |
|
import android.content.ContentValues; |
8 |
|
import android.content.Context; |
9 |
|
import android.database.Cursor; |
10 |
|
import android.database.sqlite.SQLiteDatabase; |
11 |
|
import android.database.sqlite.SQLiteOpenHelper; |
12 |
|
import android.database.sqlite.SQLiteQueryBuilder; |
13 |
|
import android.provider.BaseColumns; |
14 |
|
|
15 |
|
import com.dreamcatcher.bicycle.BicycleApp; |
16 |
|
import com.dreamcatcher.bicycle.vo.BicycleStationInfo; |
17 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 8 |
Complexity Density: 0,33 |
|
18 |
|
public class BicyclesSearchDatabase { |
19 |
|
private static final String DATABASE_NAME = "bicyclestation"; |
20 |
|
private static final String FTS_VIRTUAL_TABLE = "FTSbicyclestation"; |
21 |
|
private static final int DATABASE_VERSION = 2; |
22 |
|
|
23 |
|
private static final String BICYCLE_ID = "bicycle_id"; |
24 |
|
private static final String BICYCLE_NAME_PINYIN = "bicycle_name"; |
25 |
|
private static final HashMap<String,String> mColumnMap = buildColumnMap(); |
26 |
|
private final BicycleSearchOpenHelper mBicycleSearchOpenHelper; |
27 |
|
|
28 |
|
private static BicyclesSearchDatabase mInstance = null; |
29 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
30 |
0
|
public static BicyclesSearchDatabase getInstance(){... |
31 |
0
|
if(mInstance == null){ |
32 |
0
|
mInstance = new BicyclesSearchDatabase(); |
33 |
|
} |
34 |
0
|
return mInstance; |
35 |
|
} |
36 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
37 |
0
|
private BicyclesSearchDatabase(){... |
38 |
0
|
mBicycleSearchOpenHelper = new BicycleSearchOpenHelper(BicycleApp.getInstance()); |
39 |
|
} |
40 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0,14 |
|
41 |
0
|
private static HashMap<String,String> buildColumnMap() {... |
42 |
0
|
HashMap<String,String> map = new HashMap<String,String>(); |
43 |
0
|
map.put(BICYCLE_ID, BICYCLE_ID); |
44 |
0
|
map.put(BICYCLE_NAME_PINYIN, BICYCLE_NAME_PINYIN); |
45 |
0
|
map.put(BaseColumns._ID, "rowid AS " + |
46 |
|
BaseColumns._ID); |
47 |
0
|
map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " + |
48 |
|
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID); |
49 |
0
|
map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " + |
50 |
|
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID); |
51 |
0
|
return map; |
52 |
|
} |
53 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
54 |
0
|
public Cursor queryBicycleStationName(String query, String[] columns){... |
55 |
0
|
String selection = BICYCLE_NAME_PINYIN + " MATCH ?"; |
56 |
0
|
String[] selectionArgs = new String[] {query+"*"}; |
57 |
0
|
return query(selection, selectionArgs, columns); |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@param |
64 |
|
@param |
65 |
|
@param |
66 |
|
@return |
67 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0,3 |
|
68 |
0
|
private Cursor query(String selection, String[] selectionArgs, String[] columns) {... |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
0
|
SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); |
74 |
0
|
builder.setTables(FTS_VIRTUAL_TABLE); |
75 |
0
|
builder.setProjectionMap(mColumnMap); |
76 |
|
|
77 |
0
|
Cursor cursor = builder.query(mBicycleSearchOpenHelper.getReadableDatabase(), |
78 |
|
columns, selection, selectionArgs, null, null, null); |
79 |
|
|
80 |
0
|
if (cursor == null) { |
81 |
0
|
return null; |
82 |
0
|
} else if (!cursor.moveToFirst()) { |
83 |
0
|
cursor.close(); |
84 |
0
|
return null; |
85 |
|
} |
86 |
0
|
return cursor; |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 7 |
Complexity Density: 0,41 |
|
89 |
|
private static class BicycleSearchOpenHelper extends SQLiteOpenHelper{ |
90 |
|
private SQLiteDatabase mDatabase; |
91 |
|
private static final String FTS_TABLE_CREATE = |
92 |
|
"CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + |
93 |
|
" USING fts3 (" + |
94 |
|
BICYCLE_ID + ", " + |
95 |
|
BICYCLE_NAME_PINYIN + ");"; |
96 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
97 |
0
|
public BicycleSearchOpenHelper(Context context){... |
98 |
0
|
super(context, DATABASE_NAME, null, DATABASE_VERSION); |
99 |
|
} |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
101 |
0
|
@Override... |
102 |
|
public void onCreate(SQLiteDatabase db) { |
103 |
0
|
mDatabase = db; |
104 |
0
|
db.execSQL(FTS_TABLE_CREATE); |
105 |
0
|
addBicycleData(); |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
0
|
private void addBicycleData(){... |
109 |
0
|
new Thread(new Runnable() { |
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
110 |
0
|
public void run() {... |
111 |
0
|
ArrayList<BicycleStationInfo> arrayList = BicycleDataset.getInstance().getBicycleStationInfos(); |
112 |
0
|
for(int i = 0, n = arrayList.size(); i < n; i++){ |
113 |
0
|
BicycleStationInfo bicycleStationInfo = arrayList.get(i); |
114 |
0
|
int id = bicycleStationInfo.getId(); |
115 |
0
|
String name = bicycleStationInfo.getName(); |
116 |
0
|
addBicycleName(id, name); |
117 |
|
} |
118 |
|
} |
119 |
|
}).start(); |
120 |
|
} |
121 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
122 |
0
|
private long addBicycleName(int id, String name){... |
123 |
0
|
ContentValues contentValues = new ContentValues(); |
124 |
0
|
contentValues.put(BICYCLE_ID, id); |
125 |
0
|
contentValues.put(BICYCLE_NAME_PINYIN, name); |
126 |
0
|
return mDatabase.insert(FTS_VIRTUAL_TABLE, null, contentValues); |
127 |
|
} |
128 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
129 |
0
|
@Override... |
130 |
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
131 |
0
|
db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE); |
132 |
0
|
onCreate(db); |
133 |
|
} |
134 |
|
} |
135 |
|
} |