· 8 years ago · Jan 02, 2018, 09:36 PM
1package com.example.svetoslavkochev.fastcheckout.database
2
3import android.content.ContentValues
4import android.content.Context
5import android.database.Cursor
6import android.database.SQLException
7import android.database.sqlite.SQLiteDatabase
8import android.database.sqlite.SQLiteOpenHelper
9import android.util.Log
10
11private val DATABASE_NAME = "World"
12private val SQLITE_TABLE = "Country"
13private val DATABASE_VERSION = 1
14
15class ProductsDbAdapter(private val mCtx: Context) {
16 private var mDbHelper: DatabaseHelper? = null
17 private var mDb: SQLiteDatabase? = null
18
19 private class DatabaseHelper internal constructor(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
20 override fun onCreate(db: SQLiteDatabase) {
21 Log.w(TAG, DATABASE_CREATE)
22 db.execSQL(DATABASE_CREATE)
23 }
24
25 override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
26 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
27 + newVersion + ", which will destroy all old data")
28 db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE)
29 onCreate(db)
30 }
31 }
32
33// @Throws(SQLException::class)
34 fun open(): ProductsDbAdapter {
35 mDbHelper = DatabaseHelper(mCtx)
36 mDb = mDbHelper!!.writableDatabase
37 return this
38 }
39
40 fun close() {
41 if (mDbHelper != null) {
42 mDbHelper!!.close()
43 }
44 }
45
46// fun createCountry(code: String, name: String,
47// continent: String, region: String): Long {
48//
49// val initialValues = ContentValues()
50// initialValues.put(KEY_CODE, code)
51// initialValues.put(KEY_NAME, name)
52// initialValues.put(KEY_CONTINENT, continent)
53// initialValues.put(KEY_REGION, region)
54//
55// return mDb!!.insert(SQLITE_TABLE, null, initialValues)
56// }
57
58 fun deleteAllProducts(): Boolean {
59
60 var doneDelete = 0
61 doneDelete = mDb!!.delete(SQLITE_TABLE, null, null)
62 Log.w(TAG, Integer.toString(doneDelete))
63 return doneDelete > 0
64
65 }
66
67 @Throws(SQLException::class)
68 fun fetchProductsByName(inputText: String?): Cursor? {
69 Log.w(TAG, inputText)
70 var mCursor: Cursor? = null
71 mCursor = if (inputText == null || inputText.isEmpty()) {
72 mDb!!.query(SQLITE_TABLE, arrayOf(KEY_ROWID, KEY_BARCODE, KEY_NAME, KEY_LOCATION, KEY_DESCRIPTION, KEY_CURRENCY, KEY_IMAGE, KEY_PRICE, KEY_QUANTITY),
73 null, null, null, null, null)
74 } else {
75 mDb!!.query(true, SQLITE_TABLE, arrayOf(KEY_ROWID, KEY_BARCODE, KEY_NAME, KEY_LOCATION, KEY_DESCRIPTION, KEY_CURRENCY, KEY_IMAGE, KEY_PRICE, KEY_QUANTITY),
76 "$KEY_NAME like '%$inputText%'", null, null, null, null, null)
77 }
78 if (mCursor != null) {
79 mCursor.moveToFirst()
80 }
81 return mCursor
82 }
83
84 fun fetchAllCountries(): Cursor? {
85 val mCursor = mDb!!.query(SQLITE_TABLE, arrayOf(KEY_ROWID, KEY_BARCODE, KEY_NAME, KEY_LOCATION, KEY_DESCRIPTION, KEY_CURRENCY, KEY_IMAGE, KEY_PRICE, KEY_QUANTITY),
86 null, null, null, null, null)
87
88 mCursor?.moveToFirst()
89 return mCursor
90 }
91
92 fun insertSomeCountries() {
93// createCountry("AFG", "Afghanistan", "Asia", "Southern and Central Asia")
94// createCountry("ALB", "Albania", "Europe", "Southern Europe")
95// createCountry("DZA", "Algeria", "Africa", "Northern Africa")
96// createCountry("ASM", "American Samoa", "Oceania", "Polynesia")
97// createCountry("AND", "Andorra", "Europe", "Southern Europe")
98// createCountry("AGO", "Angola", "Africa", "Central Africa")
99// createCountry("AIA", "Anguilla", "North America", "Caribbean")
100 }
101
102 companion object {
103 private val KEY_ROWID = "_id"
104 private val KEY_BARCODE = "barcode"
105 private val KEY_NAME = "name"
106 private val KEY_DESCRIPTION = "description"
107 private val KEY_PRICE = "price"
108 private val KEY_CURRENCY = "currency"
109 private val KEY_LOCATION = "location"
110 private val KEY_QUANTITY = "quantity"
111 private val KEY_IMAGE = "image"
112
113 private val TAG = "CountriesDbAdapter"
114
115 private val DATABASE_CREATE = "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
116 KEY_ROWID + " integer PRIMARY KEY autoincrement," +
117 KEY_BARCODE + "," +
118 KEY_NAME + "," +
119 KEY_DESCRIPTION + "," +
120 KEY_PRICE + "," +
121 KEY_CURRENCY + "," +
122 KEY_LOCATION + "," +
123 KEY_QUANTITY + "," +
124 KEY_IMAGE + "," +
125 " UNIQUE (" + KEY_BARCODE + "));"
126 }
127}