DbHelper
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.patientreview.Model.AdminInfo
import com.patientreview.Model.PatientReview
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
class DBHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VARSION) {
companion object {
//---------- database information ----------- val DATABASE_NAME = "patient_review.db" val DATABASE_VARSION = 1 }
//--------- all tables ------------------ val PATIENT_REVIEW_TABLE = "patient_review_table" val ADMIN_TABLE = "admin_table" //--------- patient review table field----------- val PATIENT_ID = "patient_id" val PATIENT_NAME = "patient_name" val PATIENT_EMAIL = "patient_email" val PATIENT_RATE = "patient_rate" val PATIENT_REVIEW = "patient_review" val DATE = "date" //--------- admin table field --------------- val ADMIN_ID = "admin_id" val ADMIN_NAME = "admin_name" val ADMIN_EMAIL = "admin_email" val ADMIN_PASSWORD = "admin_password" val ADMIN_SECURITY_KEY = "admin_security_key" //----------- create table query ------------- val CREATE_PATIENT_REVIEW_TABLE_QUERY = "create table if not exists $PATIENT_REVIEW_TABLE ($PATIENT_ID integer primary key,$PATIENT_NAME text, $PATIENT_EMAIL text,$PATIENT_RATE integer,$PATIENT_REVIEW text,$DATE text)" val CREATE_ADMIN_TABLE_QUERY = "create table if not exists $ADMIN_TABLE ($ADMIN_ID integer primary key,$ADMIN_NAME text, $ADMIN_EMAIL text, $ADMIN_PASSWORD text,$ADMIN_SECURITY_KEY text)" val GET_ADMIN_INFO_QUERY = "select * from $ADMIN_TABLE where $ADMIN_ID=1" val GET_PATIENT_REVIEW_LIST_QUERY = "select * from $PATIENT_REVIEW_TABLE" override fun onCreate(db: SQLiteDatabase) {
// TODO Auto-generated method stub
//---------- execute create all table --------- db.execSQL(CREATE_PATIENT_REVIEW_TABLE_QUERY)
db.execSQL(CREATE_ADMIN_TABLE_QUERY)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
// TODO Auto-generated method stub
}
fun getAdminInfo():AdminInfo
{
var adminInfo = AdminInfo ()
val readableDataBase =this.readableDatabase val cursor = readableDataBase.rawQuery(GET_ADMIN_INFO_QUERY, null)
cursor.moveToFirst()
adminInfo.Name = cursor.getString(cursor.getColumnIndex(ADMIN_NAME))
adminInfo.Email = cursor.getString(cursor.getColumnIndex(ADMIN_EMAIL))
adminInfo.Password = cursor.getString(cursor.getColumnIndex(ADMIN_PASSWORD))
adminInfo.SecurityKey = cursor.getString(cursor.getColumnIndex(ADMIN_SECURITY_KEY))
return adminInfo
}
fun updateAdminInfo(pasword:String): Int? {
val writableDatabase = this.writableDatabase var contentValues = ContentValues()
//contentValues.put(ADMIN_NAME, adminInfo.Name) //contentValues.put(ADMIN_EMAIL, adminInfo.Email) contentValues.put(ADMIN_PASSWORD, pasword)
//contentValues.put(ADMIN_SECURITY_KEY, adminInfo.SecurityKey)
return writableDatabase.update(ADMIN_TABLE, contentValues, "$ADMIN_ID = 1 ",null)
}
fun getPatientReviewList():ArrayList<PatientReview>
{
var patientReviewList = ArrayList<PatientReview> ()
val readableDataBase =this.readableDatabase val cursor = readableDataBase.rawQuery(GET_PATIENT_REVIEW_LIST_QUERY, null)
cursor.moveToFirst()
while (!cursor.isAfterLast)
{
var patientReview = PatientReview()
patientReview.Id = cursor.getString(cursor.getColumnIndex(PATIENT_ID))
patientReview.Name = cursor.getString(cursor.getColumnIndex(PATIENT_NAME))
patientReview.Email = cursor.getString(cursor.getColumnIndex(PATIENT_EMAIL))
patientReview.Rate = cursor.getString(cursor.getColumnIndex(PATIENT_RATE))
patientReview.Review = cursor.getString(cursor.getColumnIndex(PATIENT_REVIEW))
patientReview.Date = cursor.getString(cursor.getColumnIndex(DATE))
patientReviewList.add(patientReview)
cursor.moveToNext()
}
return patientReviewList
}
fun getPatientReviewItem(id: String):PatientReview
{
val readableDataBase =this.readableDatabase var getPatientReviewItem:String = "select * from $PATIENT_REVIEW_TABLE where $PATIENT_ID=$id" val cursor = readableDataBase.rawQuery(getPatientReviewItem, null)
cursor.moveToFirst()
var patientReview = PatientReview()
patientReview.Id = cursor.getString(cursor.getColumnIndex(PATIENT_ID))
patientReview.Name = cursor.getString(cursor.getColumnIndex(PATIENT_NAME))
patientReview.Email = cursor.getString(cursor.getColumnIndex(PATIENT_EMAIL))
patientReview.Rate = cursor.getString(cursor.getColumnIndex(PATIENT_RATE))
patientReview.Review = cursor.getString(cursor.getColumnIndex(PATIENT_REVIEW))
patientReview.Date = cursor.getString(cursor.getColumnIndex(DATE))
return patientReview
}
fun updatePatientReview(patientReviewRequest: PatientReview): Int? {
val db = this.writableDatabase val contentValues = ContentValues()
contentValues.put(PATIENT_NAME, patientReviewRequest.Name)
contentValues.put(PATIENT_EMAIL, patientReviewRequest.Email)
contentValues.put(PATIENT_RATE, patientReviewRequest.Rate)
contentValues.put(PATIENT_REVIEW, patientReviewRequest.Review)
return db.update(PATIENT_REVIEW_TABLE, contentValues, PATIENT_ID + " = ? ", arrayOf(patientReviewRequest.Id))
}
fun deletePatientReview(id: String): Int? {
val db = this.writableDatabase return db.delete(PATIENT_REVIEW_TABLE,
PATIENT_ID + " = ? ",
arrayOf(id))
}
fun insertToPatientReview(patientReviewRequest: PatientReview):Long?
{
val writableDatabase = this.writableDatabase var contentValues = ContentValues()
contentValues.put(PATIENT_NAME, patientReviewRequest.Name)
contentValues.put(PATIENT_EMAIL, patientReviewRequest.Email)
contentValues.put(PATIENT_RATE, patientReviewRequest.Rate)
contentValues.put(PATIENT_REVIEW, patientReviewRequest.Review)
contentValues.put(DATE,getDateTime())
return writableDatabase.insert(PATIENT_REVIEW_TABLE, null, contentValues)
}
fun insertToAdminInfo(adminInfo: AdminInfo):Long?
{
val writableDatabase = this.writableDatabase var contentValues = ContentValues()
contentValues.put(ADMIN_NAME, adminInfo.Name)
contentValues.put(ADMIN_EMAIL, adminInfo.Email)
contentValues.put(ADMIN_PASSWORD, adminInfo.Password)
contentValues.put(ADMIN_SECURITY_KEY, adminInfo.SecurityKey)
return writableDatabase.insert(ADMIN_TABLE, null, contentValues)
}
private fun getDateTime(): String {
val dateFormat = SimpleDateFormat("yyyy/MMM/dd HH:mm:ss")
val date = Date()
return dateFormat.format(date)
}
}
Comments
Post a Comment