#lang scheme/base
(require "../../base.ss"
"type.ss")
(define-struct table (name [columns #:mutable] [constraints #:mutable]) #:transparent)
(define-struct column (table name type not-null? default) #:transparent)
(define-struct constraint (table name) #:transparent)
(define-struct (primary-key constraint) (columns) #:transparent)
(define-struct (foreign-key constraint) (local foreign) #:transparent)
(define-struct (unique-constraint constraint) (columns) #:transparent)
(define-struct (check-constraint constraint) (expression) #:transparent)
(define-struct relation () #:transparent)
(define-struct (table-alias relation) (name table) #:transparent)
(define-struct (select-alias relation) (name select) #:transparent)
(define-struct (join relation) (op left right on) #:transparent)
(define-struct expression (type) #:transparent)
(define-struct (attribute expression) (name) #:transparent)
(define-struct (column-alias attribute) (table-alias column) #:transparent)
(define-struct (expression-alias attribute) (value) #:transparent)
(define-struct (function expression) (op args) #:transparent)
(define-struct (aggregate function) () #:transparent)
(define-struct (literal expression) (value) #:transparent)
(define-struct order (expression direction) #:transparent)
(define-struct query () #:transparent)
(define-struct (create query) (table) #:transparent)
(define-struct (drop query) (table) #:transparent)
(define-struct (insert query) (table columns data) #:transparent)
(define-struct (delete query) (table using where) #:transparent)
(define-struct select
(what distinct from where group order having limit offset local-columns imported-columns)
#:transparent)
(define function-arg/c
(or/c expression? select-alias? table-alias? select? (listof expression?)))
(define aggregate-arg/c
(or/c function-arg/c table-alias? select-alias?))
(provide/contract
[function-arg/c contract?]
[aggregate-arg/c contract?]
[struct table ([name symbol?]
[columns (listof column?)]
[constraints (listof constraint?)])]
[struct column ([table table?]
[name symbol?]
[type type?]
[not-null? boolean?]
[default any/c])]
[struct constraint ([table table?]
[name symbol?])]
[struct primary-key ([table table?]
[name symbol?]
[columns (listof column?)])]
[struct foreign-key ([table table?]
[name symbol?]
[local column?]
[foreign column?])]
[struct unique-constraint ([table table?]
[name symbol?]
[columns (listof column?)])]
[struct check-constraint ([table table?]
[name symbol?]
[expression expression?])]
[struct relation ()]
[struct (table-alias relation) ([name symbol?]
[table table?])]
[struct (select-alias relation) ([name symbol?]
[select select?])]
[struct (join relation) ([op symbol?]
[left relation?]
[right relation?]
[on (or/c expression? false/c)])]
[struct expression ([type type?])]
[struct (attribute expression) ([type type?]
[name symbol?])]
[struct (column-alias attribute) ([type type?]
[name symbol?]
[table-alias table-alias?]
[column column?])]
[struct (expression-alias attribute) ([type type?]
[name symbol?]
[value expression?])]
[struct (function expression) ([type type?]
[op symbol?]
[args (listof function-arg/c)])]
[struct (aggregate function) ([type type?]
[op symbol?]
[args (listof function-arg/c)])]
[struct (literal expression) ([type type?]
[value any/c])]
[struct order ([expression expression?]
[direction (symbols 'asc 'desc)])]
[struct query ()]
[struct (create query) ([table table?])]
[struct (drop query) ([table table?])]
[struct (insert query) ([table table?]
[columns (listof column?)]
[data (listof list?)])]
[struct (delete query) ([table table?]
[using (or/c relation? false/c)]
[where (or/c expression? false/c)])]
[struct select ([what (listof column?)]
[distinct (or/c (listof expression?) false/c)]
[from relation?]
[where (or/c expression? false/c)]
[group (listof expression?)]
[order (listof order?)]
[having (or/c expression? false/c)]
[limit (or/c integer? false/c)]
[offset (or/c integer? false/c)]
[local-columns (listof attribute?)]
[imported-columns (listof attribute?)])])