class TableauServerClient::Resources::Datasource::DatasourceContent

Constants

NamedConnection

Attributes

xml[R]

Public Class Methods

new(xml) click to toggle source
# File lib/tableau_server_client/resources/datasource.rb, line 60
def initialize(xml)
  @xml = xml
end

Public Instance Methods

custom_queries() click to toggle source
# File lib/tableau_server_client/resources/datasource.rb, line 73
def custom_queries
  relations.select {|r| r['type'] == 'text' }.map {|c| c.content }
end
named_connections() click to toggle source
# File lib/tableau_server_client/resources/datasource.rb, line 67
def named_connections
  xml.xpath('//named-connection').map do |c|
    NamedConnection.new(c.first_element_child['class'], c['caption'], c['name'])
  end
end
tables() click to toggle source
# File lib/tableau_server_client/resources/datasource.rb, line 77
def tables
  tables  = []
  redshift_connections = named_connections.select {|c| c.class == 'redshift' }.map {|c| c.name }
  relations.each do |rel|
    next unless redshift_connections.include? rel['connection']
    case rel['type']
    when 'table'
      tables << rel['table']
    when 'text'
      tables.concat extract_tables(rel.content)
    else
      next
    end
  end
  tables.map {|t| t.gsub(/[\[\]")]/, '')}.uniq
end

Private Instance Methods

extract_tables(query) click to toggle source
# File lib/tableau_server_client/resources/datasource.rb, line 100
def extract_tables(query)
  q = query.dup
  q.gsub!(/(\<\[Parameters\]\.\[.*?\]\>)/, "'\\1'")
  q.gsub!(/(--[^\r\n]*)|(\/\*[\w\W]*?(?=\*\/)\*\/)/m, '')
  q.gsub!(/[\t\r\n]/, ' ')
  q.gsub!(/\s+/, ' ')

  tables = []
  may_be_table = false
  q.split(' ').each do |t|
    t.downcase!
    if may_be_table
      tables << t unless t =~ /(^select|^\(.*)/
      may_be_table = false
    end
    if ['from', 'join'].include?(t)
       may_be_table = true
    end
  end
  tables
  # ParseError with sub-query without alias name
  #PgQuery.parse(no_parameter_query).tables.each do |t|
  #  yield t
  #end
end
relations() click to toggle source
# File lib/tableau_server_client/resources/datasource.rb, line 96
def relations
  xml.xpath('//relation')
end