[Title] add db_utils.rb
authorjiil.hyoun <jiil.hyoun@samsung.com>
Tue, 5 Mar 2013 07:07:50 +0000 (16:07 +0900)
committerjiil.hyoun <jiil.hyoun@samsung.com>
Tue, 5 Mar 2013 07:07:50 +0000 (16:07 +0900)
[Type] Enhancement
[Module] Toolchain /
[Priority] Minor
[Jira#]
[Redmine#]
[Problem]
[Cause]
[Solution]
[TestCase]

Change-Id: I3f2870e1749f15b9e02e942bbd0e7b9e258bdcc4

src/build_server/BuildServer.rb
src/common/db_utils.rb [new file with mode: 0644]

index 1b3cbf4724287146c5944a5fc1204c731d3b4b37..eae94f29e20e37c4010c768e2c63282baaf46e7e 100644 (file)
@@ -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 (file)
index 0000000..159b95f
--- /dev/null
@@ -0,0 +1,62 @@
+=begin
+
+ db_utils.rb
+
+Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+
+Contact:
+Taejun Ha <taejun.ha@samsung.com>
+Jiil Hyoun <jiil.hyoun@samsung.com>
+Donghyuk Yang <donghyuk.yang@samsung.com>
+DongHee Yang <donghee.yang@samsung.com>
+
+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