Skip to content

Commit

Permalink
stop using autoload and string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
seamusabshere committed Jan 16, 2012
1 parent a752010 commit effd915
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions Rakefile
@@ -1,5 +1,6 @@
require 'bundler'
Bundler::GemHelper.install_tasks
require 'bundler/setup'

require 'rake'
require 'rake/testtask'
Expand Down
1 change: 1 addition & 0 deletions bin/mysql2csv
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby

require "mysql2xxxx"
require 'mysql2xxxx/cli'

cli = Mysql2xxxx::Cli.new
cli.parse_options
Expand Down
1 change: 1 addition & 0 deletions bin/mysql2json
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby

require "mysql2xxxx"
require 'mysql2xxxx/cli'

cli = Mysql2xxxx::Cli.new
cli.parse_options
Expand Down
1 change: 1 addition & 0 deletions bin/mysql2xml
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby

require "mysql2xxxx"
require 'mysql2xxxx/cli'

cli = Mysql2xxxx::Cli.new
cli.parse_options
Expand Down
17 changes: 10 additions & 7 deletions lib/mysql2xxxx.rb
Expand Up @@ -9,14 +9,17 @@
active_support/core_ext/hash
}.each do |active_support_3_requirement|
require active_support_3_requirement
end if ::ActiveSupport::VERSION::MAJOR == 3
end if ::ActiveSupport::VERSION::MAJOR >= 3

# will use mysql2 as soon as :stream => true is supported
# https://github.com/brianmario/mysql2/pull/223
require 'mysql'

require 'mysql2xxxx/config'
require 'mysql2xxxx/writer'
require 'mysql2xxxx/writer/json'
require 'mysql2xxxx/writer/csv'
require 'mysql2xxxx/writer/xml'

module Mysql2xxxx
autoload :JSON, 'mysql2xxxx/writer/json'
autoload :CSV, 'mysql2xxxx/writer/csv'
autoload :XML, 'mysql2xxxx/writer/xml'
autoload :Writer, 'mysql2xxxx/writer'
autoload :Properties, 'mysql2xxxx/properties'
autoload :Cli, 'mysql2xxxx/cli'
end
23 changes: 11 additions & 12 deletions lib/mysql2xxxx/properties.rb → lib/mysql2xxxx/config.rb
@@ -1,21 +1,20 @@
module Mysql2xxxx
class Properties
class Config
attr_reader :options
def initialize(options = {})
@options = options.dup
@options.stringify_keys!
@options = options.symbolize_keys
end

def user
options['user'] || active_record_config.try(:[], :username)
options[:user] || active_record_config.try(:[], :username)
end

def password
options['password'] || active_record_config.try(:[], :password)
options[:password] || active_record_config.try(:[], :password)
end

def host
options['host'] || active_record_config.try(:[], :host)
options[:host] || active_record_config.try(:[], :host)
end

# MySQL connection charset
Expand All @@ -24,7 +23,7 @@ def host
#
# Default: utf8
def charset
options['charset'] || 'utf8'
options[:charset] || 'utf8'
end

# Encoding
Expand All @@ -33,23 +32,23 @@ def charset
#
# Default: UTF-8
def encoding
options['encoding'] || 'UTF-8'
options[:encoding] || 'UTF-8'
end

def port
options['port'] || active_record_config.try(:[], :port)
options[:port] || active_record_config.try(:[], :port)
end

def socket
options['socket'] || active_record_config.try(:[], :socket)
options[:socket] || active_record_config.try(:[], :socket)
end

def database
options['database'] || active_record_connection.try(:current_database)
options[:database] || active_record_connection.try(:current_database)
end

def execute
options['execute']
options[:execute]
end

private
Expand Down
14 changes: 7 additions & 7 deletions lib/mysql2xxxx/writer.rb
Expand Up @@ -7,10 +7,10 @@

module Mysql2xxxx
class Writer
attr_reader :properties
attr_reader :config

def initialize(options = {})
@properties = Properties.new options
@config = Config.new options
end

def keys
Expand All @@ -19,7 +19,7 @@ def keys

def last_statement
return @last_statement if @last_statement.is_a? ::String
statements = properties.execute.split(';').select { |statement| statement.to_s.strip.length > 0 }
statements = config.execute.split(';').select { |statement| statement.to_s.strip.length > 0 }
@last_statement = statements.pop
statements.each do |statement|
dbh.query statement
Expand All @@ -37,8 +37,8 @@ def result
def dbh
return @dbh if @dbh.is_a? ::Mysql
@dbh = ::Mysql.init
@dbh.options ::Mysql::SET_CHARSET_NAME, properties.charset
@dbh.real_connect properties.host, properties.user, properties.password, properties.database, properties.port, properties.socket
@dbh.options ::Mysql::SET_CHARSET_NAME, config.charset
@dbh.real_connect config.host, config.user, config.password, config.database, config.port, config.socket
# so that we can use_result instead of store_result
@dbh.query_with_result = false
@dbh
Expand All @@ -64,12 +64,12 @@ def recode_as_utf8(raw_str)
return if raw_str.nil?
if ::RUBY_VERSION >= '1.9'
$stderr.puts "[mysql2xxxx] Raw - #{raw_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
recoded_str = raw_str.ensure_encoding 'UTF-8', :external_encoding => properties.encoding, :invalid_characters => :transcode
recoded_str = raw_str.ensure_encoding 'UTF-8', :external_encoding => config.encoding, :invalid_characters => :transcode
$stderr.puts "[mysql2xxxx] Recoded - #{recoded_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
recoded_str
else
$stderr.puts "[mysql2xxxx] Raw - #{raw_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
recoded_str = ::Iconv.conv('UTF-8//TRANSLIT', properties.encoding, raw_str.to_s + ' ')[0..-2]
recoded_str = ::Iconv.conv('UTF-8//TRANSLIT', config.encoding, raw_str.to_s + ' ')[0..-2]
$stderr.puts "[mysql2xxxx] Recoded - #{recoded_str}" if ::ENV['MYSQL2XXXX_DEBUG'] == 'true'
recoded_str
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mysql2xxxx/writer/xml.rb
Expand Up @@ -9,7 +9,7 @@ def escaped_keys
# i tried to use builder, but the String#to_xs nonsense got in the way
def to_file(f)
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">}
f.write %{<resultset statement="#{config.execute.to_xs}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">}
stream_arrays do |ary|
f.write %{<row>}
ary.each_with_index do |v, i|
Expand Down
1 change: 1 addition & 0 deletions mysql2xxxx.gemspec
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |s|
s.add_dependency 'fastercsv'
s.add_dependency 'ensure-encoding'

s.add_development_dependency 'rake'
s.add_development_dependency 'activerecord'
s.add_development_dependency 'shell-executer'
s.add_development_dependency 'posix-spawn'
Expand Down

0 comments on commit effd915

Please sign in to comment.