Class: OMF::OML::Sequel::Server::Query
- Inherits:
-
Base::LObject
- Object
- Base::LObject
- OMF::OML::Sequel::Server::Query
- Defined in:
- lib/omf_oml/sequel/sequel_server.rb
Class Method Summary collapse
Instance Method Summary collapse
- #convert_to_cols(args) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(queryEl, repoFactory, logger) ⇒ Query
constructor
A new instance of Query.
-
#parse_arg(pel) ⇒ Object
Return the arguments defined in @parentEl as array.
- #parse_arg_primitive(pel, value) ⇒ Object
-
#parse_args(parentEl) ⇒ Object
Return the arguments defined in @parentEl as array.
-
#parse_column(el) ⇒ Object
<col name=‘oml_sender_id’ table=‘iperf_TCP_Info’/>.
- #parse_el(el, lastRel) ⇒ Object
- #parse_repository(el) ⇒ Object
- #parse_table(el) ⇒ Object
- #relation ⇒ Object
-
#rformat ⇒ Object
Requested format for result.
Constructor Details
#initialize(queryEl, repoFactory, logger) ⇒ Query
Returns a new instance of Query.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 39 def initialize(queryEl, repoFactory, logger) @queryEl = queryEl @repoFactory = repoFactory || RepositoryFactory.new @logger = logger || Logger.new(STDOUT) @tables = {} @lastRel = nil @offset = 0 @limit = 0 queryEl.children.each do |el| @lastRel = parse_el(el, @lastRel) end if @limit > 0 @lastRel = @lastRel.limit(@limit, @offset) end end |
Class Method Details
.parse(xmls, repoFactory = RepositoryFactory.new, logger = Logger.new(STDOUT)) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 25 def self.parse(xmls, repoFactory = RepositoryFactory.new, logger = Logger.new(STDOUT)) if xmls.kind_of? String doc = REXML::Document.new(xmls) root = doc.root else root = xmls end unless root.name == 'query' raise "XML fragment needs to start with 'query' but does start with '#{root.name}" end q = self.new(root, repoFactory, logger) q.relation end |
Instance Method Details
#convert_to_cols(args) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 185 def convert_to_cols(args) args.collect do |arg| if arg.kind_of? String table = @repository.get_first_table() raise "Unknown table for column '#{arg}'" unless table table[arg] else arg end end end |
#each(&block) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 55 def each(&block) # sel_mgr = relation # unless sel_mgr.kind_of? SelectionManager # raise "Can only be called on SELECT statement" # end # puts sel_mgr.engine relation.each(&block) end |
#parse_arg(pel) ⇒ Object
Return the arguments defined in @parentEl as array
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 137 def parse_arg(pel) res = nil #col = nil pel.children.each do |el| if (el.kind_of? REXML::Text) val = el.value next if val.strip.empty? # skip text between els return parse_arg_primitive(pel, val) else name = el.name.downcase case name when /col/ res = parse_column(el) when /eq/ if res.nil? raise "Missing 'col' definiton before 'eq'." end p = parse_args(el) unless p.length == 1 raise "'eq' can only hnadle 1 argument, but is '#{p.inspect}'" end res = {res => p[0]} else raise "Need to be 'col' declaration, but is '#{name}'" end end end res end |
#parse_arg_primitive(pel, value) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 167 def parse_arg_primitive(pel, value) type = pel.attributes['type'] || 'string' case type when /string/ value when /boolean/ value.downcase == 'true' || value == '1' when /decimal/ value.to_i when /double/ value.to_f when /dateTime/ Time.xmlschema else raise "Unknown arg type '#{type}" end end |
#parse_args(parentEl) ⇒ Object
Return the arguments defined in @parentEl as array
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 124 def parse_args(parentEl) args = [] parentEl.children.each do |el| next if (el.kind_of? REXML::Text) unless el.name == 'arg' raise "Expected argument definition but got element '#{el.name}" end args << parse_arg(el) end args end |
#parse_column(el) ⇒ Object
<col name=‘oml_sender_id’ table=‘iperf_TCP_Info’/>
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 198 def parse_column(el) unless colName = el.attributes['name'] raise "Missing 'name' attribute for 'col' element" end col = colName unless tblName = el.attributes['table'] raise "Missing 'table' attribute for col '#{colName}'" end unless @tables.member?(tblName.to_sym) raise "Unknown table name '#{tblName}' (#{el})" end col = "#{tblName}__#{colName}" if colAlias = el.attributes['alias'] col = "#{col}___#{colAlias}" end col.to_sym end |
#parse_el(el, lastRel) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 74 def parse_el(el, lastRel) if (el.kind_of? REXML::Text) # skip return lastRel end args = parse_args(el) @logger.debug "CHILD #{el.name}" # keep the last table for this level to be used # to create proper columns. # NOTE: This is not fool-proof but we need columns # to later resolve the column type. # name = el.name.downcase if lastRel.nil? case name when /repository/ lastRel = repo = parse_repository(el) @tables = repo.tables @logger.debug "Created repository: #{lastRel}" else raise "Need to start with 'table' declaration, but does with '#{name}'" end elsif name == 'table' lastRel = parse_table(el) elsif name == 'project' # turn all arguments into proper columns # cols = convert_to_cols(args) lastRel = lastRel.select(*args) # elsif lastRel.kind_of?(::Arel::Table) && name == 'as' # # keep track of all created tables # lastRel = lastRel.alias(*args) # @repository.add_table(args[0], lastRel) elsif name == 'skip' @offset = args[0].to_i elsif name == 'take' @limit = args[0].to_i else @logger.debug "Sending '#{name}' to #{lastRel.class}" lastRel = lastRel.send(name, *args) end @logger.debug "lastRel for <#{el}> is #{lastRel.class}" lastRel end |
#parse_repository(el) ⇒ Object
118 119 120 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 118 def parse_repository(el) @repository = @repoFactory.create_from_xml(el, @logger) end |
#parse_table(el) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 217 def parse_table(el) unless name = el.attributes['tname'] raise "Missing 'tname' attribute for 'table' element" end if talias = el.attributes['talias'] name = "#{name}___#{talias}" @tables << talias.to_sym end @repository[name.to_sym] end |
#relation ⇒ Object
64 65 66 67 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 64 def relation raise "No query defined, yet" unless @lastRel @lastRel end |
#rformat ⇒ Object
Requested format for result. Default is ‘xml’
70 71 72 |
# File 'lib/omf_oml/sequel/sequel_server.rb', line 70 def rformat @queryEl.attributes['rformat'] || 'xml' end |