1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

insert manager is starting to take shape

This commit is contained in:
Aaron Patterson 2010-08-13 15:30:22 -07:00
parent a37bdba9be
commit b1e7bd7d02
12 changed files with 121 additions and 52 deletions

View file

@ -8,6 +8,8 @@ require 'arel/relation'
####
require 'arel/tree_manager'
require 'arel/insert_manager'
require 'arel/select_manager'
require 'arel/nodes'
#### these are deprecated

View file

@ -0,0 +1,13 @@
module Arel
class InsertManager < Arel::TreeManager
def initialize engine
super
@head = Nodes::InsertStatement.new
end
def into table
@head.relation = table
self
end
end
end

View file

@ -2,3 +2,4 @@ require 'arel/nodes/equality'
require 'arel/nodes/sql_literal'
require 'arel/nodes/select_core'
require 'arel/nodes/select_statement'
require 'arel/nodes/insert_statement'

View file

@ -0,0 +1,13 @@
module Arel
module Nodes
class InsertStatement
attr_accessor :relation, :columns, :values
def initialize
@relation = nil
@columns = []
@values = []
end
end
end
end

View file

@ -0,0 +1,29 @@
module Arel
class SelectManager < Arel::TreeManager
def initialize engine
super
@head = Nodes::SelectStatement.new
@ctx = @head.cores.last
end
def from table
@ctx.froms << table
self
end
def project projection
@ctx.projections << projection
self
end
def where expr
@ctx.wheres << expr
self
end
def take limit
@head.limit = limit
self
end
end
end

View file

@ -13,7 +13,7 @@ module Arel
end
def tm
TreeManager.new(@engine).from(self)
SelectManager.new(@engine).from(self)
end
def where condition

View file

@ -4,38 +4,12 @@ module Arel
include Arel::Relation
def initialize engine
@engine = engine
@selects = []
# default to Select
@stmt = Nodes::SelectStatement.new
@core = @stmt.cores.last
@selects << @stmt
end
def from table
@core.froms << table
self
end
def project projection
@core.projections << projection
self
end
def where expr
@core.wheres << expr
self
end
def take limit
@stmt.limit = limit
self
@engine = engine
end
def to_sql
viz = Visitors::ToSql.new @engine
viz.accept @stmt
viz.accept @head
end
end
end

View file

@ -12,6 +12,10 @@ module Arel
end
private
def visit_Arel_Nodes_InsertStatement o
"INSERT INTO #{visit o.relation}"
end
def visit_Arel_Nodes_SelectStatement o
[
o.cores.map { |x| visit x }.join,

View file

@ -0,0 +1,29 @@
require 'spec_helper'
module Arel
describe 'insert manager' do
describe 'new' do
it 'takes an engine' do
Arel::InsertManager.new Table.engine
end
end
describe 'into' do
it 'takes an engine' do
manager = Arel::InsertManager.new Table.engine
manager.into(Table.new(:users)).should == manager
end
end
describe 'to_sql' do
it 'converts to sql' do
table = Table.new :users
manager = Arel::InsertManager.new Table.engine
manager.into table
manager.to_sql.should be_like %{
INSERT INTO "users"
}
end
end
end
end

View file

@ -1,46 +1,46 @@
require 'spec_helper'
module Arel
describe 'tree manager' do
describe 'select manager' do
describe 'project' do
it 'takes strings' do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.project '*'
manager.to_sql.should == %{
manager.to_sql.should be_like %{
SELECT *
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
}
end
it "takes sql literals" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.project Nodes::SqlLiteral.new '*'
manager.to_sql.should == %{
manager.to_sql.should be_like %{
SELECT *
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
}
end
end
describe 'take' do
it "knows take" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.from(table).project(table['id'])
manager.where(table['id'].eq(1))
manager.take 1
manager.to_sql.should == %{
manager.to_sql.should be_like %{
SELECT "users"."id"
FROM "users"
WHERE "users"."id" = 1
LIMIT 1
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
}
end
it "chains" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.take(1).should == manager
end
end
@ -48,19 +48,19 @@ module Arel
describe 'where' do
it "knows where" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.from(table).project(table['id'])
manager.where(table['id'].eq(1))
manager.to_sql.should == %{
manager.to_sql.should be_like %{
SELECT "users"."id"
FROM "users"
WHERE "users"."id" = 1
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
}
end
it "chains" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.from(table)
manager.project(table['id']).where(table['id'].eq 1).should == manager
end
@ -69,18 +69,18 @@ module Arel
describe 'from' do
it "makes sql" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.from table
manager.project table['id']
manager.to_sql.should == 'SELECT "users"."id" FROM "users"'
manager.to_sql.should be_like 'SELECT "users"."id" FROM "users"'
end
it "chains" do
table = Table.new :users
manager = Arel::TreeManager.new Table.engine
manager = Arel::SelectManager.new Table.engine
manager.from(table).project(table['id']).should == manager
manager.to_sql.should == 'SELECT "users"."id" FROM "users"'
manager.to_sql.should be_like 'SELECT "users"."id" FROM "users"'
end
end
end

View file

@ -21,10 +21,10 @@ module Arel
describe 'project' do
it 'can project' do
manager = @relation.project '*'
manager.to_sql.should == %{
manager.to_sql.should be_like %{
SELECT *
FROM "users"
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
}
end
end
@ -33,11 +33,11 @@ module Arel
manager = @relation.where @relation[:id].eq 1
manager.project @relation[:id]
manager.should be_kind_of TreeManager
manager.to_sql.should == %{
manager.to_sql.should be_like %{
SELECT "users"."id"
FROM "users"
WHERE "users"."id" = 1
}.gsub("\n", '').gsub(/(^\s*|\s*$)/, '').squeeze(' ')
}
end
end

View file

@ -3,11 +3,15 @@ require 'spec'
require 'fileutils'
require 'arel'
require 'support/matchers/be_like'
if adapter = ENV['ADAPTER']
require "support/connections/#{adapter}_connection.rb"
end
Spec::Runner.configure do |config|
config.include Matchers
if defined?(ActiveRecord::Base)
tmp = File.expand_path('../../tmp', __FILE__)