Skip to content

Commit cd43725

Browse files
authored
Merge pull request #8 from sql-rb/operations-with-literals
Support operations with literals
2 parents 4e9a9a6 + f4f1bf6 commit cd43725

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

lib/sql/composer/dsl.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def call
3333
end
3434

3535
def `(value)
36-
Nodes::Literal.new(value: value)
36+
Nodes::Literal.new(value: value, backend: options[:backend])
3737
end
3838

3939
private

lib/sql/composer/nodes/identifier.rb

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# frozen_string_literal: true
22

33
require "sql/composer/nodes/core"
4+
require "sql/composer/nodes/operators"
5+
46

57
module SQL
68
module Composer
79
module Nodes
810
class Identifier < Core
11+
include Operators
12+
913
def name
1014
fetch(:name).to_s
1115
end
@@ -29,19 +33,6 @@ def qualifier
2933
def qualify?
3034
options.key?(:qualifier)
3135
end
32-
33-
# this is probably a stupid idea lol
34-
def ==(other)
35-
value = Nodes::Value.new(input: other, backend: backend)
36-
37-
operation = Operations::Eql.new(left: self, right: value)
38-
39-
if other.start_with?("%") && other.end_with?("%")
40-
tokens.add(other, value)
41-
end
42-
43-
operation
44-
end
4536
end
4637
end
4738
end

lib/sql/composer/nodes/literal.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# frozen_string_literal: true
22

33
require "sql/composer/nodes/core"
4+
require "sql/composer/nodes/operators"
45

56
module SQL
67
module Composer
78
module Nodes
89
class Literal < Core
10+
include Operators
11+
912
def value
1013
fetch(:value)
1114
end

lib/sql/composer/nodes/operators.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "sql/composer/nodes/operations/eql"
4+
require "sql/composer/nodes/operations/order_direction"
5+
6+
module SQL
7+
module Composer
8+
module Nodes
9+
module Operators
10+
def ==(other)
11+
value = Nodes::Value.new(input: other, backend: backend)
12+
13+
operation = Operations::Eql.new(left: self, right: value)
14+
15+
if other.start_with?("%") && other.end_with?("%")
16+
tokens.add(other, value)
17+
end
18+
19+
operation
20+
end
21+
22+
def asc
23+
Operations::Asc.new(self)
24+
end
25+
26+
def desc
27+
Operations::Desc.new(self)
28+
end
29+
end
30+
end
31+
end
32+
end

spec/integration/sql/compose_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ def compose(&block)
4545
end
4646
end
4747

48+
context "with literals in WHERE" do
49+
let(:query) do
50+
compose {
51+
SELECT `"users"."id"`, `"users"."name"`
52+
FROM `"users"`
53+
WHERE `"users"."name"` == 'Jane'
54+
}
55+
end
56+
57+
specify do
58+
expect(result).to eql(
59+
<<~SQL.strip
60+
SELECT "users"."id", "users"."name"
61+
FROM "users"
62+
WHERE "users"."name" == 'Jane'
63+
SQL
64+
)
65+
end
66+
end
67+
4868
context "inline syntax" do
4969
let(:query) do
5070
compose {

0 commit comments

Comments
 (0)