From: jiil.hyoun Date: Tue, 5 Mar 2013 07:07:50 +0000 (+0900) Subject: [Title] add db_utils.rb X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0e9c170d746039a60424d6e81fe0eddce10b0c64;p=sdk%2Ftools%2Fsdk-build.git [Title] add db_utils.rb [Type] Enhancement [Module] Toolchain / [Priority] Minor [Jira#] [Redmine#] [Problem] [Cause] [Solution] [TestCase] Change-Id: I3f2870e1749f15b9e02e942bbd0e7b9e258bdcc4 --- diff --git a/src/build_server/BuildServer.rb b/src/build_server/BuildServer.rb index 1b3cbf4..eae94f2 100644 --- a/src/build_server/BuildServer.rb +++ b/src/build_server/BuildServer.rb @@ -31,6 +31,7 @@ require 'fileutils' require 'dbi' require 'thread' $LOAD_PATH.unshift File.dirname(__FILE__) +$LOAD_PATH.unshift File.dirname(File.dirname(__FILE__))+"/common" require "SocketJobRequestListener.rb" require "JobManager.rb" require "JobClean.rb" @@ -38,6 +39,7 @@ require "RemoteBuildServer.rb" require "PackageSync.rb" require "ProjectManager.rb" require "DistributionManager.rb" +require "db_utils.rb" class BuildServer attr_accessor :id, :path, :status, :host_os, :log @@ -557,52 +559,26 @@ class BuildServer end def gen_db() - case @db_dsn - when /^SQLite3:/ then puts "SQLite3 DB#{@db_dsn.split(':')[1]} generating" - when /^Mysql:/ then - name = nil - host = nil - port = nil - socket = nil - flag = nil - dsn = @db_dsn.split(':') - if dsn[2].nil? then - dsn[1].split(';').each do |attr| - case attr.split('=')[0].strip - when /database/i then - name = attr.split('=')[1].strip - when /host/i then - host = attr.split('=')[1].strip - when /port/i then - port = attr.split('=')[1].strip - when /socket/i then - socket = attr.split('=')[1].strip - when /flag/i then - flag = attr.split('=')[1].strip - else - etc = attr.split('=')[1].strip - end - end - else - name = dsn[1].strip - host = dsn[2].strip - end + hash = DBUtils.dsn_parser @db_dsn + case hash[:database] + when "SQLite3" then puts "SQLite3 DB#{@db_dsn.split(':')[1]} generating" + when "Mysql" then File.open("create_db.txt","w") do |f| - f.puts "GRANT ALL ON #{name}.* TO '#{@db_user}'@'%' IDENTIFIED BY '#{@db_passwd}';" - f.puts "CREATE DATABASE #{name};" + f.puts "GRANT ALL ON #{hash[:name]}.* TO '#{@db_user}'@'%' IDENTIFIED BY '#{@db_passwd}';" + f.puts "CREATE DATABASE #{hash[:name]};" end - if host.eql? "localhost" or host.eql? "127.0.0.1" then + if hash[:host].eql? "localhost" or hash[:host].eql? "127.0.0.1" then socket_str = "" - socket_str = "--socket=#{socket}" if not socket.nil? - puts "Mysql DB #{name} generating" - system("mysql -h #{host} #{socket_str} -u #{@db_user} --password=#{@db_passwd} < create_db.txt") + socket_str = "--socket=#{hash[:socket]}" if not hash[:socket].nil? + puts "Mysql DB #{hash[:name]} generating" + system("mysql -h #{hash[:host]} #{socket_str} -u #{@db_user} --password=#{@db_passwd} < create_db.txt") else port_str = "" - port_str = "-P #{port}" if not port.nil? - puts "Mysql DB #{name} generating" - system("mysql -h #{host} #{port_str} -u #{@db_user} --password=#{@db_passwd} < create_db.txt") + port_str = "-P #{hash[:port]}" if not port.nil? + puts "Mysql DB #{hash[:name]} generating" + system("mysql -h #{hash[:host]} #{port_str} -u #{@db_user} --password=#{@db_passwd} < create_db.txt") end else puts "not support DB #{@db_dsn}" end diff --git a/src/common/db_utils.rb b/src/common/db_utils.rb new file mode 100644 index 0000000..159b95f --- /dev/null +++ b/src/common/db_utils.rb @@ -0,0 +1,62 @@ +=begin + + db_utils.rb + +Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + +Contact: +Taejun Ha +Jiil Hyoun +Donghyuk Yang +DongHee Yang + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +Contributors: +- S-Core Co., Ltd +=end + +class DBUtils + def DBUtils.dsn_parser (db_dsn) + dsn_hash={} + case db_dsn + when /^SQLite3:/ then puts "SQLite3 DB#{db_dsn.split(':')[1]} generating" + dsn_hash[:database] = "SQLite3" + when /^Mysql:/ then + dsn_hash[:database] = "Mysql" + dsn = db_dsn.split(':') + if dsn[2].nil? then + dsn[1].split(';').each do |attr| + case attr.split('=')[0].strip + when /database/i then + dsn_hash[:name] = attr.split('=')[1].strip + when /host/i then + dsn_hash[:host] = attr.split('=')[1].strip + when /port/i then + dsn_hash[:port] = attr.split('=')[1].strip + when /socket/i then + dsn_hash[:socket] = attr.split('=')[1].strip + when /flag/i then + dsn_hash[:flag] = attr.split('=')[1].strip + else + dsn_hash[:etc] = attr.split('=')[1].strip + end + end + else + dsn_hash[:name] = dsn[1].strip + dsn_hash[:host] = dsn[2].strip + end + end + return dsn_hash + end +end