sqlformat

SQL string literals interpolation with {fields} and #timestamps# inputs

Fields input:

It is similar to strformat string interpolation. Symbols { and } should be excaped with doubling it: {{ and }}

let userName = "Test User"
echo sql"SELECT * FROM Users WHERE ((user_name)='{userName}')"

# Output:
# SELECT * FROM Users WHERE ((user_name)='Test User')

Field type

It is possible to set "type" of field - string or timestamp, so appropriate value will be correct delimited with single quotes (i.e. as 'text field') for string and with ## (i.e. as #timestamp#) for timestamps. Field should be prefixed with $ for string field type and with # for timestamp field type. Symbols $ and # should be doubled for excaping

let findUser = "Test user 1"
let bornDate = initDateTime(27, tm.Month(3), 1954, 0, 0, 0)
echo sql"SELECT * FROM Users WHERE ((user_name)=${findUser}) AND ((user_bday)=>#{bornDate})"

# Output:
# SELECT * FROM Users WHERE ((user_name)='Test user 1') AND ((user_bday)=>#3/27/1954#)

Special timestamp construction

Special construction form for timestamps:

#{ <year>, <month>, <day> [, <hour> [, <minutes> [, <seconds>]]] }

let findUser = "Test user 1"

echo sql"SELECT * FROM Users WHERE ((user_bday)=>#{1999, 12, 31})"

# Output:
# SELECT * FROM Users WHERE ((user_bday)=>#12/31/1999#)

Timestamp literals and custom formats.

Timestamp literal should be delimited by ##.

echo sql"SELECT * FROM Users WHERE ((user_bday)=>#12/31/1999#)"

# Output:
# SELECT * FROM Users WHERE ((user_bday)=>#12/31/1999#)

-- Date in timestamp literal

Regardless that SQL standard requires date in timestamps to be in form m(onth)/d(ay)/y(ear) it is possible to set regional specific (custom) formats:

  • #d.m.y# (with dot as field delimiter)
  • #y-m-d# (with dash as field delimiter)

echo sql"SELECT * FROM Users WHERE ((user_bday)=>#31.12.1999#)"

# Output:
# SELECT * FROM Users WHERE ((user_bday)=>#12/31/1999#)

echo sql"SELECT * FROM Users WHERE ((user_bday)=>#1999-12-31#)"

# Output:
# SELECT * FROM Users WHERE ((user_bday)=>#12/31/1999#)

-- Time in timestamp literal

Time in timestamp literal is optional. If set it should be in form h(our):m(inutes):s(econds) with optional AM / PM suffixes

echo sql"UPDATE operations SET created=#15.1.1998 14:03#"

# Output:
# UPDATE operations SET created=#15/1/1998 14:03#

echo sql"UPDATE operations SET created=#15.1.1998 2:03p.m.#"

# Output:
# UPDATE operations SET created=#15/1/1998 2:03 PM#

Macros

macro `$&`(pattern: string): string

Templates

template dbFormatTS(dt: tm.DateTime): string
format DateTime as SQL compatible string
let bornDate = initDateTime(27, tm.Month(3), 1954, 0, 0, 0)

echo sql"SELECT * FROM Users WHERE ((user_bday)=#{bornDate})"

# Output:
# SELECT * FROM Users WHERE ((user_bday)=#3/27/1954#)
template dbFormatTS(year, month, day: int; hour: int = 0; minute: int = 0; second: int = 0): string
create timestamp from date/time components and format it for SQL
echo sql"SELECT * FROM Users WHERE ((user_bday)=#{1954, 3, 27})"

# Output:
# SELECT * FROM Users WHERE ((user_bday)=#3/27/1954#)
template sql(pattern: string): untyped
An alias for $&.