109 lines
3.3 KiB
Kotlin
109 lines
3.3 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.content.ContentValues
|
|
import android.content.Context
|
|
import android.database.sqlite.SQLiteDatabase
|
|
import android.database.sqlite.SQLiteOpenHelper
|
|
import com.google.zxing.BarcodeFormat
|
|
|
|
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 2) {
|
|
override fun onCreate(db: SQLiteDatabase?) {
|
|
db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT)")
|
|
}
|
|
|
|
override fun onUpgrade(
|
|
db: SQLiteDatabase?,
|
|
oldVersion: Int,
|
|
p2: Int
|
|
) {
|
|
if (oldVersion < 2) {
|
|
db!!.execSQL("ALTER TABLE cards ADD COLUMN color TEXT")
|
|
}
|
|
}
|
|
|
|
fun addCard(name: String, codeValue: String, codeType: BarcodeFormat, color: String?) {
|
|
val values = ContentValues()
|
|
|
|
values.put("name", name)
|
|
values.put("codeValue", codeValue)
|
|
values.put("codeType", codeType.name)
|
|
values.put("color", color)
|
|
|
|
val db = this.writableDatabase
|
|
|
|
db.insert("cards", null, values)
|
|
|
|
db.close()
|
|
}
|
|
|
|
fun editCard(id: Int, name: String, codeValue: String, codeType: BarcodeFormat, color: String?) {
|
|
val values = ContentValues()
|
|
|
|
values.put("name", name)
|
|
values.put("codeValue", codeValue)
|
|
values.put("codeType", codeType.name)
|
|
values.put("color", color)
|
|
|
|
val db = this.writableDatabase
|
|
|
|
db.update("cards", values, "id = ?", arrayOf(id.toString()))
|
|
|
|
db.close()
|
|
}
|
|
|
|
fun getCards(): List<CardInfo> {
|
|
val db = this.readableDatabase
|
|
|
|
val cursor = db.rawQuery("SELECT * FROM cards", null)
|
|
val cards = mutableListOf<CardInfo>()
|
|
|
|
while (cursor.moveToNext()) {
|
|
cards.add(CardInfo(
|
|
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
|
name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
|
|
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
|
|
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
|
|
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
|
|
))
|
|
}
|
|
|
|
cursor.close()
|
|
db.close()
|
|
|
|
return cards
|
|
}
|
|
|
|
fun getCard(id: Int): CardInfo? {
|
|
val db = this.readableDatabase
|
|
|
|
val cursor = db.rawQuery("SELECT * FROM cards WHERE id = ?", arrayOf(id.toString()))
|
|
|
|
if (!cursor.moveToFirst()) {
|
|
cursor.close()
|
|
db.close()
|
|
|
|
return null
|
|
}
|
|
|
|
val cardInfo = CardInfo(
|
|
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
|
name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
|
|
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
|
|
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
|
|
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
|
|
)
|
|
|
|
cursor.close()
|
|
db.close()
|
|
|
|
return cardInfo
|
|
}
|
|
|
|
fun removeCard(id: Int) {
|
|
val db = this.writableDatabase
|
|
|
|
db.delete("cards", "id = ?", arrayOf(id.toString()))
|
|
|
|
db.close()
|
|
}
|
|
} |