Commit b583a8bf authored by yujiwei's avatar yujiwei
parents cb216beb 1ebb80bf
......@@ -21,16 +21,22 @@ use Api\PhpUtils\Log\FileLog;
* return number 插入为lasterInsertId:String 按需自己intval(),更新和删除返回影响条数
*
* 5. 根据镜像中php的版本选择使用version1.7.10 版本的Medoo类, 并进行了改造,去掉了join操作,增加了$options参数;
* $options参数可选项:
* - $options['max_execution_time'] = 10; // 单位毫秒
* - $options['rowCount'] = true; // 返回insert后的影响行数
* @see https://medoo.in (英文网站)
* @see https://medoo.lvtao.net (中文网站)
*/
/**
* @method static MysqlBase insert($data, $options)
* @method static MysqlBase insertDuplicate($data, $duplicate)
* @method static MysqlBase update($data, $where)
* @method static MysqlBase delete($where)
* @method static MysqlBase select($columns, $where, $options)
* @method static MysqlBase selectMaster($columns, $where, $options)
* @method static MysqlBase selectForUpdate($columns, $where, $options)
* @method static MysqlBase getMaster($columns, $where, $options)
* @method static MysqlBase get($columns, $where, $options)
* @method static MysqlBase count($columns, $where)
......@@ -98,6 +104,8 @@ abstract class MysqlBase
"delete",
"selectMaster", // 或使用/*master*/强制路由,对主从实时性要求较高的场景使用,分布式系统尽量用消息代替查主库等其他方案
"getMaster",
"selectForUpdate",
"insertDuplicate",
];
/**
......@@ -154,51 +162,6 @@ abstract class MysqlBase
return self::formatResult($method, $arguments);
}
/**
* selectForUpdate
*
* @return array
*/
public static function selectForUpdate($columns, $where, $options = null)
{
$type = static::WRITE;
self::$dbCurrentConnect = self::$dbConnect[$type] = self::getConnection($type);
self::$sqlResut = self::$dbConnect[$type]->selectForUpdate(static::TABLE_NAME, $columns, $where, $options);
if (!self::catchError('selectForUpdate', [static::TABLE_NAME, $columns, $where, $options])) {
// 如果失败重试一次
self::$dbCurrentConnect = self::$dbConnect[$type] = self::getConnection($type);
self::$sqlResut = self::$dbConnect[$type]->selectForUpdate(static::TABLE_NAME, $columns, $where, $options);
if (!self::catchError('selectForUpdate', [static::TABLE_NAME, $columns, $where, $options])) {
return false;
}
}
return self::$sqlResut;
}
/**
* insertDuplicate
*
* 批量使用:Medoo::raw
* insertDuplicate([['id' => 4,'name' => 'test1'], ['id' => 5,'name' => 'test2']], ['name' => Medoo::raw('VALUES(name)')]);
*
* @return int 单条时返回1insert成功,返回2表示重复数据更新成功
*/
public static function insertDuplicate($data, $duplicate)
{
$type = static::WRITE;
self::$dbCurrentConnect = self::$dbConnect[$type] = self::getConnection($type);
self::$sqlResut = self::$dbConnect[$type]->insertDuplicate(static::TABLE_NAME, $data, $duplicate);
if (!self::catchError('insertDuplicate', [static::TABLE_NAME, $data, $duplicate])) {
// 如果失败重试一次
self::$dbCurrentConnect = self::$dbConnect[$type] = self::getConnection($type);
self::$sqlResut = self::$dbConnect[$type]->insertDuplicate(static::TABLE_NAME, $data, $duplicate);
if (!self::catchError('insertDuplicate', [static::TABLE_NAME, $data, $duplicate])) {
return false;
}
}
return self::formatResult('insertDuplicate', [static::TABLE_NAME, $data, $duplicate]);
}
/**
* 获取数据库连接
*
......
......@@ -2,7 +2,7 @@
namespace Api\PhpUtils\Mysql;
use Medoo\Medoo;
use Api\PhpUtils\Mysql\Medoo;
use Api\PhpUtils\Log\FileLog;
/**
......@@ -20,7 +20,7 @@ use Api\PhpUtils\Log\FileLog;
* return [] 查询结果为空
* return number 插入为lasterInsertId:String 按需自己intval(),更新和删除返回影响条数
*
* 5. 根据镜像中php的版本选择使用version1.7.10 版本的Medoo类,不要升级!
* 5. 根据镜像中php的版本选择使用version1.7.10 版本的Medoo类,并进行了改造,去掉了join操作,增加了$options参数;
* @see https://medoo.in (英文网站)
* @see https://medoo.lvtao.net (中文网站)
*/
......@@ -129,6 +129,8 @@ abstract class MysqlClusterBase
"delete",
"selectMaster", // 或使用/*master*/强制路由,对主从实时性要求较高的场景使用,分布式系统尽量用消息代替查主库等其他方案
"getMaster",
"selectForUpdate",
"insertDuplicate",
];
/**
......@@ -185,7 +187,7 @@ abstract class MysqlClusterBase
if (!self::catchError($method, $arguments)) {
// 如果失败重试一次
self::$dbCurrentConnect = self::$dbConnect[$type.':'.$dbShardingIndex] = self::getConnection($type, $dbShardingIndex, true);
self::$dbCurrentConnect = self::$dbConnect[$type.':'.$dbShardingIndex] = self::getConnection($type, $dbShardingIndex);
self::$sqlResut = call_user_func_array([self::$dbConnect[$type.':'.$dbShardingIndex], $method], $arguments);
if (!self::catchError($method, $arguments)) {
return false;
......@@ -429,12 +431,6 @@ abstract class MysqlClusterBase
case 'selectmaster':
case 'get':
case 'getmaster':
// 不允许用join操作
if (count($arguments) > 2) {
self::$errorInfo = ['validation', 0, "sql exception use join:" . $method . "-" . json_encode($arguments)];
FileLog::error("sql exception use join:" . $method . "-" . json_encode($arguments));
return false;
}
// 读取没有limit的强制加limit
if (!isset($arguments[1]['LIMIT'])) {
$arguments[1]['LIMIT'] = 5000;
......@@ -442,15 +438,6 @@ abstract class MysqlClusterBase
}
break;
case 'count':
// 不允许用join操作
if (count($arguments) > 1) {
self::$errorInfo = ['validation', 0, "sql exception use join:" . $method . "-" . json_encode($arguments)];
FileLog::error("sql exception use join:" . $method . "-" . json_encode($arguments));
return false;
}
break;
default:
break;
}
......@@ -591,10 +578,16 @@ abstract class MysqlClusterBase
// PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。
return self::$sqlResut->rowCount();
}
// 单条插入返回最后插入行的ID
// 对于无AUTO_INCREMENT的表,可以指定返回影响的行数的参数
if (isset($arguments[2]['rowCount']) && $arguments[2]['rowCount']) {
// PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。
return self::$sqlResut->rowCount();
}
// 单条默认插入返回最后插入行的lastInsertId,如果无AUTO_INCREMENT字段,返回NULL
return call_user_func_array([self::$dbCurrentConnect, 'id'], []);
break;
case 'update':
case 'delete':
// 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。
......
......@@ -4,6 +4,26 @@
### MysqlBase 主从模式示例
``` php
$options参数可选项
- $options['max_execution_time'] = 10; // 单位毫秒
- $options['rowCount'] = true; // 返回insert后的影响行数
/**
* insertDuplicate
*
* 批量使用:Medoo::raw
* insertDuplicate([['id' => 4,'name' => 'test1'], ['id' => 5,'name' => 'test2']], ['name' => Medoo::raw('VALUES(name)')]);
*
* @return int 单条时返回1insert成功,返回2表示重复数据更新成功
*/
public static function insertDuplicate($data, $duplicate)
{}
<?php
namespace App\Models\demo\mysql;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment