Class: Clearly::Query::Helper
- Inherits:
-
Object
- Object
- Clearly::Query::Helper
- Defined in:
- lib/clearly/query/helper.rb
Overview
Utility methods for working with Arel.
Class Method Summary collapse
-
.exists(node) ⇒ Arel::Nodes::Node
Construct a SQL EXISTS clause.
-
.named_function(name, expression, function_alias = nil) ⇒ Arel::Nodes::Node
Construct an Arel representation of a SQL function.
-
.sql_literal(value) ⇒ Arel::Nodes::Node
Construct a SQL literal.
-
.sql_quoted(value) ⇒ Arel::Nodes::Node
Construct a SQL quoted string.
-
.string_concat(*args) ⇒ Arel::Nodes::Node
Concatenate one or more strings.
-
.string_concat_infix(operator, *args) ⇒ Arel::Nodes::Node
Concatenate strings using an operator.
Class Method Details
.exists(node) ⇒ Arel::Nodes::Node
Construct a SQL EXISTS clause.
66 67 68 |
# File 'lib/clearly/query/helper.rb', line 66 def exists(node) Arel::Nodes::Exists.new(node) end |
.named_function(name, expression, function_alias = nil) ⇒ Arel::Nodes::Node
Construct an Arel representation of a SQL function.
75 76 77 |
# File 'lib/clearly/query/helper.rb', line 75 def named_function(name, expression, function_alias = nil) Arel::Nodes::NamedFunction.new(name, expression, function_alias) end |
.sql_literal(value) ⇒ Arel::Nodes::Node
Construct a SQL literal. This is useful for sql that is too complex for Arel.
51 52 53 |
# File 'lib/clearly/query/helper.rb', line 51 def sql_literal(value) Arel::Nodes::SqlLiteral.new(value) end |
.sql_quoted(value) ⇒ Arel::Nodes::Node
Construct a SQL quoted string. This is used for fragments of SQL.
59 60 61 |
# File 'lib/clearly/query/helper.rb', line 59 def sql_quoted(value) Arel::Nodes.build_quoted(value) end |
.string_concat(*args) ⇒ Arel::Nodes::Node
Concatenate one or more strings
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/clearly/query/helper.rb', line 11 def string_concat(*args) adapter = ActiveRecord::Base.connection.adapter_name.underscore.downcase case adapter when 'mysql' named_function('concat', args) when 'sqlserver' string_concat_infix('+', *args) when 'postgres' when 'sq_lite' string_concat_infix('||', *args) else fail ArgumentError, "unsupported database adapter '#{adapter}'" end end |
.string_concat_infix(operator, *args) ⇒ Arel::Nodes::Node
Concatenate strings using an operator
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/clearly/query/helper.rb', line 31 def string_concat_infix(operator, *args) if args.blank? || args.size < 2 fail ArgumentError, "string concatenation requires operator and two or more arguments, given '#{args.size}'" end result = Arel::Nodes::InfixOperation.new(operator, args[0], args[1]) if args.size > 2 args.drop(2).each do |a| result = Arel::Nodes::InfixOperation.new(operator, result, a) end end result end |