comparison src/main/kotlin/name/blackcap/passman/RenameSubcommand.kt @ 14:4dae7a15ee48

Fix bugs found in additional round of testing.
author David Barts <n5jrn@me.com>
date Tue, 31 Jan 2023 19:07:46 -0800
parents c69665ff37d0
children 0fc90892a3ae
comparison
equal deleted inserted replaced
13:302d224bbd57 14:4dae7a15ee48
1 package name.blackcap.passman 1 package name.blackcap.passman
2 2
3 import org.apache.commons.cli.* 3 import org.apache.commons.cli.*
4 import java.sql.PreparedStatement
5 import java.sql.Types
4 import kotlin.system.exitProcess 6 import kotlin.system.exitProcess
5 7
6 class RenameSubcommand(): Subcommand() { 8 class RenameSubcommand(): Subcommand() {
7 private companion object { 9 private companion object {
8 const val FORCE = "force" 10 const val FORCE = "force"
57 die("record matching ${see(destination)} already exists") 59 die("record matching ${see(destination)} already exists")
58 } 60 }
59 } 61 }
60 62
61 db.connection.prepareStatement("select username, password, notes, created, modified, accessed from passwords where id = ?").use { sourceStmt -> 63 db.connection.prepareStatement("select username, password, notes, created, modified, accessed from passwords where id = ?").use { sourceStmt ->
62 sourceStmt.setLong(1, did) 64 sourceStmt.setLong(1, sid)
63 val result = sourceStmt.executeQuery() 65 val result = sourceStmt.executeQuery()
64 result.next() 66 result.next()
65 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)").run { 67 db.connection.prepareStatement("insert into passwords (id, name, username, password, notes, created, modified, accessed) values (?, ?, ?, ?, ?, ?, ?, ?)").use {
66 setLong(1, did) 68 it.setLong(1, did)
67 setEncryptedString(2, destination, db.encryption) 69 it.setEncryptedString(2, destination, db.encryption)
68 setBytes(3, result.getBytes(1)) 70 it.setBytes(3, result.getBytes(1))
69 setBytes(4, result.getBytes(2)) 71 it.setBytes(4, result.getBytes(2))
70 setBytesOrNull(5, result.getBytes(3)) 72 it.setBytesOrNull(5, result.getBytes(3))
71 setLong(6, result.getLong(4)) 73 it.setLong(6, result.getLong(4))
72 setLong(7, System.currentTimeMillis()) 74 it.setLong(7, System.currentTimeMillis())
73 setLongOrNull(8, result.getLong(6)) 75 it.setDateOrNull(8, result.getLong(6))
74 executeUpdate() 76 it.executeUpdate()
75 } 77 }
76 } 78 }
77 79
78 deleteRecord(sid) 80 deleteRecord(sid)
79 } 81 }
91 db.connection.prepareStatement("delete from passwords where id = ?").use { 93 db.connection.prepareStatement("delete from passwords where id = ?").use {
92 it.setLong(1, id); 94 it.setLong(1, id);
93 it.executeUpdate() 95 it.executeUpdate()
94 } 96 }
95 } 97 }
98
99 private fun PreparedStatement.setDateOrNull(parameterIndex: Int, value: Long?) {
100 if (value == null || value == 0L) {
101 setNull(parameterIndex, Types.INTEGER)
102 } else {
103 setLong(parameterIndex, value)
104 }
105 }
96 } 106 }