Skip to content

Commit

Permalink
Fix renames
Browse files Browse the repository at this point in the history
  • Loading branch information
crisptrutski committed Oct 25, 2024
1 parent 594be2f commit 8c4b0e7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/macaw/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@
;; We need to pre-sanitize the SQL before its analyzed so that the AST token positions match up correctly.
;; Currently, we use a more complex and expensive sanitization method, so that it's reversible.
;; If we decide that it's OK to normalize whitespace etc. during replacement, then we can use the same helper.
(let [sql' (escape-keywords (str/replace sql #"(?m)^\n" " \n") (:non-reserved-words opts))
(let [sql' (-> (str/replace sql #"(?m)^\n" " \n")
(escape-keywords (:non-reserved-words opts)))
opts' (select-keys opts [:case-insensitive :quotes-preserve-case? :allow-unused?])
renames' (walk/postwalk (fn [x]
(if (string? x)
Expand Down
11 changes: 8 additions & 3 deletions src/macaw/rewrite.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
:else (recur (dec n) (inc next-id)))))))

(defn- ->idx [^String sql line col]
(+ col (index-of-nth sql "\n" (dec line))))
;; The second `dec` on `line` is a workaround for what appears to be an off-by-one error in this JSQLParser version.
(+ col (index-of-nth sql "\n" (dec (dec line)))))

(defn- node->idx-range
"Find the start and end index of the underlying tokens for a given AST node from a given SQL string."
Expand Down Expand Up @@ -68,7 +69,11 @@
sql
(mw/fold-query
updated-ast
{:table (replace-name #(.getFullyQualifiedName ^Table %))
{:table (replace-name #(let [fqn (.getFullyQualifiedName ^Table %)
alias (.getAlias ^Table %)]
(if alias
(str fqn " " (.getName alias))
fqn)))
:column (replace-name #(.getFullyQualifiedName ^Column %))}
[]))))

Expand Down Expand Up @@ -98,7 +103,7 @@
(let [known-rename? (set (map second updated-nodes))]
(doseq [[k items] renames]
(when-let [unknown (first (remove known-rename? items))]
(throw (ex-info (str "Unknown rename: " unknown) {:type k
(throw (ex-info (str "Unknown rename: " unknown) {:type k
:rename unknown}))))))

(defn- index-by-instances [xs]
Expand Down
9 changes: 5 additions & 4 deletions test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ from foo")
(deftest three-or-more-line-breaks-test
(doseq [f [identity ->windows]
:let [query (f implicit-semicolon)]]
(is (= (-> query (str/replace "id" "pk") (str/replace "foo" "bar"))
(m/replace-names query
{:columns {{:table "foo" :column "id"} "pk"}
:tables {{:table "foo"} "bar"}})))))
(testing (if (= ->windows f) "windows" "unix")
(is (= (-> query (str/replace "id" "pk") (str/replace "foo" "bar"))
(m/replace-names query
{:columns {{:table "foo" :column "id"} "pk"}
:tables {{:table "foo"} "bar"}}))))))

(deftest query->tables-test
(testing "Simple queries"
Expand Down

0 comments on commit 8c4b0e7

Please sign in to comment.