table.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. module Arel
  2. class Table
  3. include Arel::Crud
  4. include Arel::FactoryMethods
  5. @engine = nil
  6. class << self; attr_accessor :engine; end
  7. attr_accessor :name, :engine, :aliases, :table_alias
  8. # TableAlias and Table both have a #table_name which is the name of the underlying table
  9. alias :table_name :name
  10. def initialize name, engine = Table.engine
  11. @name = name.to_s
  12. @engine = engine
  13. @columns = nil
  14. @aliases = []
  15. @table_alias = nil
  16. @primary_key = nil
  17. if Hash === engine
  18. @engine = engine[:engine] || Table.engine
  19. # Sometime AR sends an :as parameter to table, to let the table know
  20. # that it is an Alias. We may want to override new, and return a
  21. # TableAlias node?
  22. @table_alias = engine[:as] unless engine[:as].to_s == @name
  23. end
  24. end
  25. def primary_key
  26. if $VERBOSE
  27. warn <<-eowarn
  28. primary_key (#{caller.first}) is deprecated and will be removed in ARel 4.0.0
  29. eowarn
  30. end
  31. @primary_key ||= begin
  32. primary_key_name = @engine.connection.primary_key(name)
  33. # some tables might be without primary key
  34. primary_key_name && self[primary_key_name]
  35. end
  36. end
  37. def alias name = "#{self.name}_2"
  38. Nodes::TableAlias.new(self, name).tap do |node|
  39. @aliases << node
  40. end
  41. end
  42. def from table
  43. SelectManager.new(@engine, table)
  44. end
  45. def joins manager
  46. if $VERBOSE
  47. warn "joins is deprecated and will be removed in 4.0.0"
  48. warn "please remove your call to joins from #{caller.first}"
  49. end
  50. nil
  51. end
  52. def join relation, klass = Nodes::InnerJoin
  53. return from(self) unless relation
  54. case relation
  55. when String, Nodes::SqlLiteral
  56. raise if relation.blank?
  57. klass = Nodes::StringJoin
  58. end
  59. from(self).join(relation, klass)
  60. end
  61. def group *columns
  62. from(self).group(*columns)
  63. end
  64. def order *expr
  65. from(self).order(*expr)
  66. end
  67. def where condition
  68. from(self).where condition
  69. end
  70. def project *things
  71. from(self).project(*things)
  72. end
  73. def take amount
  74. from(self).take amount
  75. end
  76. def skip amount
  77. from(self).skip amount
  78. end
  79. def having expr
  80. from(self).having expr
  81. end
  82. def columns
  83. if $VERBOSE
  84. warn <<-eowarn
  85. (#{caller.first}) Arel::Table#columns is deprecated and will be removed in
  86. Arel 4.0.0 with no replacement. PEW PEW PEW!!!
  87. eowarn
  88. end
  89. @columns ||=
  90. attributes_for @engine.connection.columns(@name, "#{@name} Columns")
  91. end
  92. def [] name
  93. ::Arel::Attribute.new self, name
  94. end
  95. def select_manager
  96. SelectManager.new(@engine)
  97. end
  98. def insert_manager
  99. InsertManager.new(@engine)
  100. end
  101. private
  102. def attributes_for columns
  103. return nil unless columns
  104. columns.map do |column|
  105. Attributes.for(column).new self, column.name.to_sym
  106. end
  107. end
  108. @@table_cache = nil
  109. def self.table_cache engine # :nodoc:
  110. if $VERBOSE
  111. warn <<-eowarn
  112. (#{caller.first}) Arel::Table.table_cache is deprecated and will be removed in
  113. Arel 4.0.0 with no replacement. PEW PEW PEW!!!
  114. eowarn
  115. end
  116. @@table_cache ||= Hash[engine.connection.tables.map { |x| [x,true] }]
  117. end
  118. end
  119. end