<?php
/* MSSql的操作类 */
namespace App\Tools;
class MSSql
{
public $conn;
private $filter = '';
private $querynum = 0;
/* 连接MSSql数据库,参数:dbsn->数据库服务器地址,dbun->登陆用户名,dbpw->登陆密码,dbname->数据库名字 */
function Connect($dbsn, $dbun, $dbpw, $dbname)
{
//参数配置
$connectionInfo = array("Database" => $dbname, "UID" => $dbun, "PWD" => $dbpw, "CharacterSet" => "UTF-8");
//连接配置
$this->conn = sqlsrv_connect($dbsn, $connectionInfo);
//判断是否连接
if ($this->conn === false) {
die(print_r(sqlsrv_errors(), true));
}
}
/**
* 获取单条数据
* @param type $param
* @return type
*/
function getOne($table)
{
$query = $this->Query("select * from [{$table}] {$this->filter}");
$record = $this->GetRow($query);
$this->Clear($query);
return $record;
}
/**
* 获取多条数据
* @param type $where
* @return type
*/
function getAll($table, $where = [])
{
$query = $this->Query("select * from [{$table}] {$this->filter}");
$record = $this->GetArr($query);
$this->Clear($query);
return $record;
}
/**
* 分页数据
* @param type $page
* @param type $limit
* @return $this
*/
function page($page = 1, $limit = 10)
{
$start = ($page - 1) * $page;
if (isset($order)) {
$this->filter .= ' limit ' . $start . ',' . $limit;
}
return $this;
}
/**
* 排序条件
* @param type $order
* @return $this
*/
function order($order = array())
{
if (isset($order)) {
$this->filter .= ' ORDER BY ';
$this->filter .= implode(',', $order);
}
return $this;
}
/**
* 转为gbk字符
* @param type $queryStr
* @return type
*/
public static function ToCNStr($queryStr)
{
return $queryStr;
return iconv('UTF-8', 'gb2312//IGNORE', $queryStr);
}
/**
* 条件添加
* @param type $where
* @return $this
*/
function where($where)
{
if (is_array($where) && !empty($where)) {
$arr = array();
foreach ($where as $key => $value) {
$tip = "$key='{$value}'";
array_push($arr, $tip);
}
$this->filter .= ' WHERE ';
$this->filter .= implode(' and ', $arr);
}
return $this;
}
/* 执行sql语句,返回对应的结果标识 */
function Query($sql)
{
if ($query = sqlsrv_query($this->conn, $sql)) {
$this->querynum++;
return $query;
} else {
$this->querynum++;
$this->halt('MSSQL Query Error', $sql);
}
}
/* 执行Insert Into语句,并返回最后的insert操作所产生的自动增长的id */
function Insert($table, $iarr)
{
//插入拼装
$value = $this->InsertSql($iarr);
//执行语句
$query = $this->Query('INSERT INTO ' . $table . ' ' . $value . ';SELECT SCOPE_IDENTITY() as id;');
//使指定语句的下一个结果(结果集、行计数或输出参数)处于活动状态。
sqlsrv_next_result($query);
//使结果集的下一行可供读取。 使用sqlsrv_get_field来读取的行的字段。
sqlsrv_fetch($query);
//从当前行的指定字段中检索数据。 必须按顺序访问字段数据。 例如,在访问第二个字段中的数据后,不能访问第一个字段中的数据。
$id = sqlsrv_get_field($query, 0);
//释放内存
$this->Clear($query);
return $id;
}
/* 执行Update语句,并返回最后的update操作所影响的行数 */
function Update($table, $uarr, $condition = '')
{
$value = $this->UpdateSql($uarr);
if ($condition) {
$condition = ' WHERE ' . $condition;
}
$query = $this->Query('UPDATE ' . $table . ' SET ' . $value . $condition . '; SELECT @@ROWCOUNT AS [rowcount];');
$record = $this->GetRow($query);
$this->Clear($query);
return $record['rowcount'];
}
/* 执行Delete语句,并返回最后的Delete操作所影响的行数 */
function Delete($table, $condition = '')
{
if ($condition) {
$condition = ' WHERE ' . $condition;
}
$query = $this->Query('DELETE ' . $table . $condition . ' SELECT @Option_ID = SCOPE_IDENTITY()');
$record = $this->GetRow($query);
$this->Clear($query);
return $record;
}
/* 将字符转为可以安全保存的sqlsrv值,比如a'a转为a''a */
function EnCode($str)
{
return str_replace("'", "''", str_replace('', '', $str));
}
/* 将可以安全保存的sqlsrv值转为正常的值,比如a''a转为a''a */
function DeCode($str)
{
return str_replace("''", "'", $str);
}
/* 将对应的列和值生成对应的insert语句,如:array('id' => 1, 'name' => 'name')返回([id], [name]) VALUES (1, 'name') */
function InsertSql($iarr)
{
if (is_array($iarr)) {
$fstr = '';
$vstr = '';
foreach ($iarr as $key => $val) {
$fstr .= '[' . $key . '], ';
$vstr .= "'" . $val . "',";
}
if ($fstr) {
$fstr = '(' . substr($fstr, 0, -2) . ')';
$vstr = '(' . substr($vstr, 0, -1) . ')';
return $fstr . ' VALUES ' . $vstr;
} else {
return '';
}
} else {
return '';
}
}
/* 将对应的列和值生成对应的insert语句,如:array('id' => 1, 'name' => 'name')返回[id] = 1, [name] = 'name' */
function UpdateSql($uarr)
{
if (is_array($uarr)) {
$ustr = '';
foreach ($uarr as $key => $val) {
$ustr .= "[{$key}] = '{$val}', ";
}
if ($ustr) {
return substr($ustr, 0, -2);
} else {
return '';
}
} else {
return '';
}
}
/**
* (字段key转utf-8)
* 返回对应的查询标识的结果的一行
*/
function GetRow($query, $result_type = SQLSRV_FETCH_ASSOC)
{
$row = sqlsrv_fetch_array($query, $result_type);
if ($row) {
foreach ($row as $key => $value) {
$keyu = iconv('gb2312//IGNORE', 'UTF-8', $key);
unset($row[$key]);
$row[$keyu] = $value;
}
}
return $row;
}
/**
* 返回多行数据 (字段key转utf-8)
* @param type $query
* @param type $result_type
* @return type
*/
function GetArr($query, $result_type = SQLSRV_FETCH_ASSOC)
{
while ($row[] = sqlsrv_fetch_array($query, $result_type)) {
}
$row = array_filter($row);
if ($row) {
foreach ($row as $kk => &$list) {
foreach ($list as $key => $value) {
$keyu = iconv('gb2312//IGNORE', 'UTF-8', $key);
unset($list[$key]);
$list[$keyu] = $value;
}
}
}
return $row;
}
/**
* 清空查询结果所占用的内存资源
*/
function Clear($query)
{
return sqlsrv_free_stmt($query);
}
/**
* 关闭数据库
*/
function Close()
{
return sqlsrv_close($this->conn);
}
function halt($message = '', $sql = '')
{
v(sqlsrv_errors());
}
}
网友评论