aboutsummaryrefslogtreecommitdiff
path: root/examples/schema.scm
blob: 50cfd6a563a1118547a4029edd7bc24c3a8dd483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env guile
!#

(use-modules (ice-9 getopt-long)
             (transform triples)
             (transform schema)
             (transform special-forms)
             (transform sql)
             (transform table))

(define (call-with-genenetwork-database connection-settings proc)
  (call-with-database "mysql" (string-join
                               (list (assq-ref connection-settings 'sql-username)
                                     (assq-ref connection-settings 'sql-password)
                                     (assq-ref connection-settings 'sql-database)
                                     "tcp"
                                     (assq-ref connection-settings 'sql-host)
                                     (number->string
                                      (assq-ref connection-settings 'sql-port)))
                               ":")
                      proc))

(define (transform-table-schema connection-settings db)
  (let ((tables (tables connection-settings db)))
    (for-each (lambda (table)
                (let ((table-id (string->identifier
                                 "table"
                                 ;; We downcase table names in
                                 ;; identifiers. So, we distinguish
                                 ;; between the user and User tables.
                                 (if (string=? (table-name table) "User")
                                     "user2"
                                     (table-name table)))))
                  (triple table-id 'rdf:type 'gn:sqlTable)
                  (triple table-id 'gn:name (table-name table))
                  (triple table-id 'gn:hasSize (string->symbol (format #f "~a" (table-size table))))
                  (for-each (lambda (column)
                              (let ((column-id (column-id (table-name table)
                                                          (column-name column))))
                                (triple column-id 'rdf:type 'gn:sqlTableField)
                                (triple column-id 'gn:name (column-name column))
                                (triple column-id 'gn:sqlFieldType (column-type column))
                                (triple table-id 'gn:hasField column-id)))
                            (table-columns table))))
              tables)))


(let* ((option-spec
        '((settings (single-char #\s) (value #t))
          (output (single-char #\o) (value #t))
          (documentation (single-char #\d) (value #t))))
       (options (getopt-long (command-line) option-spec))
       (settings (option-ref options 'settings #f))
       (output (option-ref options 'output #f))
       (documentation (option-ref options 'documentation #f))
       (%connection-settings (call-with-input-file settings read)))
  (call-with-genenetwork-database
   %connection-settings
   (lambda (db)
     (with-output-to-file output
       (lambda ()
         (prefix "rdf:" "<http://www.w3.org/1999/02/22-rdf-syntax-ns#>")
         (prefix "rdfs:" "<http://www.w3.org/2000/01/rdf-schema#>")
         (prefix "gn:" "<http://genenetwork.org/id/>")
         (prefix "gnc:" "<http://genenetwork.org/category/>")
         (prefix "gnt:" "<http://genenetwork.org/term/>")
         (prefix "xsd:" "<http://www.w3.org/2001/XMLSchema#>")
         (prefix "owl:" "<http://www.w3.org/2002/07/owl#>")
         (newline)
         (transform-table-schema %connection-settings db))))))