Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

how to use INSERT INTO tableName (name) values(HEX(DES_ENCRYPT("test",121121')))

Open 173889818 opened this issue 4 years ago • 9 comments

173889818 avatar Aug 25 '20 12:08 173889818

This should work

fun hex(exp: Expression<String>) = CustomStringFunction("HEX", exp)

fun des_encrypt(value: String, key: String) = CustomStringFunction("DES_ENCRYPT", stringParam(value), stringParam(key))

FooTable.insert {
   it[name] = hex(des_encrypt("test", "121121"))
}

Tapac avatar Aug 25 '20 13:08 Tapac

这应该工作

fun  hex(exp : 表达式< String >)=  CustomStringFunction(“ HEX ”,exp)

有趣 DES_ENCRYPT(值: 字符串,键: 字符串)=  CustomStringFunction(“ DES_ENCRYPT ”,StringParam中(值),StringParam中(密钥))

FooTable .insert {
   it [name] = hex(des_encrypt(“ test ”,“ 121121 ”))
}
dataType?
val name = varchar("name", 128).nullable()

173889818 avatar Aug 25 '20 13:08 173889818

Sorry, but I don't understand your question.

Tapac avatar Aug 26 '20 11:08 Tapac

Sorry, but I don't understand your question.

After saving is a function string that should be processed After saving the database image

HEX(DES_ENCRYPT('test', '121121'))

It should look something like this FF236DBE5D5723C7C3

object TestTable : Table("test") {
    val id = long("id").primaryKey().autoIncrement()
    val name = varchar("name", 256).nullable()
}
      val nameCustom = hex(desEncrypt("test'", "121121"))
       val id = TestTable.insert {
           it[name]=nameCustom.toString()
       }.getOrNull(TestTable.id)

173889818 avatar Aug 26 '20 11:08 173889818

Ok. I see. You don't need to call .toString() as it will insert the expression SQL in your column instead of providing expression into query. This should work;

fun hex(exp: Expression<String?>) = CustomStringFunction("HEX", exp)

fun desEncrypt(value: String, key: String) = CustomStringFunction("DES_ENCRYPT", stringParam(value), stringParam(key))

object TestTable : LongIdTable("test") {
    val name = varchar("name", 256).nullable()
}

val nameCustom = hex(desEncrypt("test", "121121"))
val id = TestTable.insertAndGetId {
    it[name] = nameCustom
}

Tapac avatar Aug 26 '20 16:08 Tapac

Ok. I see. You don't need to call .toString() as it will insert the expression SQL in your column instead of providing expression into query. This should work;

fun hex(exp: Expression<String?>) = CustomStringFunction("HEX", exp)

fun desEncrypt(value: String, key: String) = CustomStringFunction("DES_ENCRYPT", stringParam(value), stringParam(key))

object TestTable : LongIdTable("test") {
    val name = varchar("name", 256).nullable()
}

val nameCustom = hex(desEncrypt("test", "121121"))
val id = TestTable.insertAndGetId {
    it[name] = nameCustom
}

Type mismatch if you don't call toString() it [name] type is String?,nameCustom type is CustomFunction<String?>

173889818 avatar Aug 27 '20 02:08 173889818

@173889818, there is setter with Expression type, so it should work. Just copy-paste my code and check the compilation.

Tapac avatar Aug 27 '20 09:08 Tapac

object MyTbl: Table("sch.tbl") {
  val id = varchar("id", 36)
  val reasonUniqueId = varchar("reason_unique_id", 50)
}
    val uuidGen = CustomStringFunction("UUID")

    MyTbl
      .update({ MyTbl.id eq "someVal" }) {
        it[reasonUniqueId] = uuidGen
      }

this code fails - can't find setter

seik0ixtem avatar Apr 26 '22 08:04 seik0ixtem

upd: changing to

val reasonUniqueId = varchar("reason_unique_id", 50).nullable

solves.

not what I wanted, but it's another question, anyway.

seik0ixtem avatar Apr 26 '22 08:04 seik0ixtem

@seik0ixtem The CustomXFunction shortcuts are inherently nullable, producing typed results like CustomFunction<X?>.

If this is not the behavior you want, as your table column should not be nullable, then you just need to create a CustomFunction<X> directly without using the shortcut. The following compiles with no issue:

object MyTbl : Table("sch.tbl") {
    val id = varchar("id", 36)
    val reasonUniqueId = varchar("reason_unique_id", 50)
}

// only change here
val uuidGen = CustomFunction<String>("UUID", VarCharColumnType())

MyTbl
    .update({ MyTbl.id eq "someVal" }) {
        it[reasonUniqueId] = uuidGen
    }

bog-walk avatar Dec 22 '23 17:12 bog-walk