(module sql-update-internals-unit mzscheme
(require (lib "plt-match.ss")
(lib "unitsig.ss")
(lib "list.ss" "srfi" "1")
(lib "string.ss" "srfi" "13"))
(require (file "../base.ss")
(file "../era.ss")
(file "../type.ss")
(file "sql-sig.ss"))
(provide sql-update-internals@)
(define sql-update-internals@
(unit/sig sql-update-internals^
(import sql-quote^)
(define (create-fields-sql entity)
(string-join (map (match-lambda
[(struct attribute (name (app type-base base)))
(cond [(eq? base type:id) (string-append (quote-id name) " INTEGER PRIMARY KEY")]
[(eq? base type:revision) (string-append (quote-id name) " INTEGER NOT NULL DEFAULT 0")]
[(eq? base type:text) (string-append (quote-id name) " TEXT")]
[(eq? base type:integer) (string-append (quote-id name) " INTEGER")]
[(eq? base type:real) (string-append (quote-id name) " REAL")]
[(eq? base type:symbol) (string-append (quote-id name) " TEXT")]
[(eq? base type:boolean) (string-append (quote-id name) " INTEGER")]
[(eq? base type:time-tai) (string-append (quote-id name) " INTEGER")]
[else (raise-exn exn:fail:snooze
(format "Unrecognised field type: ~a" base))])])
(entity-fields entity))
", "))
(define (column-names entity)
(map (match-lambda
[(struct attribute (name type))
(quote-id name)])
(entity-fields entity)))
(define (column-values struct)
(map (match-lambda*
[(list (struct attribute (name type)) val)
(quote-data type val)])
(entity-fields (struct-entity struct))
(get-attribute-values struct)))
))
)