harn-stdlib 0.8.79

Embedded Harn standard library source catalog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
 * std/postgres/query - small SQL ergonomics over std/postgres.
 *
 * SQL remains the source of truth. These helpers normalize query records,
 * build `$n` parameter lists from readable SQL templates, and centralize
 * explicit SQL-structure fragments for the cases Postgres cannot bind.
 */
import "std/postgres"

type PgQueryMode = "one" | "many" | "exec"

type PgSqlFragment = {
  _type: "std/postgres/query.sql_fragment.v1",
  __harn_postgres_query_trusted_fragment: bool,
  sql: string,
}

type PgNamedQuery = {
  name: string,
  mode: PgQueryMode,
  sql: string,
  params: list<any>,
  options?: dict,
}

type PgQuery = {name?: string, mode?: PgQueryMode, sql: string, params?: list<any>, options?: dict}

let __QUERY_MODES = ["one", "many", "exec"]

let __IDENTIFIER_PATTERN = "^[A-Za-z_][A-Za-z0-9_]*$"

let __SQL_FRAGMENT_TYPE = "std/postgres/query.sql_fragment.v1"

fn __sql_fragment(sql: string) -> PgSqlFragment {
  if type_of(sql) != "string" {
    throw "std/postgres/query: SQL fragment must be a string"
  }
  return {_type: __SQL_FRAGMENT_TYPE, __harn_postgres_query_trusted_fragment: true, sql: sql}
}

fn __is_sql_fragment(value) -> bool {
  return type_of(value) == "dict"
    && value?._type == __SQL_FRAGMENT_TYPE
    && value?.__harn_postgres_query_trusted_fragment
}

fn __sql_fragment_text(value) -> string {
  let fragment_sql = value?.sql
  if type_of(fragment_sql) != "string" {
    throw "std/postgres/query: SQL fragment sql must be a string"
  }
  return fragment_sql
}

fn __query_name(query) -> string {
  if type_of(query) != "dict" {
    return ""
  }
  let name = query?.name
  if name == nil {
    return ""
  }
  if type_of(name) != "string" {
    throw "std/postgres/query: query name must be a string"
  }
  return name
}

fn __query_error_prefix(query) -> string {
  let name = __query_name(query)
  if name == "" {
    return "std/postgres/query: "
  }
  return "std/postgres/query `" + name + "`: "
}

fn __normalize_query(query, expected_mode = nil) {
  if type_of(query) != "dict" {
    throw "std/postgres/query: query must be a dict"
  }
  let sql = query?.sql
  if type_of(sql) != "string" || trim(sql) == "" {
    throw __query_error_prefix(query) + "sql must be a non-empty string"
  }
  let params = query?.params ?? []
  if type_of(params) != "list" {
    throw __query_error_prefix(query) + "params must be a list"
  }
  let mode = query?.mode ?? expected_mode
  if mode != nil && !contains(__QUERY_MODES, mode) {
    throw __query_error_prefix(query) + "mode must be one of one, many, exec"
  }
  if expected_mode != nil && query?.mode != nil && query.mode != expected_mode {
    throw __query_error_prefix(query) + "mode " + query.mode + " cannot run through " + expected_mode
  }
  let options = query?.options
  if options != nil && type_of(options) != "dict" {
    throw __query_error_prefix(query) + "options must be a dict"
  }
  return {name: __query_name(query), mode: mode, sql: sql, params: params, options: options}
}

fn __with_query_name(query, body) {
  try {
    return body()
  } catch (error) {
    if query.name == "" {
      throw error
    }
    throw __query_error_prefix(query) + to_string(error)
  }
}

fn __find_placeholder_end(chars: list<string>, start: int) -> int {
  var idx = start
  while idx < len(chars) {
    if chars[idx] == "}" {
      return idx
    }
    idx = idx + 1
  }
  return -1
}

fn __render_placeholder(key: string, value, params: list<any>, positions: dict) {
  if __is_sql_fragment(value) {
    return {sql: __sql_fragment_text(value), params: params, positions: positions}
  }
  let existing = positions[key]
  if existing != nil {
    return {sql: "$" + to_string(existing), params: params, positions: positions}
  }
  let next = len(params) + 1
  return {sql: "$" + to_string(next), params: params.push(value), positions: positions + {[key]: next}}
}

fn __render_sql_template(template: string, values: dict) {
  if type_of(template) != "string" || trim(template) == "" {
    throw "std/postgres/query: SQL template must be a non-empty string"
  }
  if type_of(values) != "dict" {
    throw "std/postgres/query: SQL template values must be a dict"
  }
  let template_chars = chars(template)
  var idx = 0
  var out = ""
  var params = []
  var positions = {}
  while idx < len(template_chars) {
    let char = template_chars[idx]
    if char == "{" {
      if idx + 1 < len(template_chars) && template_chars[idx + 1] == "{" {
        out = out + "{"
        idx = idx + 2
        continue
      }
      let end = __find_placeholder_end(template_chars, idx + 1)
      if end < 0 {
        throw "std/postgres/query: unclosed `{` in SQL template"
      }
      let key = trim(join(template_chars[idx + 1:end], ""))
      if key == "" {
        throw "std/postgres/query: empty SQL template placeholder"
      }
      if regex_match(__IDENTIFIER_PATTERN, key) == nil {
        throw "std/postgres/query: invalid SQL template placeholder `" + key + "`"
      }
      if !contains(values.keys(), key) {
        throw "std/postgres/query: missing SQL template value `" + key + "`"
      }
      let rendered = __render_placeholder(key, values[key], params, positions)
      out = out + rendered.sql
      params = rendered.params
      positions = rendered.positions
      idx = end + 1
      continue
    }
    if char == "}" {
      if idx + 1 < len(template_chars) && template_chars[idx + 1] == "}" {
        out = out + "}"
        idx = idx + 2
        continue
      }
      throw "std/postgres/query: unmatched `}` in SQL template"
    }
    out = out + char
    idx = idx + 1
  }
  return {sql: out, params: params}
}

/**
 * Build a serializable named-query record.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: named("list_receipts", "many", sql, [tenant_id])
 */
pub fn named(name: string, mode: PgQueryMode, sql: string, params: list<any> = []) -> PgNamedQuery {
  return __normalize_query({name: name, mode: mode, sql: sql, params: params})
}

/**
 * Build a parameterized query record from a SQL template.
 *
 * `{name}` placeholders become `$1`, `$2`, ... bind parameters. Repeated
 * placeholders reuse the first parameter index. Use `{{` and `}}` for literal
 * braces, and pass `ident(...)`, `ident_path(...)`, or `unsafe_sql(...)` only
 * for source-controlled SQL structure.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: sql("SELECT * FROM receipts WHERE tenant_id = {tenant_id}", {tenant_id: tenant_id})
 */
pub fn sql(template: string, values: dict = {}, options = nil) -> PgQuery {
  let rendered = __render_sql_template(template, values)
  return __normalize_query({sql: rendered.sql, params: rendered.params, options: options})
}

/**
 * Build a named parameterized query record from a SQL template.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: named_sql("list_receipts", "many", "SELECT * FROM receipts WHERE tenant_id = {tenant_id}", {tenant_id: tenant_id})
 */
pub fn named_sql(
  name: string,
  mode: PgQueryMode,
  template: string,
  values: dict = {},
  options = nil,
) -> PgNamedQuery {
  let rendered = __render_sql_template(template, values)
  return __normalize_query(
    {name: name, mode: mode, sql: rendered.sql, params: rendered.params, options: options},
  )
}

/**
 * Run a query that returns zero or one row.
 *
 * @effects: [postgres]
 * @allocation: heap
 * @errors: validation, postgres
 * @api_stability: experimental
 * @example: one(db, {name: "get_receipt", sql: sql, params: [id]})
 */
pub fn one(handle, query: PgQuery) {
  let q = __normalize_query(query, "one")
  return __with_query_name(q, { -> pg_query_one(handle, q.sql, q.params, q.options) })
}

/**
 * Run a query that returns a list of rows.
 *
 * @effects: [postgres]
 * @allocation: heap
 * @errors: validation, postgres
 * @api_stability: experimental
 * @example: many(db, {name: "list_receipts", sql: sql, params: [tenant_id]})
 */
pub fn many(handle, query: PgQuery) -> list {
  let q = __normalize_query(query, "many")
  return __with_query_name(q, { -> pg_query(handle, q.sql, q.params, q.options) })
}

/**
 * Execute a statement and return the underlying PgExecuteResult.
 *
 * @effects: [postgres]
 * @allocation: heap
 * @errors: validation, postgres
 * @api_stability: experimental
 * @example: exec(db, {name: "mark_read", sql: sql, params: [id, status]})
 */
pub fn exec(handle, query: PgQuery) -> PgExecuteResult {
  let q = __normalize_query(query, "exec")
  if q.options != nil {
    throw __query_error_prefix(q) + "options are only supported by one and many"
  }
  return __with_query_name(q, { -> pg_execute(handle, q.sql, q.params) })
}

/**
 * Run a named query according to its `mode`.
 *
 * @effects: [postgres]
 * @allocation: heap
 * @errors: validation, postgres
 * @api_stability: experimental
 * @example: run(db, named("list_receipts", "many", sql, [tenant_id]))
 */
pub fn run(handle, query: PgNamedQuery) {
  let q = __normalize_query(query)
  if q.mode == nil {
    throw __query_error_prefix(q) + "mode is required"
  }
  if q.mode == "one" {
    return one(handle, q)
  }
  if q.mode == "many" {
    return many(handle, q)
  }
  return exec(handle, q)
}

/**
 * Validate and return a static SQL identifier for projection helpers.
 *
 * @effects: []
 * @allocation: stack-only
 * @errors: validation
 * @api_stability: experimental
 * @example: identifier("created_at")
 */
pub fn identifier(name: string) -> string {
  if type_of(name) != "string" || trim(name) == "" {
    throw "std/postgres/query: SQL identifier must be a non-empty string"
  }
  if regex_match(__IDENTIFIER_PATTERN, name) == nil {
    throw "std/postgres/query: unsafe SQL identifier `" + to_string(name) + "`"
  }
  return name
}

/**
 * Quote one SQL identifier part with PostgreSQL double-quote escaping.
 *
 * Dynamic values still belong in template placeholders; quoted identifiers are
 * only for SQL structure such as table or column names.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: quote_identifier("receipt events")
 */
pub fn quote_identifier(name: string) -> string {
  if type_of(name) != "string" || trim(name) == "" {
    throw "std/postgres/query: SQL identifier must be a non-empty string"
  }
  return "\"" + replace(name, "\"", "\"\"") + "\""
}

/**
 * Render a quoted SQL identifier fragment for use inside `sql(...)`.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: sql("SELECT {column} FROM receipts", {column: ident("created_at")})
 */
pub fn ident(name: string) -> PgSqlFragment {
  return __sql_fragment(quote_identifier(name))
}

/**
 * Render a dotted quoted SQL identifier path for use inside `sql(...)`.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: ident_path(["app", "receipts"])
 */
pub fn ident_path(parts: list<string>) -> PgSqlFragment {
  if type_of(parts) != "list" || len(parts) == 0 {
    throw "std/postgres/query: SQL identifier path must be a non-empty list"
  }
  var quoted = []
  for part in parts {
    quoted = quoted.push(quote_identifier(part))
  }
  return __sql_fragment(join(quoted, "."))
}

/**
 * Mark a source-controlled SQL fragment for insertion into `sql(...)`.
 *
 * This bypasses parameter binding and must never wrap user input. Prefer
 * normal placeholders for values and `ident(...)` / `ident_path(...)` for
 * identifiers.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: unsafe_sql("COUNT(*)")
 */
pub fn unsafe_sql(fragment: string) -> PgSqlFragment {
  return __sql_fragment(fragment)
}

/**
 * Render `SELECT a, b, c` from already-static projection fragments.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: select_clause([uuid_text("id"), "payload"])
 */
pub fn select_clause(fragments: list<string>) -> string {
  if type_of(fragments) != "list" {
    throw "std/postgres/query: select_clause requires a list of fragments"
  }
  if len(fragments) == 0 {
    throw "std/postgres/query: select_clause requires at least one fragment"
  }
  return "SELECT " + join(fragments, ", ")
}

/**
 * Render `column::text AS column` for UUID columns.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: uuid_text("id")
 */
pub fn uuid_text(name: string) -> string {
  let ident = identifier(name)
  return ident + "::text AS " + ident
}

/**
 * Render `to_json(column)#>>'{}' AS column` for timestamp columns.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: timestamptz_json("created_at")
 */
pub fn timestamptz_json(name: string) -> string {
  let ident = identifier(name)
  return "to_json(" + ident + ")#>>'{}' AS " + ident
}

/**
 * Render a nullable timestamp projection that preserves nulls as null.
 *
 * @effects: []
 * @allocation: heap
 * @errors: validation
 * @api_stability: experimental
 * @example: nullable_timestamptz_json("finished_at")
 */
pub fn nullable_timestamptz_json(name: string) -> string {
  let ident = identifier(name)
  return "CASE WHEN "
    + ident
    + " IS NULL THEN NULL ELSE to_json("
    + ident
    + ")#>>'{}' END AS "
    + ident
}