diff src/main/kotlin/name/blackcap/passman/Entry.kt @ 12:a38a2a1036c3

Add import subcommand.
author David Barts <n5jrn@me.com>
date Sun, 22 Jan 2023 09:22:53 -0800
parents c69665ff37d0
children 3a3067ba673b
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Entry.kt	Sat Jan 21 15:39:42 2023 -0800
+++ b/src/main/kotlin/name/blackcap/passman/Entry.kt	Sun Jan 22 09:22:53 2023 -0800
@@ -1,5 +1,6 @@
 package name.blackcap.passman
 
+import java.sql.ResultSet
 import java.util.*
 import kotlin.reflect.KProperty
 import kotlin.reflect.full.declaredMemberProperties
@@ -30,6 +31,25 @@
             )
         }
 
+        fun fromDatabase(db: Database, name: String): Entry? {
+            db.connection.prepareStatement("select name, username, password, notes, created, modified, accessed from passwords where id = ?").use {
+                it.setLong(1, db.makeKey(name))
+                val results = it.executeQuery()
+                if (!results.next()) {
+                    return null
+                }
+                return Entry(
+                    name = results.getDecryptedString(1, db.encryption)!!,
+                    username = results.getDecryptedString(2, db.encryption)!!,
+                    password = results.getDecrypted(3, db.encryption)!!,
+                    notes = results.getDecryptedString(4, db.encryption),
+                    created = results.getDate(5),
+                    modified = results.getDate(6),
+                    accessed = results.getDate(7)
+                )
+            }
+        }
+
         private fun _genPassword(length: Int, allowSymbols: Boolean, verbose: Boolean): CharArray {
             val generated = generate(length, allowSymbols)
             if (verbose) {
@@ -47,6 +67,35 @@
         private fun _getNotes() = readLine("Notes: ")
     }
 
+    fun insert(db: Database) {
+        db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)")
+            .use {
+                it.setLong(1, db.makeKey(name))
+                it.setEncryptedString(2, name, db.encryption)
+                it.setEncryptedString(3, username, db.encryption)
+                it.setEncrypted(4, password, db.encryption)
+                it.setEncryptedString(5, notes, db.encryption)
+                it.setLongOrNull(6, created?.time)
+                it.setLongOrNull(7, modified?.time)
+                it.setLongOrNull(8, accessed?.time)
+                it.executeUpdate()
+            }
+    }
+
+    fun update(db: Database) {
+        db.connection.prepareStatement("update passwords set name = ?, username = ?, password = ?, notes = ?, created = ?, modified = ?, accessed = ? where id = ?").use {
+            it.setEncryptedString(1, name, db.encryption)
+            it.setEncryptedString(2, username, db.encryption)
+            it.setEncrypted(3, password, db.encryption)
+            it.setEncryptedString(4, notes, db.encryption)
+            it.setLongOrNull(5, created?.time)
+            it.setLongOrNull(6, modified?.time)
+            it.setLongOrNull(7, accessed?.time)
+            it.setLong(8, db.makeKey(name))
+            it.executeUpdate()
+        }
+    }
+
     val modifiedOrCreated get() = modified ?: created!!
 
     fun print(redactPassword: String? = null) {