dbwrite.php ➔ DbWrite()   C
last analyzed

Complexity

Conditions 13
Paths 82

Size

Total Lines 98

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 82
nop 4
dl 0
loc 98
rs 5.3369
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package		fwolflib
4
 * @copyright	Copyright 2006, Fwolf
5
 * @author		Fwolf, [email protected]
6
 * @since		2006-07-08
7
 * @version		$Id$
8
 */
9
10
11
require_once(dirname(__FILE__) . '/../fwolflib.php');
12
13
14
/**
15
 * 向数据库中写入数据
16
 * 根据指定的主键或联合主键自动判断是insert还是update
17
 * 当然主键必须有值包含在数据中
18
 *
19
 * @param	object	$db		ADOdb数据库连接对象
20
 * @param	string	$tbl	写入数据表名
21
 * @param	array	$data	要写入的数据,数组array(column=>value,...)形式
22
 * @param	mixed	$pkey	主键,可以是字符串或数组(也可以是定位数据的其他条件,但要求键和值必须包含在data数组中
23
 * @return	mixed			出错返回错误信息,成功返回1(即处理的行数)
24
 */
25
function DbWrite($db, $tbl, $data, $pkey)
26
{
27
	//对要写入数据库的内容进行处理,主要是根据字段类型添加引号
28
	//:TODO:改用缓存机制
29
	$col = $db->MetaColumns($tbl);
30
	foreach ($data as $key => $val)
31
	{
32
		if (isset($col[strtoupper($key)]))
33
		{
34
			//根据字段类型添加引号
35
			if (!in_array($col[strtoupper($key)]->type, array('int', 'integer', 'tinyint', 'decimal', 'bolean', 'numeric')))
36
				$data[$key] = "'" . addslashes($val) . "'";
37
		}
38
		else
39
		{
40
			return("Column $key is not found in db schema.");
41
		}
42
	}
43
	//检查要写入的数据是否存在 insert or update?
44
	$sql = "select count(1) as c from $tbl where 1=1 ";
45
	if (is_array($pkey))
46
	{
47
		//多个值的定位
48
		//$s_pkey will be used again when actually write to db
49
		$s_pkey = '';
50
		foreach ($pkey as $key)
51
		{
52
			//检查键值是否被指定,如果没有被指定,中止操作
53
			if (isset($data[$key]))
54
				$s_pkey .= " and $key = $data[$key] ";
55
			else
56
			{
57
				return("Key $key has not assigned a value.");
58
			}
59
		}
60
		$sql .= $s_pkey;
61
	}
62
	else
63
	{
64
		//单个键值
65
		$s_pkey = " and $pkey = $data[$pkey] ";
66
		$sql .= $s_pkey;
67
	}
68
	$rs = $db->Execute($sql);
69
	$i = $rs->fields['c'];
70
	if (0 == $i)
71
	{
72
		//its insert
73
		$sql = "insert into $tbl (";
74
		//keys
75
		foreach ($data as $key=>$val)
76
		{
77
			$sql .= "$key, ";
78
		}
79
		$sql = substr($sql, 0, strlen($sql) - 2);
80
		$sql .= ") values (";
81
		//values
82
		foreach ($data as $key=>$val)
83
		{
84
			$sql .= "$val, ";
85
		}
86
		$sql = substr($sql, 0, strlen($sql) - 2);
87
		$sql .= ")";
88
	}
89
	elseif (1 == $i)
90
	{
91
		//its update
92
		$sql = "update $tbl set ";
93
		foreach ($data as $key=>$val)
94
		{
95
			$sql .= "$key = $val, ";
96
		}
97
		$sql = substr($sql, 0, strlen($sql) - 2);
98
		$sql .= " where 1=1 ";
99
		//use pkey to locate data
100
		$sql .= $s_pkey;
101
	}
102
	else
103
	{
104
		//got too many match rows
105
		return("Got >1 rows by given pkey, which to update ?");
106
	}
107
	//finally, write to database
108
	//$db->debug = true;
109
	$rs = $db->Execute($sql);
0 ignored issues
show
Unused Code introduced by
$rs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
110
	$i = $db->ErrorNo();
111
	if (0 == $i)
112
	{
113
		//no error
114
		//echo("1 row writed to database, no error.<br />\n");
115
		return 1;
116
	}
117
	else
118
	{
119
		//error occur
120
		return($db->ErrorMsg());
121
	}
122
} // end of function DbWrite
123
124
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
125