Module: ActiveRecord::Refined::AST

Defined in:
lib/active_record/refined/ast.rb

Defined Under Namespace

Modules: Arithmetics, BitwiseOperands, ComputedJson, JsonComparable, JsonDocument, JsonSteps, NumericArithmetics, Predications, SetSubquery, Windowing Classes: Aggregate, And, Arithmetic, ArrayPredicate, As, Bitwise, BitwiseNot, Case, Cast, Column, Comparison, DatetimeValueFunction, DistinctFrom, Exists, Extract, Function, GroupingSets, In, JsonAggregate, JsonBuild, JsonContains, JsonExcept, JsonHasKey, JsonKeys, JsonLiteral, JsonPath, JsonSet, Like, Match, Node, Not, Operation, Or, Ordering, Over, Predicate, Quantified, Sql, TruthValue, Value, WindowFunction

Constant Summary collapse

NAME =
/[[:alpha:]_][[:alnum:]_$]*/
ALIAS_NAME =
/\A#{NAME}\z/
FUNCTION_NAME =
/\A#{NAME}(\.#{NAME})?\z/
TYPE_NAME =

A SQL type as cast writes it: words, at most parenthesized with lengths -- double precision, decimal(10,2).

/\A[[:alpha:]_][[:alnum:]_ ]*(\(\d+(, ?\d+)?\))?\z/
OPERATOR =

The characters PostgreSQL allows an operator to be made of, the widest operator alphabet of the three; op admits nothing else, so a letter, a space or a quote never reaches the SQL as an operator.

%r{\A[+\-*/<>=~!@\#%^&|`?]+\z}
ADAPTER_FAMILIES =

Which family of spellings an adapter belongs to. MariaDB answers to the mysql2 adapter and is counted with MySQL, though the two part company over JSON. An adapter nobody has classified keeps the standard spellings and is left to say for itself what it cannot do.

pglite is PostgreSQL itself compiled to WebAssembly, reached through wasmify-rails' adapter; the server it answers for is the same one.

{
  "sqlite3" => :sqlite,
  "postgresql" => :postgresql,
  "postgis" => :postgresql,
  "pglite" => :postgresql,
  "mysql2" => :mysql,
  "trilogy" => :mysql,
}.freeze

Class Method Summary collapse

Class Method Details

.adapter_family(model) ⇒ Object



36
37
38
# File 'lib/active_record/refined/ast.rb', line 36

def self.adapter_family(model)
  ADAPTER_FAMILIES[model.connection_db_config.adapter] || :unknown
end

.check_name(name, pattern, what) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
# File 'lib/active_record/refined/ast.rb', line 40

def self.check_name(name, pattern, what)
  return name if pattern.match?(name.to_s)
  raise ArgumentError, "#{name.inspect} is not a plain #{what}"
end

.json_argument(value, model) ⇒ Object

A Ruby document or boolean written where JSON is wanted. Taken as it is, a document would be the string that spells it and a boolean SQLite's own 1. SQLite's json() marks the literal for the JSON functions; the MySQL family, which has no json(), reads it with JSON_EXTRACT.



50
51
52
53
54
55
56
57
58
# File 'lib/active_record/refined/ast.rb', line 50

def self.json_argument(value, model)
  json = Arel::Nodes.build_quoted(JSON.generate(value))
  if adapter_family(model) == :sqlite
    Arel::Nodes::NamedFunction.new("json", [json])
  else
    Arel::Nodes::NamedFunction.new(
      "JSON_EXTRACT", [json, Arel::Nodes.build_quoted("$")])
  end
end