Skip to content

Database Query Functions API (Racket)

Ovidiu edited this page Feb 12, 2018 · 15 revisions

Source: https://docs.racket-lang.org/db/query-api.html

Simple Queries

query-exec

(query-exec connection stmt arg ...) → void?
  connection : connection?
  stmt : statement?
  arg : any/c

Executes a SQL statement for effect.

query(connection, stmt[, arg ...]) → void

query-rows

Executes a SQL query, which must produce rows, and returns the list of rows (as vectors) from the query.

(query-rows connection	 	 	 	 
  stmt	 	 	 	 
  arg ...
  [ #:group groupings	 	 	 	 
    #:group-mode group-mode]) → (listof vector?)
query_rows(connection, stmt[, arg ...]) → list<vector>

query-list

(query-list connection stmt arg ...) → list?
  connection : connection?
  stmt : statement?
  arg : any/c

Executes a SQL query, which must produce rows of exactly one column, and returns the list of values from the query.

query_list(connection, stmt[, arg ...]) → list

query-row

(query-row connection stmt arg ...) → vector?
  connection : connection?
  stmt : statement?
  arg : any/c

Executes a SQL query, which must produce exactly one row, and returns its (single) row result as a vector.

query_row(connection, stmt[, args ...]) → vector

query-maybe-row

(query-maybe-row connection stmt arg ...) → (or/c vector? #f)
  connection : connection?
  stmt : statement?
  arg : any/c
query_maybe_row(connection, stmt[, arg ...]) → null<vector>

In Racket API, the result of (query-maybe-row) is not-nullable.

query-value

(query-value connection stmt arg ...) → any/c
  connection : connection?
  stmt : statement?
  arg : any/c

Executes a SQL query, which must produce exactly one row of exactly one column, and returns its single value result.

query_value(connection, stmt[, arg ...]) → variant
// Compared to deprecated db_value() this does not get the field name as parameter
// so when stmt will select multiple fields, the first field will be returned
//
// deprecated: db_value(field, stmt, connection)
query_value_named(connection, field, stmt[, arg ...]) → variant

In Racket API, the result of (query_value) is not-nullable.

query-maybe-value

(query-maybe-value connection stmt arg ...) → (or/c any/c #f)
  connection : connection?
  stmt : statement?
  arg : any/c
query_maybe_value(connection, stmt[, arg ...]) → null<variant>

in-query

(in-query connection
  stmt	 	 	 	 
  arg ...
  [ #:fetch fetch-size	 	 	 	 
    #:group groupings	 	 	 	 
    #:group-mode group-mode] ) → sequence?
  connection : connection?
  stmt : statement?
  arg : any/c
  fetch-size : (or/c exact-positive-integer? +inf.0) = +inf.0
  groupings : (let* ([field/c (or/c string? exact-nonnegative-integer?)]
       [grouping/c (or/c field/c (vectorof field/c))])
  (or/c grouping/c (listof grouping/c))) = null
  group-mode : (listof (or/c 'preserve-null 'list)) = null

General Query Support

query

(query connection stmt arg ...)
 → (or/c simple-result? rows-result?)
  connection : connection?
  stmt : statement?
  arg : any/c
query(connection, stmt[, arg ...]) → variant

group-rows

(group-rows result
  #:group groupings	 	 	 	 
  [ #:group-mode group-mode]) → rows-result?
  result : rows-result?
  groupings : (let* ([field/c (or/c string? exact-nonnegative-integer?)]
       [grouping/c (or/c field/c (vectorof field/c))])
  (or/c grouping/c (listof grouping/c)))
  group-mode : (listof (or/c 'preserve-null 'list)) = null

rows->dict

(rows->dict result
  #:key key-field/s	 	 	 	 
  #:value value-field/s	 	 	 	 
  [ #:value-mode value-mode]) → dict?
  result : rows-result?
  key-field/s :	(let ([field/c (or/c string? exact-nonnegative-integer?)])
    (or/c field/c (vectorof field/c)))
  value-field/s : (let ([field/c (or/c string? exact-nonnegative-integer?)])
    (or/c field/c (vectorof field/c)))
  value-mode : (listof (or/c 'list 'preserve-null)) = null

Prepared Statements

prepare

(prepare connection stmt) → prepared-statement?
  connection : connection?
  stmt : (or/c string? virtual-statement?)
prepare(connection, stmt) -> prepared_statement

Transactions

start-transaction

(start-transaction c
  [ #:isolation isolation-level	 	 	 	 
    #:option option]) → void?
  c : connection?
  isolation-level : (or/c 'serializable
      'repeatable-read
      'read-committed
      'read-uncommitted
      #f) = #f
  option : any/c = #f

commit-transaction

(commit-transaction c) → void?
  c : connection?

rollback-transaction

(rollback-transaction c) → void?
  c : connection?

in-transaction?

(in-transaction? c) → boolean?
  c : connection?

needs-rollback?

(needs-rollback? c) → boolean?
  c : connection?

call-with-transaction

(call-with-transaction c
  proc	 	 	 	 
  [ #:isolation isolation-level	 	 	 	 
    #:option option]) →	any
  c : connection?
  proc : (-> any)
  isolation-level : (or/c 'serializable
      'repeatable-read
      'read-committed
      'read-uncommitted
      #f) = #f
  option : any/c = #f

Database Catalog Information

list-tables

(list-tables c [#:schema schema]) → (listof string?)
  c : connection?
  schema : (or/c 'search-or-current 'search 'current)
    = 'search-or-current
list_tables(connection) → list<string>

table-exists?

(table-exists? c
  table-name	 	 	 	 
  [ #:schema schema	 	 	 	 
    #:case-sensitive? case-sensitive?]) → boolean?
  c : connection?
  table-name : string?
  schema : (or/c 'search-or-current 'search 'current)
    = 'search-or-current
  case-sensitive? : any/c = #f
table_exists(table_name) → boolean

Clone this wiki locally