rorx / redmine_wiki_sql

Redmine plugin that allows you to run SQL queries and have them shown on your wiki in table format
http://rodrigoramalho.com/redmine-wiki-sql-plugin/
11 stars 13 forks source link

Unable to use COUNT(*) #3

Closed frypan-handler closed 11 years ago

frypan-handler commented 12 years ago

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

  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 = '<tr>'
            column_names.each do |column_name|
              _thead << '<th>' + column_name.to_s + '</th>'
            end
            _thead << '</tr>'

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

            _table = '<table>' << _thead << _tbody << '</table>' 

            result.free
            return _table
          else
            result.free
            return '0 records returned'
          end
        else
          result.free
          return 'No result returned'
        end
    end 
  end
rorx commented 11 years ago

Plugin updated!