Advertisement
okardec

db.tk

Sep 22nd, 2020
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.22 KB | None | 0 0
  1. package core
  2.  
  3. import org.sqlite.SQLiteConfig
  4. import java.sql.Connection
  5. import java.sql.DriverManager
  6. import java.sql.SQLException
  7. import java.sql.Statement
  8.  
  9.  
  10. class DB {
  11.  
  12.     private val tableName = "tableTeste"
  13.     private val primaryKey = "id"
  14.  
  15.     private val JDBC_URL = "jdbc:sqlite:C:\\testes\\db_teste.db"
  16.     private lateinit var con: Connection ///DriverManager.getConnection("jdbc:sqlite:C:\\testes\\db_teste.db")
  17.     private lateinit var stmt: Statement;
  18.  
  19.     val fields = mutableMapOf<String, Any>()
  20.  
  21.     fun addField(fieldName: String, fieldValue: String){
  22.         fields.put(fieldName, fieldValue)
  23.     }
  24.     fun addField(fieldName: String, fieldValue: Int){
  25.         fields.put(fieldName, fieldValue)
  26.     }
  27.     fun getField(fieldName: String): Any? {
  28.         return fields.get(fieldName)
  29.     }
  30.  
  31.  
  32.     constructor(){
  33.         openConnection()
  34.     }
  35.  
  36.     protected fun finalize() {
  37.         closeConnection()
  38.     }
  39.  
  40.  
  41.  
  42.     private fun openConnection() {
  43.         //if (con==null) {
  44.             val config = SQLiteConfig()
  45.             config.enforceForeignKeys(true)
  46.             con = DriverManager.getConnection(JDBC_URL, config.toProperties())
  47.  
  48.             stmt = con.createStatement()
  49.         ///}
  50.     }
  51.  
  52.     fun closeConnection() {
  53.         con.close()
  54.     }
  55.  
  56.  
  57.     private fun runQuery(sqlString: String){
  58.  
  59.         try {
  60.             stmt.executeUpdate(sqlString)
  61.         } catch (e: SQLException) {
  62.             println("Erro ao executar a query : " + e.message)
  63.         }
  64.  
  65.     }
  66.  
  67.  
  68.     fun create() {
  69.         runQuery("CREATE TABLE IF NOT EXISTS " + tableName + " (id INTEGER, title TEXT, PRIMARY KEY(id))")
  70.     }
  71.  
  72.     fun has(id: Int): Boolean {
  73.         return hasRecord("id", id);
  74.     }
  75.     fun has(title: String): Boolean {
  76.         return hasRecord("title", title);
  77.     }
  78.  
  79.     private fun hasRecord(fieldName: String, value: Any): Boolean{
  80.         //println(fieldName + " - " + value)
  81.         val qry = stmt.executeQuery("SELECT * FROM " + tableName + " WHERE $fieldName = '$value'")
  82.         val exists = qry?.next()
  83.         qry?.close()
  84.  
  85.         return exists ?: false
  86.     }
  87.  
  88.  
  89.     fun insert(){
  90.  
  91.         var fString = arrayListOf<String>()
  92.         var vString = arrayListOf<Any>()
  93.  
  94.         fields.forEach{
  95.             fString.add(it.key)
  96.             vString.add(it.value)
  97.         }
  98.  
  99.         runQuery("INSERT INTO " + tableName + "(" + fString.joinToString(",") + ") VALUES('" + vString.joinToString("','") + "')")
  100.  
  101.      }
  102.  
  103.     fun update(){
  104.  
  105.         if (getField(primaryKey)==null){
  106.             println("A chave primaria não foi informada")
  107.             return;
  108.         }
  109.  
  110.         var fString = arrayListOf<String>()
  111.         fields.forEach{
  112.             if (it.key!=primaryKey) {
  113.                 fString.add(it.key + "='" + it.value + "'")
  114.             }
  115.         }
  116.  
  117.         runQuery("UPDATE " + tableName + " SET " + fString.joinToString(", ") + " WHERE " + primaryKey + "=" + getField(primaryKey))
  118.  
  119.     }
  120.  
  121.     fun delete(){
  122.         if (getField(primaryKey)==null){
  123.             println("A chave primaria não foi informada")
  124.             return;
  125.         }
  126.         runQuery("DELETE FROM " + tableName + " WHERE " + primaryKey + "=" + getField(primaryKey))
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement