/usr/lib/ruby/vendor_ruby/sequel/extensions/sql_expr.rb is in ruby-sequel 3.33.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # The sql_expr extension adds the sql_expr method to every object, which
# returns an object that works nicely with Sequel's DSL. This is
# best shown by example:
#
# 1.sql_expr < :a # 1 < a
# false.sql_expr & :a # FALSE AND a
# true.sql_expr | :a # TRUE OR a
# ~nil.sql_expr # NOT NULL
# "a".sql_expr + "b" # 'a' || 'b'
module Sequel
module SQL
# The GenericComplexExpression acts like a
# GenericExpression in terms of methods,
# but has an internal structure of a
# ComplexExpression. It is used by Object#sql_expr.
# Since we don't know what specific type of object
# we are dealing with it, we treat it similarly to
# how we treat symbols or literal strings, allowing
# many different types of methods.
class GenericComplexExpression < ComplexExpression
include AliasMethods
include BooleanMethods
include CastMethods
include ComplexExpressionMethods
include InequalityMethods
include NumericMethods
include OrderMethods
include StringMethods
include SubscriptMethods
end
end
end
class Object
# Return a copy of the object wrapped in a
# Sequel::SQL::GenericComplexExpression. Allows easy use
# of the Object with Sequel's DSL. You'll probably have
# to make sure that Sequel knows how to literalize the
# object properly, though.
def sql_expr
Sequel::SQL::GenericComplexExpression.new(:NOOP, self)
end
end
class FalseClass
# Returns a copy of the object wrapped in a
# Sequel::SQL::BooleanExpression, allowing easy use
# of Sequel's DSL:
#
# false.sql_expr & :a # FALSE AND a
def sql_expr
Sequel::SQL::BooleanExpression.new(:NOOP, self)
end
end
class NilClass
# Returns a copy of the object wrapped in a
# Sequel::SQL::BooleanExpression, allowing easy use
# of Sequel's DSL:
#
# ~nil.sql_expr # NOT NULL
def sql_expr
Sequel::SQL::BooleanExpression.new(:NOOP, self)
end
end
class Numeric
# Returns a copy of the object wrapped in a
# Sequel::SQL::NumericExpression, allowing easy use
# of Sequel's DSL:
#
# 1.sql_expr < :a # 1 < a
def sql_expr
Sequel::SQL::NumericExpression.new(:NOOP, self)
end
end
class Proc
# Evaluates the proc as a virtual row block.
# If a hash or array of two element arrays is returned,
# they are converted to a Sequel::SQL::BooleanExpression. Otherwise,
# unless the object returned is already an Sequel::SQL::Expression,
# convert the object to an Sequel::SQL::GenericComplexExpression.
#
# proc{a(b)}.sql_expr + 1 # a(b) + 1
# proc{{a=>b}}.sql_expr | true # (a = b) OR TRUE
# proc{1}.sql_expr + :a # 1 + a
def sql_expr
o = Sequel.virtual_row(&self)
if Sequel.condition_specifier?(o)
Sequel::SQL::BooleanExpression.from_value_pairs(o, :AND)
elsif o.is_a?(Sequel::SQL::Expression)
o
else
Sequel::SQL::GenericComplexExpression.new(:NOOP, o)
end
end
end
class String
# Returns a copy of the object wrapped in a
# Sequel::SQL::StringExpression, allowing easy use
# of Sequel's DSL:
#
# "a".sql_expr + :a # 'a' || a
def sql_expr
Sequel::SQL::StringExpression.new(:NOOP, self)
end
end
class TrueClass
# Returns a copy of the object wrapped in a
# Sequel::SQL::BooleanExpression, allowing easy use
# of Sequel's DSL:
#
# true.sql_expr | :a # TRUE OR a
def sql_expr
Sequel::SQL::BooleanExpression.new(:NOOP, self)
end
end
|