5 Utilities
The bindings described in this section are provided by the specific modules below, not by db or db/base.
5.1 Datetime Type Utilities
(require db/util/datetime) | package: db-lib |
procedure
(sql-datetime->srfi-date t) → srfi:date?
t : (or/c sql-date? sql-time? sql-timestamp?)
procedure
(srfi-date->sql-date d) → sql-date?
d : srfi:date?
procedure
(srfi-date->sql-time d) → sql-time?
d : srfi:date?
procedure
d : srfi:date?
procedure
d : srfi:date?
procedure
d : srfi:date?
> (sql-datetime->srfi-date (query-value pgc "select time '7:30'")) (date* 0 30 7 1 1 0 0 0 #f 0 0 "")
> (sql-datetime->srfi-date (query-value pgc "select date '25-dec-1980'")) (date* 0 0 0 25 12 1980 4 359 #f 0 0 "")
> (sql-datetime->srfi-date (query-value pgc "select timestamp 'epoch'")) (date* 0 0 0 1 1 1970 4 0 #f 0 0 "")
procedure
(sql-day-time-interval->seconds interval) → rational?
interval : sql-day-time-interval?
5.2 Geometric Types
(require db/util/geometry) | package: db-lib |
The following structures and functions deal with geometric values based on the OpenGIS (ISO 19125) model.
Note: Geometric columns defined using the PostGIS extension to PostgreSQL are not directly supported. Instead, data should be exchanged in the Well-Known Binary format; conversion of the following structures to and from WKB format is supported by the wkb->geometry and geometry->wkb functions.
struct
(struct line-string (points))
points : (listof point?)
struct
exterior : linear-ring? interiors : (listof linear-ring?)
struct
(struct multi-point (elements))
elements : (listof point?)
struct
(struct multi-line-string (elements))
elements : (listof line-string?)
struct
(struct multi-polygon (elements))
elements : (listof polygon?)
struct
(struct geometry-collection (elements))
elements : (listof geometry2d?)
procedure
(geometry2d? x) → boolean?
x : any/c
procedure
(linear-ring? x) → boolean?
x : any/c
procedure
(geometry->wkb g #:big-endian? big-endian?) → bytes?
g : geometry2d? big-endian? : (system-big-endian?)
procedure
(wkb->geometry b) → geometry2d?
b : bytes?
5.3 PostgreSQL-specific Types
(require db/util/postgresql) | package: db-lib |
struct
(struct pg-array ( dimensions dimension-lengths dimension-lower-bounds contents)) dimensions : exact-nonnegative-integer? dimension-lengths : (listof exact-positive-integer?) dimension-lower-bounds : (listof exact-integer?) contents : vector?
procedure
(pg-array-ref arr index ...+) → any/c
arr : pg-array? index : exact-integer?
procedure
(pg-array->list arr) → list?
arr : pg-array?
procedure
(list->pg-array lst) → pg-array?
lst : list?
struct
(struct pg-empty-range ())
The lb and ub fields must have the same type; the permissible types are exact integers, real numbers, and sql-timestamps. Either or both bounds may also be #f, which indicates the range is unbounded on that end.
procedure
(pg-range-or-empty? v) → boolean?
v : any/c
| ‹uuid› | ::= | ‹digit16›{8,8} - ‹digit16›{4,4} - ‹digit16›{4,4} - ‹digit16›{4,4} - ‹digit16›{12,12} |
The digits themselves are case-insensitive, accepting both uppercase and lowercase characters. Otherwise, if v is not a string matching the above pattern, this function returns #f.
Added in version 1.1 of package db-lib.
Changed in version 1.8: Made the check stricter: no characters are allowed
before or after the UUID.
struct
ne : point? sw : point?
struct
closed? : boolean? points : (listof point?)
struct
center : point? radius : real?
Note: PostgreSQL’s built-in geometric types are distinct from those provided by the PostGIS extension library (see Geometric Types).
procedure
(pg-custom-type typeid typename [ basetype #:recv recv-convert #:send send-convert]) → pg-custom-type? typeid : exact-nonnegative-integer? typename : symbol? basetype : (or/c #f symbol? exact-nonnegative-integer?) = #f recv-convert : (or/c #f (-> any/c any/c)) = values send-convert : (or/c #f (-> any/c any/c)) = values
The typeid refers to the OID of a row in the server’s pg_type system table. The typename symbol is the name this library uses for the type in parameter descriptions, error messages, etc; it is not necessarily the same as the server’s type name for typeid. The server’s type name must be used in SQL statements.
When the type identified by typeid appears in a query result, the result value is first received according to basetype, then the resulting Racket value is converted using recv-convert. If basetype is #f (the default), it is treated like the bytea type; that is, recv-convert gets the byte string sent by the server. If recv-convert is #f, the type is not allowed as a result type.
When the type identified by typeid is used as a query parameter, the argument value is first converted using send-convert, and the converted value is sent according to basetype. If send-convert is #f, the type is not allowed as a parameter type.
> (define cidr-typeid (query-value pgc "select oid from pg_type where typname = $1" "cidr")) > cidr-typeid 650
> (send pgc add-custom-types (list (pg-custom-type cidr-typeid 'cidr #:recv bytes->list #:send list->bytes))) > (query-value pgc "select cidr '127.0.0.0/24'") '(2 24 1 4 127 0 0 0)
Added in version 1.8 of package db-lib.
procedure
(pg-custom-type? v) → boolean?
v : any/c
Added in version 1.8 of package db-lib.
|
method
(send a-postgresql-connection add-custom-types types) → void?
types : (listof pg-custom-type?) Registers the given types with a-postgresql-connection for use as query parameter and result types. See pg-custom-type for details.Added in version 1.8 of package db-lib.
method
(send a-postgresql-connection async-message-evt) → evt?
Returns a synchronizable event that becomes ready when input is available from the backend. When the event is selected, it attempts to handle any asynchronous notice and notification messages; its synchronization result is #t if any messages were handled by the event’s synchronization, #f otherwise.Note that the event is highly prone to “false alarms”, when it becomes ready but produces #f.
Added in version 1.8 of package db-lib.
Attempts to cancel any queries currently in progress on a-postgresql-connection.Added in version 1.9 of package db-lib.
5.4 Cassandra-Specific Functionality
(require db/util/cassandra) | package: db-lib |
parameter
→
(or/c 'any 'one 'two 'three 'quorum 'all 'local-quorum 'each-quorum 'serial 'local-serial 'local-one) (cassandra-consistency consistency) → void?
consistency :
(or/c 'any 'one 'two 'three 'quorum 'all 'local-quorum 'each-quorum 'serial 'local-serial 'local-one)
The default consistency level is 'one.
5.5 Testing Database Programs
(require db/util/testing) | package: db-lib |
This module provides utilities for testing programs that use database connections.
procedure
(high-latency-connection connection latency [ #:sleep-atomic? sleep-atomic?]) → connection? connection : connection? latency : (>=/c 0) sleep-atomic? : any/c = #f
Use this function in performance testing to roughly simulate environments with high-latency communication with a database back end.
If sleep-atomic? is true, then the proxy enters atomic mode before sleeping, to better simulate the effect of a long-running FFI call (see FFI-Based Connections and Concurrency). Even so, it may not accurately simulate an ODBC connection that internally uses cursors to fetch data on demand, as each fetch would introduce additional latency.
5.6 Unsafe SQLite3 Extensions
The procedures documented in this section are unsafe.
In the functions below, the connection argument must be a SQLite connection; otherwise, an exception is raised.
(require db/unsafe/sqlite3) | package: db-lib |
Added in version 1.4 of package db-lib.
procedure
(sqlite3-load-extension c extension-path) → void?
c : connection? extension-path : path-string?
procedure
(sqlite3-create-function c name arity func) → void?
c : connection? name : (or/c string? symbol?) arity : (or/c exact-nonnegative-integer? #f) func : procedure?
procedure
(sqlite3-create-aggregate c name arity init-acc step-func final-func) → void? c : connection? name : (or/c string? symbol?) arity : (or/c exact-nonnegative-integer? #f) init-acc : any/c step-func : procedure? final-func : procedure?
init-acc is the initial accumulator value.
step-func receives arity+1 arguments: the current accumulator value, followed by the arguments of the current “step”; the function’s result becomes the accumulator value for the next step. The step arguments are SQLite values; the accumulator argument and result can be arbitrary Racket values.
final-func receives one argument: the final accumulator value; the function produces the result of the aggregate function, which must be a SQLite value.
The following relationship roughly holds:
(begin (sqlite3-create-aggregate c "agg" 1 init-acc step-func final-func) (query-value c "select agg(expr) from table")) = (final-func (for/fold ([accum init-acc]) ([v (in-query c "select expr from table")]) (step-func accum v)))