· 8 years ago · May 29, 2018, 02:16 PM
1package my.dzramats.iotsecurityscanner.db;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9public class Database extends SQLiteOpenHelper {
10
11 public static final String DATABASE_NAME = "IoTSecurityScanner";
12 private static final int DATABASE_VERSION = 2;
13 private static final String OUI_TABLE = "ouis";
14 private static final String PORT_TABLE = "ports";
15 private static final String MAC_FIELD = "mac";
16 private static final String VENDOR_FIELD = "vendor";
17 private static final String PORT_FIELD = "port";
18 private static final String DESCRIPTION_FIELD = "description";
19 private static final String CREATE_OUI_TABLE = "CREATE TABLE " + OUI_TABLE + " (" + MAC_FIELD + " TEXT NOT NULL, " + VENDOR_FIELD + " TEXT NOT NULL);";
20 private static final String CREATE_PORT_TABLE = "CREATE TABLE " + PORT_TABLE + " (" + PORT_FIELD + " INTEGER NOT NULL, " + DESCRIPTION_FIELD + " TEXT);";
21 private static final String CREATE_PORT_INDEX = "CREATE INDEX IF NOT EXISTS idx_ports_port ON " + PORT_TABLE + " (" + PORT_FIELD + ");";
22 private static final String CREATE_MAC_INDEX = "CREATE INDEX IF NOT EXISTS idx_ouis_mac ON " + OUI_TABLE + " (" + MAC_FIELD + ");";
23
24 private static Database singleton;
25 private SQLiteDatabase db;
26
27 /**
28 * Atgriež šīs klases vienoto eksemplÄru vai izveido vienu, ja tas vÄ“l neeksistÄ“.
29 *
30 * @param context
31 * @return
32 */
33 public static Database getInstance(Context context) {
34 if (singleton == null) {
35 singleton = new Database(context);
36 }
37
38 return singleton;
39 }
40
41 /**
42 * Izveido datu bÄzi un atgriež tai handleri
43 *
44 * @param context
45 */
46 private Database(Context context) {
47 super(context, DATABASE_NAME, null, DATABASE_VERSION);
48 db = this.getWritableDatabase();
49 }
50
51 /**
52 * Tiek uzsÄkta transakcija, kas ļauj vairÄkiem lasÄ«tÄjiem un vienam rakstÄ«tÄjam
53 *
54 * @return
55 */
56 public Database beginTransaction() {
57 db.beginTransactionNonExclusive();
58 return this;
59 }
60
61 /**
62 * Pabeidz transakciju
63 *
64 * @return
65 */
66 public Database endTransaction() {
67 db.endTransaction();
68 return this;
69 }
70
71 /**
72 * NorÄda transakciju kÄ veiksmÄ«gu un veic transakciju
73 *
74 * @return
75 */
76 public Database setTransactionSuccessful() {
77 db.setTransactionSuccessful();
78 return this;
79 }
80
81 /**
82 * Funkcija tiek izsaukta, kad datubÄze nepastÄv un nepiecieÅ¡ama tÄs shÄ“mas izveide.
83 *
84 * @param db
85 */
86 @Override
87 public void onCreate(final SQLiteDatabase db) {
88 db.execSQL(CREATE_OUI_TABLE);
89 db.execSQL(CREATE_PORT_TABLE);
90 db.execSQL(CREATE_PORT_INDEX);
91 db.execSQL(CREATE_MAC_INDEX);
92 }
93
94 /**
95 * PÄrvalda datubÄzes atjauninÄjumus
96 *
97 * @param db
98 * @param oldVersion
99 * @param newVersion
100 */
101 @Override
102 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
103 switch (oldVersion) {
104
105 // Indexes weren't initially created on the first iteration of the schema.
106 case 1:
107 db.execSQL(CREATE_PORT_INDEX);
108 db.execSQL(CREATE_MAC_INDEX);
109 }
110 }
111
112 /**
113 *Ievieto jaunu OUI ierakstu, kurÄ ir MAC adrese un tÄ saistÄ«tais piegÄdÄtÄjs.
114 *
115 * @param mac
116 * @param vendor
117 * @return
118 */
119 public long insertOui(String mac, String vendor) {
120 ContentValues values = new ContentValues();
121 values.put(MAC_FIELD, mac);
122 values.put(VENDOR_FIELD, vendor);
123
124 return db.insert(OUI_TABLE, null, values);
125 }
126
127 /**
128 *Ievieto jaunu portu ar porta numuru un ar to saistīto aprakstu.
129 *
130 * @param port
131 * @param description
132 * @return
133 */
134 public long insertPort(String port, String description) {
135 ContentValues values = new ContentValues();
136 values.put(PORT_FIELD, port);
137 values.put(DESCRIPTION_FIELD, description);
138
139 return db.insert(PORT_TABLE, null, values);
140 }
141
142 /**
143 * NotÄ«ra visus OUI, kas Å¡obrÄ«d ir datu bÄzÄ“.
144 *
145 * @return
146 */
147 public Database clearOuis() {
148 db.execSQL("DELETE FROM " + OUI_TABLE);
149 db.execSQL("VACUUM");
150 return this;
151 }
152
153 /**
154 * Noslauka visas pieslÄ“gvietas, kuras paÅ¡laik atrodas datu bÄzÄ“.
155 *
156 * @return
157 */
158 public Database clearPorts() {
159 db.execSQL("DELETE FROM " + PORT_TABLE);
160 db.execSQL("VACUUM");
161 return this;
162 }
163
164 /**
165 * MeklÄ“ piegÄdÄtÄja nosaukumu, pamatojoties uz sniegto MAC adresi..
166 *
167 * @param mac
168 * @return
169 */
170 public String selectVendor(String mac) {
171 Cursor cursor = db.rawQuery("SELECT " + VENDOR_FIELD + " FROM " + OUI_TABLE + " WHERE " + MAC_FIELD + " = ?", new String[]{mac});
172 String vendor;
173 if (cursor.moveToFirst()) {
174 vendor = cursor.getString(cursor.getColumnIndex("vendor"));
175 } else {
176 vendor = "Vendor not in database";
177 }
178
179 cursor.close();
180
181 return vendor;
182 }
183
184 /**
185 * Meklē pieslēgvietas aprakstu, pamatojoties uz sniegto pieslēgvietu.
186 *
187 * @param port
188 * @return
189 */
190 public String selectPortDescription(String port) {
191 Cursor cursor = db.rawQuery("SELECT " + DESCRIPTION_FIELD + " FROM " + PORT_TABLE + " WHERE " + PORT_FIELD + " = ?", new String[]{port});
192 String name = "";
193 if (cursor.moveToFirst()) {
194 name = cursor.getString(cursor.getColumnIndex(DESCRIPTION_FIELD));
195 }
196
197 cursor.close();
198
199 return name;
200 }
201
202}