Skip to content

Commit 27701c6

Browse files
committed
Added options hash to Node#to_sql methods.
1 parent fe8b0db commit 27701c6

11 files changed

+46
-46
lines changed

lib/sql_tree/node.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(attributes = {}) # :nodoc:
2020

2121
# Pretty prints this instance for inspection
2222
def inspect
23-
"#{self.class.name}[#{self.to_sql}]"
23+
"#{self.class.name}[#{self.to_sql(options)}]"
2424
end
2525

2626
# Quotes a variable name so that it can be safely used within

lib/sql_tree/node/delete_query.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def initialize(table, where = nil)
1818
end
1919

2020
# Generates an SQL DELETE query from this node.
21-
def to_sql
22-
sql = "DELETE FROM #{table.to_sql}"
23-
sql << " WHERE #{where.to_sql}" if self.where
21+
def to_sql(options = {})
22+
sql = "DELETE FROM #{table.to_sql(options)}"
23+
sql << " WHERE #{where.to_sql(options)}" if self.where
2424
sql
2525
end
2626

lib/sql_tree/node/expression.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class PrefixOperator < SQLTree::Node::Expression
7272
attr_accessor :rhs
7373

7474
# Generates an SQL fragment for this prefix operator expression.
75-
def to_sql
76-
"#{operator.upcase} #{rhs.to_sql}"
75+
def to_sql(options = {})
76+
"#{operator.upcase} #{rhs.to_sql(options)}"
7777
end
7878

7979
def ==(other) # :nodoc:
@@ -123,8 +123,8 @@ def ==(other) # :nodoc:
123123
end
124124

125125
# Generates an SQL fragment for this postfix operator expression.
126-
def to_sql
127-
"#{lhs.to_sql} #{operator}"
126+
def to_sql(options = {})
127+
"#{lhs.to_sql(options)} #{operator}"
128128
end
129129

130130
# Parses a postfix operator expression. This method is not yet implemented.
@@ -175,8 +175,8 @@ class BinaryOperator < SQLTree::Node::Expression
175175
attr_accessor :rhs
176176

177177
# Generates an SQL fragment for this exression.
178-
def to_sql
179-
"(#{lhs.to_sql} #{operator} #{rhs.to_sql})"
178+
def to_sql(options = {})
179+
"(#{lhs.to_sql(options)} #{operator} #{rhs.to_sql(options)})"
180180
end
181181

182182
def ==(other) # :nodoc:
@@ -282,8 +282,8 @@ def initialize(*items)
282282
end
283283

284284
# Generates an SQL fragment for this list.
285-
def to_sql
286-
"(#{items.map {|i| i.to_sql}.join(', ')})"
285+
def to_sql(options = {})
286+
"(#{items.map {|i| i.to_sql(options)}.join(', ')})"
287287
end
288288

289289
def ==(other) # :nodoc:
@@ -336,8 +336,8 @@ class FunctionCall < SQLTree::Node::Expression
336336
attr_accessor :argument_list
337337

338338
# Generates an SQL fragment for this function call.
339-
def to_sql
340-
"#{function}(" + argument_list.items.map { |e| e.to_sql }.join(', ') + ")"
339+
def to_sql(options = {})
340+
"#{function}(" + argument_list.items.map { |e| e.to_sql(options) }.join(', ') + ")"
341341
end
342342

343343
def ==(other) # :nodoc:
@@ -383,7 +383,7 @@ def initialize(value) # :nodoc:
383383
#
384384
# @return [String] A correctly quoted value that can be used safely
385385
# within an SQL query
386-
def to_sql
386+
def to_sql(options = {})
387387
case value
388388
when nil then 'NULL'
389389
when String then quote_str(@value)
@@ -434,7 +434,7 @@ def initialize(name) # :nodoc:
434434
#
435435
# @return [String] A correctly quoted variable that can be safely
436436
# used in SQL queries
437-
def to_sql
437+
def to_sql(options = {})
438438
quote_var(@name)
439439
end
440440

@@ -481,7 +481,7 @@ def initialize(name, table = nil)
481481

482482
# Generates a correctly quoted reference to the field, which can
483483
# be incorporated safely into an SQL query.
484-
def to_sql
484+
def to_sql(options = {})
485485
@table.nil? ? quote_var(@name) : quote_var(@table) + '.' + quote_var(@name)
486486
end
487487

lib/sql_tree/node/insert_query.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def initialize(table, fields = nil, values = [])
88
@table, @fields, @values = table, fields, values
99
end
1010

11-
def to_sql
12-
sql = "INSERT INTO #{table.to_sql} "
13-
sql << '(' + fields.map { |f| f.to_sql }.join(', ') + ') ' if fields
14-
sql << 'VALUES (' + values.map { |v| v.to_sql }.join(', ') + ')'
11+
def to_sql(options = {})
12+
sql = "INSERT INTO #{table.to_sql(options)} "
13+
sql << '(' + fields.map { |f| f.to_sql(options) }.join(', ') + ') ' if fields
14+
sql << 'VALUES (' + values.map { |v| v.to_sql(options) }.join(', ') + ')'
1515
sql
1616
end
1717

lib/sql_tree/node/join.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def initialize(values = {})
88
values.each { |key, value| self.send(:"#{key}=", value) }
99
end
1010

11-
def to_sql
11+
def to_sql(options = {})
1212
join_sql = join_type ? "#{join_type.to_s.upcase} " : ""
13-
join_sql << "JOIN #{table_reference.to_sql} "
14-
join_sql << "ON #{join_expression.to_sql}"
13+
join_sql << "JOIN #{table_reference.to_sql(options)} "
14+
join_sql << "ON #{join_expression.to_sql(options)}"
1515
join_sql
1616
end
1717

lib/sql_tree/node/ordering.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def initialize(expression, direction = nil)
88
@expression, @direction = expression, direction
99
end
1010

11-
def to_sql
12-
sql = expression.to_sql
11+
def to_sql(options = {})
12+
sql = expression.to_sql(options)
1313
sql << " #{direction.to_s.upcase}" if direction
1414
sql
1515
end

lib/sql_tree/node/select_declaration.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class SelectDeclaration < Base
44

55
attr_accessor :expression, :variable
66

7-
def to_sql
8-
sql = @expression.to_sql
7+
def to_sql(options = {})
8+
sql = @expression.to_sql(options)
99
sql << " AS " << quote_var(@variable) if @variable
1010
return sql
1111
end
@@ -60,7 +60,7 @@ def ==(other)
6060
other.class == self.class && other.table == self.table
6161
end
6262

63-
def to_sql
63+
def to_sql(options = {})
6464
table ? "#{quote_var(table)}.*" : '*'
6565
end
6666
end

lib/sql_tree/node/select_query.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ def initialize
99
@select = []
1010
end
1111

12-
def to_sql
12+
def to_sql(options = {})
1313
raise "At least one SELECT expression is required" if self.select.empty?
1414
sql = (self.distinct) ? "SELECT DISTINCT " : "SELECT "
15-
sql << select.map { |s| s.to_sql }.join(', ')
16-
sql << " FROM " << from.map { |f| f.to_sql }.join(', ') if from
17-
sql << " WHERE " << where.to_sql if where
18-
sql << " GROUP BY " << group_by.map { |g| g.to_sql }.join(', ') if group_by
19-
sql << " ORDER BY " << order_by.map { |o| o.to_sql }.join(', ') if order_by
20-
sql << " HAVING " << having.to_sql if having
15+
sql << select.map { |s| s.to_sql(options) }.join(', ')
16+
sql << " FROM " << from.map { |f| f.to_sql(options) }.join(', ') if from
17+
sql << " WHERE " << where.to_sql(options) if where
18+
sql << " GROUP BY " << group_by.map { |g| g.to_sql(options) }.join(', ') if group_by
19+
sql << " ORDER BY " << order_by.map { |o| o.to_sql(options) }.join(', ') if order_by
20+
sql << " HAVING " << having.to_sql(options) if having
2121
return sql
2222
end
2323

lib/sql_tree/node/source.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def table_alias
1616
table_reference.table_alias
1717
end
1818

19-
def to_sql
20-
sql = table_reference.to_sql
21-
sql << ' ' << joins.map { |j| j.to_sql }.join(' ') if joins.any?
19+
def to_sql(options = {})
20+
sql = table_reference.to_sql(options)
21+
sql << ' ' << joins.map { |j| j.to_sql(options) }.join(' ') if joins.any?
2222
return sql
2323
end
2424

lib/sql_tree/node/table_reference.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(table, table_alias = nil)
88
@table, @table_alias = table, table_alias
99
end
1010

11-
def to_sql
11+
def to_sql(options = {})
1212
sql = quote_var(table)
1313
sql << " AS " << quote_var(table_alias) if table_alias
1414
return sql

lib/sql_tree/node/update_query.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def initialize(table, updates = [], where = nil)
2222

2323
# Generates the SQL UPDATE query.
2424
# @return [String] The SQL update query
25-
def to_sql
26-
sql = "UPDATE #{table.to_sql} SET "
27-
sql << updates.map { |u| u.to_sql }.join(', ')
28-
sql << " WHERE " << where.to_sql if self.where
25+
def to_sql(options = {})
26+
sql = "UPDATE #{table.to_sql(options)} SET "
27+
sql << updates.map { |u| u.to_sql(options) }.join(', ')
28+
sql << " WHERE " << where.to_sql(options) if self.where
2929
sql
3030
end
3131

@@ -78,8 +78,8 @@ def initialize(field, expression = nil)
7878
# Generates an SQL fragment for this node.
7979
# @return [String] An SQL fragment that can be embedded in the SET
8080
# clause of on SQL UPDATE query.
81-
def to_sql
82-
"#{field.to_sql} = #{expression.to_sql}"
81+
def to_sql(options = {})
82+
"#{field.to_sql(options)} = #{expression.to_sql(options)}"
8383
end
8484

8585
# Parses an Assignment node from a stream of tokens. Syntax:

0 commit comments

Comments
 (0)