Skip to content

Commit

Permalink
Get query's tables and columns (#3)
Browse files Browse the repository at this point in the history
* WIP

* Set up build for Java

* Add ASTWalker.java!

* Almost got SqlVisitor worked out

* Add SqlVisitor and ASTWalker; use for query->columns

* Cleanup

* Build before testing on CI

* Build before checking on CI

* Put Java file in the right spot 😮

* Refactor to use a Clojure map + callbacks

* Add some basic build scripts

* Update docs

* atom->transient

* Make ASTWalker more robust/foolproof

* Back to atoms
  • Loading branch information
tsmacdonald authored Feb 20, 2024
1 parent 00b0f0a commit 4f8e0ec
Show file tree
Hide file tree
Showing 9 changed files with 1,427 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ jobs:
restore-keys: |
v1-${{ hashFiles('./deps.edn') }}-
v1-
- run: clojure -T:build compile
name: Compile Java files
- run: clojure -X:dev:test
name: Run tests
env:
Expand Down Expand Up @@ -102,6 +104,8 @@ jobs:
restore-keys: |
v1-${{ hashFiles('./deps.edn') }}-
v1-
- run: clojure -T:build compile
name: Compile Java files
- run: clojure -M:check
name: Check namespaces

Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,29 @@ Macaw is a limited Clojure wrapper for
[JSqlParser](https://github.com/JSQLParser/JSqlParser). Similar to its parrot
namesake, it's intelligent, can be taught to speak SQL, and has many colors
(supports many dialects).


## Building

To build a local JAR, use

```
./bin/build-jar
```

This will create a JAR in the `target` directory.

## Working with the Java files

To compile the Java files, use

```
./bin/compile-java
```

If you're working on Macaw and make changes to a Java file, you must:

1. Recompile
2. Restart your Clojure REPL

for the changes to take effect.
25 changes: 25 additions & 0 deletions bin/build-jar
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
if [[ -n "${TRACE-}" ]]; then
set -o xtrace
fi

if [ $# -ge 1 ] && [[ "$1" =~ ^-*h(elp)?$ ]]; then
echo 'Usage: ./bin/build-jar
Build a JAR
'
exit
fi

# Ensure we're in the project root
cd "$(dirname "$0")"/..

main() {
clj -T:build jar
}

main "$@"
25 changes: 25 additions & 0 deletions bin/java-compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
if [[ -n "${TRACE-}" ]]; then
set -o xtrace
fi

if [ $# -ge 1 ] && [[ "$1" =~ ^-*h(elp)?$ ]]; then
echo 'Usage: ./bin/java-compile
Compiles all the Java files used in the project so that they can be used in local Clojure development. To build a JAR, use ./build-jar instead.
'
exit
fi

# Ensure we're in the project root
cd "$(dirname "$0")"/..

main() {
clj -T:build compile
}

main "$@"
47 changes: 47 additions & 0 deletions build.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
;; Further info: https://clojure.org/guides/tools_build#_mixed_java_clojure_build

(ns build
(:require
[clojure.java.shell :as sh]
[clojure.string :as str]
[clojure.tools.build.api :as b]))

(def lib 'metabase/macaw)

(def major-minor-version "0.1")

(defn commit-number []
(or (-> (sh/sh "git" "rev-list" "HEAD" "--count")
:out
str/trim
parse-long)
"9999-SNAPSHOT"))

(def version (str major-minor-version \. (commit-number)))
(def target "target")
(def class-dir (format "%s/classes" target))

(def jar-file (format "target/%s-%s.jar" (name lib) version))

(def basis (delay (b/create-basis {:project "deps.edn"})))

(defn clean [_]
(b/delete {:path target}))

(defn compile [_]
(b/javac {:src-dirs ["java"]
:class-dir class-dir
:basis @basis
:javac-opts ["--release" "11"]}))

(defn jar [_]
(compile nil)
(b/write-pom {:class-dir class-dir
:lib lib
:version version
:basis @basis
:src-dirs ["src"]})
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/jar {:class-dir class-dir
:jar-file jar-file}))
6 changes: 3 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{:paths
["src" "resources"]
["src" "java" "resources" "target/classes"]

:deps
{com.github.jsqlparser/jsqlparser {:mvn/version "4.8"}} ; The actual SQL Parser to wrap!
Expand Down Expand Up @@ -41,8 +41,8 @@
{:deps {com.github.camsaul/whitespace-linter {:sha "e35bc252ccf5cc74f7d543ef95ad8a3e5131f25b"}}
:ns-default whitespace-linter
:exec-fn whitespace-linter/lint
:exec-args {:paths ["deps.edn" "src" "test" ".github"]
:include-patterns ["\\.clj[cs]?$" "\\.edn$" "\\.yaml$" "\\.md$"]}}
:exec-args {:paths ["deps.edn" "src" "java" "test" ".github"]
:include-patterns ["\\.clj[cs]?$" "\\.edn$" "\\.java$" "\\.yaml$" "\\.md$"]}}

;; Run tests
;;
Expand Down
Loading

0 comments on commit 4f8e0ec

Please sign in to comment.