Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_utils
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
php_utils
Commits
19e36765
Commit
19e36765
authored
Jun 16, 2021
by
cuiweifeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update : 修改mysqlbase基类,支持 forupdate 和 on duplicate
parent
0a49b637
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
2086 additions
and
24 deletions
+2086
-24
Medoo.php
src/Mysql/Medoo.php
+2026
-0
MysqlBase.php
src/Mysql/MysqlBase.php
+60
-24
No files found.
src/Mysql/Medoo.php
0 → 100644
View file @
19e36765
This diff is collapsed.
Click to expand it.
src/Mysql/MysqlBase.php
View file @
19e36765
...
...
@@ -2,7 +2,7 @@
namespace
Api\PhpUtils\Mysql
;
use
Medoo
\Medoo
;
use
Api\PhpUtils\Mysql
\Medoo
;
use
Api\PhpUtils\Log\FileLog
;
/**
...
...
@@ -20,20 +20,20 @@ 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 (中文网站)
*/
/**
* @method static MysqlBase insert($data)
* @method static MysqlBase insert($data
, $options
)
* @method static MysqlBase update($data, $where)
* @method static MysqlBase delete($where)
* @method static MysqlBase select($columns, $where)
* @method static MysqlBase selectMaster($columns, $where)
* @method static MysqlBase getMaster($columns, $where)
* @method static MysqlBase get($columns, $where)
* @method static MysqlBase count($where)
* @method static MysqlBase select($columns, $where
, $options
)
* @method static MysqlBase selectMaster($columns, $where
, $options
)
* @method static MysqlBase getMaster($columns, $where
, $options
)
* @method static MysqlBase get($columns, $where
, $options
)
* @method static MysqlBase count($
columns, $
where)
* @method static MysqlBase action( $callback )
*/
abstract
class
MysqlBase
...
...
@@ -154,6 +154,51 @@ 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
]);
}
/**
* 获取数据库连接
*
...
...
@@ -281,12 +326,6 @@ abstract class MysqlBase
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
;
...
...
@@ -294,15 +333,6 @@ abstract class MysqlBase
}
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
;
}
...
...
@@ -361,12 +391,18 @@ abstract class MysqlBase
// 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'
:
case
'insertduplicate'
:
// 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。
return
self
::
$sqlResut
->
rowCount
();
break
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment