forked from omni/OmniCards
149 lines
4.5 KiB
Kotlin
149 lines
4.5 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, 5) {
|
|
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")
|
|
}
|
|
|
|
if (oldVersion < 3) {
|
|
db!!.execSQL("ALTER TABLE cards ADD COLUMN position INTEGER DEFAULT 0")
|
|
db.execSQL("ALTER TABLE cards ADD COLUMN iconPath TEXT")
|
|
db.execSQL("UPDATE cards SET position = id")
|
|
}
|
|
|
|
if (oldVersion < 4) {
|
|
db!!.execSQL("ALTER TABLE cards ADD COLUMN monochrome INTEGER DEFAULT 0")
|
|
}
|
|
|
|
if (oldVersion < 5) {
|
|
db!!.execSQL("ALTER TABLE cards DROP COLUMN iconPath")
|
|
db.execSQL("ALTER TABLE cards DROP COLUMN monochrome")
|
|
db.execSQL("ALTER TABLE cards DROP COLUMN position")
|
|
}
|
|
}
|
|
|
|
fun addCard(name: String, codeValue: String, codeType: BarcodeFormat, color: String?) {
|
|
val db = this.writableDatabase
|
|
|
|
val values = ContentValues()
|
|
|
|
values.put("name", name)
|
|
values.put("codeValue", codeValue)
|
|
values.put("codeType", codeType.name)
|
|
values.put("color", color)
|
|
|
|
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 ORDER BY id ASC", 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()
|
|
}
|
|
|
|
fun clearDatabase() {
|
|
val db = this.writableDatabase
|
|
|
|
db.execSQL("DELETE FROM cards")
|
|
}
|
|
|
|
fun addCards(cards: List<CardInfo>) {
|
|
val db = this.writableDatabase
|
|
|
|
for (card in cards) {
|
|
val values = ContentValues()
|
|
|
|
values.put("id", card.id)
|
|
values.put("name", card.name)
|
|
values.put("codeValue", card.codeValue)
|
|
values.put("codeType", card.codeType.name)
|
|
values.put("color", card.color)
|
|
|
|
db.insert("cards", null, values)
|
|
}
|
|
|
|
db.close()
|
|
}
|
|
} |