Class: SQL
- Inherits:
-
Object
show all
- Defined in:
- lib/eno/sql.rb
Constant Summary
collapse
- H_EMPTY =
{}.freeze
Instance Method Summary
collapse
-
#_i(value) ⇒ Object
-
#_l(value) ⇒ Object
-
#all(sym = nil) ⇒ Object
-
#cond(props) ⇒ Object
-
#context ⇒ Object
-
#default ⇒ Object
-
#default_select ⇒ Object
-
#except(*queries, **props) ⇒ Object
-
#from(*members, **props) ⇒ Object
-
#initialize(escape_proc: nil, **ctx) ⇒ SQL
constructor
-
#intersect(*queries, **props) ⇒ Object
-
#limit(*members) ⇒ Object
-
#method_missing(sym, *args) ⇒ Object
-
#order_by(*members, **props) ⇒ Object
-
#quote(expr) ⇒ Object
-
#select(*members, **props) ⇒ Object
-
#to_sql(&block) ⇒ Object
-
#union(*queries, **props) ⇒ Object
-
#where(expr) ⇒ Object
-
#window(sym, &block) ⇒ Object
-
#with(*members, **props) ⇒ Object
Constructor Details
#initialize(escape_proc: nil, **ctx) ⇒ SQL
Returns a new instance of SQL.
15
16
17
18
|
# File 'lib/eno/sql.rb', line 15
def initialize(escape_proc: nil, **ctx)
@escape_proc = escape_proc
@ctx = ctx
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/eno/sql.rb', line 72
def method_missing(sym, *args)
if @ctx.has_key?(sym)
value = @ctx[sym]
return Symbol === value ? Expressions::Identifier.new(value) : value
end
super if sym == :to_hash
if args.empty?
Expressions::Identifier.new(sym)
else
Expressions::FunctionCall.new(sym, *args)
end
end
|
Instance Method Details
#_i(value) ⇒ Object
64
65
66
|
# File 'lib/eno/sql.rb', line 64
def _i(value)
Expressions::Identifier.new(value)
end
|
#_l(value) ⇒ Object
60
61
62
|
# File 'lib/eno/sql.rb', line 60
def _l(value)
Expressions::Literal.new(value)
end
|
#cond(props) ⇒ Object
132
133
134
|
# File 'lib/eno/sql.rb', line 132
def cond(props)
Expressions::Case.new(props)
end
|
#context ⇒ Object
56
57
58
|
# File 'lib/eno/sql.rb', line 56
def context
@ctx
end
|
#default ⇒ Object
136
137
138
|
# File 'lib/eno/sql.rb', line 136
def default
:default
end
|
#default_select ⇒ Object
68
69
70
|
# File 'lib/eno/sql.rb', line 68
def default_select
Expressions::Select.new(:*)
end
|
#except(*queries, **props) ⇒ Object
148
149
150
|
# File 'lib/eno/sql.rb', line 148
def except(*queries, **props)
@combination = Expressions::Combination.new(*queries, kind: :except, **props)
end
|
#from(*members, **props) ⇒ Object
100
101
102
|
# File 'lib/eno/sql.rb', line 100
def from(*members, **props)
@from = Expressions::From.new(*members, **props)
end
|
#intersect(*queries, **props) ⇒ Object
144
145
146
|
# File 'lib/eno/sql.rb', line 144
def intersect(*queries, **props)
@combination = Expressions::Combination.new(*queries, kind: :intersect, **props)
end
|
#limit(*members) ⇒ Object
120
121
122
|
# File 'lib/eno/sql.rb', line 120
def limit(*members)
@limit = Expressions::Limit.new(*members)
end
|
#order_by(*members, **props) ⇒ Object
116
117
118
|
# File 'lib/eno/sql.rb', line 116
def order_by(*members, **props)
@order_by = Expressions::OrderBy.new(*members, **props)
end
|
#quote(expr) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/eno/sql.rb', line 36
def quote(expr)
if @escape_proc
value = @escape_proc.(expr)
return value if value
end
case expr
when Query::Query
S_PARENS % expr.to_sql(@ctx).strip
when Expressions::Expression
expr.to_sql(self)
when Symbol
expr.to_s
when String
S_QUOTES % expr
else
expr.inspect
end
end
|
#select(*members, **props) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/eno/sql.rb', line 92
def select(*members, **props)
if members.empty? && !props.empty?
members = props.map { |k, v| Expressions::Alias.new(v, k) }
props = {}
end
@select = Expressions::Select.new(*members, **props)
end
|
#to_sql(&block) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/eno/sql.rb', line 20
def to_sql(&block)
instance_eval(&block)
return @combination.to_sql(self) if @combination
[
@with,
@select || default_select,
@from,
@where,
@window,
@order_by,
@limit
].compact.map { |c| c.to_sql(self) }.join(S_SPACE)
end
|
#union(*queries, **props) ⇒ Object
140
141
142
|
# File 'lib/eno/sql.rb', line 140
def union(*queries, **props)
@combination = Expressions::Combination.new(*queries, kind: :union, **props)
end
|
#where(expr) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/eno/sql.rb', line 104
def where(expr)
if @where
@where.members << expr
else
@where = Expressions::Where.new(expr)
end
end
|
#window(sym, &block) ⇒ Object
112
113
114
|
# File 'lib/eno/sql.rb', line 112
def window(sym, &block)
@window = Expressions::Window.new(sym, &block)
end
|
#with(*members, **props) ⇒ Object
86
87
88
|
# File 'lib/eno/sql.rb', line 86
def with(*members, **props)
@with = Expressions::With.new(*members, **props)
end
|