<?php
/**
 * @version $Id: database.func.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

/**
 * 取得记录数数
 * 
 * @param String $condition 查询条件
 * @param String $table 表名
 */
function countRecords($table, $condition = '')
{
	global $DB, $db_prefix;
	$SQL = "SELECT count(*) num FROM {$db_prefix}{$table}";
	if ($condition)
	{
		$SQL .= " WHERE {$condition}";
	}
	$result = $DB->db_fetch_one_array($SQL);
	return $result['num'];
}

/**
 * 更新记录
 * 
 * @param Array $filed 字段值，array('字段名' => '值')
 * @param String $condition 更新条件
 */
function updateRecords($field = array(), $table, $condition = '')
{
	global $DB, $db_prefix;
	if (empty($field) || !is_array($field))
	{
		return FALSE;
	}
	$SQL = "UPDATE {$db_prefix}{$table} SET ";
	foreach ($field as $key => $value)
	{
		if (strpos($value, '[!--selffiled--]') !== FALSE)
		{
			$value = str_replace('[!--selffiled--]', $key, $value);
			$SQL .= "$key = $value,";
		}
		else
		{
			$SQL .= "$key = '$value',";
		}
	}
	$SQL = substr($SQL, 0, -1);
	if ($condition)
	{
		$SQL .= " WHERE $condition";
	}
	$DB->db_query($SQL);
}

/**
 * 取得字段值
 * 
 * @param Array $filed 字段值，array('字段名')
 * @param String $table 表名
 * @param String $condition 更新条件
 */
function getFiledValues($field = array(), $table, $condition = '')
{
	global $DB, $db_prefix;
	$ra = array();
	if ($field)
	{
		$field = implode(',', $field);
	}
	else
	{
		$field = '*';
	}
	$SQL = "SELECT {$field} FROM {$db_prefix}{$table}";
	if ($condition)
	{
		$SQL .= " WHERE $condition";
	}
	$DB->db_query($SQL);
	while ($rs = $DB->db_fetch_array())
	{
		$ra[] = $rs;
	}
	return $ra;
}
/**
 * 增加新记录
 * 
 * @param Array $item 字段记录关联数组 array('filed'=>value)
 * @param String $table 表名
 */
function addRecords($item = array(), $table)
{
	global $DB, $db_prefix;
	$field = $fieldValue = '';
	$SQL = "INSERT INTO {$db_prefix}{$table} (%s) 
			VALUES(%s)";
	if ($item && is_array($item))
	{
		foreach ($item as $key => $value)
		{
			$field .= "{$key},";
			$fieldValue .= "'{$value}',";
		}
		$field = substr($field, 0, -1);
		$fieldValue = substr($fieldValue, 0, -1);
		$SQL = sprintf($SQL, $field, $fieldValue);
	}
	$result = $DB->db_query($SQL);
}

/**
 * 删除记录
 * 
 * @param String $condition 查询条件
 * @param String $table 表名
 */
function delRecords($table, $condition = '')
{
	global $DB, $db_prefix;
	$SQL = "DELETE FROM {$db_prefix}{$table}";
	if ($condition)
	{
		$SQL .= " WHERE {$condition}";
	}
	$DB->db_query($SQL);
}

/**
 * 返回关联标签文章
 */
function getTidByTag($tagname)
{
	global $DB, $db_prefix;
	$SQL = "SELECT tc.tid 
			FROM {$db_prefix}tagcache tc, {$db_prefix}tags t 
			WHERE tc.tagid = t.tagid 
			AND t.tagname = '$tagname'";
	$DB->db_query($SQL);
	$tid = array();
	while ($result = $DB->db_fetch_array())
	{
		$tid[] = $result['tid'];
	}
	return $tid;
}

/**
 * 返回文章数量
 */
function countArticleRecords($condition)
{
	global $DB, $db_prefix;
	$SQL = "SELECT COUNT(*) num 
			FROM {$db_prefix}threads t 
			WHERE {$condition}";
	$rt = $DB->db_fetch_one_array($SQL);
	return $rt['num'];
}

/**
 * 返回评论数量
 */
function countCommentRecords($condition)
{
	global $DB, $db_prefix;
	$SQL = "SELECT COUNT(*) num 
			FROM {$db_prefix}comments  
			WHERE {$condition}";
	$rt = $DB->db_fetch_one_array($SQL);
	return $rt['num'];
}

/**
 * 取得字段值
 * 
 * @param Array $filed 字段值，array('字段名')
 * @param String $table 表名
 * @param String $condition 更新条件
 */
function getSingleFiledValues($field = array(), $table, $condition = '')
{
	global $DB, $db_prefix;
	if ($field)
	{
		$field = implode(',', $field);
	}
	else
	{
		$field = '*';
	}
	$SQL = "SELECT {$field} FROM {$db_prefix}{$table}";
	if ($condition)
	{
		$SQL .= " WHERE $condition";
	}
	$rs = $DB->db_fetch_one_array($SQL);
	return $rs;
}
?>