1ed2c59997
* feat: persist and expire quick chats * fix: address quick chat review feedback * fix: resolve quick chat review findings * fix: close remaining quick chat review gaps * fix: avoid repeated quick chat expiration rechecks * fix: guard quick chat expiration deletes * fix: expire abandoned starting quick chats * fix: ignore missing quick chat expiration candidates
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package dialect
|
|
|
|
import "fmt"
|
|
|
|
// DurationMs returns the SQL expression for the difference between two timestamps in milliseconds.
|
|
//
|
|
// SQLite: (julianday(end) - julianday(start)) * 86400000
|
|
// Postgres: EXTRACT(EPOCH FROM (end - start)) * 1000
|
|
func DurationMs(driver, end, start string) string {
|
|
if IsPostgres(driver) {
|
|
return fmt.Sprintf("EXTRACT(EPOCH FROM (%s - %s)) * 1000", end, start)
|
|
}
|
|
return fmt.Sprintf("(julianday(%s) - julianday(%s)) * 86400000", end, start)
|
|
}
|
|
|
|
// DateOf returns the SQL expression to extract the date portion from a timestamp.
|
|
//
|
|
// SQLite: date(expr)
|
|
// Postgres: (expr)::date
|
|
func DateOf(driver, expr string) string {
|
|
if IsPostgres(driver) {
|
|
return fmt.Sprintf("(%s)::date", expr)
|
|
}
|
|
return fmt.Sprintf("date(%s)", expr)
|
|
}
|
|
|
|
// Now returns the SQL expression for the current timestamp.
|
|
//
|
|
// SQLite: datetime('now')
|
|
// Postgres: NOW()
|
|
func Now(driver string) string {
|
|
if IsPostgres(driver) {
|
|
return "NOW()"
|
|
}
|
|
return "datetime('now')"
|
|
}
|
|
|
|
// NowMinusHours returns the SQL expression for "current time minus N hours",
|
|
// where hoursExpr is a column or expression producing the number of hours.
|
|
//
|
|
// SQLite: datetime('now', '-' || hoursExpr || ' hours')
|
|
// Postgres: NOW() - (hoursExpr || ' hours')::interval
|
|
func NowMinusHours(driver, hoursExpr string) string {
|
|
if IsPostgres(driver) {
|
|
return fmt.Sprintf("NOW() - (%s || ' hours')::interval", hoursExpr)
|
|
}
|
|
return fmt.Sprintf("datetime('now', '-' || %s || ' hours')", hoursExpr)
|
|
}
|
|
|
|
// GreatestTimestamp returns the greater of two timestamp expressions.
|
|
//
|
|
// SQLite: max(left, right)
|
|
// Postgres: GREATEST(left, right)
|
|
func GreatestTimestamp(driver, left, right string) string {
|
|
if IsPostgres(driver) {
|
|
return fmt.Sprintf("GREATEST(%s, %s)", left, right)
|
|
}
|
|
return fmt.Sprintf("max(%s, %s)", left, right)
|
|
}
|
|
|
|
// CurrentDate returns the SQL expression for the current date (no time component).
|
|
//
|
|
// SQLite: date('now')
|
|
// Postgres: CURRENT_DATE
|
|
func CurrentDate(driver string) string {
|
|
if IsPostgres(driver) {
|
|
return "CURRENT_DATE"
|
|
}
|
|
return "date('now')"
|
|
}
|
|
|
|
// DateNowMinusDays returns the SQL expression for "current date minus N days",
|
|
// where daysExpr is a parameter placeholder (e.g., "?") for the number of days.
|
|
//
|
|
// SQLite: date('now', '-' || ? || ' days')
|
|
// Postgres: CURRENT_DATE - (? || ' days')::interval
|
|
func DateNowMinusDays(driver, daysExpr string) string {
|
|
if IsPostgres(driver) {
|
|
return fmt.Sprintf("CURRENT_DATE - (%s || ' days')::interval", daysExpr)
|
|
}
|
|
return fmt.Sprintf("date('now', '-' || %s || ' days')", daysExpr)
|
|
}
|
|
|
|
// DatePlusOneDay returns the SQL expression to add one day to a date expression.
|
|
//
|
|
// SQLite: date(expr, '+1 day')
|
|
// Postgres: (expr)::date + INTERVAL '1 day'
|
|
func DatePlusOneDay(driver, expr string) string {
|
|
if IsPostgres(driver) {
|
|
return fmt.Sprintf("(%s)::date + INTERVAL '1 day'", expr)
|
|
}
|
|
return fmt.Sprintf("date(%s, '+1 day')", expr)
|
|
}
|