rorx/redmine_wiki_sql — Issue #3: Unable to use COUNT(*)
Author: blyxx86
Created: 2012-01-05 03:35:35+00:00
State: open

Using the code in the previous issue, I modified it to allow replacing of the *, where previously it would be erased.

```ruby
Redmine::WikiFormatting::Macros.register do
desc "Run SQL query"
macro :sql do |obj, args|

_sentence = args.join(",")
_sentence = _sentence.gsub("\\(", "(")
_sentence = _sentence.gsub("\\)", ")")
_sentence = _sentence.gsub("\\*", "*")

result = ActiveRecord::Base.connection.execute(_sentence)
unless result.nil?
unless result.num_rows() == 0
column_names = []
for columns in result.fetch_fields.each do
column_names += columns.name.to_a
end

_thead = '
'
column_names.each do |column_name|
_thead << '	' + column_name.to_s + ''
end
_thead << '
'

_tbody = ''
result.each_hash do |record|
unless record.nil?
_tbody << '
'
column_names.each do |column_name|
_tbody << '	' + record[column_name].to_s + ''
end
_tbody << '
'
end
end

_table = '

' << _thead << _tbody << '

'

result.free
return _table
else
result.free
return '0 records returned'
end
else
result.free
return 'No result returned'
end
end
end
```