Module: DBSetup

Included in:
ManqodServer
Defined in:
lib/DBSetup.rb

Overview

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint([email protected])

Instance Method Summary collapse

Instance Method Details

#check_sql_db(conn_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/DBSetup.rb', line 46

def check_sql_db(conn_name)
  begin
    conn=@conns[conn_name]
    require 'DrbDB/MyMultiSQL/mysql-ruby.rb'
    sql=MySQL.new(conn['sql_host'],conn['sql_user'],conn['sql_password'],conn['sql_db'])
    sql.close
  rescue => err
    eerror("#{err} #{err.backtrace.join("\n\t")}")
    raise "#{err}"
  end
end

#create_conn(conn_name) ⇒ Object



28
29
30
31
# File 'lib/DBSetup.rb', line 28

def create_conn(conn_name)
  @conns[conn_name]=Hash.new
  save_conns
end

#create_sql_db_and_perms(conn_name, su, sup) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/DBSetup.rb', line 58

def create_sql_db_and_perms(conn_name,su,sup)
  begin
    conn=@conns[conn_name]
    require 'DrbDB/MyMultiSQL/mysql-ruby.rb'
    sql=MySQL.new(conn['sql_host'],su,sup,nil)
    sql.query("drop database if exists `#{conn['sql_db']}`")
    sql.query("create database `#{conn['sql_db']}`")
    begin
      sql.query("create user '#{conn['sql_user']}' identified by '#{conn['sql_password']}'")
    rescue =>e
      einfo("failed to create user '#{conn['sql_user']}':#{e}")
    end
    sql.query("grant all privileges on #{conn['sql_db']}.* to '#{conn['sql_user']}'")
    sql.query("flush privileges")
#     conn['sql_user'],conn['sql_password'],conn['sql_db'])
    sql.close
  rescue => err
    eerror("#{err} #{err.backtrace.join("\n\t")}")
    raise "#{err}"
  end
end

#load_connsObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/DBSetup.rb', line 6

def load_conns
  einfo("loading server connections")
  begin
    f=File.new(@connections_conf)
    @conns=Marshal.load(f.read)
    f.close
  rescue =>err
    ewarn("can't load config: #{@connections_conf}\n#{err}")
  end
end

#populate_manqod_db(conn_name, structure_only = false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/DBSetup.rb', line 80

def populate_manqod_db(conn_name,structure_only=false)
  begin
    conn=@conns[conn_name]
    require 'DrbDB/MyMultiSQL/mysql-ruby.rb'
    sql=MySQL.new(conn['sql_host'],conn['sql_user'],conn['sql_password'],conn['sql_db'])
    filename=structure_only ? "manqod_structure.sql" : "manqod.sql"
    file=File.open(File::expand_path(File.join(File.join(@path,"doc"),filename)),"r:binary")
    while q=file.gets(";\n")
      qq=q.gsub("\n","")
      sql.query(qq) if q.length>5
    end
    file.close
    sql.close
  rescue =>err
    eerror("#{err} #{err.backtrace.join("\n\t")}")
    raise "#{err}"
  end
end

#remove_conn(conn_name) ⇒ Object



33
34
35
36
37
# File 'lib/DBSetup.rb', line 33

def remove_conn(conn_name)
  free_conn(conn_name)
  @conns.delete(conn_name)
  save_conns
end

#save_connsObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/DBSetup.rb', line 16

def save_conns
  einfo("saving server connections")
  begin
    f=File.new(@connections_conf,"w+")
    f.write(Marshal.dump(@conns))
  rescue =>err
    ewarn("can't write config: #{@connections_conf}\n{err}")
  ensure
    f.close
  end
end

#set_conn_variables(conn_name, conn_vars) ⇒ Object



39
40
41
42
43
44
# File 'lib/DBSetup.rb', line 39

def set_conn_variables(conn_name,conn_vars)
  conn_vars.each_pair{|var,val|
    @conns[conn_name][var]=val
  }
  save_conns
end