Commit 9b3b9c7b authored by yujiwei's avatar yujiwei
parents 3d53f0bf 65546514
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
"require": { "require": {
"php": ">=7.2", "php": ">=7.2",
"guzzlehttp/guzzle": "6.3", "guzzlehttp/guzzle": "6.3",
"catfan/medoo": "1.7.10",
"mongodb/mongodb": "1.4.3", "mongodb/mongodb": "1.4.3",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-exif": "*", "ext-exif": "*",
......
This diff is collapsed.
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Api\PhpUtils\Mysql; namespace Api\PhpUtils\Mysql;
use Medoo\Medoo; use Api\PhpUtils\Mysql\Medoo;
use Api\PhpUtils\Log\FileLog; use Api\PhpUtils\Log\FileLog;
/** /**
...@@ -20,20 +20,20 @@ use Api\PhpUtils\Log\FileLog; ...@@ -20,20 +20,20 @@ use Api\PhpUtils\Log\FileLog;
* return [] 查询结果为空 * return [] 查询结果为空
* return number 插入为lasterInsertId:String 按需自己intval(),更新和删除返回影响条数 * 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.in (英文网站)
* @see https://medoo.lvtao.net (中文网站) * @see https://medoo.lvtao.net (中文网站)
*/ */
/** /**
* @method static MysqlBase insert($data) * @method static MysqlBase insert($data, $options)
* @method static MysqlBase update($data, $where) * @method static MysqlBase update($data, $where)
* @method static MysqlBase delete($where) * @method static MysqlBase delete($where)
* @method static MysqlBase select($columns, $where) * @method static MysqlBase select($columns, $where, $options)
* @method static MysqlBase selectMaster($columns, $where) * @method static MysqlBase selectMaster($columns, $where, $options)
* @method static MysqlBase getMaster($columns, $where) * @method static MysqlBase getMaster($columns, $where, $options)
* @method static MysqlBase get($columns, $where) * @method static MysqlBase get($columns, $where, $options)
* @method static MysqlBase count($where) * @method static MysqlBase count($columns, $where)
* @method static MysqlBase action( $callback ) * @method static MysqlBase action( $callback )
*/ */
abstract class MysqlBase abstract class MysqlBase
...@@ -154,6 +154,51 @@ abstract class MysqlBase ...@@ -154,6 +154,51 @@ abstract class MysqlBase
return self::formatResult($method, $arguments); 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]);
}
/** /**
* 获取数据库连接 * 获取数据库连接
* *
...@@ -281,12 +326,6 @@ abstract class MysqlBase ...@@ -281,12 +326,6 @@ abstract class MysqlBase
case 'selectmaster': case 'selectmaster':
case 'get': case 'get':
case 'getmaster': 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 // 读取没有limit的强制加limit
if (!isset($arguments[1]['LIMIT'])) { if (!isset($arguments[1]['LIMIT'])) {
$arguments[1]['LIMIT'] = 5000; $arguments[1]['LIMIT'] = 5000;
...@@ -294,15 +333,6 @@ abstract class MysqlBase ...@@ -294,15 +333,6 @@ abstract class MysqlBase
} }
break; 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: default:
break; break;
} }
...@@ -361,12 +391,18 @@ abstract class MysqlBase ...@@ -361,12 +391,18 @@ abstract class MysqlBase
// PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。 // PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。
return self::$sqlResut->rowCount(); 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'], []); return call_user_func_array([self::$dbCurrentConnect, 'id'], []);
break; break;
case 'update': case 'update':
case 'delete': case 'delete':
case 'insertduplicate':
// 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。 // 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。
return self::$sqlResut->rowCount(); return self::$sqlResut->rowCount();
break; break;
......
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