Skip to content

Commit 7ed0ae0

Browse files
committed
feat: wrap join alias in quotes
BREAKING CHANGE: this allows to avoid keywords being used as aliases (e.g. "user")
1 parent 7767aab commit 7ed0ae0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

spec/query/join_spec.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe "Atom::Query#join" do
1919
q = Atom::Query(User).new.join(:authored_posts)
2020

2121
q.to_s.should eq <<-SQL
22-
SELECT users.* FROM users INNER JOIN posts AS authored_posts ON authored_posts.author_uuid = users.uuid
22+
SELECT users.* FROM users INNER JOIN posts AS "authored_posts" ON "authored_posts".author_uuid = users.uuid
2323
SQL
2424

2525
q.params.should be_nil
@@ -31,7 +31,7 @@ describe "Atom::Query#join" do
3131
q = Atom::Query(User).new.join(:authored_posts, type: :right, as: "the_posts", select: {"created_at", "id"})
3232

3333
q.to_s.should eq <<-SQL
34-
SELECT users.*, '' AS _authored_posts, the_posts.created_at, the_posts.id FROM users RIGHT JOIN posts AS the_posts ON the_posts.author_uuid = users.uuid
34+
SELECT users.*, '' AS _authored_posts, "the_posts".created_at, "the_posts".id FROM users RIGHT JOIN posts AS "the_posts" ON "the_posts".author_uuid = users.uuid
3535
SQL
3636

3737
q.params.should be_nil
@@ -44,7 +44,7 @@ describe "Atom::Query#join" do
4444
q = Atom::Query(Post).new.join(:author, type: :left, select: "id")
4545

4646
q.to_s.should eq <<-SQL
47-
SELECT posts.*, '' AS _author, author.id FROM posts LEFT JOIN users AS author ON posts.author_uuid = author.uuid
47+
SELECT posts.*, '' AS _author, "author".id FROM posts LEFT JOIN users AS "author" ON posts.author_uuid = "author".uuid
4848
SQL
4949

5050
q.params.should be_nil

src/atom/query/join.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ class Atom
101101
select _select : Char | String | Enumerable(String | Char) | Nil = nil
102102
)
103103
on = if reference.direct?
104-
"#{T.table}.#{reference.key} = #{_as}.#{reference.primary_key}"
104+
"#{T.table}.#{reference.key} = \"#{_as}\".#{reference.primary_key}"
105105
else
106-
"#{_as}.#{reference.foreign_key} = #{T.table}.#{T.primary_key.key}"
106+
"\"#{_as}\".#{reference.foreign_key} = #{T.table}.#{T.primary_key.key}"
107107
end
108108

109109
if _select
@@ -115,10 +115,10 @@ class Atom
115115

116116
if _select.is_a?(Enumerable)
117117
_select.each do |s|
118-
self.select(_as ? _as + '.' + s : s)
118+
self.select(_as ? "\"#{_as}\"" + '.' + s : s)
119119
end
120120
else
121-
self.select(_as ? _as + '.' + _select : _select)
121+
self.select(_as ? "\"#{_as}\"" + '.' + _select : _select)
122122
end
123123
end
124124

@@ -141,7 +141,7 @@ class Atom
141141
unless @join.nil?
142142
{{query}} += @join.not_nil!.join(" ") do |join|
143143
j = " #{join.type} JOIN #{join.table}"
144-
j += (" AS #{join.alias}") if join.alias && join.table != join.alias
144+
j += (" AS \"#{join.alias}\"") if join.alias && join.table != join.alias
145145
j += " ON #{join.on}"
146146
end
147147
end

0 commit comments

Comments
 (0)