diff --git a/project.clj b/project.clj index 9d312f2..8e9329a 100644 --- a/project.clj +++ b/project.clj @@ -4,4 +4,5 @@ :license {:name "GNU General Public License,version 2.0 or (at your option) any later version" :url "https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html"} :dependencies [[org.clojure/clojure "1.8.0"] + [clj-time "0.14.2"] [instaparse "1.4.8"]]) diff --git a/src/squirrel_parse/to_adl.clj b/src/squirrel_parse/to_adl.clj index 4684a5c..18c66e2 100644 --- a/src/squirrel_parse/to_adl.clj +++ b/src/squirrel_parse/to_adl.clj @@ -2,6 +2,8 @@ :author "Simon Brooke"} squirrel-parse.to-adl (:require [clojure.xml :refer [emit-element]] + [clj-time.core :refer [now]] + [clj-time.format :refer [formatters unparse]] [squirrel-parse.parser :refer [parse]] [squirrel-parse.simplify :refer [simplify]])) @@ -30,6 +32,12 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(def xml-header + "XML header for ADL files." + " + ") + (def sql-datatype-to-adl-datatype "Map to convert SQL datatypes to the nearest ADL equivalent." {:DT-BIGINT :integer @@ -84,7 +92,7 @@ (defn get-name "Return the value the first top-level :NAME element of this `subtree`." [subtree] - (let [name-elt (make-property subtree :NAME)] + (let [name-elt (get-first-child-of-type subtree :NAME)] (if name-elt (second name-elt)))) (defn get-column-datatype @@ -146,10 +154,21 @@ (reduce table-definition-to-entity {} statements))) -(defn to-adl [filename name] - (let [entities (table-definitions-to-entities (simplify (parse (slurp filename))))] - [{:tag :application - :name name +(defn to-adl + "Take this `input` (filename, url, whatever) assumed to contain a stream of SQL + statements; convert them to ADL with this `application-name`; if `version` is + provided, tag it with that version number else tag it with a version number drawn + from the current date; if `output` is provided write it as XML to that output." + ([input application-name] + (to-adl input application-name (unparse (formatters :basic-date) (now)))) + ([input application-name version] + (let [entities (table-definitions-to-entities (simplify (parse (slurp input))))] + {:tag :application + :attrs {:name application-name + :version version } :content (vals entities)} - nil])) - + )) + ([input application-name version output] + (let [adl (to-adl input application-name version)] + (spit output (str xml-header "\n" (with-out-str (emit-element adl)))) + adl)))