1 Star 0 Fork 1

funsun/Hutool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mysql.php 4.79 KB
一键复制 编辑 原始数据 按行查看 历史
funsun 提交于 2023-12-06 22:32 . 新增函数
<?php
class Mysql
{
/** @var mysqli */
private $connection;
protected $db_list = [];
protected $time_limit = 200;
public function __construct($db_name = 'location')
{
$this->time_limit = config('time_limit', null, 'mysql');
$this->db_list = config('db_config');
$this->connect($db_name);
}
public function connect($db_name = 'location')
{
if (array_key_exists($db_name, $this->db_list)) {
if (isset($this->connection)) {
mysqli_close($this->connection);
}
$db_conf = $this->db_list[$db_name];
$conn = new mysqli(
$db_conf['DB_HOST'],
$db_conf['DB_USER'],
$db_conf['DB_PWD'],
$db_conf['DB_NAME'],
$db_conf['DB_PORT']
);
$this->connection = $conn;
}
}
/**
* @param string $db_name
* @param $sql
* @return object
*/
public function select($sql, callable $callback = null, $option = MYSQLI_STORE_RESULT)
{
$conn = $this->connection;
$result = $conn->query($sql, $option);
if ($result === false) {
throw new Exception(json_encode(['error' => $conn->error, 'sql' => $sql], JSON_UNESCAPED_UNICODE));
} else {
if (is_callable($callback)) {
$response = $callback($result);
if ($option === MYSQLI_USE_RESULT and is_a($result, 'mysqli_result')) {
$result->free();
}
return $response;
} else {
return $result;
}
}
}
/**
* @param $sql
* @param $option array {
’time_limit‘
* }
* @return array
* @throws Exception
*/
public function DQL($sql, array $option=[])
{
$time_limit = $option['time_limit'] ?? null;
if (strpos($sql, '%TIME_LIMIT%') !== false) {
$replace_str = '/*+ MAX_EXECUTION_TIME('.($time_limit ?? $this->time_limit).') */';
$sql = str_replace('%TIME_LIMIT%', $replace_str, $sql);
}
return (array)$this->select($sql, function (mysqli_result $dbResult) {
$result = [];
while ($row = $dbResult->fetch_assoc()) {
$result[] = $row;
}
return $result;
},
MYSQLI_USE_RESULT);
}
public function insert($table, array $fields, array $data, $fetch_sql = false)
{
$conn = $this->connection;
$sql = 'insert into ' . $table . ' (' . implode(',', $fields) . ') values ';
$value = [];
foreach ($data as $v) {
$s_value = [];
foreach ($fields as $k) {
$s_value[] = '\'' . $conn->real_escape_string($v[$k]) . '\'';
}
$value[] = "(" . implode(', ', $s_value) . ")";
}
$sql .= implode(',', $value);
if ($fetch_sql) {
return $sql;
}
if ($conn->query($sql) === true) {
return $conn->affected_rows;
} else {
throw new Exception($conn->error);
}
}
public function replace($table, array $fields, array $data, $fetch_sql = false)
{
$conn = $this->connection;
$sql = 'replace into ' . $table . ' (' . implode(',', $fields) . ') values ';
$value = [];
foreach ($data as $v) {
$s_string = '';
foreach ($fields as $k) {
if (array_key_exists($k, $v) and $v[$k] !== null) {
$s_string .= '\'' . $conn->real_escape_string($v[$k]) . '\', ';
} else {
$s_string .= 'null, ';
}
}
$value[] = '(' . rtrim($s_string, ', ') . ')';
}
$sql .= implode(',', $value);
if ($fetch_sql) {
return $sql;
}
if ($conn->query($sql) === true) {
return $conn->affected_rows;
} else {
throw new Exception($conn->error);
}
}
public function update($sql)
{
$conn = $this->connection;
if ($conn->query($sql) === true) {
return $conn->affected_rows;
} else {
throw new Exception($conn->error);
}
}
public function delete($sql)
{
return $this->update($sql);
}
public function __call($name, $arguments)
{
if (method_exists($this->connection, $name)) {
return call_user_func_array([$this->connection, $name], $arguments);
} else {
throw new Exception('method not implemented: ' . $name);
}
}
public function __destruct()
{
$this->connection->close();
}
public function getLastInsID()
{
return $this->connection->insert_id;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/funsun/hutool-h.git
git@gitee.com:funsun/hutool-h.git
funsun
hutool-h
Hutool
master

搜索帮助