crud.rb 1.3 KB
Newer Older
1 2 3 4
module Arel
  ###
  # FIXME hopefully we can remove this
  module Crud
5
    def compile_update values
6
      um = UpdateManager.new @engine
A
Aaron Patterson 已提交
7

8
      if Nodes::SqlLiteral === values
9
        relation = @ctx.from
A
Aaron Patterson 已提交
10
      else
11
        relation = values.first.first.relation
A
Aaron Patterson 已提交
12
      end
13
      um.table relation
14
      um.set values
15
      um.take @ast.limit.expr if @ast.limit
A
Aaron Patterson 已提交
16
      um.order(*@ast.orders)
A
Aaron Patterson 已提交
17
      um.wheres = @ctx.wheres
18 19 20 21 22 23 24
      um
    end

    # FIXME: this method should go away
    def update values
      if $VERBOSE
        warn <<-eowarn
X
Xavier Noria 已提交
25
update (#{caller.first}) is deprecated and will be removed in Arel 4.0.0. Please
26 27 28
switch to `compile_update`
        eowarn
      end
29

30
      um = compile_update values
31
      @engine.connection.update um.to_sql, 'AREL'
32 33
    end

A
Aaron Patterson 已提交
34
    def compile_insert values
A
Aaron Patterson 已提交
35
      im = create_insert
36
      im.insert values
A
Aaron Patterson 已提交
37 38 39
      im
    end

A
Aaron Patterson 已提交
40 41 42 43
    def create_insert
      InsertManager.new @engine
    end

A
Aaron Patterson 已提交
44 45 46 47
    # FIXME: this method should go away
    def insert values
      if $VERBOSE
        warn <<-eowarn
X
Xavier Noria 已提交
48
insert (#{caller.first}) is deprecated and will be removed in Arel 4.0.0. Please
A
Aaron Patterson 已提交
49 50 51 52
switch to `compile_insert`
        eowarn
      end
      @engine.connection.insert compile_insert(values).to_sql
53
    end
A
Aaron Patterson 已提交
54

55
    def compile_delete
A
Aaron Patterson 已提交
56 57
      dm = DeleteManager.new @engine
      dm.wheres = @ctx.wheres
58
      dm.from @ctx.froms
59 60 61
      dm
    end

62 63
  end
end