/
base.cr
47 lines (40 loc) · 1.34 KB
/
base.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Marten
module DB
module Query
module SQL
module Predicate
abstract class Base
class_getter predicate_name : String = ""
def self.predicate_name(name : String?)
@@predicate_name = name
end
def initialize(
@left_operand : Field::Base,
@right_operand : Field::Any | Array(Field::Any),
@alias_prefix : String
)
end
def to_sql(connection : Connection::Base)
{"%s %s" % [sql_left_operand(connection), sql_right_operand(connection)], sql_params(connection)}
end
private def sql_left_operand(connection)
connection.left_operand_for(
"#{@alias_prefix}.#{@left_operand.db_column}",
self.class.predicate_name
)
end
private def sql_params(connection)
[sql_right_operand_param(connection)].flatten
end
private def sql_right_operand(connection)
connection.operator_for(self.class.predicate_name) % "%s"
end
private def sql_right_operand_param(_connection) : ::DB::Any
@left_operand.to_db(@right_operand.as(Field::Any))
end
end
end
end
end
end
end