使用Yii查询CodeStriker

公司使用的质量管理系统(评审活动、缺陷跟踪和文档库)中,需要与CodeStriker互动,开发如下类实现基本功能。

 

View Code
  1 <?php

  2 

  3 /*

  4  * 与codestriker的交互类,主要方法如下:

  5  * GetTopic(id):根据id查询CI的话题

  6  * 2013.1.27:

  7  * 目前hardcode了参数:codestriker链接、数据库服务器、端口、

  8  * 数据库用户名、密码,需要改为可配置参数。

  9  */

 10 

 11 /**

 12  * Description of CodeStriker

 13  *

 14  * @author Administrator

 15  */

 16 class CodeStriker {

 17     public static $url='http://xxxxxx/codestriker/codestriker.pl';

 18     private $mysqlInstance; // mysql 实例

 19     private $cfg=array(); // 配置信息

 20     private function __construct() {

 21         ;

 22     }

 23 

 24     /*

 25      * 查询一个topic的属性,返回数组,内容如下:

 26      * array(

 27      *      id: topic id

 28      *      author: 作者,email全写,假设与QMS一致

 29      *      title: 标题

 30      *      description:说明

 31      *      document: 

 32      *      state: 状态

 33      *      creation_ts: 创建时间

 34      *      modified_ts: 修改时间

 35      *      version: 版本

 36      *      start_tag:

 37      *      end_tag:

 38      *      module:

 39      *      repository:

 40      *      projectid:

 41      * )

 42      */

 43     static function GetTopic($id) {

 44         $param = array();

 45         $param['id'] = $id;

 46         $topiclist = self::Get('topic', $param);

 47         if($topiclist == null || count($topiclist) != 1)  {

 48             return null;

 49         }

 50         return $topiclist[0];

 51     }

 52     

 53     static function GetComment($id, $email=null) {

 54         $param = array();

 55         $param['id'] = $id;

 56         $param['author'] = $email;

 57 

 58         $commentlist = self::Get('commentnumber', $param);

 59         if($commentlist == null || count($commentlist) <= 0)  {

 60             return null;

 61         }

 62         return $commentlist;

 63     }

 64     

 65     /*

 66      * 根据命令查询codestrikerdb

 67      */

 68     public static function Get($cmd, $param=null) {

 69         $instance = new CodeStriker();

 70         

 71         if(! $instance->init()) {

 72             return false;

 73         }

 74 

 75         return $instance->handleRequest($cmd, $param);

 76     }

 77     

 78     private function handleRequest($cmd, $param) {

 79         $ret = null;

 80         switch($cmd) {

 81             case 'topic':

 82                 // 查询话题信息,参数$param为id

 83                 $sql = "select * from topic where id=".$param['id'];

 84                 $ret = $this->mysqlInstance->runQuery($sql);

 85                 break;

 86             case 'commentnumber':

 87                 $where = ' where st.id=data.commentstateid and m.id=st.id and '.

 88                     'st.topicid='.$param['id'];

 89                 if(isset($param['author']) && $param['author'] != null) {

 90                     $where .= ' and data.author="'.$param['author'].'"';

 91                 } 

 92                 $sql = 'select st.id as id, st.state as state, st.version as version,'.

 93                         'data.commentfield as comment, data.author as author, m.value as metric'.

 94                     ' from commentdata data,commentstate st, commentstatemetric m' . $where;

 95                 $ret = $this->mysqlInstance->runQuery($sql);

 96                 break;

 97             default:

 98                 break;

 99         }

100         return $ret;

101     }

102     

103     private function init() {

104         $this->mysqlInstance = MySQLInstance::instance();

105         $this->cfg = $this->SetCfg();

106         $ret = $this->mysqlInstance->init(

107                 $this->cfg['user'],

108                 $this->cfg['password'],

109                 $this->cfg['database'],

110                 $this->cfg['host']

111                 );

112         if($ret != 0) {

113             return $ret;

114         }

115         return true;

116     }

117 

118     public static function AjaxQuery($table, $uk, $query_field, $query_value, 

119             $initshow,

120             $selectedval = null, $selectmode = 1, $orderby = null, 

121             $init_condition = null, $output_fields = null, $displayfield = null, 

122             $number = 10, $page = 1) {

123 

124         $instance = new CodeStriker();

125         if(! $instance->init()) {

126             return null;

127         }

128 

129         if (empty($output_fields))

130             $output_fields = "*";

131 

132         $output = null;

133 

134         $condition = null;

135         if (isset($query_value) && !empty($query_value)) {

136             $field_array = explode(',', $query_field);

137             $condition = '(';

138             foreach ($field_array as $idx => $fieldname) {

139                 if ($idx > 0) {

140                     $condition .= ' or ';

141                 }

142                 $condition .= '(' . $fieldname . ' like "%' . $query_value . '%")';

143             }

144             $condition .= ')';

145         }

146         if (isset($init_condition) && !empty($init_condition)) {

147             if (strlen($condition) > 0) {

148                 $condition .= ' and ';

149             }

150             $condition .= ' (' . $init_condition . ') ';

151         }

152         if (!empty($selectedval)) {

153             $str = QmsHelper::convertStr2DoubleQuoted($selectedval);

154             if (!empty($condition)) {

155                 $condition .= " or ";

156             }

157             $condition .= "(" . $uk . ' in (' . $str . '))';

158         }

159 

160         if(empty($condition) && $initshow==0) {

161             // 如果关闭了默认不显示initshow,且没有查询条件,则不显示

162             $condition = "1=2"; 

163         }

164 

165         $sql = 'select count(*) as record_num from ' . $table;

166         if(!empty($condition)) {

167             $sql .= ' where ' . $condition;

168         }

169         $dataReader = $instance->mysqlInstance->runQuery($sql);

170         $total_number = 0;

171         foreach ($dataReader as $row) {

172             $total_number = $row['record_num'];

173         }

174 

175         if ($total_number <= 0) {

176             return "查询结果为空";

177         }

178         // 设置最小分页和最大分页

179         if ($number > 0) {

180             if ($number < 10)

181                 $number = 10;

182             else if ($number > 20)

183                 $number = 20;

184         }

185         else {

186             $number = 0;

187         }

188         $left = 0;

189         $pages = 0;

190         $limit_str = null;

191         if ($number > 0) {

192             if ($total_number / $number > self::MAX_RECORD_PER_PAGE) {

193                 return "查询结果超过10页,请重新查询";

194             }

195 

196             // 组装页码

197             $left = $total_number % $number;

198             $pages = round($total_number / $number);

199             $output .= '<div class="pagination">共' . $total_number . '条记录,分页<ul>';

200             for ($i = 1; $i <= $pages; $i++) {

201                 $output .= '<li><span class="page_number" value="' . $i . '">' . $i . '</li>';

202             }

203             $output .= '</ul></div>';

204 

205             if ($page <= 1)

206                 $page = 1;

207             else if ($page > self::MAX_PAGE_NUMBER)

208                 $page = self::MAX_PAGE_NUMBER;

209             $limit_str = ' limit ' . ( ($page - 1) * $number) . ',' . $number;

210         } // end of $number > 0

211         else {

212             $output .= '<span class="pagination">共' . $total_number . '条记录</span>';

213         }

214 

215         $sql = 'select * from ' . $table;

216         if(!empty($condition)) {

217             $sql .= ' where ' . $condition;

218         }

219         $orderby_sql = ' order by ' . $uk;

220         if (isset($orderby) && !empty($orderby)) {

221             $orderby_sql = ' order by ' . $orderby;

222         }

223         $sql .= $orderby_sql . $limit_str;

224 

225         $dataReader = $instance->mysqlInstance->runQuery($sql);

226         $fields = explode(',', $output_fields);

227         $output .= '<table class="query_result_table">';

228         foreach ($dataReader as $row) {

229             // 判断当前是否已经被选择

230             $flag = strpos($selectedval, $row[$uk]);

231             $checked = null;

232             if ($flag!==false) {

233                 $checked = "checked=true";

234             }

235             if ($selectmode == 2) {

236                 $select_field = '<input class="query_result_checkbox_button" ' . $checked .

237                         ' type="checkbox" name="checkboxgroup" value="' . $row[$uk] . '"';

238             } else if ($selectmode == 1) {

239                 $select_field = '<input class="query_result_radio_button" ' .

240                         'type="radio" name="radiogroup" value="' . $row[$uk] . '" ' .

241                         $checked;

242             }

243             $output .= '<tr>';

244             $output .= '<td>' .

245                     '<input id="display_' . $row[$uk] . '" type="hidden" value="' . $row[$displayfield] . '">' . // 显示字段

246                     $select_field .

247                     '</td>';

248             foreach ($fields as $key => $fieldname) {

249                 $fieldname = ltrim(rtrim($fieldname));

250                 $output .= '<td>' . $row[$fieldname] . '</td>';

251             }

252             $output .= '</tr>';

253         }

254         $output .= '</table>';

255 

256         $output .= '<input type="hidden" id="ajax_query_selected">';

257         return $output;

258     }

259     private function SetCfg() {

260         return array(

261             'user' => 'dbuser',

262             'password' => 'password',

263             'database' => 'codestrikerdb',

264             'host' => '1.2.3.4',

265             'port' => '3306',

266         );

267     }

268     

269     public static function GetTopicUrl($id) {

270         return self::$url . '?action=view&topic=' . $id . '&mode=1';

271     }

272 }

273 

274 ?>

 

你可能感兴趣的:(code)