create some helper functions for handling operation items in the database

This commit is contained in:
tibbi 2017-07-26 22:14:28 +02:00
parent ce3934612c
commit d14a64d744
3 changed files with 44 additions and 6 deletions

View File

@ -124,3 +124,6 @@ val SOURCE_IMPORTED_ICS = 2
// Google Sync
val PRIMARY = "primary"
val POPUP = "popup"
val OPERATION_INSERT = 1
val OPERATION_UPDATE = 2
val OPERATION_DELETE = 3

View File

@ -1,20 +1,20 @@
package com.simplemobiletools.calendar.helpers
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.simplemobiletools.calendar.models.GoogleOperation
import com.simplemobiletools.commons.extensions.getIntValue
// database for storing operations performed on google events locally, while the user was offline
class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
private val MAIN_TABLE_NAME = "operations"
private val OPERATIONS_TABLE_NAME = "operations"
private val COL_ID = "id"
private val COL_EVENT_ID = "event_id"
private val COL_OPERATION = "operation"
private val OPERATION_INSERT = 1
private val OPERATION_UPDATE = 2
private val OPERATION_DELETE = 3
private val mDb: SQLiteDatabase = writableDatabase
companion object {
@ -31,10 +31,42 @@ class GoogleSyncQueueDB private constructor(val context: Context) : SQLiteOpenHe
}
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_EVENT_ID INTEGER, $COL_OPERATION INTEGER)")
db.execSQL("CREATE TABLE $OPERATIONS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_EVENT_ID INTEGER, $COL_OPERATION INTEGER)")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
}
fun insert(operation: Int, eventId: Int) {
val contentValues = ContentValues().apply {
put(COL_OPERATION, operation)
put(COL_EVENT_ID, eventId)
}
mDb.insert(OPERATIONS_TABLE_NAME, null, contentValues)
}
fun getOperations(): ArrayList<GoogleOperation> {
val operations = ArrayList<GoogleOperation>()
val projection = arrayOf(COL_EVENT_ID, COL_OPERATION)
var cursor: Cursor? = null
try {
cursor = mDb.query(OPERATIONS_TABLE_NAME, projection, null, null, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val eventId = cursor.getIntValue(COL_EVENT_ID)
val operation = cursor.getIntValue(COL_OPERATION)
operations.add(GoogleOperation(eventId, operation))
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
return operations
}
fun delete(eventId: Int) {
val selection = "$COL_EVENT_ID = $eventId"
mDb.delete(OPERATIONS_TABLE_NAME, selection, null)
}
}

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.calendar.models
data class GoogleOperation(val eventId: Int, val operation: Int)