Changeset 14792 in project


Ignore:
Timestamp:
05/26/09 17:41:57 (10 years ago)
Author:
sjamaan
Message:

Make the interface more awesome by making row/column numbers optional

Location:
release/4/postgresql/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/4/postgresql/trunk/postgresql.scm

    r14769 r14792  
    392392
    393393;; Unchecked version, for speed
    394 (define (value-at* result row column #!key raw)
     394(define (value-at* result column row #!key raw)
    395395  (if (PQgetisnull (pg-result-ptr result) row column)
    396396      (sql-null)
     
    410410            ((vector-ref (pg-result-value-parsers result) column) value)))))
    411411
    412 (define (value-at result row column #!key raw)
     412(define (value-at result #!optional (column 0) (row 0) #!key raw)
    413413  (check-row-index! result row 'value)
    414414  (check-column-index! result column 'value)
    415   (value-at* result row column raw: raw))
    416 
    417 (define (row-values result row #!key raw)
     415  (value-at* result column row raw: raw))
     416
     417(define (row-values result #!optional (row 0) #!key raw)
    418418  (check-row-index! result row 'row)
    419419  (let loop ((list '())
     
    421421    (if (= column 0)
    422422        list
    423         (loop (cons (value-at* result row (sub1 column) raw: raw) list)
     423        (loop (cons (value-at* result (sub1 column) row raw: raw) list)
    424424              (sub1 column)))))
    425425
    426 (define (column-values result column #!key raw)
     426(define (column-values result #!optional (column 0) #!key raw)
    427427  (check-column-index! result column 'column)
    428428  (let loop ((list '())
     
    430430    (if (= row 0)
    431431        list
    432         (loop (cons (value-at* result (sub1 row) column raw: raw) list)
     432        (loop (cons (value-at* result column (sub1 row) raw: raw) list)
    433433              (sub1 row)))))
    434434
    435 ;; (define (row-alist result row)
     435;; (define (row-alist result #!optional (row 0))
    436436;;   (map cons (column-names result) (row-values result row)))
    437 (define (row-alist result row)
     437(define (row-alist result #!optional (row 0))
    438438  (check-row-index! result row 'row-alist)
    439439  (let loop ((alist '())
     
    443443        (loop (cons (cons (string->symbol
    444444                           (PQfname (pg-result-ptr result) (sub1 column)))
    445                           (value-at* result row (sub1 column))) alist)
     445                          (value-at* result (sub1 column) row)) alist)
    446446              (sub1 column)))))
    447447
  • release/4/postgresql/trunk/tests/run.scm

    r14768 r14792  
    109109  (test "Result value string for strings"
    110110        "test"
    111         (value-at
    112          (query conn "SELECT 'test'") 0 0))
     111        (value-at (query conn "SELECT 'test'")))
    113112  (test "Result row values"
    114113        '("one" "two")
    115114        (row-values
    116115         (query conn "SELECT 'one', 'two' UNION SELECT 'three', 'four'") 0))
     116  (test "Result row values for second row"
     117        '("three" "four")
     118        (row-values
     119         (query conn "SELECT 'one', 'two' UNION SELECT 'three', 'four'") 1))
    117120  (test "Result row alist"
    118121        '((a . "one") (b . "two"))
    119122        (row-alist
    120123         (query conn "SELECT 'one' AS a, 'two' AS b UNION SELECT 'three', 'four'") 0))
     124  (test "Result row alist for second row"
     125        '((a . "three") (b . "four"))
     126        (row-alist
     127         (query conn "SELECT 'one' AS a, 'two' AS b UNION SELECT 'three', 'four'") 1))
    121128  (test "Result column values"
    122129        '("one" "three")
    123130        (column-values
    124131         (query conn "SELECT 'one', 'two' UNION SELECT 'three', 'four'") 0))
     132  (test "Result column values for second column"
     133        '("two" "four")
     134        (column-values
     135         (query conn "SELECT 'one', 'two' UNION SELECT 'three', 'four'") 1))
    125136  (test "Result value number for numbers"
    126137        1
    127         (value-at
    128          (query conn "SELECT 1") 0 0))
     138        (value-at (query conn "SELECT 1")))
    129139  (test "Result value string for raw numbers"
    130140        "1"
    131         (value-at
    132          (query conn "SELECT 1") 0 0 raw: #t))
     141        (value-at (query conn "SELECT 1") 0 0 raw: #t))
    133142  ;; We are using two levels of escaping here because the ::bytea cast
    134143  ;; performs another string interpretation. Yes, this is kinda confusing...
    135144  (test "Result value for null-terminated byte array"
    136145        (blob->u8vector (string->blob "h\x00ello"))
    137         (value-at
    138          (query conn "SELECT E'h\\\\000ello'::bytea") 0 0))
     146        (value-at (query conn "SELECT E'h\\\\000ello'::bytea")))
    139147  (test "Result value for raw null-terminated byte array"
    140148        "h\\000ello"
    141         (value-at
    142          (query conn "SELECT E'h\\\\000ello'::bytea") 0 0 raw: #t))
     149        (value-at (query conn "SELECT E'h\\\\000ello'::bytea") 0 0 raw: #t))
    143150
    144151  (test "Result value blob for binary string"
    145152        (string->blob "hello")
    146         (value-at
    147          (query* conn "SELECT 'hello'" '() format: 'binary) 0 0))
     153        (value-at (query* conn "SELECT 'hello'" '() format: 'binary)))
    148154 
    149155  (test "Result value blob for binary integer"
    150156        (u8vector->blob (u8vector 0 0 0 1))
    151         (value-at (query* conn "SELECT 1::int4" '() format: 'binary) 0 0))
     157        (value-at (query* conn "SELECT 1::int4" '() format: 'binary)))
    152158
    153159  (test "Result value for binary string with NUL bytes"
    154160        (string->blob "h\x00ello")
    155         (value-at (query* conn "SELECT E'h\\\\000ello'::bytea" '() format: 'binary) 0 0))
    156 
     161        (value-at (query* conn "SELECT E'h\\\\000ello'::bytea" '() format: 'binary)))
     162
     163  (test "Result value at row 0, column 1"
     164        2
     165        (value-at (query conn "SELECT 1, 2 UNION SELECT 3, 4") 1 0))
     166  (test "Result value at row 1, column 0"
     167        3
     168        (value-at (query conn "SELECT 1, 2 UNION SELECT 3, 4") 0 1))
    157169  (test-assert "Result value sql-null for NULL"
    158                (sql-null? (value-at
    159                            (query conn "SELECT NULL") 0 0)))
     170               (sql-null? (value-at (query conn "SELECT NULL"))))
     171  (test-error "Result value error for out of bounds row"
     172              (value-at (query conn "SELECT NULL") 0 1))
    160173  (test-error "Result value error for out of bounds column"
    161               (value-at
    162                (query conn "SELECT NULL") 0 1))
    163   (test-error "Result value error for out of bounds row"
    164               (value-at
    165                (query conn "SELECT NULL") 1 0))
     174              (value-at (query conn "SELECT NULL") 1 0))
    166175  (test "Number of affected rows false with SELECT"
    167176        #f
     
    219228  (test "boolean parameters"
    220229        '(#t #f)
    221         (row-values (query conn "SELECT $1::bool, $2::bool" #t #f) 0)))
     230        (row-values (query conn "SELECT $1::bool, $2::bool" #t #f))))
    222231
    223232(test-group "value escaping"
Note: See TracChangeset for help on using the changeset viewer.