Module: DohDb

Defined in:
lib/dohmysql/error.rb,
lib/dohmysql/handle.rb,
lib/dohmysql/logger.rb,
lib/dohmysql/db_date.rb,
lib/dohmysql/hash_row.rb,
lib/dohmysql/load_sql.rb,
lib/dohmysql/file_util.rb,
lib/dohmysql/smart_row.rb,
lib/dohmysql/abstract_row.rb,
lib/dohmysql/current_date.rb,
lib/dohmysql/readonly_row.rb,
lib/dohmysql/writable_row.rb,
lib/dohmysql/metadata_util.rb,
lib/dohmysql/connector_util.rb,
lib/dohmysql/migrate/runner.rb,
lib/dohmysql/cache_connector.rb,
lib/dohmysql/migrate/options.rb,
lib/dohmysql/raw_row_builder.rb,
lib/dohmysql/database_creator.rb,
lib/dohmysql/migrate/analyzer.rb,
lib/dohmysql/typed_row_builder.rb,
lib/dohmysql/connector_instance.rb

Defined Under Namespace

Modules: Migrate Classes: AbstractRow, AbstractSmartRow, CacheConnector, CannotBeNull, DatabaseCreator, Handle, HashRow, MigrateAnalyzer, MigrateRunner, RawRowBuilder, ReadOnlyRow, SmartRow, StubLogger, TypedRowBuilder, UnexpectedQueryResult, UnknownColumn, WritableRow

Constant Summary collapse

@@cached_column_info =
{}
@@tables_by_database =
{}

Class Method Summary collapse

Class Method Details

.all_tables(database = nil) ⇒ Object



41
42
43
44
# File 'lib/dohmysql/metadata_util.rb', line 41

def self.all_tables(database = nil)
  database ||= DohDb.connector_instance.config[:database]
  @@tables_by_database[database] ||= Doh.db.select_list("SELECT table_name FROM information_schema.tables WHERE table_schema = '#{database}'")
end

.chop_character_fields(table, row) ⇒ Object



28
29
30
# File 'lib/dohmysql/metadata_util.rb', line 28

def self.chop_character_fields(table, row)
  chop_character_fields!(table, row.dup)
end

.chop_character_fields!(table, row) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/dohmysql/metadata_util.rb', line 18

def self.chop_character_fields!(table, row)
  column_info(table).each do |field, attribs|
    maxlen = attribs['character_maximum_length']
    if maxlen && row[field].to_s.size > maxlen
      row[field] = row[field].to_s[0, maxlen]
    end
  end
  row
end

.column_info(table, database = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/dohmysql/metadata_util.rb', line 6

def self.column_info(table, database = nil)
  database ||= DohDb.connector_instance.config[:database]
  lookup_str = database + '.' + table
  return @@cached_column_info[lookup_str] if @@cached_column_info[lookup_str]
  stmt = "SELECT column_name, is_nullable, data_type, character_maximum_length, numeric_scale, column_type FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='#{database}' AND TABLE_NAME='#{table}'"
  @@cached_column_info[lookup_str] = Doh.db.select_transpose(stmt)
end

.connector_instanceObject



7
8
9
# File 'lib/dohmysql/connector_instance.rb', line 7

def self.connector_instance
  @@connector_instance
end

.create_and_connect(connector, new_default_database = nil, drop_first = true) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/dohmysql/connector_util.rb', line 3

def self.create_and_connect(connector, new_default_database = nil, drop_first = true)
  connector.reset
  connector.database = new_default_database if new_default_database
  dbh = connector.request_handle('')
  dbh.query("DROP DATABASE IF EXISTS #{connector.database}") if drop_first
  dbh.query("CREATE DATABASE IF NOT EXISTS #{connector.database}")
  dbh.query("USE #{connector.database}")
  dbh
end

.current_dateObject



6
7
8
# File 'lib/dohmysql/current_date.rb', line 6

def self.current_date
  Doh.current_date(DohDb.today)
end

.current_datetimeObject



10
11
12
# File 'lib/dohmysql/current_date.rb', line 10

def self.current_datetime
  Doh.current_datetime(DohDb.now)
end

.drop_create_and_connect(connector, new_default_database = nil) ⇒ Object



13
14
15
# File 'lib/dohmysql/connector_util.rb', line 13

def self.drop_create_and_connect(connector, new_default_database = nil)
  create_and_connect(connector, new_default_database, true)
end

.field_character_size(table, field, database = nil) ⇒ Object



14
15
16
# File 'lib/dohmysql/metadata_util.rb', line 14

def self.field_character_size(table, field, database = nil)
  column_info(table, database).fetch(field, {}).fetch('character_maximum_length')
end

.field_exist?(table, field, database = nil) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dohmysql/metadata_util.rb', line 32

def self.field_exist?(table, field, database = nil)
  column_info(table, database).key?(field)
end

.field_list(table, database = nil) ⇒ Object



36
37
38
# File 'lib/dohmysql/metadata_util.rb', line 36

def self.field_list(table, database = nil)
  column_info(table, database).keys
end

.load_sql(dbconfig, filenames) ⇒ Object

pass through so we can change implemenation easily



32
33
34
35
36
37
38
# File 'lib/dohmysql/load_sql.rb', line 32

def self.load_sql(dbconfig, filenames)
  if RUBY_ENGINE == "jruby"
    DohDb.load_sql_using_each_backtick(dbconfig, filenames)
  else
    DohDb.load_sql_using_one_popen(dbconfig, filenames)
  end
end

.load_sql_using_each_backtick(dbconfig, filenames) ⇒ Object

this works on JRuby, but is slower than popen



22
23
24
25
26
27
28
29
# File 'lib/dohmysql/load_sql.rb', line 22

def self.load_sql_using_each_backtick(dbconfig, filenames)
  dohlog.debug("loading sql file: " + filenames.first) if filenames.size == 1

  basecmd = 'mysql' + mysql_arg(dbconfig[:host], 'h') + mysql_arg(dbconfig[:username], 'u') + mysql_arg(dbconfig[:password], 'p') + ' ' + dbconfig[:database] + ' < '
  filenames.each do |elem|
    `#{basecmd} #{elem}`
  end
end

.load_sql_using_each_open3(dbconfig, filenames) ⇒ Object

trying Open3 now for better error handling; unknown as of yet if this works under JRuby



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dohmysql/load_sql.rb', line 41

def self.load_sql_using_each_open3(dbconfig, filenames)
  dohlog.debug("loading sql file: " + filenames.first) if filenames.size == 1

  basecmd = 'mysql' + mysql_arg(dbconfig[:host], 'h') + mysql_arg(dbconfig[:username], 'u') + mysql_arg(dbconfig[:password], 'p') + ' ' + dbconfig[:database] + ' < '
  filenames.each do |elem|
    mysqlcmd = "#{basecmd} #{elem}"
    stdin, stdout, stderr = Open3.popen3(mysqlcmd)
    stdoutstr = stdout.read
    stdout.close
    stderrstr = stderr.read
    raise "#{stderrstr} occured while loading file: #{elem}" if !stderrstr.empty?
  end
end

.load_sql_using_one_popen(dbconfig, filenames) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/dohmysql/load_sql.rb', line 10

def self.load_sql_using_one_popen(dbconfig, filenames)
  dohlog.debug("loading sql file: " + filenames.first) if filenames.size == 1

  mysqlcmd = 'mysql' + mysql_arg(dbconfig[:host], 'h') + mysql_arg(dbconfig[:username], 'u') + mysql_arg(dbconfig[:password], 'p') + ' ' + dbconfig[:database]
  io = IO.popen(mysqlcmd, 'r+')
  filenames.each do |elem|
    open(elem) {|file| io << file.read}
  end
  io.close
end

.loggerObject



8
9
10
# File 'lib/dohmysql/logger.rb', line 8

def self.logger
  @logger
end

.mysql_arg(value, option_specifier) ⇒ Object



6
7
8
# File 'lib/dohmysql/load_sql.rb', line 6

def self.mysql_arg(value, option_specifier)
  if value.to_s.strip.empty? then '' else " -#{option_specifier}#{value}" end
end

.nowObject



13
14
15
16
17
18
19
# File 'lib/dohmysql/db_date.rb', line 13

def self.now
  retval = DateTime.zow
  def retval.to_sql
    'NOW()'
  end
  retval
end

.reconfigure_connector(cfg, connector = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/dohmysql/connector_util.rb', line 17

def self.reconfigure_connector(cfg, connector = nil)
  connector ||= DohDb.connector_instance
  connector.reset
  connector.host = cfg['host'] if cfg.key?('host')
  connector.username = cfg['username'] if cfg.key?('username')
  connector.password = cfg['password'] if cfg.key?('password')
  connector.database = cfg['database'] if cfg.key?('database')
  connector.port = cfg['port'] if cfg.key?('port')
end

.set_connector_instance(conn) ⇒ Object



3
4
5
# File 'lib/dohmysql/connector_instance.rb', line 3

def self.set_connector_instance(conn)
  @@connector_instance = conn
end

.set_logger(logger) ⇒ Object



12
13
14
# File 'lib/dohmysql/logger.rb', line 12

def self.set_logger(logger)
  @logger = logger
end

.sql_files_path(database = nil) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/dohmysql/file_util.rb', line 3

def self.sql_files_path(database = nil)
  if database
    File.join(Doh.root, "data/mysql/#{database}")
  else
    File.join(Doh.root, 'data/mysql')
  end
end

.todayObject



5
6
7
8
9
10
11
# File 'lib/dohmysql/db_date.rb', line 5

def self.today
  retval = Date.today
  def retval.to_sql
    'CURDATE()'
  end
  retval
end