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
53e93d00
Commit
53e93d00
authored
Jun 16, 2021
by
cuiweifeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update : save
parent
65546514
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
64 deletions
+40
-64
MysqlBase.php
src/Mysql/MysqlBase.php
+8
-45
MysqlClusterBase.php
src/Mysql/MysqlClusterBase.php
+12
-19
README.md
src/Mysql/README.md
+20
-0
No files found.
src/Mysql/MysqlBase.php
View file @
53e93d00
...
...
@@ -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
]);
}
/**
* 获取数据库连接
*
...
...
src/Mysql/MysqlClusterBase.php
View file @
53e93d00
...
...
@@ -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 语句受影响的行数。
...
...
src/Mysql/README.md
View file @
53e93d00
...
...
@@ -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
;
...
...
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