13 Relations
The relation structure itself is immutable, but they may contains mutable field values. Mutating a field value may cause the relation’s lookups to behave incorrectly.
The relations perform all field comparisons using equal?.
(require scramble/relation) | package: scramble-lib |
Added in version 0.4 of package scramble-lib.
procedure
(relation-heading rel) → (vectorof symbol?)
rel : relation?
procedure
(relation-tuples rel) → (vectorof vector?)
rel : relation?
syntax
(relation maybe-name heading tuples lookup ...)
maybe-name =
| #:name name-expr heading = #:heading [field-name-expr ...] tuples = #:tuples [field-value-expr ...] ... lookup = #:lookup field-name-expr | #:unique field-name-expr
field-name-expr : symbol?
A “lookup” would be called an “index” in database terminology, but this library uses the term “lookup” to avoid ambiguity with “index” as a numeric position.
> (define food (relation #:name 'food #:heading ['food 'type 'color] #:tuples ['apple 'fruit 'red] ['banana 'fruit 'yellow] ['shishkebab 'meat 'brown] ['artichoke 'vegetable 'green] ['broccoli 'vegetable 'green] #:unique 'food)) > food #<relation:food>
procedure
(make-relation name heading tuples) → relation?
name : (or/c #f symbol?) heading : (or/c (listof symbol?) (vectorof symbol?))
tuples :
(let ([tuple/c (or/c list? vector?)]) (or/c (listof tuple/c) (vectorof tuple/c)))
procedure
(relation-add-lookup rel field unique?) → relation?
rel : relation? field : symbol? unique? : boolean?
procedure
(relation-index rel field)
→ (or/c #f exact-nonnegative-integer?) rel : relation? field : symbol?
> (relation-index food 'food) 0
> (relation-index food 'color) 2
> (relation-index food 'calories) #f
procedure
(in-relation rel field/s) → sequence?
rel : relation? field/s : (or/c symbol? (listof symbol?))
> (sequence->list (in-relation food 'type)) '(fruit fruit meat vegetable vegetable)
> (sequence->list (in-relation food '(food type)))
'((apple fruit)
(banana fruit)
(shishkebab meat)
(artichoke vegetable)
(broccoli vegetable))
procedure
(relation-find rel key-field key-value [all?])
→ (if/c all? (listof vector?) (or/c vector? #f)) rel : relation? key-field : symbol? key-value : any/c all? : boolean? = #f
> (relation-find food 'type 'fruit) '#(apple fruit red)
> (relation-find food 'type 'vegetable #t) '(#(artichoke vegetable green) #(broccoli vegetable green))
procedure
(relation-ref rel key-field key-value result-field/s [ default]) → any rel : relation? key-field : symbol? key-value : any/c result-field/s : (or/c symbol? (listof symbol?)) default : any/c = #f
If result-field/s is a single symbol, then the corresponding field value is returned. If result-field/s is a list of symbols, then a list of the corresponding field values is returned.
If rel contains no matching tuple, default is called if it is a procedure or returned otherwise. Note that unlike hash-ref, the default value of dfault is #f, not an error-raising procedure.
> (relation-ref food 'food 'apple 'type) 'fruit
> (relation-ref food 'food 'brie 'type) #f
> (relation-ref food 'food 'artichoke '(type color)) '(vegetable green)
procedure
(relation-ref-all rel key-field key-value result-field/s) → list? rel : relation? key-field : symbol? key-value : any/c result-field/s : (or/c symbool? (listof symbol?))
> (relation-ref-all food 'type 'fruit '(food color)) '((apple red) (banana yellow))
procedure
(relation-prepare-find rel key-field [all?])
→
(-> any/c (if/c all? (listof vector?) (or/c #f vector?))) rel : relation? key-field : symbol? all? : boolean? = #f
procedure
(relation-prepare-ref rel key-field result-field/s) → (->* [any/c] [any/c] any) rel : relation? key-field : symbol? result-field/s : (or/c symbol? (listof symbol?))
procedure
(relation-prepare-ref-all rel key-field result-field/s) → (-> any/c list?) rel : relation? key-field : symbol? result-field/s : (or/c symbol? (listof symbol?))
> (define food-color (relation-prepare-ref food 'food 'color)) > (food-color 'banana) 'yellow
> (food-color 'artichoke) 'green
> (food-color 'haggis 'unknown) 'unknown