@@ -83,142 +83,68 @@ data class ReportApi(
8383}
8484
8585abstract class ApiTable (
86- val tableName : String
87- ) {
88- abstract fun fromCursor (it : Cursor ): IApi
89-
90- @SuppressLint(" Range" )
91- fun listAll (ctx : Context , where : String = ""): List <IApi > {
92-
93- val sql = " SELECT * FROM $tableName $where ORDER BY ${Db .COLUMN_DESC } "
94-
95- val ret: MutableList <IApi > = mutableListOf ()
96-
97- val cursor = Db .getInstance(ctx).readableDatabase.rawQuery(sql, null )
98-
99- cursor.use {
100- if (it.moveToFirst()) {
101- do {
102- ret + = fromCursor(it)
103- } while (it.moveToNext())
104- }
105- return ret
106- }
107- }
108-
109- abstract fun addNewRecord (ctx : Context , r : IApi ): Long
110- abstract fun addRecordWithId (ctx : Context , r : IApi )
111- abstract fun updateById (ctx : Context , id : Long , r : IApi ): Boolean
112-
113- fun deleteById (ctx : Context , id : Long ): Boolean {
114- val sql = " DELETE FROM $tableName WHERE ${Db .COLUMN_ID } = $id "
115- val cursor = Db .getInstance(ctx).writableDatabase.rawQuery(sql, null )
116-
117- return cursor.use {
118- it.moveToFirst()
119- }
120- }
121-
122- fun clearAll (ctx : Context ) {
123- val db = Db .getInstance(ctx).writableDatabase
124- val sql = " DELETE FROM $tableName "
125- db.execSQL(sql)
86+ tableName : String
87+ ) : BasicTable<IApi>(tableName) {
88+ abstract override fun fromCursor (cursor : Cursor ): IApi
89+ fun listAll (ctx : Context ): List <IApi > {
90+ return listAll(ctx, orderBy = Db .COLUMN_DESC )
12691 }
12792}
12893
12994class QueryApiTable : ApiTable (Db .TABLE_API_QUERY ) {
13095 @SuppressLint(" Range" )
131- override fun fromCursor (it : Cursor ): IApi {
96+ override fun fromCursor (cursor : Cursor ): IApi {
13297 val actionsConfig =
133- it .getStringOrNull(it .getColumnIndex(Db .COLUMN_ACTIONS )) ? : " "
98+ cursor .getStringOrNull(cursor .getColumnIndex(Db .COLUMN_ACTIONS )) ? : " "
13499 val actions = actionsConfig.parseActions()
135100
136101 return QueryApi (
137- id = it .getLong(it .getColumnIndex(Db .COLUMN_ID )),
138- desc = it .getStringOrNull(it .getColumnIndex(Db .COLUMN_DESC )) ? : " " ,
102+ id = cursor .getLong(cursor .getColumnIndex(Db .COLUMN_ID )),
103+ desc = cursor .getStringOrNull(cursor .getColumnIndex(Db .COLUMN_DESC )) ? : " " ,
139104 actions = actions,
140- enabled = it .getIntOrNull(it .getColumnIndex(Db .COLUMN_ENABLED )) == 1 ,
105+ enabled = cursor .getIntOrNull(cursor .getColumnIndex(Db .COLUMN_ENABLED )) == 1 ,
141106 )
142107 }
143108
144- override fun addNewRecord (ctx : Context , r : IApi ): Long {
145- val db = Db .getInstance(ctx).writableDatabase
146- val cv = ContentValues ()
147- cv.put(Db .COLUMN_DESC , r.desc)
148- cv.put(Db .COLUMN_ACTIONS , r.actions.serialize())
149- cv.put(Db .COLUMN_ENABLED , if (r.enabled) 1 else 0 )
150- return db.insert(tableName, null , cv)
151- }
152-
153- override fun addRecordWithId (ctx : Context , r : IApi ) {
154- val db = Db .getInstance(ctx).writableDatabase
155- val cv = ContentValues ()
156- cv.put(Db .COLUMN_ID , r.id)
157- cv.put(Db .COLUMN_DESC , r.desc)
158- cv.put(Db .COLUMN_ACTIONS , r.actions.serialize())
159- cv.put(Db .COLUMN_ENABLED , if (r.enabled) 1 else 0 )
160- db.insert(tableName, null , cv)
161- }
162-
163- override fun updateById (ctx : Context , id : Long , r : IApi ): Boolean {
164- val db = Db .getInstance(ctx).writableDatabase
109+ override fun toContentValues (item : IApi , includeId : Boolean ): ContentValues {
165110 val cv = ContentValues ()
166- cv.put(Db .COLUMN_DESC , r.desc)
167- cv.put(Db .COLUMN_ACTIONS , r.actions.serialize())
168- cv.put(Db .COLUMN_ENABLED , if (r.enabled) 1 else 0 )
169-
170- return db.update(tableName, cv, " ${Db .COLUMN_ID } = $id " , null ) >= 0
111+ if (includeId) {
112+ cv.put(Db .COLUMN_ID , item.id)
113+ }
114+ cv.put(Db .COLUMN_DESC , item.desc)
115+ cv.put(Db .COLUMN_ACTIONS , item.actions.serialize())
116+ cv.put(Db .COLUMN_ENABLED , if (item.enabled) 1 else 0 )
117+ return cv
171118 }
172119}
173120
174121class ReportApiTable : ApiTable (Db .TABLE_API_REPORT ) {
175122 @SuppressLint(" Range" )
176- override fun fromCursor (it : Cursor ): IApi {
123+ override fun fromCursor (cursor : Cursor ): IApi {
177124 val actionsConfig =
178- it .getStringOrNull(it .getColumnIndex(Db .COLUMN_ACTIONS )) ? : " "
125+ cursor .getStringOrNull(cursor .getColumnIndex(Db .COLUMN_ACTIONS )) ? : " "
179126 val actions = actionsConfig.parseActions()
180127
181128 return ReportApi (
182- id = it .getLong(it .getColumnIndex(Db .COLUMN_ID )),
183- desc = it .getStringOrNull(it .getColumnIndex(Db .COLUMN_DESC )) ? : " " ,
129+ id = cursor .getLong(cursor .getColumnIndex(Db .COLUMN_ID )),
130+ desc = cursor .getStringOrNull(cursor .getColumnIndex(Db .COLUMN_DESC )) ? : " " ,
184131 actions = actions,
185- enabled = it .getIntOrNull(it .getColumnIndex(Db .COLUMN_ENABLED )) == 1 ,
132+ enabled = cursor .getIntOrNull(cursor .getColumnIndex(Db .COLUMN_ENABLED )) == 1 ,
186133 // `true` if `1` or `null`(old version doesn't have this field)
187- autoReportTypes = it .getIntOrNull(it .getColumnIndex(Db .COLUMN_AUTO_REPORT_TYPES )) ? : AutoReportTypes .DefaultTypes
134+ autoReportTypes = cursor .getIntOrNull(cursor .getColumnIndex(Db .COLUMN_AUTO_REPORT_TYPES )) ? : AutoReportTypes .DefaultTypes
188135 )
189136 }
190137
191- override fun addNewRecord (ctx : Context , r : IApi ): Long {
192- val db = Db .getInstance(ctx).writableDatabase
193- val cv = ContentValues ()
194- cv.put(Db .COLUMN_DESC , r.desc)
195- cv.put(Db .COLUMN_ACTIONS , r.actions.serialize())
196- cv.put(Db .COLUMN_ENABLED , if (r.enabled) 1 else 0 )
197- val rr = r as ReportApi
198- cv.put(Db .COLUMN_AUTO_REPORT_TYPES , rr.autoReportTypes)
199- return db.insert(tableName, null , cv)
200- }
201-
202- override fun addRecordWithId (ctx : Context , r : IApi ) {
203- val db = Db .getInstance(ctx).writableDatabase
204- val cv = ContentValues ()
205- cv.put(Db .COLUMN_ID , r.id)
206- cv.put(Db .COLUMN_DESC , r.desc)
207- cv.put(Db .COLUMN_ACTIONS , r.actions.serialize())
208- cv.put(Db .COLUMN_ENABLED , if (r.enabled) 1 else 0 )
209- val rr = r as ReportApi
210- cv.put(Db .COLUMN_AUTO_REPORT_TYPES , rr.autoReportTypes)
211- db.insert(tableName, null , cv)
212- }
213-
214- override fun updateById (ctx : Context , id : Long , r : IApi ): Boolean {
215- val db = Db .getInstance(ctx).writableDatabase
138+ override fun toContentValues (item : IApi , includeId : Boolean ): ContentValues {
216139 val cv = ContentValues ()
217- cv.put(Db .COLUMN_DESC , r.desc)
218- cv.put(Db .COLUMN_ACTIONS , r.actions.serialize())
219- cv.put(Db .COLUMN_ENABLED , if (r.enabled) 1 else 0 )
220- val rr = r as ReportApi
140+ if (includeId) {
141+ cv.put(Db .COLUMN_ID , item.id)
142+ }
143+ cv.put(Db .COLUMN_DESC , item.desc)
144+ cv.put(Db .COLUMN_ACTIONS , item.actions.serialize())
145+ cv.put(Db .COLUMN_ENABLED , if (item.enabled) 1 else 0 )
146+ val rr = item as ReportApi
221147 cv.put(Db .COLUMN_AUTO_REPORT_TYPES , rr.autoReportTypes)
222- return db.update(tableName, cv, " ${ Db . COLUMN_ID } = $id " , null ) >= 0
148+ return cv
223149 }
224- }
150+ }
0 commit comments