Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.52 KB | None | 0 0
  1. ;;; Holy yeah
  2.  
  3. #lang racket
  4.  
  5. (require (for-syntax racket/syntax))
  6. (require racket/format)
  7.  
  8.  
  9. (provide (all-from-out racket)
  10. CREATE
  11. INSERT
  12. SELECT
  13. DELETE
  14. EXPORT
  15. ;;; EVAL
  16. ;;; PARSE-FUNC
  17. ;;; PARSE-NAME
  18. ;;; PARSE-NAMES-SELECT
  19. ;;; PARSE-FUNC-SELECT
  20. ;;; SHOW
  21. ;;; convert-contents
  22. ;;; natural-join
  23. ;;; natural-join-relations
  24. ;;; relation-contents
  25. ;;; write-contents
  26. ;;; triple-name
  27. ;;; triple-value
  28. )
  29.  
  30. (struct triple (name type value)
  31. #:mutable
  32. #:transparent
  33. )
  34.  
  35. (struct tuple (name type)
  36. #:mutable
  37. #:transparent
  38. )
  39.  
  40. (struct relation (headers contents)
  41. #:mutable
  42. #:transparent
  43. )
  44.  
  45. ;------------------------------------------- Some utility functions ---------------------------------------
  46. (define/contract (triple-contain-name? trpl name)
  47. (triple? symbol? . -> . boolean?)
  48. (equal? (triple-name trpl) name)
  49. )
  50.  
  51. (define/contract (tuple-contain-name? attr name)
  52. (tuple? symbol? . -> . boolean?)
  53. (equal? (tuple-name attr) name)
  54. )
  55.  
  56. (define (pretty-print-f str)
  57. (define b (~s str
  58. #:align 'center
  59. #:width 15
  60. #:pad-string " "))
  61. (display b)
  62. )
  63.  
  64. (define (write-headers hdrs)
  65. (define (match-type t)
  66. (cond [(equal? t integer?) 'integer]
  67. [(equal? t string?) 'string]
  68. [else 'other]
  69. )
  70. )
  71.  
  72. (define (write-headers-name hds)
  73. (cond [(empty? hds) (printf "~n")]
  74. [else (
  75. let ()
  76. ;;; (pretty-print-f (string->symbol (tuple-name (car hds))))
  77. (pretty-print-f (tuple-name (car hds)))
  78. (printf "|")
  79. (write-headers-name (cdr hds))
  80. )]
  81. )
  82. )
  83.  
  84. (define (write-headers-type hds)
  85. (cond [(empty? hds) (printf "~n")]
  86. [else (
  87. let ()
  88. (pretty-print-f (match-type (tuple-type (car hds))))
  89. (printf "|")
  90. (write-headers-type (cdr hds))
  91. )]
  92. )
  93. )
  94. (printf "|")
  95. (write-headers-name hdrs)
  96. (printf "|")
  97. (write-headers-type hdrs)
  98. )
  99.  
  100. (define (write-contents cnts)
  101. (for-each
  102. (lambda (cnt)
  103. (for-each
  104. (lambda (trpl)
  105. (printf "|")
  106. (pretty-print-f (triple-value trpl))
  107. )
  108. cnt
  109. )
  110. (printf "|")
  111. (printf "~n")
  112. )
  113. cnts
  114. )
  115. )
  116.  
  117. (define (SHOW rel)
  118. (define headers (relation-headers rel))
  119. (define contents (relation-contents rel))
  120.  
  121. (define n-cols (length headers))
  122. (define n-chars (+ (* n-cols 16) 1))
  123.  
  124.  
  125.  
  126.  
  127.  
  128. (display (make-string n-chars #\-)) (newline)
  129.  
  130. (write-headers headers)
  131. (display (make-string n-chars #\-)) (newline)
  132. (write-contents contents)
  133. (display (make-string n-chars #\-)) (newline)
  134. )
  135.  
  136. ; content -> get value by name
  137. (define (content-get-value-by-name content name)
  138.  
  139. (define found-triple
  140. (filter (lambda (trpl) (equal? (triple-name trpl) name)) content)
  141. )
  142.  
  143. (if (empty? found-triple) (error "Not have the column ~a" name) (triple-value (car found-triple)))
  144. )
  145.  
  146. ; Two have same col names
  147. (define (union-two-relations rel1 rel2)
  148. (define headers (relation-headers rel1))
  149. (define union-contents (remove-duplicates (append (relation-contents rel1) (relation-contents rel2)) equal?))
  150. (relation headers union-contents)
  151. )
  152.  
  153. (define (intersect-two-relations rel1 rel2)
  154. (define headers (relation-headers rel1))
  155. (define intersect-contents (
  156. filter (lambda (cnt) (member cnt (relation-contents rel2))) (relation-contents rel1)
  157. ))
  158. (relation headers intersect-contents)
  159. )
  160.  
  161. (define (union-two-lists-contents contents1 contents2)
  162. (remove-duplicates (append contents1 contents2) equal?)
  163. )
  164.  
  165. (define (intersect-two-lists-contents contents1 contents2)
  166. (filter (lambda (cnt) (member cnt contents2)) contents1)
  167. )
  168.  
  169. ;------------------------------------------- END ---------------------------------------
  170.  
  171. (define-syntax (print-hehe stx)
  172. (define (conv expression)
  173. (with-syntax ([e expression])
  174. (symbol->string (quote e)))
  175. )
  176.  
  177. (syntax-case stx ()
  178. [(_ expr)
  179. (with-syntax
  180. (
  181. )
  182.  
  183. #'(begin
  184. (write expr
  185. )
  186. (newline)
  187. )
  188. )
  189. ]
  190. )
  191. )
  192.  
  193. (define (make-empty-relation raw-headers)
  194.  
  195. (define (match-type t)
  196. (cond [(equal? t 'integer) integer?]
  197. [(equal? t 'string) string?]
  198. [else (error "Wrong type...")]
  199. )
  200. )
  201.  
  202. (define (looop hdrs)
  203. (cond [(empty? hdrs) '()]
  204. [else (
  205. let ()
  206. (define h (car hdrs))
  207. (define name (car h))
  208. (define type (match-type (car (cdr h))))
  209.  
  210. ;;; (write name) (newline)
  211. ;;; (write type) (newline)
  212.  
  213. (cons (tuple name type) (looop (cdr hdrs)))
  214. )]
  215. )
  216. )
  217.  
  218. (define headers (looop raw-headers))
  219. (relation headers '())
  220. )
  221.  
  222. (define-syntax (CREATE stx)
  223. (define (conv expression)
  224. (with-syntax ([e expression])
  225. #'(symbol->string (quote e)))
  226. )
  227.  
  228. (define (conv-header expression)
  229. (with-syntax ([e expression])
  230. #'(quote e)
  231. )
  232. )
  233.  
  234. (syntax-case stx ()
  235. [
  236. (_ relname (raw-header ...))
  237.  
  238. (let
  239. ()
  240.  
  241. (with-syntax
  242. (
  243. [
  244. (converted-header ...)
  245. (map conv-header
  246. (syntax->list #' (raw-header ...))
  247. )
  248. ]
  249. )
  250.  
  251. #'(begin
  252. ;;; (define headers (list converted-header ...))
  253. (define relname (make-empty-relation (list converted-header ...)))
  254. )
  255. )
  256. )
  257. ]
  258. )
  259. )
  260.  
  261. ;;; contents are raw (just list of list)
  262. (define (insert-raw rel raw-contents)
  263. (define headers (relation-headers rel))
  264.  
  265. (define syntax-ok #t)
  266.  
  267. (define (loop-triples h b)
  268. (cond [(empty? h) '()]
  269. [else (
  270. let ()
  271. (define name (tuple-name (car h)))
  272. (define value (car b))
  273. (define type (tuple-type (car h)))
  274.  
  275. (if (type value)
  276. (cons (triple name type value) (loop-triples (cdr h) (cdr b)))
  277. (begin
  278. (set! syntax-ok #f)
  279. '()
  280. )
  281. )
  282. )]
  283. )
  284. )
  285.  
  286. (define (loop-rows h bs)
  287. (cond [(empty? bs) '()]
  288. [else (
  289. let ()
  290. (if syntax-ok
  291.  
  292. (let ()
  293. (define b (car bs))
  294. (if (= (length h) (length b))
  295. (cons (loop-triples h b) (loop-rows h (cdr bs)))
  296. (begin
  297. ;;; (write "raw fail 2") (newline)
  298. (set! syntax-ok #f)
  299. '()
  300. )
  301. )
  302. )
  303.  
  304. '()
  305. )
  306. )]
  307. )
  308. )
  309.  
  310. (cond
  311. [(empty? raw-contents) (void)]
  312.  
  313. [(list? (car raw-contents)) (
  314. let ()
  315. (define contents (loop-rows headers raw-contents))
  316. (if syntax-ok
  317. (let ([appended-contents (remove-duplicates (append (relation-contents rel) contents))])
  318. (set-relation-contents! rel appended-contents)
  319. )
  320.  
  321. (begin
  322. (display "Error input - no rows inserted ...") (newline)
  323. )
  324. )
  325. )]
  326.  
  327. [else (
  328. let ()
  329. (define contents (loop-rows headers (list raw-contents)))
  330. (if syntax-ok
  331. (let ([appended-contents (remove-duplicates (append (relation-contents rel) contents))])
  332. (set-relation-contents! rel appended-contents)
  333. )
  334.  
  335. (begin
  336. (display "Error input - no rows inserted ...") (newline)
  337. )
  338. )
  339. )]
  340. )
  341. )
  342.  
  343. ;;; contents are formatted - in the same form as contents of relations
  344. (define (insert rel contents)
  345. (define headers (relation-headers rel))
  346.  
  347. (define syntax-ok #t)
  348.  
  349. (define (loop-triples h b)
  350. (cond [(empty? h) '()]
  351. [else (
  352. let ()
  353. (define trpl (car b))
  354. (define name (tuple-name (car h)))
  355. (define type (tuple-type (car h)))
  356. (define value (triple-value trpl))
  357. (define name-t (triple-name trpl))
  358. (define type-t (triple-type trpl))
  359.  
  360. (if (and (and (equal? name name-t) (equal? type type-t)) (type value))
  361. (cons (triple name type value) (loop-triples (cdr h) (cdr b)))
  362. (begin
  363. (write "bt fail 1") (newline)
  364. (write "b") (newline)
  365. (write trpl) (newline)
  366. (set! syntax-ok #f)
  367. '()
  368. )
  369. )
  370. )]
  371. )
  372. )
  373.  
  374. (define (loop-rows h bs)
  375. (cond [(empty? bs) '()]
  376. [else (
  377. let ()
  378. (if syntax-ok
  379.  
  380. (let ()
  381. (define b (car bs))
  382. (if (= (length h) (length b))
  383. (cons (loop-triples h b) (loop-rows h (cdr bs)))
  384. (begin
  385. (write "bt fail 2") (newline)
  386. (set! syntax-ok #f)
  387. '()
  388. )
  389. )
  390. )
  391.  
  392. '()
  393. )
  394. )]
  395. )
  396. )
  397.  
  398. (cond
  399. [(empty? contents) (void)]
  400. [else (
  401. let ()
  402. (define return-contents (loop-rows headers contents))
  403. (if syntax-ok
  404. (let ()
  405. (define appended-contents (remove-duplicates (append (relation-contents rel) return-contents) equal?))
  406. (set-relation-contents! rel appended-contents)
  407. )
  408.  
  409. (begin
  410. (display "Error input - no rows inserted...") (newline)
  411. )
  412. )
  413. )]
  414. )
  415. )
  416.  
  417. (define-syntax (INSERT stx)
  418.  
  419. (define (conv-content expression)
  420. (with-syntax ([e expression])
  421. #'(quote e))
  422. )
  423.  
  424. (syntax-case stx (INTO VALUES SELECT)
  425. [(_ INTO relname VALUES raw-content ...)
  426.  
  427. (with-syntax
  428. (
  429. [
  430. (converted-content ...)
  431. (map conv-content
  432. (syntax->list #' (raw-content ...))
  433. )
  434. ]
  435. )
  436.  
  437. #'(begin
  438. (let
  439. (
  440. [converted-contents (list converted-content ...)]
  441. )
  442.  
  443. (insert-raw relname converted-contents)
  444. )
  445. )
  446. )
  447. ]
  448.  
  449. [(_ INTO relname SELECT x ...)
  450.  
  451. (with-syntax
  452. (
  453. [contents
  454. #'(SELECT x ...)
  455. ]
  456. )
  457.  
  458. ;;; #'(begin
  459. ;;; (SHOW relname) (newline)
  460. ;;; (write-contents contents) (newline)
  461. ;;; (insert-raw relname contents)
  462. ;;; )
  463.  
  464. #'(insert-raw relname contents)
  465. )
  466. ]
  467. )
  468. )
  469.  
  470. ;;; relation and predicate to filter
  471. (define (where contents pred)
  472. (filter
  473. pred
  474. contents
  475. )
  476. )
  477.  
  478. (define-syntax (get-real-op stx)
  479. (syntax-case stx ()
  480. [(_ op value1 value2)
  481. (with-syntax
  482. (
  483. [real-op
  484. (cond [(equal? (symbol->string (syntax->datum #'op)) "!=")
  485. #'(not (equal? value1 value2))
  486. ]
  487.  
  488. [(equal? (symbol->string (syntax->datum #'op)) "=")
  489. #'(equal? value1 value2)
  490. ]
  491.  
  492. [else #'(op value1 value2)]
  493. )
  494. ]
  495. )
  496.  
  497. #'(begin
  498. real-op
  499. )
  500. )
  501. ]
  502. )
  503. )
  504.  
  505. (define-syntax (WHEREH stx)
  506. (syntax-case stx (= != > < >= <= AND OR)
  507.  
  508. [(_ contents (expr1 AND expr2))
  509. (with-syntax
  510. (
  511. [get-expr1
  512. #'(WHEREH contents expr1)
  513. ]
  514.  
  515. [get-expr2
  516. #'(WHEREH contents expr2)
  517. ]
  518. )
  519.  
  520. #'(begin
  521. (let ([c1 get-expr1] [c2 get-expr2])
  522. (intersect-two-lists-contents c1 c2)
  523. )
  524. )
  525. )
  526. ]
  527.  
  528. [(_ contents (expr1 OR expr2))
  529. (with-syntax
  530. (
  531. [get-expr1
  532. #'(WHEREH contents expr1)
  533. ]
  534.  
  535. [get-expr2
  536. #'(WHEREH contents expr2)
  537. ]
  538. )
  539.  
  540. #'(begin
  541. (let ([c1 get-expr1] [c2 get-expr2])
  542. (union-two-lists-contents c1 c2)
  543. )
  544. )
  545. )
  546. ]
  547.  
  548. [(_ contents expr1 AND expr2 ...)
  549. (with-syntax
  550. (
  551. [get-expr1
  552. #'(WHEREH contents expr1)
  553. ]
  554.  
  555. [get-expr2
  556. #'(WHEREH contents expr2 ...)
  557. ]
  558. )
  559.  
  560. #'(begin
  561. (let ([c1 get-expr1] [c2 get-expr2])
  562. (intersect-two-lists-contents c1 c2)
  563. )
  564. )
  565. )
  566. ]
  567.  
  568. [(_ contents expr1 OR expr2 ...)
  569. (with-syntax
  570. (
  571. [get-expr1
  572. #'(WHEREH contents expr1)
  573. ]
  574.  
  575. [get-expr2
  576. #'(WHEREH contents expr2 ...)
  577. ]
  578. )
  579.  
  580. #'(begin
  581. (let ([c1 get-expr1] [c2 get-expr2])
  582. (union-two-lists-contents c1 c2)
  583. )
  584. )
  585. )
  586. ]
  587.  
  588. [(_ contents (expr1 op expr2))
  589. (with-syntax
  590. (
  591. [compare
  592. #'(begin
  593. (lambda (cnt)
  594. (let ([value1 (EVAL cnt expr1)] [value2 (EVAL cnt expr2)])
  595. (get-real-op op value1 value2)
  596. )
  597. )
  598. )
  599. ]
  600. )
  601.  
  602. #'(begin
  603. (where contents compare)
  604. )
  605. )
  606. ]
  607. )
  608. )
  609.  
  610. (define-syntax (EVAL stx)
  611. (syntax-case stx ()
  612. [(_ content (e1 (e2 e3)))
  613. #'(e1 (EVAL content (e2 e3)))
  614. ]
  615.  
  616. [(_ content (e1 e2))
  617. #'(e1 (EVAL content e2))
  618. ]
  619.  
  620. [(_ content e)
  621. (with-syntax
  622. (
  623. [value
  624. (cond [(symbol? (syntax->datum #'e))
  625. #'(content-get-value-by-name content 'e)
  626. ]
  627.  
  628. [else
  629. #'e
  630. ]
  631. )
  632. ]
  633. )
  634.  
  635. #'value
  636. )
  637. ]
  638. )
  639. )
  640.  
  641. ;;; stx here is for example (a (b (c X))) -> '(c b a)
  642. (define-syntax (PARSE-FUNC stx)
  643. (syntax-case stx ()
  644. [(_ (e1 (e2 e3)))
  645. (with-syntax
  646. (
  647. [rest
  648. #'(PARSE-FUNC (e2 e3))
  649. ]
  650. )
  651.  
  652. #'(flatten (list rest e1))
  653. )
  654. ]
  655.  
  656. [(_ (e1 e2))
  657. #'(list e1)
  658. ]
  659. )
  660. )
  661.  
  662. ;;; stx here is for example (a (b (c X))) -> 'X
  663. (define-syntax (PARSE-NAME stx)
  664. (syntax-case stx ()
  665. [(_ (_ (e2 e3)))
  666. #'(PARSE-NAME (e2 e3))
  667. ]
  668.  
  669. [(_ (_ e2))
  670. #'(quote e2)
  671. ]
  672. )
  673. )
  674.  
  675. (define-syntax (FROMH stx)
  676. (syntax-case stx ()
  677. [(_ (rel))
  678. #'(relation-contents rel)
  679. ]
  680.  
  681. [(_ (rel1 rel2))
  682. (with-syntax
  683. (
  684. [c1
  685. #'(FROMH (rel1))
  686. ]
  687.  
  688. [c2
  689. #'(FROMH (rel2))
  690. ]
  691. )
  692.  
  693. #'(natural-join c1 c2)
  694. )
  695. ]
  696.  
  697. [(_ (rel1 rel2 rel3 ...))
  698. (with-syntax
  699. (
  700. [c1
  701. #'(FROMH (rel1 rel2))
  702. ]
  703.  
  704. [c2
  705. #'(FROMH (rel3 ...))
  706. ]
  707. )
  708.  
  709. #'(natural-join c1 c2)
  710. )
  711. ]
  712. )
  713. )
  714.  
  715. ;;; a b c MAX(d) -> ('a 'b 'c 'd)
  716. (define-syntax (PARSE-NAMES-SELECT stx)
  717. (syntax-case stx (MIN MAX MED)
  718.  
  719. [(_ MIN(a))
  720. #'(list 'a)
  721. ]
  722.  
  723. [(_ MIN(a) b ...)
  724. #'(flatten (list 'a (PARSE-NAMES-SELECT b ...)))
  725. ]
  726.  
  727. [(_ MAX(a))
  728. #'(list 'a)
  729. ]
  730.  
  731. [(_ MAX(a) b ...)
  732. #'(flatten (list 'a (PARSE-NAMES-SELECT b ...)))
  733. ]
  734.  
  735. [(_ MED(a))
  736. #'(list 'a)
  737. ]
  738.  
  739. [(_ MED(a) b ...)
  740. #'(flatten (list 'a (PARSE-NAMES-SELECT b ...)))
  741. ]
  742.  
  743. [(_ a)
  744. #'(list 'a)
  745. ]
  746.  
  747. [(_ a b ...)
  748. #'(flatten (list 'a (PARSE-NAMES-SELECT b ...)))
  749. ]
  750. )
  751. )
  752.  
  753. ;;; a b c MAX(d) -> ('MAX 'd)
  754. (define-syntax (PARSE-FUNC-SELECT stx)
  755. (syntax-case stx (MIN MAX MED)
  756.  
  757. [(_ MIN(a))
  758. #'(list 'MIN 'a)
  759. ]
  760.  
  761. [(_ MIN(a) b ...)
  762. #'(list 'MIN 'a)
  763. ]
  764.  
  765. [(_ MAX(a))
  766. #'(list 'MAX 'a)
  767. ]
  768.  
  769. [(_ MAX(a) b ...)
  770. #'(list 'MAX 'a)
  771. ]
  772.  
  773. [(_ MED(a))
  774. #'(list 'MED 'a)
  775. ]
  776.  
  777. [(_ MED(a) b ...)
  778. #'(list 'MED 'a)
  779. ]
  780.  
  781. [(_ a)
  782. #'(list 'NAH 'a)
  783. ]
  784.  
  785. [(_ a b ...)
  786. #'(PARSE-FUNC-SELECT b ...)
  787. ]
  788. )
  789. )
  790.  
  791. (define-syntax (SELECT stx)
  792.  
  793. (define (conv expression)
  794. (with-syntax ([e expression])
  795. #'(quote e))
  796. )
  797.  
  798. (syntax-case stx (FROM WHERE *)
  799.  
  800. [(_ * FROM (f ...) WHERE w ...)
  801. (with-syntax*
  802. (
  803. [f-contents
  804. #'(FROMH (f ...))
  805. ]
  806.  
  807. [w-contents
  808. #'(WHEREH f-contents w ...)
  809. ]
  810. )
  811.  
  812. #'(convert-contents (select '() w-contents #t))
  813.  
  814. )
  815. ]
  816.  
  817. [(_ * FROM (f ...))
  818. (with-syntax
  819. (
  820. [f-contents
  821. #'(FROMH (f ...))
  822. ]
  823. )
  824.  
  825. #'(convert-contents (select '() f-contents #t))
  826.  
  827. )
  828. ]
  829.  
  830. [(_ (s ...) FROM (f ...) WHERE w ...)
  831. (with-syntax*
  832. (
  833. [f-contents
  834. #'(FROMH (f ...))
  835. ]
  836.  
  837. [w-contents
  838. #'(WHEREH f-contents w ...)
  839. ]
  840.  
  841. [names
  842. #'(PARSE-NAMES-SELECT s ...)
  843. ]
  844.  
  845. [spec
  846. #'(PARSE-FUNC-SELECT s ...)
  847. ]
  848. )
  849.  
  850. #'(convert-contents (select-with-func names w-contents #f spec))
  851.  
  852. )
  853. ]
  854.  
  855. [(_ (s ...) FROM (f ...))
  856. (with-syntax*
  857. (
  858. [f-contents
  859. #'(FROMH (f ...))
  860. ]
  861.  
  862. [names
  863. #'(PARSE-NAMES-SELECT s ...)
  864. ]
  865.  
  866. [spec
  867. #'(PARSE-FUNC-SELECT s ...)
  868. ]
  869. )
  870.  
  871. #'(convert-contents (select-with-func names f-contents #f spec))
  872. )
  873. ]
  874. )
  875. )
  876.  
  877. (define-syntax (SELECT-CONTENTS stx)
  878. (syntax-case stx (FROM WHERE *)
  879.  
  880. [(_ * FROM (f ...) WHERE w ...)
  881. (with-syntax*
  882. (
  883. [f-contents
  884. #'(FROMH (f ...))
  885. ]
  886.  
  887. [w-contents
  888. #'(WHEREH f-contents w ...)
  889. ]
  890. )
  891.  
  892. #'(select '() w-contents #t)
  893. )
  894. ]
  895. )
  896. )
  897.  
  898. (define (delete rel contents)
  899. (define original-contents (relation-contents rel))
  900. (define next-contents (remove* contents original-contents equal?))
  901. (set-relation-contents! rel next-contents)
  902. )
  903.  
  904. (define-syntax (DELETE stx)
  905. (syntax-case stx (FROM WHERE *)
  906. [(_ FROM relname WHERE x ...)
  907. (with-syntax
  908. (
  909. [selected-contents
  910. #'(SELECT-CONTENTS * FROM (relname) WHERE x ...)
  911. ]
  912. )
  913.  
  914. #'(delete relname selected-contents)
  915. )
  916. ]
  917. )
  918. )
  919.  
  920. ;;; filename is string
  921. (define (export relname filename)
  922.  
  923. ;;; a row is a content
  924. ;;; loop through a row - triples
  925. (define (print-row row out-port)
  926. (define (print r o-p first?)
  927. (cond [(empty? r)
  928. (void)
  929. ]
  930. [else (
  931. let ([value (triple-value (car r))])
  932. (if first?
  933. (begin
  934. (write value o-p)
  935. (print (cdr r) o-p #f)
  936. )
  937.  
  938. (begin
  939. (display "," o-p)
  940. (write value o-p)
  941. (print (cdr r) o-p #f)
  942. )
  943. )
  944. )]
  945. )
  946. )
  947. (print row out-port #t)
  948. )
  949.  
  950. ;;; loop through contents - list of rows
  951. (define (print-contents contents out-port)
  952. (define (print cnts o-p first?)
  953. (cond [(empty? cnts)
  954. (void)
  955. ]
  956.  
  957. [else
  958. (if first?
  959. (begin
  960. (print-row (car cnts) o-p)
  961. (print (cdr cnts) o-p #f)
  962. )
  963.  
  964. (begin
  965. (display "\n" o-p)
  966. (print-row (car cnts) o-p)
  967. (print (cdr cnts) o-p #f)
  968. )
  969. )
  970. ]
  971. )
  972. )
  973.  
  974. (print contents out-port #t)
  975. )
  976.  
  977. (define out-port (open-output-file filename #:mode 'text #:exists 'replace))
  978. (print-contents (relation-contents relname) out-port)
  979. (close-output-port out-port)
  980. )
  981.  
  982. (define-syntax (EXPORT stx)
  983. (syntax-case stx ()
  984. [(_ relname filename)
  985. #'(export relname (symbol->string (quote filename)))
  986. ]
  987. )
  988. )
  989.  
  990. ;;; execute a list of functions on the initial variable var
  991. ;;; execute one by one function consecutively.
  992. (define (exec funcs var)
  993. (cond [(empty? funcs)
  994. var
  995. ]
  996. [else (
  997. let* ([f (car funcs)] [v (f var)])
  998. (exec (cdr funcs) v)
  999. )]
  1000. )
  1001. )
  1002.  
  1003. ;;; return contents (not headers) based on col-names of rel
  1004. (define (select col-names contents is-full?)
  1005. (define (filter-each-content content col-names)
  1006. (cond [(empty? col-names) '()]
  1007. [else (
  1008. let ()
  1009. (define name (car col-names))
  1010. (define filtered-content
  1011. (filter (lambda (x) (triple-contain-name? x name)) content
  1012. )
  1013. )
  1014. (if (empty? filtered-content) (error "Not have column " name) (cons (car filtered-content) (filter-each-content content (cdr col-names))))
  1015. )]
  1016. )
  1017. )
  1018.  
  1019. (cond [(equal? is-full? #t)
  1020. contents
  1021. ]
  1022. [else
  1023. (map
  1024. (lambda (cntnt) (filter-each-content cntnt col-names))
  1025. contents
  1026. )
  1027. ]
  1028. )
  1029. )
  1030.  
  1031. ;;; return just ONE row
  1032. (define (select-with-func col-names contents is-full? spec)
  1033. ;;; sort the contents by the column name
  1034. (define (sort-contents contents name)
  1035. (sort contents
  1036. (lambda (cnt1 cnt2)
  1037. (let (
  1038. [value1 (content-get-value-by-name cnt1 name)]
  1039. [value2 (content-get-value-by-name cnt2 name)]
  1040. )
  1041.  
  1042. (< value1 value2)
  1043. )
  1044. )
  1045. )
  1046. )
  1047.  
  1048. (define (traverse-n sorted-contents n)
  1049. (cond [(equal? n 0)
  1050. (car sorted-contents)
  1051. ]
  1052.  
  1053. [else
  1054. (traverse-n (cdr sorted-contents) (- n 1))
  1055. ]
  1056. )
  1057. )
  1058.  
  1059. (define (max sorted-contents)
  1060. (define l (length sorted-contents))
  1061. (define n (- l 1))
  1062. (traverse-n sorted-contents n)
  1063. )
  1064.  
  1065. (define (min sorted-contents)
  1066. (define n 0)
  1067. (traverse-n sorted-contents n)
  1068. )
  1069.  
  1070. (define (med sorted-contents)
  1071. (define l (length sorted-contents))
  1072. (define n
  1073. (cond [(equal? (modulo l 2) 0)
  1074. (- (/ l 2) 1)
  1075. ]
  1076.  
  1077. [else
  1078. (- (/ (+ l 1) 2) 1)
  1079. ]
  1080. )
  1081. )
  1082.  
  1083. (traverse-n sorted-contents n)
  1084. )
  1085.  
  1086. (define selected-contents (select col-names contents is-full?))
  1087.  
  1088. (cond [(equal? (car spec) 'NAH)
  1089. selected-contents
  1090. ]
  1091. [else (
  1092. let ()
  1093. (define func-literal (first spec))
  1094. (define sort-name (last spec))
  1095. (define sorted-contents (sort-contents selected-contents sort-name))
  1096. (cond [(equal? func-literal 'MAX)
  1097. (max sorted-contents)
  1098. ]
  1099.  
  1100. [(equal? func-literal 'MIN)
  1101. (min sorted-contents)
  1102. ]
  1103.  
  1104. [(equal? func-literal 'MED)
  1105. (med sorted-contents)
  1106. ]
  1107.  
  1108. [else
  1109. (error "Cannot find the correct func... " func-literal)
  1110. ]
  1111. )
  1112. )]
  1113. )
  1114. )
  1115.  
  1116. (define (natural-join contents1 contents2)
  1117.  
  1118. (define (construct-headers c)
  1119. (cond [(empty? c) '()]
  1120. [else (
  1121. let ()
  1122. (define name (triple-name (car c)))
  1123. (define type (triple-type (car c)))
  1124.  
  1125. (cons (tuple name type) (construct-headers (cdr c)))
  1126. )]
  1127. )
  1128. )
  1129.  
  1130. ;;; get common column names of those contents by looping via the first content of each list
  1131. (define (get-common-names hds1 hds2)
  1132. (filter-map (lambda (x) (and (member x hds2) (tuple-name x))) hds1)
  1133. )
  1134.  
  1135. ;;; get the rest of the names after remove common names
  1136. (define (remove-common-names names headers)
  1137. (filter-map (lambda (x) (and (not (member (tuple-name x) names)) (tuple-name x))) headers)
  1138. )
  1139.  
  1140. ;;; o - origin
  1141. ;;; f - filtered - contains only common names
  1142. ;;; d - diff - contains the rest
  1143. (define (carte-prod o-cont1 f-cont1 f-cont2 d-cont2)
  1144.  
  1145. ;;; loop through each row of contents 2
  1146. (define (loop-rel-1 o-row1 f-row1 f-cont2 d-cont2)
  1147. (cond [(empty? f-cont2) '()]
  1148. [else (
  1149. let ()
  1150. (define f-row2 (car f-cont2))
  1151. (define d-row2 (car d-cont2))
  1152.  
  1153. (define merged-row
  1154. (if (equal? f-row1 f-row2)
  1155. (append o-row1 d-row2)
  1156. '()
  1157. )
  1158. )
  1159.  
  1160. (cons merged-row (loop-rel-1 o-row1 f-row1 (cdr f-cont2) (cdr d-cont2)))
  1161. )]
  1162. )
  1163. )
  1164.  
  1165. ;;; loop through each row of contents 1
  1166. (define (loop-rel o-cont1 f-cont1 f-cont2 d-cont2)
  1167. (cond [(empty? f-cont1) '()]
  1168. [else (
  1169. let ()
  1170. (define o-row1 (car o-cont1))
  1171. (define f-row1 (car f-cont1))
  1172.  
  1173. (define merged-cont (loop-rel-1 o-row1 f-row1 f-cont2 d-cont2))
  1174.  
  1175. (append merged-cont (loop-rel (cdr o-cont1) (cdr f-cont1) f-cont2 d-cont2))
  1176. )]
  1177. )
  1178. )
  1179.  
  1180. (loop-rel o-cont1 f-cont1 f-cont2 d-cont2)
  1181. )
  1182.  
  1183. (define headers1 (construct-headers (car contents1)))
  1184. (define headers2 (construct-headers (car contents2)))
  1185.  
  1186. (define common-names (get-common-names headers1 headers2))
  1187. (define diff-names-rel2 (remove-common-names common-names headers2))
  1188.  
  1189. (define o-cont1 contents1)
  1190. (define o-cont2 contents2)
  1191.  
  1192. (define f-cont1 (select common-names o-cont1 #f))
  1193. (define f-cont2 (select common-names o-cont2 #f))
  1194.  
  1195. (define d-cont2 (select diff-names-rel2 o-cont2 #f))
  1196.  
  1197. (remove* (list '())
  1198. (carte-prod o-cont1 f-cont1 f-cont2 d-cont2)
  1199. )
  1200. )
  1201. ;;; intersect
  1202.  
  1203. (define (natural-join-relations rel1 rel2)
  1204.  
  1205. ;;; get common column names of rel1 and rel2 (headers)
  1206. (define (get-common-names rel1 rel2)
  1207. (define headers1 (relation-headers rel1))
  1208. (define headers2 (relation-headers rel2))
  1209.  
  1210. (filter-map (lambda (x) (and (member x headers2) (tuple-name x))) headers1)
  1211. )
  1212.  
  1213. ;;; get the rest of the names after remove common names
  1214. (define (remove-common-names names rel)
  1215. (filter-map (lambda (x) (and (not (member (tuple-name x) names)) (tuple-name x))) (relation-headers rel))
  1216. )
  1217.  
  1218. ;;; o - origin
  1219. ;;; f - filtered - contains only common names
  1220. ;;; d - diff - contains the rest
  1221. (define (carte-prod o-cont1 f-cont1 f-cont2 d-cont2)
  1222.  
  1223. ;;; loop through each row of contents 2
  1224. (define (loop-rel-1 o-row1 f-row1 f-cont2 d-cont2)
  1225. (cond [(empty? f-cont2) '()]
  1226. [else (
  1227. let ()
  1228. (define f-row2 (car f-cont2))
  1229. (define d-row2 (car d-cont2))
  1230.  
  1231. (define merged-row
  1232. (if (equal? f-row1 f-row2)
  1233. (append o-row1 d-row2)
  1234. '()
  1235. )
  1236. )
  1237.  
  1238. (cons merged-row (loop-rel-1 o-row1 f-row1 (cdr f-cont2) (cdr d-cont2)))
  1239. )]
  1240. )
  1241. )
  1242.  
  1243. ;;; loop through each row of contents 1
  1244. (define (loop-rel o-cont1 f-cont1 f-cont2 d-cont2)
  1245. (cond [(empty? f-cont1) '()]
  1246. [else (
  1247. let ()
  1248. (define o-row1 (car o-cont1))
  1249. (define f-row1 (car f-cont1))
  1250.  
  1251. (define merged-cont (loop-rel-1 o-row1 f-row1 f-cont2 d-cont2))
  1252.  
  1253. (append merged-cont (loop-rel (cdr o-cont1) (cdr f-cont1) f-cont2 d-cont2))
  1254. )]
  1255. )
  1256. )
  1257.  
  1258. (loop-rel o-cont1 f-cont1 f-cont2 d-cont2)
  1259. )
  1260.  
  1261. (define common-names (get-common-names rel1 rel2))
  1262. (define diff-names-rel2 (remove-common-names common-names rel2))
  1263.  
  1264. (define o-cont1 (relation-contents rel1))
  1265. (define o-cont2 (relation-contents rel2))
  1266.  
  1267. (define f-cont1 (select common-names o-cont1 #f))
  1268. (define f-cont2 (select common-names o-cont2 #f))
  1269.  
  1270. (define d-cont2 (select diff-names-rel2 o-cont2 #f))
  1271.  
  1272. (remove* (list '())
  1273. (carte-prod o-cont1 f-cont1 f-cont2 d-cont2)
  1274. )
  1275. )
  1276.  
  1277. (define (extract-headers content)
  1278. (define (loop trpls)
  1279. (cond [(empty? trpls) '()]
  1280. [else (
  1281. let ()
  1282. (define trpl (car trpls))
  1283. (define tpl (tuple (triple-name trpl) (triple-type trpl)))
  1284. (cons tpl (loop (cdr trpls)))
  1285. )]
  1286. )
  1287. )
  1288.  
  1289. (loop content)
  1290. )
  1291.  
  1292. (define (convert-contents contents)
  1293. (define (loop-row cnt)
  1294. (cond [(empty? cnt) '()]
  1295. [else
  1296. (cons (triple-value (car cnt)) (loop-row (cdr cnt)))
  1297. ]
  1298. )
  1299. )
  1300.  
  1301. (define (loop-contents cnts)
  1302. (cond [(empty? cnts) '()]
  1303. [else
  1304. (cons (loop-row (car cnts)) (loop-contents (cdr cnts)))
  1305. ]
  1306. )
  1307. )
  1308.  
  1309. (cond [(empty? contents) '()]
  1310. [(list? (car contents))
  1311. (loop-contents contents)
  1312. ]
  1313. [else
  1314. (loop-row contents)
  1315. ]
  1316. )
  1317. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement