#!/usr/bin/perl
#####################################################################
# Function : 同步QCS中的缺陷原与公司定义的缺陷原因一致
#
# Bug : 如果QCS中不存在 cf_bugwhen 表,将会同步的失败。
# 如果是报错信息为:"DBD::mysql::st execute failed: Unknown column 'cf_bugwhen' in 'field list' at xxx.pl line 50."
# 说明不存在 cf_bugwhen 表,证明你的QCS在初始化安装时 "Template数据导入" 没有成功.,请重新导入公司标准模板,详见
# "QCS 快速安装指南 .doc"的 "3.5 Template数据导入".
# 特请注意:如果重新导入模板将丢失所有已存在的数据!!!!
#
# Author : liulin
#
#
# Last Edit date : 2011年1月25日
#
# Change comment :
######################################################################
use DBI;
# =============== Connect to the database.===============
my $dbh = DBI->connect("DBI:mysql:database=bugs;host=localhost",
"bugs", "123123",
{'RaiseError' => 1});
# =============== test mysql conn===============
eval { $dbh->do("SET NAMES gbk;") };
print "Set chareset failed: $@/n" if $@;
# 初始 公司的标准 缺陷原因
my @data_array=("需求不完整、不清晰", "新增(变更)需求", "接口考虑不足", "与需求不一致",
"系统架构考虑不足", "语法、逻辑错误", "异常考虑不周", "接口不匹配", "界面展示问题",
"测试环境问题", "测试数据问题", "版本问题", "改进建议");
# =============== 查询数据总数,以便随机更新 ===============
my $sth = $dbh->prepare(' select count(*) count from bugs;');
$sth->execute();
my $ref = $sth->fetchrow_hashref();
my $count = $ref->{'count'};
# =============== 执行更新 bugs表的 'cf_bugwhen' 字段 ===============
if ( $count>0 ) { # 如果没有数据就不做更新
updateBugsTable_bugs();
}
# =============== 执行更新 bugs表的 'cf_bugwhen' 字段 ===============
updateBugsTable_cf_bugwhen();
##########################################################################
# function : 执行更新bugs表的 'cf_bugwhen' 字段 为rand公司定义的缺陷原因,
# 注:以前的数据就不存在了....请知晓!!!
# parameter:-
# return :-
#########################################################################
sub updateBugsTable_bugs
{
srand; #要先宣告srand函数,才能产生Perl随机数的效果
my $array_length = @data_array; # 取数组长度
for (my $i=1; $i<=$count; $i++ ) {
# 随机取缺陷原因,并更新已存在的bugs表中数据
my $sqlstm = ' update bugs set cf_bugwhen="'.@data_array[int(rand($array_length))-1].'" where bug_id='.$i;
#print $sqlstm."/n";
my $sth = $dbh->prepare( $sqlstm );
$sth->execute();
}
}
##########################################################################
# function : 执行更新'cf_bugwhen表的 'value' 字段 为公司定义的缺陷原因,
# 注:以前的数据将被覆盖....请知晓!!!
# parameter:-
# return :-
#########################################################################
sub updateBugsTable_cf_bugwhen
{
my $array_length = @data_array; # 取缺陷原因数组长度
# ============= 1. 删除表中多余数据;
my $sqlstm = 'delete from cf_bugwhen where id>'.($array_length+1);
my $sth = $dbh->prepare( $sqlstm );
$sth->execute();
# ============= 2. 更新缺陷原因 =========================
for (my $i=2; $i<=$array_length+1; $i++ ) { # 因为第一条缺陷原因为 '--'
my $sqlstm = ' update cf_bugwhen set value="'.@data_array[$i-2].'" where id='.$i;
my $sth = $dbh->prepare( $sqlstm );
$sth->execute();
}
}