Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #32097 from matthewd/arel
Merge Arel
- Loading branch information
Showing
145 changed files
with
11,379 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Arel # :nodoc: all | ||
class ArelError < StandardError | ||
end | ||
|
||
class EmptyJoinError < ArelError | ||
end | ||
end |
Oops, something went wrong.