|
| 1 | +package org.wisdomrider.lazylibrarydemo.sqlite |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.os.Bundle |
| 5 | +import kotlinx.android.synthetic.main.activity_add_or_update.* |
| 6 | +import kotlinx.android.synthetic.main.dialog.* |
| 7 | +import org.wisdomrider.lazylibrary.LazyBase |
| 8 | +import org.wisdomrider.lazylibrary.lazyMap |
| 9 | +import org.wisdomrider.lazylibrary.modules.sqlite.SQLITECONSTANTS.AND |
| 10 | +import org.wisdomrider.lazylibrary.modules.sqlite.SQLITECONSTANTS.OR |
| 11 | +import org.wisdomrider.lazylibrary.modules.sqlite.insert |
| 12 | +import org.wisdomrider.lazylibrary.modules.sqlite.update |
| 13 | +import org.wisdomrider.lazylibrary.modules.sqlite.updateTable |
| 14 | +import org.wisdomrider.lazylibrary.modules.sqlite.where |
| 15 | +import org.wisdomrider.lazylibrary.modules.toast |
| 16 | +import org.wisdomrider.lazylibrarydemo.R |
| 17 | +import org.wisdomrider.lazylibrarydemo.utils.BOOK |
| 18 | +import org.wisdomrider.lazylibrarydemo.utils.IS_CALLED_FROM_UPDATE |
| 19 | + |
| 20 | +class AddOrUpdateActivity : LazyBase() { |
| 21 | + var iscalledFromUpdate: Boolean = false |
| 22 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 23 | + super.onCreate(savedInstanceState) |
| 24 | + setContentView(R.layout.activity_add_or_update) |
| 25 | + supportActionBar!!.title = "Add or update Books" |
| 26 | + supportActionBar!!.setDisplayHomeAsUpEnabled(true) |
| 27 | + var bundle = intent.extras |
| 28 | + iscalledFromUpdate = bundle[IS_CALLED_FROM_UPDATE] as Boolean |
| 29 | + |
| 30 | + if(iscalledFromUpdate) { |
| 31 | + var books = intent.extras[BOOK] as Books |
| 32 | + et_book_name.setText(books.name) |
| 33 | + et_book_author.setText(books.author) |
| 34 | + et_book_price.setText(books.price.toString()) |
| 35 | + et_book_description.setText(books.description) |
| 36 | + } |
| 37 | + |
| 38 | + btn_insert_or_update.setOnClickListener { |
| 39 | + var bookName = et_book_name.text.toString() |
| 40 | + var bookAuthor = et_book_author.text.toString() |
| 41 | + var bookPrice = et_book_price.text.toString().toDouble() |
| 42 | + var bookDescription = et_book_description.text.toString() |
| 43 | + if (!iscalledFromUpdate) { |
| 44 | + // set property on Book object |
| 45 | + var books = Books( |
| 46 | + name = bookName, |
| 47 | + price = bookPrice, |
| 48 | + author = bookAuthor, |
| 49 | + description = bookDescription |
| 50 | + ) |
| 51 | + addBooksOnsqliteDatabase(books) |
| 52 | + } else { |
| 53 | + var books = intent.extras[BOOK] as Books |
| 54 | + books.name = bookName |
| 55 | + books.author = bookAuthor |
| 56 | + books.price = bookPrice |
| 57 | + books.description = bookDescription |
| 58 | + // books.updateTable().lazy() |
| 59 | + books |
| 60 | + .update(type = OR, condition = lazyMap("id" to books.id |
| 61 | + |
| 62 | + ) |
| 63 | + , autoInsert = false |
| 64 | + ).lazy() |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + "Update Sucessfully".toast().lazy() |
| 69 | + var intent = Intent(this, SqliteActivity::class.java) |
| 70 | + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) |
| 71 | + startActivity(intent) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private fun addBooksOnsqliteDatabase(books: Books) { |
| 78 | + books.insert().lazy() |
| 79 | + "Insert sucessfully".toast().lazy() |
| 80 | + var intent = Intent(this, SqliteActivity::class.java) |
| 81 | + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) |
| 82 | + startActivity(intent) |
| 83 | + /* Books().createTable().lazy() |
| 84 | + Books().removeAll().lazy() |
| 85 | + Books("a2x", "'Book2", 10, 21312).insert().lazy() |
| 86 | +
|
| 87 | +
|
| 88 | + Books("a1x", "'Book2", 10, 21312) |
| 89 | + .update(type = OR, condition = lazyMap("id" to "a1x", "price" to 10) |
| 90 | + , autoInsert = false |
| 91 | + ).lazy() |
| 92 | +
|
| 93 | +
|
| 94 | + Books().where(type = AND, condition = lazyMap("id" to "a1x", "price" to 10)) { |
| 95 | + Log.e("UPDATE", "A") |
| 96 | + }.lazy() |
| 97 | +
|
| 98 | + Books().delete(type = AND, condition = lazyMap("id" to "a1x", "price" to 10)).lazy() |
| 99 | +
|
| 100 | + "select * from Books".rawQuery { |
| 101 | + Log.e("CURSOR",it.toString()); |
| 102 | + }.lazy()*/ |
| 103 | + |
| 104 | + } |
| 105 | +} |
0 commit comments