Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposed SQL Datetime query condition

I have a table that has two fields of type Datetime and int.

object DummyTable : IntIdTable("db.table", columnName = "id") {
    val createdAt = datetime("created_at")
    val mins = integer("mins")
    override val primaryKey = PrimaryKey(id)
}

I have a function where i want to check the condition that createdAt plus mins is less than the Datetime now.

 fun isValid(id: String): String? {
      return DummyTable.select{
          COND??
        }.single()[DummyTable.age]
    }

Something like this:

DummyTable.createdAt plus minutes   lessEq  DateTime.now()??

What would be the query condition?

like image 971
Avenger Avatar asked Jun 18 '26 23:06

Avenger


1 Answers

I thought that the answer on github was enough.

class DateAddMins(val dateExp: Expression<DateTime>, val addMins: Expression<Int?>) : Expression<DateTime>() {
    override fun toQueryBuilder(queryBuilder: QueryBuilder) = queryBuilder {
        append("DATE_ADD(", dateExp, ", INTERVAL IFNULL(", addMins, ", 0) MINUTE)")
    }
}

DummyTable.select { 
    DateAddMins(DummyTable.createdAt, DummyTable.mins) lessEq DateTime.now) 
}
like image 62
Tapac Avatar answered Jun 20 '26 15:06

Tapac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!