Sha256: d377f171f6a3a3f908e4da4427bb0cb6f9d0d1c8cc68bec40494de2879d1952f

Contents?: true

Size: 922 Bytes

Versions: 1

Compression:

Stored size: 922 Bytes

Contents

module Mysql2xxxx
  class XML
    include ExtraOutputs
    
    attr_reader :properties
    
    def initialize(options = {})
      @properties = Properties.new options
    end
    
    # this matches the xml generated by "mysql --xml"
    # i tried to use builder, but the String#to_xs nonsense got in the way
    def to_file(f)
      @client = ::Mysql2::Client.new properties.database_config
      f.write %{<?xml version="1.0" encoding="utf-8" ?>}
      f.write %{<resultset statement="#{properties.execute.to_xs}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">}
      @client.query(properties.execute).each do |hsh|
        f.write %{<row>}
        hsh.each do |k, v|
          f.write %{<field name="#{k.to_xs}"#{' xsi:nil="true"' if v.nil?}>#{v.to_s.to_xs}</field>}
        end
        f.write %{</row>}
      end
      f.write %{</resultset>}
      nil
    ensure
      @client.try :close
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mysql2xxxx-0.0.3 lib/mysql2xxxx/xml.rb