db: "recordset" -> "rows-result", "rows"

This commit is contained in:
Ryan Culpepper 2011-08-26 04:32:22 -06:00
parent 7bd370cd33
commit a91e6f6452
11 changed files with 76 additions and 76 deletions

View File

@ -84,19 +84,19 @@
(define (query1 c fsym stmt) (define (query1 c fsym stmt)
(send c query fsym stmt)) (send c query fsym stmt))
;; query/recordset : connection symbol Statement nat/#f -> void ;; query/rows : connection symbol Statement nat/#f -> void
(define (query/recordset c fsym sql want-columns) (define (query/rows c fsym sql want-columns)
(let [(result (query1 c fsym sql))] (let [(result (query1 c fsym sql))]
(unless (recordset? result) (unless (rows-result? result)
(uerror fsym "query did not return recordset: ~e" sql)) (uerror fsym "query did not return rows: ~e" sql))
(let ([got-columns (length (recordset-headers result))]) (let ([got-columns (length (rows-result-headers result))])
(when (and want-columns (not (= got-columns want-columns))) (when (and want-columns (not (= got-columns want-columns)))
(uerror fsym "query returned ~a ~a (expected ~a): ~e" (uerror fsym "query returned ~a ~a (expected ~a): ~e"
got-columns (if (= got-columns 1) "column" "columns") want-columns sql))) got-columns (if (= got-columns 1) "column" "columns") want-columns sql)))
result)) result))
(define (recordset->row fsym rs sql maybe-row? one-column?) (define (rows-result->row fsym rs sql maybe-row? one-column?)
(define rows (recordset-rows rs)) (define rows (rows-result-rows rs))
(cond [(null? rows) (cond [(null? rows)
(cond [maybe-row? #f] (cond [maybe-row? #f]
[else (uerror fsym "query returned zero rows (expected 1): ~e" sql)])] [else (uerror fsym "query returned zero rows (expected 1): ~e" sql)])]
@ -136,46 +136,46 @@
;; query-rows : connection Statement arg ... -> (listof (vectorof 'a)) ;; query-rows : connection Statement arg ... -> (listof (vectorof 'a))
(define (query-rows c sql . args) (define (query-rows c sql . args)
(let ([sql (compose-statement 'query-rows c sql args 'recordset)]) (let ([sql (compose-statement 'query-rows c sql args 'rows)])
(recordset-rows (query/recordset c 'query-rows sql #f)))) (rows-result-rows (query/rows c 'query-rows sql #f))))
;; query-list : connection Statement arg ... -> (listof 'a) ;; query-list : connection Statement arg ... -> (listof 'a)
;; Expects to get back a recordset with one field per row. ;; Expects to get back a rows-result with one field per row.
(define (query-list c sql . args) (define (query-list c sql . args)
(let ([sql (compose-statement 'query-list c sql args 1)]) (let ([sql (compose-statement 'query-list c sql args 1)])
(map (lambda (v) (vector-ref v 0)) (map (lambda (v) (vector-ref v 0))
(recordset-rows (query/recordset c 'query-list sql 1))))) (rows-result-rows (query/rows c 'query-list sql 1)))))
;; query-row : connection Statement arg ... -> (vector-of 'a) ;; query-row : connection Statement arg ... -> (vector-of 'a)
;; Expects to get back a recordset of zero or one rows. ;; Expects to get back a rows-result of zero or one rows.
(define (query-row c sql . args) (define (query-row c sql . args)
(let ([sql (compose-statement 'query-row c sql args 'recordset)]) (let ([sql (compose-statement 'query-row c sql args 'rows)])
(recordset->row 'query-row (rows-result->row 'query-row
(query/recordset c 'query-row sql #f) (query/rows c 'query-row sql #f)
sql #f #f))) sql #f #f)))
;; query-maybe-row : connection Statement arg ... -> (vector-of 'a) or #f ;; query-maybe-row : connection Statement arg ... -> (vector-of 'a) or #f
;; Expects to get back a recordset of zero or one rows. ;; Expects to get back a rows-result of zero or one rows.
(define (query-maybe-row c sql . args) (define (query-maybe-row c sql . args)
(let ([sql (compose-statement 'query-maybe-row c sql args 'recordset)]) (let ([sql (compose-statement 'query-maybe-row c sql args 'rows)])
(recordset->row 'query-maybe-row (rows-result->row 'query-maybe-row
(query/recordset c 'query-maybe-row sql #f) (query/rows c 'query-maybe-row sql #f)
sql #t #f))) sql #t #f)))
;; query-value : connection string arg ... -> value | raises error ;; query-value : connection string arg ... -> value | raises error
;; Expects to get back a recordset of exactly one row, exactly one column. ;; Expects to get back a rows-result of exactly one row, exactly one column.
(define (query-value c sql . args) (define (query-value c sql . args)
(let ([sql (compose-statement 'query-value c sql args 1)]) (let ([sql (compose-statement 'query-value c sql args 1)])
(recordset->row 'query-value (rows-result->row 'query-value
(query/recordset c 'query-value sql 1) (query/rows c 'query-value sql 1)
sql #f #t))) sql #f #t)))
;; query-maybe-value : connection Statement arg ... -> value/#f ;; query-maybe-value : connection Statement arg ... -> value/#f
;; Expects to get back a recordset of zero or one rows, exactly one column. ;; Expects to get back a rows-result of zero or one rows, exactly one column.
(define (query-maybe-value c sql . args) (define (query-maybe-value c sql . args)
(let ([sql (compose-statement 'query-maybe-value c sql args 1)]) (let ([sql (compose-statement 'query-maybe-value c sql args 1)])
(recordset->row 'query-maybe-value (rows-result->row 'query-maybe-value
(query/recordset c 'query-maybe-value sql 1) (query/rows c 'query-maybe-value sql 1)
sql #t #t))) sql #t #t)))
;; query-exec : connection Statement arg ... -> void ;; query-exec : connection Statement arg ... -> void
@ -224,9 +224,9 @@
(apply raise-type-error 'in-query "connection" 0 c stmt args)) (apply raise-type-error 'in-query "connection" 0 c stmt args))
(unless (statement? stmt) (unless (statement? stmt)
(apply raise-type-error 'in-query "statement" 1 c stmt args)) (apply raise-type-error 'in-query "statement" 1 c stmt args))
(let* ([check (or vars 'recordset)] (let* ([check (or vars 'rows)]
[stmt (compose-statement 'in-query c stmt args check)]) [stmt (compose-statement 'in-query c stmt args check)])
(recordset-rows (query/recordset c 'in-query stmt vars)))) (rows-result-rows (query/rows c 'in-query stmt vars))))
;; ======================================== ;; ========================================

View File

@ -5,7 +5,7 @@
prepared-statement<%> prepared-statement<%>
(struct-out simple-result) (struct-out simple-result)
(struct-out recordset) (struct-out rows-result)
(struct-out statement-binding) (struct-out statement-binding)
@ -122,10 +122,10 @@
;; An query-result is one of: ;; An query-result is one of:
;; - (simple-result alist) ;; - (simple-result alist)
;; - (recordset Header data) ;; - (rows-result Header data)
;; for user-visible recordsets: headers present, data is (listof vector) ;; for user-visible rows-results: headers present, data is (listof vector)
(struct simple-result (info) #:transparent) (struct simple-result (info) #:transparent)
(struct recordset (headers rows) #:transparent) (struct rows-result (headers rows) #:transparent)
;; A Header is (listof FieldInfo) ;; A Header is (listof FieldInfo)
;; A FieldInfo is an alist, contents dbsys-dependent ;; A FieldInfo is an alist, contents dbsys-dependent

View File

@ -3,7 +3,7 @@
"sql-data.rkt" "sql-data.rkt"
"functions.rkt") "functions.rkt")
(provide (struct-out simple-result) (provide (struct-out simple-result)
(struct-out recordset) (struct-out rows-result)
statement-binding? statement-binding?
(except-out (all-from-out "sql-data.rkt") (except-out (all-from-out "sql-data.rkt")
make-sql-bits/bytes make-sql-bits/bytes

View File

@ -39,17 +39,17 @@
(define/public (get-result-types) (define/public (get-result-types)
(send dbsystem describe-typeids result-typeids)) (send dbsystem describe-typeids result-typeids))
;; checktype is either #f, 'recordset, or exact-positive-integer ;; checktype is either #f, 'rows, or exact-positive-integer
(define/public (check-results fsym checktype obj) (define/public (check-results fsym checktype obj)
(cond [(eq? checktype 'recordset) (cond [(eq? checktype 'rows)
(unless (positive? (get-result-count)) (unless (positive? (get-result-count))
(when close-on-exec? (finalize)) (when close-on-exec? (finalize))
(error fsym "expected statement producing recordset, got ~e" obj))] (error fsym "expected statement producing rows, got ~e" obj))]
[(exact-positive-integer? checktype) [(exact-positive-integer? checktype)
(unless (= (get-result-count) checktype) (unless (= (get-result-count) checktype)
(when close-on-exec? (finalize)) (when close-on-exec? (finalize))
(error fsym (error fsym
"expected statement producing recordset with ~a ~a, got ~e" "expected statement producing rows with ~a ~a, got ~e"
checktype checktype
(if (= checktype 1) "column" "columns") (if (= checktype 1) "column" "columns")
obj))] obj))]

View File

@ -293,7 +293,7 @@
[(struct result-set-header-packet (fields extra)) [(struct result-set-header-packet (fields extra))
(let* ([field-dvecs (query1:get-fields fsym binary?)] (let* ([field-dvecs (query1:get-fields fsym binary?)]
[rows (query1:get-rows fsym field-dvecs binary? wbox)]) [rows (query1:get-rows fsym field-dvecs binary? wbox)])
(vector 'recordset field-dvecs rows))]))) (vector 'rows field-dvecs rows))])))
(define/private (query1:get-fields fsym binary?) (define/private (query1:get-fields fsym binary?)
(let ([r (recv fsym 'field)]) (let ([r (recv fsym 'field)])
@ -317,8 +317,8 @@
(define/private (query1:process-result fsym result) (define/private (query1:process-result fsym result)
(match result (match result
[(vector 'recordset field-dvecs rows) [(vector 'rows field-dvecs rows)
(recordset (map field-dvec->field-info field-dvecs) rows)] (rows-result (map field-dvec->field-info field-dvecs) rows)]
[(vector 'command command-info) [(vector 'command command-info)
(simple-result command-info)])) (simple-result command-info)]))
@ -377,7 +377,7 @@
[i (in-naturals)]) [i (in-naturals)])
(and (equal? (field-dvec->name dvec) name) i))) (and (equal? (field-dvec->name dvec) name) i)))
(match result (match result
[(vector 'recordset field-dvecs rows) [(vector 'rows field-dvecs rows)
(let ([code-index (find-index "Code" field-dvecs)] (let ([code-index (find-index "Code" field-dvecs)]
[message-index (find-index "Message" field-dvecs)]) [message-index (find-index "Message" field-dvecs)])
(for ([row (in-list rows)]) (for ([row (in-list rows)])
@ -506,7 +506,7 @@ According to that page, the following statements may be prepared:
CALL, CREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE, CALL, CREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE,
and most SHOW statements and most SHOW statements
On the other hand, we want to force all recordset-returning statements On the other hand, we want to force all rows-returning statements
through the prepared-statement path to use the binary data through the prepared-statement path to use the binary data
protocol. That would seem to be the following: protocol. That would seem to be the following:

View File

@ -54,7 +54,7 @@
(check-valid-tx-status fsym) (check-valid-tx-status fsym)
(query1 fsym stmt)))]) (query1 fsym stmt)))])
(statement:after-exec stmt*) (statement:after-exec stmt*)
(cond [(pair? dvecs) (recordset (map field-dvec->field-info dvecs) rows)] (cond [(pair? dvecs) (rows-result (map field-dvec->field-info dvecs) rows)]
[else (simple-result '())]))) [else (simple-result '())])))
(define/private (query1 fsym stmt) (define/private (query1 fsym stmt)
@ -534,8 +534,8 @@
(handle-status fsym (SQLFreeHandle SQL_HANDLE_STMT stmt) stmt) (handle-status fsym (SQLFreeHandle SQL_HANDLE_STMT stmt) stmt)
(values result-dvecs rows))))) (values result-dvecs rows)))))
;; Layout is: #(catalog schema table table-type remark) ;; Layout is: #(catalog schema table table-type remark)
(recordset (map field-dvec->field-info dvecs) (rows-result (map field-dvec->field-info dvecs)
rows)) rows))
|# |#
;; Handler ;; Handler

View File

@ -283,7 +283,7 @@
[(struct RowDescription (field-dvecs)) [(struct RowDescription (field-dvecs))
(let* ([rows (query1:data-loop fsym)]) (let* ([rows (query1:data-loop fsym)])
(query1:expect-close-complete fsym) (query1:expect-close-complete fsym)
(vector 'recordset field-dvecs rows))] (vector 'rows field-dvecs rows))]
[(struct NoData ()) [(struct NoData ())
(let* ([command (query1:expect-completion fsym)]) (let* ([command (query1:expect-completion fsym)])
(query1:expect-close-complete fsym) (query1:expect-close-complete fsym)
@ -318,7 +318,7 @@
(define/private (query1:process-result fsym result) (define/private (query1:process-result fsym result)
(match result (match result
[(vector 'recordset field-dvecs rows) [(vector 'rows field-dvecs rows)
(let* ([type-reader-v (let* ([type-reader-v
(list->vector (query1:get-type-readers fsym field-dvecs))] (list->vector (query1:get-type-readers fsym field-dvecs))]
[convert-row! [convert-row!
@ -329,7 +329,7 @@
row row
type-reader-v))]) type-reader-v))])
(for-each convert-row! rows) (for-each convert-row! rows)
(recordset (map field-dvec->field-info field-dvecs) rows))] (rows-result (map field-dvec->field-info field-dvecs) rows))]
[(vector 'command command) [(vector 'command command)
(simple-result command)])) (simple-result command)]))

View File

@ -42,7 +42,7 @@
(check-valid-tx-status fsym) (check-valid-tx-status fsym)
(query1 fsym stmt)))]) (query1 fsym stmt)))])
(statement:after-exec stmt) (statement:after-exec stmt)
(cond [(pair? info) (recordset info rows)] (cond [(pair? info) (rows-result info rows)]
[else (simple-result '())]))) [else (simple-result '())])))
(define/private (query1 fsym stmt) (define/private (query1 fsym stmt)

View File

@ -64,14 +64,14 @@ system to system and is subject to change.)
[(query pgc "insert into the_numbers values (3, 'a crowd')") [(query pgc "insert into the_numbers values (3, 'a crowd')")
(simple-result '((command insert 0 1)))] (simple-result '((command insert 0 1)))]
[(query pgc "select n, d from the_numbers where n % 2 = 0") [(query pgc "select n, d from the_numbers where n % 2 = 0")
(recordset (rows-result
(list (list
'((name . "n") (typeid . 23)) '((name . "n") (typeid . 23))
'((name . "d") (typeid . 1043))) '((name . "d") (typeid . 1043)))
'(#(0 "nothing") #(2 "company")))] '(#(0 "nothing") #(2 "company")))]
] ]
When the query is known to return a recordset and when the field When the query is known to return rows and when the field
descriptions are not needed, it is more convenient to use the descriptions are not needed, it is more convenient to use the
@racket[query-rows] function. @racket[query-rows] function.
@ -80,16 +80,16 @@ descriptions are not needed, it is more convenient to use the
'(#(0 "nothing") #(2 "company"))] '(#(0 "nothing") #(2 "company"))]
] ]
Use @racket[query-row] for queries that are known to return a Use @racket[query-row] for queries that are known to return exactly
recordset of exactly one row. one row.
@my-interaction[ @my-interaction[
[(query-row pgc "select * from the_numbers where n = 0") [(query-row pgc "select * from the_numbers where n = 0")
(vector 0 "nothing")] (vector 0 "nothing")]
] ]
Similarly, use @racket[query-list] for queries that produce a Similarly, use @racket[query-list] for queries that produce rows of
recordset of exactly one column. exactly one column.
@my-interaction[ @my-interaction[
[(query-list pgc "select d from the_numbers order by n") [(query-list pgc "select d from the_numbers order by n")

View File

@ -89,8 +89,8 @@ All query functions require both a connection and a
The simple query API consists of a set of functions specialized to The simple query API consists of a set of functions specialized to
various types of queries. For example, @racket[query-value] is various types of queries. For example, @racket[query-value] is
specialized to queries that return a recordset of exactly one column specialized to queries that return exactly one row of exactly one
and exactly one row. column.
If a statement takes parameters, the parameter values are given as If a statement takes parameters, the parameter values are given as
additional arguments immediately after the SQL statement. Only a additional arguments immediately after the SQL statement. Only a
@ -121,8 +121,8 @@ The types of parameters and returned fields are described in
[arg any/c] ...) [arg any/c] ...)
(listof vector?)]{ (listof vector?)]{
Executes a SQL query, which must produce a recordset, and returns the Executes a SQL query, which must produce rows, and returns the list
list of rows (as vectors) from the query. of rows (as vectors) from the query.
@examples/results[ @examples/results[
[(query-rows pgc "select * from the_numbers where n = $1" 2) [(query-rows pgc "select * from the_numbers where n = $1" 2)
@ -137,7 +137,7 @@ The types of parameters and returned fields are described in
[arg any/c] ...) [arg any/c] ...)
list?]{ list?]{
Executes a SQL query, which must produce a recordset of exactly one Executes a SQL query, which must produce rows of exactly one
column, and returns the list of values from the query. column, and returns the list of values from the query.
@examples/results[ @examples/results[
@ -153,8 +153,8 @@ The types of parameters and returned fields are described in
[arg any/c] ...) [arg any/c] ...)
vector?]{ vector?]{
Executes a SQL query, which must produce a recordset of exactly one Executes a SQL query, which must produce exactly one row, and
row, and returns its (single) row result as a vector. returns its (single) row result as a vector.
@examples/results[ @examples/results[
[(query-row myc "select * from the_numbers where n = ?" 2) [(query-row myc "select * from the_numbers where n = ?" 2)
@ -185,8 +185,8 @@ The types of parameters and returned fields are described in
[arg any/c] ...) [arg any/c] ...)
any/c]{ any/c]{
Executes a SQL query, which must produce a recordset of exactly one Executes a SQL query, which must produce exactly one row of exactly
column and exactly one row, and returns its single value result. one column, and returns its single value result.
@examples/results[ @examples/results[
[(query-value pgc "select timestamp 'epoch'") [(query-value pgc "select timestamp 'epoch'")
@ -217,9 +217,9 @@ The types of parameters and returned fields are described in
[arg any/c] ...) [arg any/c] ...)
sequence?]{ sequence?]{
Executes a SQL query, which must produce a recordset, and returns a Executes a SQL query, which must produce rows, and returns a
sequence. Each step in the sequence produces as many values as the sequence. Each step in the sequence produces as many values as the
recordset has columns. rows have columns.
@examples/results[ @examples/results[
[(for/list ([n (in-query pgc "select n from the_numbers where n < 2")]) [(for/list ([n (in-query pgc "select n from the_numbers where n < 2")])
@ -249,7 +249,7 @@ based on the number of variables in the clause's left-hand side:
@section{General Query Support} @section{General Query Support}
A general query result is either a @racket[simple-result] or a A general query result is either a @racket[simple-result] or a
@racket[recordset]. @racket[rows-result].
@defstruct*[simple-result @defstruct*[simple-result
([info any/c])]{ ([info any/c])]{
@ -262,7 +262,7 @@ rely on its contents; it varies based on database system and may
change in future versions of this library (even new minor versions). change in future versions of this library (even new minor versions).
} }
@defstruct*[recordset @defstruct*[rows-result
([headers (listof any/c)] ([headers (listof any/c)]
[rows (listof vector?)])]{ [rows (listof vector?)])]{
@ -279,11 +279,11 @@ future version of this library (even new minor versions).
@defproc[(query [connection connection?] @defproc[(query [connection connection?]
[stmt statement?] [stmt statement?]
[arg any/c] ...) [arg any/c] ...)
(or/c simple-result? recordset?)]{ (or/c simple-result? rows-result?)]{
Executes a query, returning a structure that describes the Executes a query, returning a structure that describes the
results. Unlike the more specialized query functions, @racket[query] results. Unlike the more specialized query functions, @racket[query]
supports both recordset-returning and effect-only queries. supports both rows-returning and effect-only queries.
} }
@ -347,11 +347,11 @@ closed.
@defproc[(prepared-statement-result-types [pst prepared-statement?]) @defproc[(prepared-statement-result-types [pst prepared-statement?])
(listof (list/c boolean? (or/c symbol? #f) any/c))]{ (listof (list/c boolean? (or/c symbol? #f) any/c))]{
If @racket[pst] is a recordset-producing statement (eg, If @racket[pst] is a rows-returning statement (eg, @tt{SELECT}),
@tt{SELECT}), returns a list of type descriptions as described returns a list of type descriptions as described above, identifying
above, identifying the SQL types (or pseudotypes) of the result the SQL types (or pseudotypes) of the result columns. If
columns. If @racket[pst] does not produce a recordset, the function @racket[pst] is not a rows-returning statement, the function returns
returns the empty list. the empty list.
} }
@defproc[(bind-prepared-statement @defproc[(bind-prepared-statement

View File

@ -139,10 +139,10 @@
(test-case "query - select" (test-case "query - select"
(with-connection c (with-connection c
(let [(q (query c "select N from the_numbers"))] (let [(q (query c "select N from the_numbers"))]
(check-pred recordset? q) (check-pred rows-result? q)
(check set-equal? (check set-equal?
(map vector (map car test-data)) (map vector (map car test-data))
(recordset-rows q))))) (rows-result-rows q)))))
(test-case "query - update" (test-case "query - update"
(unless (ANYFLAGS 'isora 'isdb2) (unless (ANYFLAGS 'isora 'isdb2)
(with-connection c (with-connection c