Class: MiniSql::Builder
- Inherits:
-
Object
- Object
- MiniSql::Builder
- Defined in:
- lib/mini_sql/builder.rb
Instance Method Summary collapse
-
#initialize(connection, template) ⇒ Builder
constructor
A new instance of Builder.
- #to_sql ⇒ Object
Constructor Details
#initialize(connection, template) ⇒ Builder
Returns a new instance of Builder.
5 6 7 8 9 10 |
# File 'lib/mini_sql/builder.rb', line 5 def initialize(connection, template) @args = nil @sql = template @sections = {} @connection = connection end |
Instance Method Details
#to_sql ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mini_sql/builder.rb', line 26 def to_sql sql = @sql.dup @sections.each do |k, v| joined = nil case k when :select joined = (+"SELECT ") << v.join(" , ") when :where, :where2 joined = (+"WHERE ") << v.map { |c| (+"(") << c << ")" }.join(" AND ") when :join joined = v.map { |item| (+"JOIN ") << item }.join("\n") when :left_join joined = v.map { |item| (+"LEFT JOIN ") << item }.join("\n") when :limit joined = (+"LIMIT ") << v.last.to_i.to_s when :offset joined = (+"OFFSET ") << v.last.to_i.to_s when :order_by joined = (+"ORDER BY ") << v.join(" , ") when :set joined = (+"SET ") << v.join(" , ") end sql.sub!("/*#{k}*/", joined) end sql end |