Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #32097 from matthewd/arel
Merge Arel
  • Loading branch information
matthewd committed Apr 24, 2018
2 parents 7e81541 + 354f1c2 commit 989b1cb
Show file tree
Hide file tree
Showing 145 changed files with 11,379 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Gemfile.lock
Expand Up @@ -57,7 +57,6 @@ PATH
activerecord (6.0.0.alpha)
activemodel (= 6.0.0.alpha)
activesupport (= 6.0.0.alpha)
arel (>= 9.0)
activestorage (6.0.0.alpha)
actionpack (= 6.0.0.alpha)
activerecord (= 6.0.0.alpha)
Expand Down Expand Up @@ -106,7 +105,6 @@ GEM
amq-protocol (2.2.0)
archive-zip (0.7.0)
io-like (~> 0.3.0)
arel (9.0.0)
ast (2.4.0)
aws-partitions (1.20.0)
aws-sdk-core (3.3.0)
Expand Down
2 changes: 2 additions & 0 deletions activerecord/MIT-LICENSE
@@ -1,5 +1,7 @@
Copyright (c) 2004-2018 David Heinemeier Hansson

Arel originally copyright (c) 2007-2016 Nick Kallen, Bryan Helmkamp, Emilio Tagua, Aaron Patterson

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
Expand Down
2 changes: 0 additions & 2 deletions activerecord/activerecord.gemspec
Expand Up @@ -30,6 +30,4 @@ Gem::Specification.new do |s|

s.add_dependency "activesupport", version
s.add_dependency "activemodel", version

s.add_dependency "arel", ">= 9.0"
end
40 changes: 40 additions & 0 deletions activerecord/lib/arel.rb
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require "arel/errors"

require "arel/crud"
require "arel/factory_methods"

require "arel/expressions"
require "arel/predications"
require "arel/window_predications"
require "arel/math"
require "arel/alias_predication"
require "arel/order_predications"
require "arel/table"
require "arel/attributes"
require "arel/compatibility/wheres"

require "arel/visitors"
require "arel/collectors/sql_string"

require "arel/tree_manager"
require "arel/insert_manager"
require "arel/select_manager"
require "arel/update_manager"
require "arel/delete_manager"
require "arel/nodes"

module Arel # :nodoc: all
VERSION = "10.0.0"

def self.sql(raw_sql)
Arel::Nodes::SqlLiteral.new raw_sql
end

def self.star
sql "*"
end
## Convenience Alias
Node = Arel::Nodes::Node
end
9 changes: 9 additions & 0 deletions activerecord/lib/arel/alias_predication.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module AliasPredication
def as(other)
Nodes::As.new self, Nodes::SqlLiteral.new(other)
end
end
end
22 changes: 22 additions & 0 deletions activerecord/lib/arel/attributes.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "arel/attributes/attribute"

module Arel # :nodoc: all
module Attributes
###
# Factory method to wrap a raw database +column+ to an Arel Attribute.
def self.for(column)
case column.type
when :string, :text, :binary then String
when :integer then Integer
when :float then Float
when :decimal then Decimal
when :date, :datetime, :timestamp, :time then Time
when :boolean then Boolean
else
Undefined
end
end
end
end
37 changes: 37 additions & 0 deletions activerecord/lib/arel/attributes/attribute.rb
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module Attributes
class Attribute < Struct.new :relation, :name
include Arel::Expressions
include Arel::Predications
include Arel::AliasPredication
include Arel::OrderPredications
include Arel::Math

###
# Create a node for lowering this attribute
def lower
relation.lower self
end

def type_cast_for_database(value)
relation.type_cast_for_database(name, value)
end

def able_to_type_cast?
relation.able_to_type_cast?
end
end

class String < Attribute; end
class Time < Attribute; end
class Boolean < Attribute; end
class Decimal < Attribute; end
class Float < Attribute; end
class Integer < Attribute; end
class Undefined < Attribute; end
end

Attribute = Attributes::Attribute
end
24 changes: 24 additions & 0 deletions activerecord/lib/arel/collectors/bind.rb
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module Collectors
class Bind
def initialize
@binds = []
end

def <<(str)
self
end

def add_bind(bind)
@binds << bind
self
end

def value
@binds
end
end
end
end
32 changes: 32 additions & 0 deletions activerecord/lib/arel/collectors/composite.rb
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module Collectors
class Composite
def initialize(left, right)
@left = left
@right = right
end

def <<(str)
left << str
right << str
self
end

def add_bind(bind, &block)
left.add_bind bind, &block
right.add_bind bind, &block
self
end

def value
[left.value, right.value]
end

protected

attr_reader :left, :right
end
end
end
20 changes: 20 additions & 0 deletions activerecord/lib/arel/collectors/plain_string.rb
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module Collectors
class PlainString
def initialize
@str = "".dup
end

def value
@str
end

def <<(str)
@str << str
self
end
end
end
end
24 changes: 24 additions & 0 deletions activerecord/lib/arel/collectors/sql_string.rb
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "arel/collectors/plain_string"

module Arel # :nodoc: all
module Collectors
class SQLString < PlainString
def initialize(*)
super
@bind_index = 1
end

def add_bind(bind)
self << yield(@bind_index)
@bind_index += 1
self
end

def compile(bvs)
value
end
end
end
end
29 changes: 29 additions & 0 deletions activerecord/lib/arel/collectors/substitute_binds.rb
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module Collectors
class SubstituteBinds
def initialize(quoter, delegate_collector)
@quoter = quoter
@delegate = delegate_collector
end

def <<(str)
delegate << str
self
end

def add_bind(bind)
self << quoter.quote(bind)
end

def value
delegate.value
end

protected

attr_reader :quoter, :delegate
end
end
end
35 changes: 35 additions & 0 deletions activerecord/lib/arel/compatibility/wheres.rb
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Arel # :nodoc: all
module Compatibility # :nodoc:
class Wheres # :nodoc:
include Enumerable

module Value # :nodoc:
attr_accessor :visitor
def value
visitor.accept self
end

def name
super.to_sym
end
end

def initialize(engine, collection)
@engine = engine
@collection = collection
end

def each
to_sql = Visitors::ToSql.new @engine

@collection.each { |c|
c.extend(Value)
c.visitor = to_sql
yield c
}
end
end
end
end
42 changes: 42 additions & 0 deletions activerecord/lib/arel/crud.rb
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Arel # :nodoc: all
###
# FIXME hopefully we can remove this
module Crud
def compile_update(values, pk)
um = UpdateManager.new

if Nodes::SqlLiteral === values
relation = @ctx.from
else
relation = values.first.first.relation
end
um.key = pk
um.table relation
um.set values
um.take @ast.limit.expr if @ast.limit
um.order(*@ast.orders)
um.wheres = @ctx.wheres
um
end

def compile_insert(values)
im = create_insert
im.insert values
im
end

def create_insert
InsertManager.new
end

def compile_delete
dm = DeleteManager.new
dm.take @ast.limit.expr if @ast.limit
dm.wheres = @ctx.wheres
dm.from @ctx.froms
dm
end
end
end
25 changes: 25 additions & 0 deletions activerecord/lib/arel/delete_manager.rb
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Arel # :nodoc: all
class DeleteManager < Arel::TreeManager
def initialize
super
@ast = Nodes::DeleteStatement.new
@ctx = @ast
end

def from(relation)
@ast.relation = relation
self
end

def take(limit)
@ast.limit = Nodes::Limit.new(Nodes.build_quoted(limit)) if limit
self
end

def wheres=(list)
@ast.wheres = list
end
end
end
9 changes: 9 additions & 0 deletions activerecord/lib/arel/errors.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Arel # :nodoc: all
class ArelError < StandardError
end

class EmptyJoinError < ArelError
end
end

0 comments on commit 989b1cb

Please sign in to comment.