about summary refs log tree commit diff
path: root/transform/special-forms.scm
diff options
context:
space:
mode:
authorMunyoki Kilyungi2026-02-09 11:00:43 +0300
committerMunyoki Kilyungi2026-02-09 11:00:43 +0300
commit0e3a7305f45d70bb4173b6cd1e8d543957b6899a (patch)
tree21b200d4bb95183f998842f4f6156f40af6a7740 /transform/special-forms.scm
parent210afc463ae2e7fad367fce3011a57f65943f07d (diff)
downloadgn-transform-databases-0e3a7305f45d70bb4173b6cd1e8d543957b6899a.tar.gz
Use shorter triple-forms for ttl files where possible.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'transform/special-forms.scm')
-rw-r--r--transform/special-forms.scm41
1 files changed, 37 insertions, 4 deletions
diff --git a/transform/special-forms.scm b/transform/special-forms.scm
index ddb3180..8de4966 100644
--- a/transform/special-forms.scm
+++ b/transform/special-forms.scm
@@ -1,5 +1,6 @@
 (define-module (transform special-forms)
   #:use-module (srfi srfi-1)
+  #:use-module (ice-9 regex)
   #:use-module (ice-9 match)
   #:use-module (srfi srfi-26)
   #:use-module (transform sql)
@@ -18,8 +19,39 @@
             syntax-let
             map-alist
 	    with-documentation
+            emit-short-turtle
             define-transformer))
 
+(define (emit-short-turtle subject po-alist)
+  (let loop ((pairs po-alist) (first? #t))
+    (match pairs
+      (((p . o) rest ...)
+       ;; subject only on first line
+       (when first?
+         (format #t "~a " subject))
+       (when (not first?)
+         (format #t "\t"))   ; indent following lines
+
+       (match o
+         ((? symbol?)
+          (format #t "~a ~a" p (symbol->string o)))
+         ((or (? (lambda (el) (and (string? el)
+                                   (string-match "^\\(.*\\)$" el))))
+              (? (lambda (el) (and (string? el)
+                                   (string-match "^\\[.*\\]$" el)))))
+          (format #t "~a ~s" p o))
+         (_
+          (format #t "~a \"~a\"" p o)))
+
+       (if (null? rest)
+           (format #t " .~%")   ; last triple
+           (format #t " ;~%"))  ; continuation
+
+       (loop rest #f))
+
+      (() #f))))
+
+
 (define (key->assoc-ref alist x)
   "Recursively translate (key k) forms in source X to (assoc-ref ALIST
 k) forms."
@@ -505,10 +537,11 @@ The above query results to triples that have the form:
                                 (list 'triple-predicate ...)
                                 (list 'triple-object ...)))
                    (_ (error "Invalid schema triples clause:" #'schema-triples-clause)))
-	       (sql-for-each (lambda (row)
-                               (scm->triples
-                                (map-alist row #,@(field->key #'(predicate-clauses ...)))
-                                #,(field->assoc-ref #'row #'subject)))
+	       (sql-for-each  (lambda (row)
+                                (let* ((subject-val #,(field->assoc-ref #'row #'subject))
+                                       (po-alist
+                                        (map-alist row #,@(field->key #'(predicate-clauses ...)))))
+                                  (emit-short-turtle subject-val po-alist)))
                              db
                              (select-query #,(collect-fields #'(subject predicate-clauses ...))
                                            (primary-table other-tables ...)