lib/baza/commands/select.rb in baza-0.0.35 vs lib/baza/commands/select.rb in baza-0.0.36
- old
+ new
@@ -16,11 +16,11 @@
end
add_select_sql
add_terms_sql
add_order_sql
- add_limit_sql
+ add_limit_sql unless @db.mssql?
result = execute_query
# Return result if a block wasnt given.
if @block
@@ -34,25 +34,27 @@
def add_select_sql
# Set up IDQuery-stuff if that is given in arguments.
if @args && @args[:idquery]
if @args.fetch(:idquery) == true
- select_sql = "#{@db.sep_col}id#{@db.sep_col}"
+ select_sql = "#{@db.quote_column(:id)}"
@col = :id
else
- select_sql = "#{@db.sep_col}#{@db.escape_column(@args.fetch(:idquery))}#{@db.sep_col}"
+ select_sql = "#{@db.quote_column(@args.fetch(:idquery))}"
@col = @args.fetch(:idquery)
end
end
select_sql ||= "*"
- @sql << "SELECT #{select_sql} FROM"
+ @sql << "SELECT"
+ @sql << top_sql if @db.mssql?
+ @sql << " #{select_sql} FROM"
if @table_name.is_a?(Array)
- @sql << " #{@sep_table}#{@table_name.first}#{@sep_table}.#{@sep_table}#{@table_name.last}#{@sep_table}"
+ @sql << " #{@db.quote_table(@table_name.first)}.#{@db.quote_table(@table_name.last)}"
else
- @sql << " #{@sep_table}#{@table_name}#{@sep_table}"
+ @sql << " #{@db.quote_table(@table_name)}"
end
end
def add_terms_sql
@sql << " WHERE #{@db.sql_make_where(@terms)}" if !@terms.nil? && !@terms.empty?
@@ -67,14 +69,14 @@
if @args.fetch(:orderby).is_a?(Array)
first = true
@args.fetch(:orderby).each do |order_by|
@sql << "," unless first
first = false if first
- @sql << " #{@db.sep_col}#{@db.escape_column(order_by)}#{@db.sep_col}"
+ @sql << " #{@db.quote_column(order_by)}"
end
else
- @sql << " #{@db.sep_col}#{@db.escape_column(@args.fetch(:orderby))}#{@db.sep_col}"
+ @sql << " #{@db.quote_column(@args.fetch(:orderby))}"
end
end
end
def add_limit_sql
@@ -95,9 +97,14 @@
raise "'limit_to' was not numeric: '#{@args[:limit_to]}'."
end
@sql << " LIMIT #{@args.fetch(:limit_from)}, #{@args.fetch(:limit_to)}"
end
+ end
+
+ def top_sql
+ return unless @args[:limit]
+ " TOP #{@args[:limit].to_i}"
end
def execute_query
# Do IDQuery if given in arguments.
if @args && @args[:idquery]